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, |
Walter Dörwald | 8757878 | 2007-08-30 15:30:09 +0000 | [diff] [blame] | 798 | const char *input, Py_ssize_t insize, Py_ssize_t *startinpos, |
| 799 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 800 | PyObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 801 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 802 | 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] | 803 | |
| 804 | PyObject *restuple = NULL; |
| 805 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 806 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
| 807 | Py_ssize_t requiredsize; |
| 808 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 809 | Py_UNICODE *repptr; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 810 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 811 | int res = -1; |
| 812 | |
| 813 | if (*errorHandler == NULL) { |
| 814 | *errorHandler = PyCodec_LookupError(errors); |
| 815 | if (*errorHandler == NULL) |
| 816 | goto onError; |
| 817 | } |
| 818 | |
| 819 | if (*exceptionObject == NULL) { |
| 820 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 821 | encoding, input, insize, *startinpos, *endinpos, reason); |
| 822 | if (*exceptionObject == NULL) |
| 823 | goto onError; |
| 824 | } |
| 825 | else { |
| 826 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 827 | goto onError; |
| 828 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
| 829 | goto onError; |
| 830 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 831 | goto onError; |
| 832 | } |
| 833 | |
| 834 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 835 | if (restuple == NULL) |
| 836 | goto onError; |
| 837 | if (!PyTuple_Check(restuple)) { |
| 838 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 839 | goto onError; |
| 840 | } |
| 841 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
| 842 | goto onError; |
| 843 | if (newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 844 | newpos = insize+newpos; |
| 845 | if (newpos<0 || newpos>insize) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 846 | 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] | 847 | goto onError; |
| 848 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 849 | |
| 850 | /* need more space? (at least enough for what we |
| 851 | have+the replacement+the rest of the string (starting |
| 852 | at the new input position), so we won't have to check space |
| 853 | when there are no errors in the rest of the string) */ |
| 854 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 855 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 856 | requiredsize = *outpos + repsize + insize-newpos; |
| 857 | if (requiredsize > outsize) { |
| 858 | if (requiredsize<2*outsize) |
| 859 | requiredsize = 2*outsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 860 | if (PyUnicode_Resize(output, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 861 | goto onError; |
| 862 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
| 863 | } |
| 864 | *endinpos = newpos; |
| 865 | *inptr = input + newpos; |
| 866 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 867 | *outptr += repsize; |
| 868 | *outpos += repsize; |
| 869 | /* we made it! */ |
| 870 | res = 0; |
| 871 | |
| 872 | onError: |
| 873 | Py_XDECREF(restuple); |
| 874 | return res; |
| 875 | } |
| 876 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 877 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 878 | |
| 879 | /* see RFC2152 for details */ |
| 880 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 881 | static |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 882 | char utf7_special[128] = { |
| 883 | /* indicate whether a UTF-7 character is special i.e. cannot be directly |
| 884 | encoded: |
| 885 | 0 - not special |
| 886 | 1 - special |
| 887 | 2 - whitespace (optional) |
| 888 | 3 - RFC2152 Set O (optional) */ |
| 889 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, |
| 890 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 891 | 2, 3, 3, 3, 3, 3, 3, 0, 0, 0, 3, 1, 0, 0, 0, 1, |
| 892 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, |
| 893 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 894 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, |
| 895 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 896 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 1, 1, |
| 897 | |
| 898 | }; |
| 899 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 900 | /* Note: The comparison (c) <= 0 is a trick to work-around gcc |
| 901 | warnings about the comparison always being false; since |
| 902 | utf7_special[0] is 1, we can safely make that one comparison |
| 903 | true */ |
| 904 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 905 | #define SPECIAL(c, encodeO, encodeWS) \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 906 | ((c) > 127 || (c) <= 0 || utf7_special[(c)] == 1 || \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 907 | (encodeWS && (utf7_special[(c)] == 2)) || \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 908 | (encodeO && (utf7_special[(c)] == 3))) |
| 909 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 910 | #define B64(n) \ |
| 911 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 912 | #define B64CHAR(c) \ |
| 913 | (isalnum(c) || (c) == '+' || (c) == '/') |
| 914 | #define UB64(c) \ |
| 915 | ((c) == '+' ? 62 : (c) == '/' ? 63 : (c) >= 'a' ? \ |
| 916 | (c) - 71 : (c) >= 'A' ? (c) - 65 : (c) + 4 ) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 917 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 918 | #define ENCODE(out, ch, bits) \ |
| 919 | while (bits >= 6) { \ |
| 920 | *out++ = B64(ch >> (bits-6)); \ |
| 921 | bits -= 6; \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 922 | } |
| 923 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 924 | #define DECODE(out, ch, bits, surrogate) \ |
| 925 | while (bits >= 16) { \ |
| 926 | Py_UNICODE outCh = (Py_UNICODE) ((ch >> (bits-16)) & 0xffff); \ |
| 927 | bits -= 16; \ |
| 928 | if (surrogate) { \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 929 | /* We have already generated an error for the high surrogate \ |
| 930 | 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] | 931 | surrogate = 0; \ |
| 932 | } else if (0xDC00 <= outCh && outCh <= 0xDFFF) { \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 933 | /* This is a surrogate pair. Unfortunately we can't represent \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 934 | it in a 16-bit character */ \ |
| 935 | surrogate = 1; \ |
| 936 | errmsg = "code pairs are not supported"; \ |
| 937 | goto utf7Error; \ |
| 938 | } else { \ |
| 939 | *out++ = outCh; \ |
| 940 | } \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 941 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 942 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 943 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 944 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 945 | const char *errors) |
| 946 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 947 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 948 | Py_ssize_t startinpos; |
| 949 | Py_ssize_t endinpos; |
| 950 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 951 | const char *e; |
| 952 | PyUnicodeObject *unicode; |
| 953 | Py_UNICODE *p; |
| 954 | const char *errmsg = ""; |
| 955 | int inShift = 0; |
| 956 | unsigned int bitsleft = 0; |
| 957 | unsigned long charsleft = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 958 | int surrogate = 0; |
| 959 | PyObject *errorHandler = NULL; |
| 960 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 961 | |
| 962 | unicode = _PyUnicode_New(size); |
| 963 | if (!unicode) |
| 964 | return NULL; |
| 965 | if (size == 0) |
| 966 | return (PyObject *)unicode; |
| 967 | |
| 968 | p = unicode->str; |
| 969 | e = s + size; |
| 970 | |
| 971 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 972 | Py_UNICODE ch; |
| 973 | restart: |
| 974 | ch = *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 975 | |
| 976 | if (inShift) { |
| 977 | if ((ch == '-') || !B64CHAR(ch)) { |
| 978 | inShift = 0; |
| 979 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 980 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 981 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 982 | if (bitsleft >= 6) { |
| 983 | /* The shift sequence has a partial character in it. If |
| 984 | bitsleft < 6 then we could just classify it as padding |
| 985 | but that is not the case here */ |
| 986 | |
| 987 | errmsg = "partial character in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 988 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 989 | } |
| 990 | /* According to RFC2152 the remaining bits should be zero. We |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 991 | choose to signal an error/insert a replacement character |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 992 | here so indicate the potential of a misencoded character. */ |
| 993 | |
| 994 | /* On x86, a << b == a << (b%32) so make sure that bitsleft != 0 */ |
| 995 | if (bitsleft && charsleft << (sizeof(charsleft) * 8 - bitsleft)) { |
| 996 | errmsg = "non-zero padding bits in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 997 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | if (ch == '-') { |
| 1001 | if ((s < e) && (*(s) == '-')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1002 | *p++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1003 | inShift = 1; |
| 1004 | } |
| 1005 | } else if (SPECIAL(ch,0,0)) { |
| 1006 | errmsg = "unexpected special character"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1007 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1008 | } else { |
| 1009 | *p++ = ch; |
| 1010 | } |
| 1011 | } else { |
| 1012 | charsleft = (charsleft << 6) | UB64(ch); |
| 1013 | bitsleft += 6; |
| 1014 | s++; |
| 1015 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1016 | } |
| 1017 | } |
| 1018 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1019 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1020 | s++; |
| 1021 | if (s < e && *s == '-') { |
| 1022 | s++; |
| 1023 | *p++ = '+'; |
| 1024 | } else |
| 1025 | { |
| 1026 | inShift = 1; |
| 1027 | bitsleft = 0; |
| 1028 | } |
| 1029 | } |
| 1030 | else if (SPECIAL(ch,0,0)) { |
Walter Dörwald | 9d04542 | 2007-08-30 15:34:55 +0000 | [diff] [blame] | 1031 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1032 | errmsg = "unexpected special character"; |
| 1033 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1034 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1035 | } |
| 1036 | else { |
| 1037 | *p++ = ch; |
| 1038 | s++; |
| 1039 | } |
| 1040 | continue; |
| 1041 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1042 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1043 | endinpos = s-starts; |
| 1044 | if (unicode_decode_call_errorhandler( |
| 1045 | errors, &errorHandler, |
| 1046 | "utf7", errmsg, |
| 1047 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1048 | (PyObject **)&unicode, &outpos, &p)) |
| 1049 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | if (inShift) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1053 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1054 | endinpos = size; |
| 1055 | if (unicode_decode_call_errorhandler( |
| 1056 | errors, &errorHandler, |
| 1057 | "utf7", "unterminated shift sequence", |
| 1058 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1059 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1060 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1061 | if (s < e) |
| 1062 | goto restart; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1065 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1066 | goto onError; |
| 1067 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1068 | Py_XDECREF(errorHandler); |
| 1069 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1070 | return (PyObject *)unicode; |
| 1071 | |
| 1072 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1073 | Py_XDECREF(errorHandler); |
| 1074 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1075 | Py_DECREF(unicode); |
| 1076 | return NULL; |
| 1077 | } |
| 1078 | |
| 1079 | |
| 1080 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1081 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1082 | int encodeSetO, |
| 1083 | int encodeWhiteSpace, |
| 1084 | const char *errors) |
| 1085 | { |
| 1086 | PyObject *v; |
| 1087 | /* It might be possible to tighten this worst case */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1088 | Py_ssize_t cbAllocated = 5 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1089 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1090 | Py_ssize_t i = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1091 | unsigned int bitsleft = 0; |
| 1092 | unsigned long charsleft = 0; |
| 1093 | char * out; |
| 1094 | char * start; |
| 1095 | |
| 1096 | if (size == 0) |
| 1097 | return PyString_FromStringAndSize(NULL, 0); |
| 1098 | |
| 1099 | v = PyString_FromStringAndSize(NULL, cbAllocated); |
| 1100 | if (v == NULL) |
| 1101 | return NULL; |
| 1102 | |
| 1103 | start = out = PyString_AS_STRING(v); |
| 1104 | for (;i < size; ++i) { |
| 1105 | Py_UNICODE ch = s[i]; |
| 1106 | |
| 1107 | if (!inShift) { |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1108 | if (ch == '+') { |
| 1109 | *out++ = '+'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1110 | *out++ = '-'; |
| 1111 | } else if (SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1112 | charsleft = ch; |
| 1113 | bitsleft = 16; |
| 1114 | *out++ = '+'; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1115 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1116 | inShift = bitsleft > 0; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1117 | } else { |
| 1118 | *out++ = (char) ch; |
| 1119 | } |
| 1120 | } else { |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1121 | if (!SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1122 | *out++ = B64(charsleft << (6-bitsleft)); |
| 1123 | charsleft = 0; |
| 1124 | bitsleft = 0; |
| 1125 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 1126 | so no '-' is required, except if the character is itself a '-' */ |
| 1127 | if (B64CHAR(ch) || ch == '-') { |
| 1128 | *out++ = '-'; |
| 1129 | } |
| 1130 | inShift = 0; |
| 1131 | *out++ = (char) ch; |
| 1132 | } else { |
| 1133 | bitsleft += 16; |
| 1134 | charsleft = (charsleft << 16) | ch; |
| 1135 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
| 1136 | |
| 1137 | /* If the next character is special then we dont' need to terminate |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1138 | 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] | 1139 | or '-' then the shift sequence will be terminated implicitly and we |
| 1140 | don't have to insert a '-'. */ |
| 1141 | |
| 1142 | if (bitsleft == 0) { |
| 1143 | if (i + 1 < size) { |
| 1144 | Py_UNICODE ch2 = s[i+1]; |
| 1145 | |
| 1146 | if (SPECIAL(ch2, encodeSetO, encodeWhiteSpace)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1147 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1148 | } else if (B64CHAR(ch2) || ch2 == '-') { |
| 1149 | *out++ = '-'; |
| 1150 | inShift = 0; |
| 1151 | } else { |
| 1152 | inShift = 0; |
| 1153 | } |
| 1154 | |
| 1155 | } |
| 1156 | else { |
| 1157 | *out++ = '-'; |
| 1158 | inShift = 0; |
| 1159 | } |
| 1160 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1161 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1162 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1163 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1164 | if (bitsleft) { |
| 1165 | *out++= B64(charsleft << (6-bitsleft) ); |
| 1166 | *out++ = '-'; |
| 1167 | } |
| 1168 | |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 1169 | _PyString_Resize(&v, out - start); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1170 | return v; |
| 1171 | } |
| 1172 | |
| 1173 | #undef SPECIAL |
| 1174 | #undef B64 |
| 1175 | #undef B64CHAR |
| 1176 | #undef UB64 |
| 1177 | #undef ENCODE |
| 1178 | #undef DECODE |
| 1179 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1180 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 1181 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1182 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1183 | char utf8_code_length[256] = { |
| 1184 | /* Map UTF-8 encoded prefix byte to sequence length. zero means |
| 1185 | illegal prefix. see RFC 2279 for details */ |
| 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 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1193 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 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 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1197 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1198 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1199 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1200 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 1201 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 |
| 1202 | }; |
| 1203 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1204 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1205 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1206 | const char *errors) |
| 1207 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1208 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 1209 | } |
| 1210 | |
| 1211 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1212 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1213 | const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1214 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1215 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1216 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1217 | int n; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1218 | Py_ssize_t startinpos; |
| 1219 | Py_ssize_t endinpos; |
| 1220 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1221 | const char *e; |
| 1222 | PyUnicodeObject *unicode; |
| 1223 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1224 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1225 | PyObject *errorHandler = NULL; |
| 1226 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1227 | |
| 1228 | /* Note: size will always be longer than the resulting Unicode |
| 1229 | character count */ |
| 1230 | unicode = _PyUnicode_New(size); |
| 1231 | if (!unicode) |
| 1232 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1233 | if (size == 0) { |
| 1234 | if (consumed) |
| 1235 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1236 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1237 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1238 | |
| 1239 | /* Unpack UTF-8 encoded data */ |
| 1240 | p = unicode->str; |
| 1241 | e = s + size; |
| 1242 | |
| 1243 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1244 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1245 | |
| 1246 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1247 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1248 | s++; |
| 1249 | continue; |
| 1250 | } |
| 1251 | |
| 1252 | n = utf8_code_length[ch]; |
| 1253 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1254 | if (s + n > e) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1255 | if (consumed) |
| 1256 | break; |
| 1257 | else { |
| 1258 | errmsg = "unexpected end of data"; |
| 1259 | startinpos = s-starts; |
| 1260 | endinpos = size; |
| 1261 | goto utf8Error; |
| 1262 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1263 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1264 | |
| 1265 | switch (n) { |
| 1266 | |
| 1267 | case 0: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1268 | errmsg = "unexpected code byte"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1269 | startinpos = s-starts; |
| 1270 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1271 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1272 | |
| 1273 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1274 | errmsg = "internal error"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1275 | startinpos = s-starts; |
| 1276 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1277 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1278 | |
| 1279 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1280 | if ((s[1] & 0xc0) != 0x80) { |
| 1281 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1282 | startinpos = s-starts; |
| 1283 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1284 | goto utf8Error; |
| 1285 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1286 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1287 | if (ch < 0x80) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1288 | startinpos = s-starts; |
| 1289 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1290 | errmsg = "illegal encoding"; |
| 1291 | goto utf8Error; |
| 1292 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1293 | else |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1294 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1295 | break; |
| 1296 | |
| 1297 | case 3: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1298 | if ((s[1] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1299 | (s[2] & 0xc0) != 0x80) { |
| 1300 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1301 | startinpos = s-starts; |
| 1302 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1303 | goto utf8Error; |
| 1304 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1305 | 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] | 1306 | if (ch < 0x0800) { |
| 1307 | /* Note: UTF-8 encodings of surrogates are considered |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1308 | legal UTF-8 sequences; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1309 | |
| 1310 | XXX For wide builds (UCS-4) we should probably try |
| 1311 | to recombine the surrogates into a single code |
| 1312 | unit. |
| 1313 | */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1314 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1315 | startinpos = s-starts; |
| 1316 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1317 | goto utf8Error; |
| 1318 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1319 | else |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1320 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1321 | break; |
| 1322 | |
| 1323 | case 4: |
| 1324 | if ((s[1] & 0xc0) != 0x80 || |
| 1325 | (s[2] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1326 | (s[3] & 0xc0) != 0x80) { |
| 1327 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1328 | startinpos = s-starts; |
| 1329 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1330 | goto utf8Error; |
| 1331 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1332 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 1333 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 1334 | /* validate and convert to UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1335 | if ((ch < 0x10000) /* minimum value allowed for 4 |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1336 | byte encoding */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1337 | || (ch > 0x10ffff)) /* maximum value allowed for |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1338 | UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1339 | { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1340 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1341 | startinpos = s-starts; |
| 1342 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1343 | goto utf8Error; |
| 1344 | } |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1345 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1346 | *p++ = (Py_UNICODE)ch; |
| 1347 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1348 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1349 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1350 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 1351 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1352 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1353 | /* high surrogate = top 10 bits added to D800 */ |
| 1354 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1355 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1356 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 1357 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1358 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1359 | break; |
| 1360 | |
| 1361 | default: |
| 1362 | /* Other sizes are only needed for UCS-4 */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1363 | errmsg = "unsupported Unicode code range"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1364 | startinpos = s-starts; |
| 1365 | endinpos = startinpos+n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1366 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1367 | } |
| 1368 | s += n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1369 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1370 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1371 | utf8Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1372 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1373 | if (unicode_decode_call_errorhandler( |
| 1374 | errors, &errorHandler, |
| 1375 | "utf8", errmsg, |
| 1376 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1377 | (PyObject **)&unicode, &outpos, &p)) |
| 1378 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1379 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1380 | if (consumed) |
| 1381 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1382 | |
| 1383 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1384 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1385 | goto onError; |
| 1386 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1387 | Py_XDECREF(errorHandler); |
| 1388 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1389 | return (PyObject *)unicode; |
| 1390 | |
| 1391 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1392 | Py_XDECREF(errorHandler); |
| 1393 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1394 | Py_DECREF(unicode); |
| 1395 | return NULL; |
| 1396 | } |
| 1397 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1398 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 1399 | and allocate exactly as much space needed at the end. Else allocate the |
| 1400 | maximum possible needed (4 result bytes per Unicode character), and return |
| 1401 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1402 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1403 | PyObject * |
| 1404 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1405 | Py_ssize_t size, |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1406 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1407 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1408 | #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] | 1409 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1410 | Py_ssize_t i; /* index into s of next input byte */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1411 | PyObject *v; /* result string object */ |
| 1412 | char *p; /* next free byte in output buffer */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1413 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 1414 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1415 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1416 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1417 | assert(s != NULL); |
| 1418 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1419 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1420 | if (size <= MAX_SHORT_UNICHARS) { |
| 1421 | /* Write into the stack buffer; nallocated can't overflow. |
| 1422 | * At the end, we'll allocate exactly as much heap space as it |
| 1423 | * turns out we need. |
| 1424 | */ |
| 1425 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
| 1426 | v = NULL; /* will allocate after we're done */ |
| 1427 | p = stackbuf; |
| 1428 | } |
| 1429 | else { |
| 1430 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 1431 | nallocated = size * 4; |
| 1432 | if (nallocated / 4 != size) /* overflow! */ |
| 1433 | return PyErr_NoMemory(); |
| 1434 | v = PyString_FromStringAndSize(NULL, nallocated); |
| 1435 | if (v == NULL) |
| 1436 | return NULL; |
| 1437 | p = PyString_AS_STRING(v); |
| 1438 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1439 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1440 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1441 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1442 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1443 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1444 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1445 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1446 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1447 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1448 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 1449 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 1450 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1451 | } |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1452 | else { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1453 | /* Encode UCS2 Unicode ordinals */ |
| 1454 | if (ch < 0x10000) { |
| 1455 | /* Special case: check for high surrogate */ |
| 1456 | if (0xD800 <= ch && ch <= 0xDBFF && i != size) { |
| 1457 | Py_UCS4 ch2 = s[i]; |
| 1458 | /* Check for low surrogate and combine the two to |
| 1459 | form a UCS4 value */ |
| 1460 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1461 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1462 | i++; |
| 1463 | goto encodeUCS4; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1464 | } |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1465 | /* Fall through: handles isolated high surrogates */ |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1466 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1467 | *p++ = (char)(0xe0 | (ch >> 12)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1468 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 1469 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 1470 | continue; |
| 1471 | } |
| 1472 | encodeUCS4: |
| 1473 | /* Encode UCS4 Unicode ordinals */ |
| 1474 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 1475 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 1476 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 1477 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 1478 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1479 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 1480 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1481 | if (v == NULL) { |
| 1482 | /* This was stack allocated. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 1483 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1484 | assert(nneeded <= nallocated); |
| 1485 | v = PyString_FromStringAndSize(stackbuf, nneeded); |
| 1486 | } |
| 1487 | else { |
| 1488 | /* Cut back to size actually needed. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 1489 | nneeded = p - PyString_AS_STRING(v); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1490 | assert(nneeded <= nallocated); |
| 1491 | _PyString_Resize(&v, nneeded); |
| 1492 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1493 | return v; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1494 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1495 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1496 | } |
| 1497 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1498 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 1499 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1500 | if (!PyUnicode_Check(unicode)) { |
| 1501 | PyErr_BadArgument(); |
| 1502 | return NULL; |
| 1503 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 1504 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 1505 | PyUnicode_GET_SIZE(unicode), |
| 1506 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 1509 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 1510 | |
| 1511 | PyObject * |
| 1512 | PyUnicode_DecodeUTF32(const char *s, |
| 1513 | Py_ssize_t size, |
| 1514 | const char *errors, |
| 1515 | int *byteorder) |
| 1516 | { |
| 1517 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 1518 | } |
| 1519 | |
| 1520 | PyObject * |
| 1521 | PyUnicode_DecodeUTF32Stateful(const char *s, |
| 1522 | Py_ssize_t size, |
| 1523 | const char *errors, |
| 1524 | int *byteorder, |
| 1525 | Py_ssize_t *consumed) |
| 1526 | { |
| 1527 | const char *starts = s; |
| 1528 | Py_ssize_t startinpos; |
| 1529 | Py_ssize_t endinpos; |
| 1530 | Py_ssize_t outpos; |
| 1531 | PyUnicodeObject *unicode; |
| 1532 | Py_UNICODE *p; |
| 1533 | #ifndef Py_UNICODE_WIDE |
| 1534 | int i, pairs; |
| 1535 | #else |
| 1536 | const int pairs = 0; |
| 1537 | #endif |
| 1538 | const unsigned char *q, *e; |
| 1539 | int bo = 0; /* assume native ordering by default */ |
| 1540 | const char *errmsg = ""; |
Walter Dörwald | 20b40d3 | 2007-08-17 16:52:50 +0000 | [diff] [blame] | 1541 | /* Offsets from q for retrieving bytes in the right order. */ |
| 1542 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 1543 | int iorder[] = {0, 1, 2, 3}; |
| 1544 | #else |
| 1545 | int iorder[] = {3, 2, 1, 0}; |
| 1546 | #endif |
Walter Dörwald | 9ab80a9 | 2007-08-17 16:58:43 +0000 | [diff] [blame] | 1547 | PyObject *errorHandler = NULL; |
| 1548 | PyObject *exc = NULL; |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 1549 | /* On narrow builds we split characters outside the BMP into two |
| 1550 | codepoints => count how much extra space we need. */ |
| 1551 | #ifndef Py_UNICODE_WIDE |
| 1552 | for (i = pairs = 0; i < size/4; i++) |
| 1553 | if (((Py_UCS4 *)s)[i] >= 0x10000) |
| 1554 | pairs++; |
| 1555 | #endif |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 1556 | |
| 1557 | /* This might be one to much, because of a BOM */ |
| 1558 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 1559 | if (!unicode) |
| 1560 | return NULL; |
| 1561 | if (size == 0) |
| 1562 | return (PyObject *)unicode; |
| 1563 | |
| 1564 | /* Unpack UTF-32 encoded data */ |
| 1565 | p = unicode->str; |
| 1566 | q = (unsigned char *)s; |
| 1567 | e = q + size; |
| 1568 | |
| 1569 | if (byteorder) |
| 1570 | bo = *byteorder; |
| 1571 | |
| 1572 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 1573 | byte order setting accordingly. In native mode, the leading BOM |
| 1574 | mark is skipped, in all other modes, it is copied to the output |
| 1575 | stream as-is (giving a ZWNBSP character). */ |
| 1576 | if (bo == 0) { |
| 1577 | if (size >= 4) { |
| 1578 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 1579 | (q[iorder[1]] << 8) | q[iorder[0]]; |
| 1580 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 1581 | if (bom == 0x0000FEFF) { |
| 1582 | q += 4; |
| 1583 | bo = -1; |
| 1584 | } |
| 1585 | else if (bom == 0xFFFE0000) { |
| 1586 | q += 4; |
| 1587 | bo = 1; |
| 1588 | } |
| 1589 | #else |
| 1590 | if (bom == 0x0000FEFF) { |
| 1591 | q += 4; |
| 1592 | bo = 1; |
| 1593 | } |
| 1594 | else if (bom == 0xFFFE0000) { |
| 1595 | q += 4; |
| 1596 | bo = -1; |
| 1597 | } |
| 1598 | #endif |
| 1599 | } |
| 1600 | } |
| 1601 | |
| 1602 | if (bo == -1) { |
| 1603 | /* force LE */ |
| 1604 | iorder[0] = 0; |
| 1605 | iorder[1] = 1; |
| 1606 | iorder[2] = 2; |
| 1607 | iorder[3] = 3; |
| 1608 | } |
| 1609 | else if (bo == 1) { |
| 1610 | /* force BE */ |
| 1611 | iorder[0] = 3; |
| 1612 | iorder[1] = 2; |
| 1613 | iorder[2] = 1; |
| 1614 | iorder[3] = 0; |
| 1615 | } |
| 1616 | |
| 1617 | while (q < e) { |
| 1618 | Py_UCS4 ch; |
| 1619 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 1620 | if (e-q<4) { |
| 1621 | if (consumed) |
| 1622 | break; |
| 1623 | errmsg = "truncated data"; |
| 1624 | startinpos = ((const char *)q)-starts; |
| 1625 | endinpos = ((const char *)e)-starts; |
| 1626 | goto utf32Error; |
| 1627 | /* The remaining input chars are ignored if the callback |
| 1628 | chooses to skip the input */ |
| 1629 | } |
| 1630 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 1631 | (q[iorder[1]] << 8) | q[iorder[0]]; |
| 1632 | |
| 1633 | if (ch >= 0x110000) |
| 1634 | { |
| 1635 | errmsg = "codepoint not in range(0x110000)"; |
| 1636 | startinpos = ((const char *)q)-starts; |
| 1637 | endinpos = startinpos+4; |
| 1638 | goto utf32Error; |
| 1639 | } |
| 1640 | #ifndef Py_UNICODE_WIDE |
| 1641 | if (ch >= 0x10000) |
| 1642 | { |
| 1643 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 1644 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 1645 | } |
| 1646 | else |
| 1647 | #endif |
| 1648 | *p++ = ch; |
| 1649 | q += 4; |
| 1650 | continue; |
| 1651 | utf32Error: |
| 1652 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1653 | if (unicode_decode_call_errorhandler( |
| 1654 | errors, &errorHandler, |
| 1655 | "utf32", errmsg, |
| 1656 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1657 | (PyObject **)&unicode, &outpos, &p)) |
| 1658 | goto onError; |
| 1659 | } |
| 1660 | |
| 1661 | if (byteorder) |
| 1662 | *byteorder = bo; |
| 1663 | |
| 1664 | if (consumed) |
| 1665 | *consumed = (const char *)q-starts; |
| 1666 | |
| 1667 | /* Adjust length */ |
| 1668 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
| 1669 | goto onError; |
| 1670 | |
| 1671 | Py_XDECREF(errorHandler); |
| 1672 | Py_XDECREF(exc); |
| 1673 | return (PyObject *)unicode; |
| 1674 | |
| 1675 | onError: |
| 1676 | Py_DECREF(unicode); |
| 1677 | Py_XDECREF(errorHandler); |
| 1678 | Py_XDECREF(exc); |
| 1679 | return NULL; |
| 1680 | } |
| 1681 | |
| 1682 | PyObject * |
| 1683 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 1684 | Py_ssize_t size, |
| 1685 | const char *errors, |
| 1686 | int byteorder) |
| 1687 | { |
| 1688 | PyObject *v; |
| 1689 | unsigned char *p; |
| 1690 | #ifndef Py_UNICODE_WIDE |
| 1691 | int i, pairs; |
| 1692 | #else |
| 1693 | const int pairs = 0; |
| 1694 | #endif |
| 1695 | /* Offsets from p for storing byte pairs in the right order. */ |
| 1696 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 1697 | int iorder[] = {0, 1, 2, 3}; |
| 1698 | #else |
| 1699 | int iorder[] = {3, 2, 1, 0}; |
| 1700 | #endif |
| 1701 | |
| 1702 | #define STORECHAR(CH) \ |
| 1703 | do { \ |
| 1704 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 1705 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 1706 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 1707 | p[iorder[0]] = (CH) & 0xff; \ |
| 1708 | p += 4; \ |
| 1709 | } while(0) |
| 1710 | |
| 1711 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 1712 | so we need less space. */ |
| 1713 | #ifndef Py_UNICODE_WIDE |
| 1714 | for (i = pairs = 0; i < size-1; i++) |
| 1715 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 1716 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 1717 | pairs++; |
| 1718 | #endif |
| 1719 | v = PyString_FromStringAndSize(NULL, |
| 1720 | 4 * (size - pairs + (byteorder == 0))); |
| 1721 | if (v == NULL) |
| 1722 | return NULL; |
| 1723 | |
| 1724 | p = (unsigned char *)PyString_AS_STRING(v); |
| 1725 | if (byteorder == 0) |
| 1726 | STORECHAR(0xFEFF); |
| 1727 | if (size == 0) |
| 1728 | return v; |
| 1729 | |
| 1730 | if (byteorder == -1) { |
| 1731 | /* force LE */ |
| 1732 | iorder[0] = 0; |
| 1733 | iorder[1] = 1; |
| 1734 | iorder[2] = 2; |
| 1735 | iorder[3] = 3; |
| 1736 | } |
| 1737 | else if (byteorder == 1) { |
| 1738 | /* force BE */ |
| 1739 | iorder[0] = 3; |
| 1740 | iorder[1] = 2; |
| 1741 | iorder[2] = 1; |
| 1742 | iorder[3] = 0; |
| 1743 | } |
| 1744 | |
| 1745 | while (size-- > 0) { |
| 1746 | Py_UCS4 ch = *s++; |
| 1747 | #ifndef Py_UNICODE_WIDE |
| 1748 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 1749 | Py_UCS4 ch2 = *s; |
| 1750 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 1751 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 1752 | s++; |
| 1753 | size--; |
| 1754 | } |
| 1755 | } |
| 1756 | #endif |
| 1757 | STORECHAR(ch); |
| 1758 | } |
| 1759 | return v; |
| 1760 | #undef STORECHAR |
| 1761 | } |
| 1762 | |
| 1763 | PyObject *PyUnicode_AsUTF32String(PyObject *unicode) |
| 1764 | { |
| 1765 | if (!PyUnicode_Check(unicode)) { |
| 1766 | PyErr_BadArgument(); |
| 1767 | return NULL; |
| 1768 | } |
| 1769 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
| 1770 | PyUnicode_GET_SIZE(unicode), |
| 1771 | NULL, |
| 1772 | 0); |
| 1773 | } |
| 1774 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1775 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 1776 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1777 | PyObject * |
| 1778 | PyUnicode_DecodeUTF16(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1779 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1780 | const char *errors, |
| 1781 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1782 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1783 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 1784 | } |
| 1785 | |
| 1786 | PyObject * |
| 1787 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1788 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1789 | const char *errors, |
| 1790 | int *byteorder, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1791 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1792 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1793 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1794 | Py_ssize_t startinpos; |
| 1795 | Py_ssize_t endinpos; |
| 1796 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1797 | PyUnicodeObject *unicode; |
| 1798 | Py_UNICODE *p; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1799 | const unsigned char *q, *e; |
| 1800 | int bo = 0; /* assume native ordering by default */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1801 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1802 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 1803 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 1804 | int ihi = 1, ilo = 0; |
| 1805 | #else |
| 1806 | int ihi = 0, ilo = 1; |
| 1807 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1808 | PyObject *errorHandler = NULL; |
| 1809 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1810 | |
| 1811 | /* Note: size will always be longer than the resulting Unicode |
| 1812 | character count */ |
| 1813 | unicode = _PyUnicode_New(size); |
| 1814 | if (!unicode) |
| 1815 | return NULL; |
| 1816 | if (size == 0) |
| 1817 | return (PyObject *)unicode; |
| 1818 | |
| 1819 | /* Unpack UTF-16 encoded data */ |
| 1820 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1821 | q = (unsigned char *)s; |
| 1822 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1823 | |
| 1824 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1825 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1826 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1827 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 1828 | byte order setting accordingly. In native mode, the leading BOM |
| 1829 | mark is skipped, in all other modes, it is copied to the output |
| 1830 | stream as-is (giving a ZWNBSP character). */ |
| 1831 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1832 | if (size >= 2) { |
| 1833 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1834 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1835 | if (bom == 0xFEFF) { |
| 1836 | q += 2; |
| 1837 | bo = -1; |
| 1838 | } |
| 1839 | else if (bom == 0xFFFE) { |
| 1840 | q += 2; |
| 1841 | bo = 1; |
| 1842 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1843 | #else |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1844 | if (bom == 0xFEFF) { |
| 1845 | q += 2; |
| 1846 | bo = 1; |
| 1847 | } |
| 1848 | else if (bom == 0xFFFE) { |
| 1849 | q += 2; |
| 1850 | bo = -1; |
| 1851 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1852 | #endif |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1853 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1854 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1855 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1856 | if (bo == -1) { |
| 1857 | /* force LE */ |
| 1858 | ihi = 1; |
| 1859 | ilo = 0; |
| 1860 | } |
| 1861 | else if (bo == 1) { |
| 1862 | /* force BE */ |
| 1863 | ihi = 0; |
| 1864 | ilo = 1; |
| 1865 | } |
| 1866 | |
| 1867 | while (q < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1868 | Py_UNICODE ch; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1869 | /* remaining bytes at the end? (size should be even) */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1870 | if (e-q<2) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1871 | if (consumed) |
| 1872 | break; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1873 | errmsg = "truncated data"; |
| 1874 | startinpos = ((const char *)q)-starts; |
| 1875 | endinpos = ((const char *)e)-starts; |
| 1876 | goto utf16Error; |
| 1877 | /* The remaining input chars are ignored if the callback |
| 1878 | chooses to skip the input */ |
| 1879 | } |
| 1880 | ch = (q[ihi] << 8) | q[ilo]; |
| 1881 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1882 | q += 2; |
| 1883 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1884 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 1885 | *p++ = ch; |
| 1886 | continue; |
| 1887 | } |
| 1888 | |
| 1889 | /* UTF-16 code pair: */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1890 | if (q >= e) { |
| 1891 | errmsg = "unexpected end of data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1892 | startinpos = (((const char *)q)-2)-starts; |
| 1893 | endinpos = ((const char *)e)-starts; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1894 | goto utf16Error; |
| 1895 | } |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 1896 | if (0xD800 <= ch && ch <= 0xDBFF) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1897 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 1898 | q += 2; |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 1899 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1900 | #ifndef Py_UNICODE_WIDE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1901 | *p++ = ch; |
| 1902 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1903 | #else |
| 1904 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1905 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1906 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1907 | } |
| 1908 | else { |
| 1909 | errmsg = "illegal UTF-16 surrogate"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1910 | startinpos = (((const char *)q)-4)-starts; |
| 1911 | endinpos = startinpos+2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1912 | goto utf16Error; |
| 1913 | } |
| 1914 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1915 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1916 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1917 | startinpos = (((const char *)q)-2)-starts; |
| 1918 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1919 | /* Fall through to report the error */ |
| 1920 | |
| 1921 | utf16Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1922 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1923 | if (unicode_decode_call_errorhandler( |
| 1924 | errors, &errorHandler, |
| 1925 | "utf16", errmsg, |
| 1926 | starts, size, &startinpos, &endinpos, &exc, (const char **)&q, |
| 1927 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1928 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
| 1931 | if (byteorder) |
| 1932 | *byteorder = bo; |
| 1933 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1934 | if (consumed) |
| 1935 | *consumed = (const char *)q-starts; |
| 1936 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1937 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1938 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1939 | goto onError; |
| 1940 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1941 | Py_XDECREF(errorHandler); |
| 1942 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1943 | return (PyObject *)unicode; |
| 1944 | |
| 1945 | onError: |
| 1946 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1947 | Py_XDECREF(errorHandler); |
| 1948 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1949 | return NULL; |
| 1950 | } |
| 1951 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1952 | PyObject * |
| 1953 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1954 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1955 | const char *errors, |
| 1956 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1957 | { |
| 1958 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1959 | unsigned char *p; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1960 | #ifdef Py_UNICODE_WIDE |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1961 | int i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1962 | #else |
| 1963 | const int pairs = 0; |
| 1964 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1965 | /* Offsets from p for storing byte pairs in the right order. */ |
| 1966 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 1967 | int ihi = 1, ilo = 0; |
| 1968 | #else |
| 1969 | int ihi = 0, ilo = 1; |
| 1970 | #endif |
| 1971 | |
| 1972 | #define STORECHAR(CH) \ |
| 1973 | do { \ |
| 1974 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 1975 | p[ilo] = (CH) & 0xff; \ |
| 1976 | p += 2; \ |
| 1977 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1978 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1979 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1980 | for (i = pairs = 0; i < size; i++) |
| 1981 | if (s[i] >= 0x10000) |
| 1982 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1983 | #endif |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1984 | v = PyString_FromStringAndSize(NULL, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1985 | 2 * (size + pairs + (byteorder == 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1986 | if (v == NULL) |
| 1987 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1988 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1989 | p = (unsigned char *)PyString_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1990 | if (byteorder == 0) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1991 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 1992 | if (size == 0) |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 1993 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1994 | |
| 1995 | if (byteorder == -1) { |
| 1996 | /* force LE */ |
| 1997 | ihi = 1; |
| 1998 | ilo = 0; |
| 1999 | } |
| 2000 | else if (byteorder == 1) { |
| 2001 | /* force BE */ |
| 2002 | ihi = 0; |
| 2003 | ilo = 1; |
| 2004 | } |
| 2005 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2006 | while (size-- > 0) { |
| 2007 | Py_UNICODE ch = *s++; |
| 2008 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2009 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2010 | if (ch >= 0x10000) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2011 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2012 | ch = 0xD800 | ((ch-0x10000) >> 10); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2013 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2014 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2015 | STORECHAR(ch); |
| 2016 | if (ch2) |
| 2017 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2018 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2019 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2020 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2021 | } |
| 2022 | |
| 2023 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 2024 | { |
| 2025 | if (!PyUnicode_Check(unicode)) { |
| 2026 | PyErr_BadArgument(); |
| 2027 | return NULL; |
| 2028 | } |
| 2029 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
| 2030 | PyUnicode_GET_SIZE(unicode), |
| 2031 | NULL, |
| 2032 | 0); |
| 2033 | } |
| 2034 | |
| 2035 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 2036 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2037 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 2038 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2039 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2040 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2041 | const char *errors) |
| 2042 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2043 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2044 | Py_ssize_t startinpos; |
| 2045 | Py_ssize_t endinpos; |
| 2046 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2047 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2048 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2049 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2050 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2051 | char* message; |
| 2052 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2053 | PyObject *errorHandler = NULL; |
| 2054 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2055 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2056 | /* Escaped strings will always be longer than the resulting |
| 2057 | 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] | 2058 | length after conversion to the true value. |
| 2059 | (but if the error callback returns a long replacement string |
| 2060 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2061 | v = _PyUnicode_New(size); |
| 2062 | if (v == NULL) |
| 2063 | goto onError; |
| 2064 | if (size == 0) |
| 2065 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2066 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2067 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2068 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2069 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2070 | while (s < end) { |
| 2071 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2072 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2073 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2074 | |
| 2075 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 2076 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2077 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2078 | continue; |
| 2079 | } |
| 2080 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2081 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2082 | /* \ - Escapes */ |
| 2083 | s++; |
Guido van Rossum | 1c1ac38 | 2007-10-29 22:15:05 +0000 | [diff] [blame] | 2084 | c = *s++; |
| 2085 | if (s > end) |
| 2086 | c = '\0'; /* Invalid after \ */ |
| 2087 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2088 | |
| 2089 | /* \x escapes */ |
| 2090 | case '\n': break; |
| 2091 | case '\\': *p++ = '\\'; break; |
| 2092 | case '\'': *p++ = '\''; break; |
| 2093 | case '\"': *p++ = '\"'; break; |
| 2094 | case 'b': *p++ = '\b'; break; |
| 2095 | case 'f': *p++ = '\014'; break; /* FF */ |
| 2096 | case 't': *p++ = '\t'; break; |
| 2097 | case 'n': *p++ = '\n'; break; |
| 2098 | case 'r': *p++ = '\r'; break; |
| 2099 | case 'v': *p++ = '\013'; break; /* VT */ |
| 2100 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 2101 | |
| 2102 | /* \OOO (octal) escapes */ |
| 2103 | case '0': case '1': case '2': case '3': |
| 2104 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2105 | x = s[-1] - '0'; |
Guido van Rossum | 1c1ac38 | 2007-10-29 22:15:05 +0000 | [diff] [blame] | 2106 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2107 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 1c1ac38 | 2007-10-29 22:15:05 +0000 | [diff] [blame] | 2108 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2109 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2110 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2111 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2112 | break; |
| 2113 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2114 | /* hex escapes */ |
| 2115 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2116 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2117 | digits = 2; |
| 2118 | message = "truncated \\xXX escape"; |
| 2119 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2120 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2121 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2122 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2123 | digits = 4; |
| 2124 | message = "truncated \\uXXXX escape"; |
| 2125 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2126 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2127 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2128 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2129 | digits = 8; |
| 2130 | message = "truncated \\UXXXXXXXX escape"; |
| 2131 | hexescape: |
| 2132 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2133 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2134 | if (s+digits>end) { |
| 2135 | endinpos = size; |
| 2136 | if (unicode_decode_call_errorhandler( |
| 2137 | errors, &errorHandler, |
| 2138 | "unicodeescape", "end of string in escape sequence", |
| 2139 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2140 | (PyObject **)&v, &outpos, &p)) |
| 2141 | goto onError; |
| 2142 | goto nextByte; |
| 2143 | } |
| 2144 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2145 | c = (unsigned char) s[i]; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2146 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2147 | endinpos = (s+i+1)-starts; |
| 2148 | if (unicode_decode_call_errorhandler( |
| 2149 | errors, &errorHandler, |
| 2150 | "unicodeescape", message, |
| 2151 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2152 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2153 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2154 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2155 | } |
| 2156 | chr = (chr<<4) & ~0xF; |
| 2157 | if (c >= '0' && c <= '9') |
| 2158 | chr += c - '0'; |
| 2159 | else if (c >= 'a' && c <= 'f') |
| 2160 | chr += 10 + c - 'a'; |
| 2161 | else |
| 2162 | chr += 10 + c - 'A'; |
| 2163 | } |
| 2164 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 2165 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2166 | /* _decoding_error will have already written into the |
| 2167 | target buffer. */ |
| 2168 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2169 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2170 | /* when we get here, chr is a 32-bit unicode character */ |
| 2171 | if (chr <= 0xffff) |
| 2172 | /* UCS-2 character */ |
| 2173 | *p++ = (Py_UNICODE) chr; |
| 2174 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2175 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2176 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2177 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2178 | *p++ = chr; |
| 2179 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2180 | chr -= 0x10000L; |
| 2181 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2182 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2183 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2184 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2185 | endinpos = s-starts; |
| 2186 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2187 | if (unicode_decode_call_errorhandler( |
| 2188 | errors, &errorHandler, |
| 2189 | "unicodeescape", "illegal Unicode character", |
| 2190 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2191 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2192 | goto onError; |
| 2193 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2194 | break; |
| 2195 | |
| 2196 | /* \N{name} */ |
| 2197 | case 'N': |
| 2198 | message = "malformed \\N character escape"; |
| 2199 | if (ucnhash_CAPI == NULL) { |
| 2200 | /* load the unicode data module */ |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2201 | PyObject *m, *api; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2202 | m = PyImport_ImportModule("unicodedata"); |
| 2203 | if (m == NULL) |
| 2204 | goto ucnhashError; |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2205 | api = PyObject_GetAttrString(m, "ucnhash_CAPI"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2206 | Py_DECREF(m); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2207 | if (api == NULL) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2208 | goto ucnhashError; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2209 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCObject_AsVoidPtr(api); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2210 | Py_DECREF(api); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2211 | if (ucnhash_CAPI == NULL) |
| 2212 | goto ucnhashError; |
| 2213 | } |
| 2214 | if (*s == '{') { |
| 2215 | const char *start = s+1; |
| 2216 | /* look for the closing brace */ |
| 2217 | while (*s != '}' && s < end) |
| 2218 | s++; |
| 2219 | if (s > start && s < end && *s == '}') { |
| 2220 | /* found a name. look it up in the unicode database */ |
| 2221 | message = "unknown Unicode character name"; |
| 2222 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 2223 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2224 | goto store; |
| 2225 | } |
| 2226 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2227 | endinpos = s-starts; |
| 2228 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2229 | if (unicode_decode_call_errorhandler( |
| 2230 | errors, &errorHandler, |
| 2231 | "unicodeescape", message, |
| 2232 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2233 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2234 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2235 | break; |
| 2236 | |
| 2237 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2238 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2239 | message = "\\ at end of string"; |
| 2240 | s--; |
| 2241 | endinpos = s-starts; |
| 2242 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2243 | if (unicode_decode_call_errorhandler( |
| 2244 | errors, &errorHandler, |
| 2245 | "unicodeescape", message, |
| 2246 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2247 | (PyObject **)&v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2248 | goto onError; |
| 2249 | } |
| 2250 | else { |
| 2251 | *p++ = '\\'; |
| 2252 | *p++ = (unsigned char)s[-1]; |
| 2253 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2254 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2255 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2256 | nextByte: |
| 2257 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2258 | } |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2259 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2260 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 2261 | Py_XDECREF(errorHandler); |
| 2262 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2263 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2264 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2265 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2266 | PyErr_SetString( |
| 2267 | PyExc_UnicodeError, |
| 2268 | "\\N escapes not supported (can't load unicodedata module)" |
| 2269 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2270 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2271 | Py_XDECREF(errorHandler); |
| 2272 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 2273 | return NULL; |
| 2274 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2275 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2276 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2277 | Py_XDECREF(errorHandler); |
| 2278 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2279 | return NULL; |
| 2280 | } |
| 2281 | |
| 2282 | /* Return a Unicode-Escape string version of the Unicode object. |
| 2283 | |
| 2284 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 2285 | appropriate. |
| 2286 | |
| 2287 | */ |
| 2288 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 2289 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
Fredrik Lundh | 95e2a91 | 2006-05-26 11:38:15 +0000 | [diff] [blame] | 2290 | Py_ssize_t size, |
| 2291 | Py_UNICODE ch) |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 2292 | { |
| 2293 | /* like wcschr, but doesn't stop at NULL characters */ |
| 2294 | |
| 2295 | while (size-- > 0) { |
| 2296 | if (*s == ch) |
| 2297 | return s; |
| 2298 | s++; |
| 2299 | } |
| 2300 | |
| 2301 | return NULL; |
| 2302 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 2303 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2304 | static |
| 2305 | PyObject *unicodeescape_string(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2306 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2307 | int quotes) |
| 2308 | { |
| 2309 | PyObject *repr; |
| 2310 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2311 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2312 | static const char *hexdigit = "0123456789abcdef"; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2313 | |
Neal Norwitz | 17753ec | 2006-08-21 22:21:19 +0000 | [diff] [blame] | 2314 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 2315 | better to choose a different scheme. Perhaps scan the |
| 2316 | first N-chars of the string and allocate based on that size. |
| 2317 | */ |
| 2318 | /* Initial allocation is based on the longest-possible unichr |
| 2319 | escape. |
| 2320 | |
| 2321 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 2322 | unichr, so in this case it's the longest unichr escape. In |
| 2323 | narrow (UTF-16) builds this is five chars per source unichr |
| 2324 | since there are two unichrs in the surrogate pair, so in narrow |
| 2325 | (UTF-16) builds it's not the longest unichr escape. |
| 2326 | |
| 2327 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 2328 | so in the narrow (UTF-16) build case it's the longest unichr |
| 2329 | escape. |
| 2330 | */ |
| 2331 | |
| 2332 | repr = PyString_FromStringAndSize(NULL, |
| 2333 | 2 |
| 2334 | #ifdef Py_UNICODE_WIDE |
| 2335 | + 10*size |
| 2336 | #else |
| 2337 | + 6*size |
| 2338 | #endif |
| 2339 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2340 | if (repr == NULL) |
| 2341 | return NULL; |
| 2342 | |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2343 | p = PyString_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2344 | |
| 2345 | if (quotes) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2346 | *p++ = 'u'; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2347 | *p++ = (findchar(s, size, '\'') && |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2348 | !findchar(s, size, '"')) ? '"' : '\''; |
| 2349 | } |
| 2350 | while (size-- > 0) { |
| 2351 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2352 | |
Hye-Shik Chang | 835b243 | 2005-12-17 04:38:31 +0000 | [diff] [blame] | 2353 | /* Escape quotes and backslashes */ |
| 2354 | if ((quotes && |
| 2355 | ch == (Py_UNICODE) PyString_AS_STRING(repr)[1]) || ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2356 | *p++ = '\\'; |
| 2357 | *p++ = (char) ch; |
Guido van Rossum | ad9744a | 2001-09-21 15:38:17 +0000 | [diff] [blame] | 2358 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2359 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2360 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 2361 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2362 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 2363 | else if (ch >= 0x10000) { |
| 2364 | *p++ = '\\'; |
| 2365 | *p++ = 'U'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2366 | *p++ = hexdigit[(ch >> 28) & 0x0000000F]; |
| 2367 | *p++ = hexdigit[(ch >> 24) & 0x0000000F]; |
| 2368 | *p++ = hexdigit[(ch >> 20) & 0x0000000F]; |
| 2369 | *p++ = hexdigit[(ch >> 16) & 0x0000000F]; |
| 2370 | *p++ = hexdigit[(ch >> 12) & 0x0000000F]; |
| 2371 | *p++ = hexdigit[(ch >> 8) & 0x0000000F]; |
| 2372 | *p++ = hexdigit[(ch >> 4) & 0x0000000F]; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2373 | *p++ = hexdigit[ch & 0x0000000F]; |
| 2374 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2375 | } |
Neal Norwitz | 17753ec | 2006-08-21 22:21:19 +0000 | [diff] [blame] | 2376 | #else |
| 2377 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2378 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 2379 | Py_UNICODE ch2; |
| 2380 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2381 | |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2382 | ch2 = *s++; |
| 2383 | size--; |
| 2384 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 2385 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 2386 | *p++ = '\\'; |
| 2387 | *p++ = 'U'; |
| 2388 | *p++ = hexdigit[(ucs >> 28) & 0x0000000F]; |
| 2389 | *p++ = hexdigit[(ucs >> 24) & 0x0000000F]; |
| 2390 | *p++ = hexdigit[(ucs >> 20) & 0x0000000F]; |
| 2391 | *p++ = hexdigit[(ucs >> 16) & 0x0000000F]; |
| 2392 | *p++ = hexdigit[(ucs >> 12) & 0x0000000F]; |
| 2393 | *p++ = hexdigit[(ucs >> 8) & 0x0000000F]; |
| 2394 | *p++ = hexdigit[(ucs >> 4) & 0x0000000F]; |
| 2395 | *p++ = hexdigit[ucs & 0x0000000F]; |
| 2396 | continue; |
| 2397 | } |
| 2398 | /* Fall through: isolated surrogates are copied as-is */ |
| 2399 | s--; |
| 2400 | size++; |
| 2401 | } |
Neal Norwitz | 17753ec | 2006-08-21 22:21:19 +0000 | [diff] [blame] | 2402 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2403 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2404 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2405 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2406 | *p++ = '\\'; |
| 2407 | *p++ = 'u'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2408 | *p++ = hexdigit[(ch >> 12) & 0x000F]; |
| 2409 | *p++ = hexdigit[(ch >> 8) & 0x000F]; |
| 2410 | *p++ = hexdigit[(ch >> 4) & 0x000F]; |
| 2411 | *p++ = hexdigit[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2412 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2413 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2414 | /* Map special whitespace to '\t', \n', '\r' */ |
| 2415 | else if (ch == '\t') { |
| 2416 | *p++ = '\\'; |
| 2417 | *p++ = 't'; |
| 2418 | } |
| 2419 | else if (ch == '\n') { |
| 2420 | *p++ = '\\'; |
| 2421 | *p++ = 'n'; |
| 2422 | } |
| 2423 | else if (ch == '\r') { |
| 2424 | *p++ = '\\'; |
| 2425 | *p++ = 'r'; |
| 2426 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2427 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2428 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 2429 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2430 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2431 | *p++ = 'x'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2432 | *p++ = hexdigit[(ch >> 4) & 0x000F]; |
| 2433 | *p++ = hexdigit[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2434 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2435 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2436 | /* Copy everything else as-is */ |
| 2437 | else |
| 2438 | *p++ = (char) ch; |
| 2439 | } |
| 2440 | if (quotes) |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2441 | *p++ = PyString_AS_STRING(repr)[1]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2442 | |
| 2443 | *p = '\0'; |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 2444 | _PyString_Resize(&repr, p - PyString_AS_STRING(repr)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2445 | return repr; |
| 2446 | } |
| 2447 | |
| 2448 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2449 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2450 | { |
| 2451 | return unicodeescape_string(s, size, 0); |
| 2452 | } |
| 2453 | |
| 2454 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
| 2455 | { |
| 2456 | if (!PyUnicode_Check(unicode)) { |
| 2457 | PyErr_BadArgument(); |
| 2458 | return NULL; |
| 2459 | } |
| 2460 | return PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 2461 | PyUnicode_GET_SIZE(unicode)); |
| 2462 | } |
| 2463 | |
| 2464 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 2465 | |
| 2466 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2467 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2468 | const char *errors) |
| 2469 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2470 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2471 | Py_ssize_t startinpos; |
| 2472 | Py_ssize_t endinpos; |
| 2473 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2474 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2475 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2476 | const char *end; |
| 2477 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2478 | PyObject *errorHandler = NULL; |
| 2479 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2480 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2481 | /* Escaped strings will always be longer than the resulting |
| 2482 | 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] | 2483 | length after conversion to the true value. (But decoding error |
| 2484 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2485 | v = _PyUnicode_New(size); |
| 2486 | if (v == NULL) |
| 2487 | goto onError; |
| 2488 | if (size == 0) |
| 2489 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2490 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2491 | end = s + size; |
| 2492 | while (s < end) { |
| 2493 | unsigned char c; |
Martin v. Löwis | 047c05e | 2002-03-21 08:55:28 +0000 | [diff] [blame] | 2494 | Py_UCS4 x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2495 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2496 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2497 | |
| 2498 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 2499 | if (*s != '\\') { |
| 2500 | *p++ = (unsigned char)*s++; |
| 2501 | continue; |
| 2502 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2503 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2504 | |
| 2505 | /* \u-escapes are only interpreted iff the number of leading |
| 2506 | backslashes if odd */ |
| 2507 | bs = s; |
| 2508 | for (;s < end;) { |
| 2509 | if (*s != '\\') |
| 2510 | break; |
| 2511 | *p++ = (unsigned char)*s++; |
| 2512 | } |
| 2513 | if (((s - bs) & 1) == 0 || |
| 2514 | s >= end || |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2515 | (*s != 'u' && *s != 'U')) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2516 | continue; |
| 2517 | } |
| 2518 | p--; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2519 | count = *s=='u' ? 4 : 8; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2520 | s++; |
| 2521 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2522 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2523 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2524 | for (x = 0, i = 0; i < count; ++i, ++s) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2525 | c = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2526 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2527 | endinpos = s-starts; |
| 2528 | if (unicode_decode_call_errorhandler( |
| 2529 | errors, &errorHandler, |
| 2530 | "rawunicodeescape", "truncated \\uXXXX", |
| 2531 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2532 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2533 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2534 | goto nextByte; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2535 | } |
| 2536 | x = (x<<4) & ~0xF; |
| 2537 | if (c >= '0' && c <= '9') |
| 2538 | x += c - '0'; |
| 2539 | else if (c >= 'a' && c <= 'f') |
| 2540 | x += 10 + c - 'a'; |
| 2541 | else |
| 2542 | x += 10 + c - 'A'; |
| 2543 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2544 | #ifndef Py_UNICODE_WIDE |
| 2545 | if (x > 0x10000) { |
| 2546 | if (unicode_decode_call_errorhandler( |
| 2547 | errors, &errorHandler, |
| 2548 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
| 2549 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2550 | (PyObject **)&v, &outpos, &p)) |
| 2551 | goto onError; |
| 2552 | } |
| 2553 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2554 | *p++ = x; |
| 2555 | nextByte: |
| 2556 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2557 | } |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2558 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 2559 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2560 | Py_XDECREF(errorHandler); |
| 2561 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2562 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2563 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2564 | onError: |
| 2565 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2566 | Py_XDECREF(errorHandler); |
| 2567 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2568 | return NULL; |
| 2569 | } |
| 2570 | |
| 2571 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2572 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2573 | { |
| 2574 | PyObject *repr; |
| 2575 | char *p; |
| 2576 | char *q; |
| 2577 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2578 | static const char *hexdigit = "0123456789abcdef"; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2579 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2580 | #ifdef Py_UNICODE_WIDE |
| 2581 | repr = PyString_FromStringAndSize(NULL, 10 * size); |
| 2582 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2583 | repr = PyString_FromStringAndSize(NULL, 6 * size); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2584 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2585 | if (repr == NULL) |
| 2586 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 2587 | if (size == 0) |
| 2588 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2589 | |
| 2590 | p = q = PyString_AS_STRING(repr); |
| 2591 | while (size-- > 0) { |
| 2592 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2593 | #ifdef Py_UNICODE_WIDE |
| 2594 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 2595 | if (ch >= 0x10000) { |
| 2596 | *p++ = '\\'; |
| 2597 | *p++ = 'U'; |
| 2598 | *p++ = hexdigit[(ch >> 28) & 0xf]; |
| 2599 | *p++ = hexdigit[(ch >> 24) & 0xf]; |
| 2600 | *p++ = hexdigit[(ch >> 20) & 0xf]; |
| 2601 | *p++ = hexdigit[(ch >> 16) & 0xf]; |
| 2602 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 2603 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 2604 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 2605 | *p++ = hexdigit[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2606 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2607 | else |
| 2608 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2609 | /* Map 16-bit characters to '\uxxxx' */ |
| 2610 | if (ch >= 256) { |
| 2611 | *p++ = '\\'; |
| 2612 | *p++ = 'u'; |
| 2613 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 2614 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 2615 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 2616 | *p++ = hexdigit[ch & 15]; |
| 2617 | } |
| 2618 | /* Copy everything else as-is */ |
| 2619 | else |
| 2620 | *p++ = (char) ch; |
| 2621 | } |
| 2622 | *p = '\0'; |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 2623 | _PyString_Resize(&repr, p - q); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2624 | return repr; |
| 2625 | } |
| 2626 | |
| 2627 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 2628 | { |
| 2629 | if (!PyUnicode_Check(unicode)) { |
| 2630 | PyErr_BadArgument(); |
| 2631 | return NULL; |
| 2632 | } |
| 2633 | return PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 2634 | PyUnicode_GET_SIZE(unicode)); |
| 2635 | } |
| 2636 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2637 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 2638 | |
| 2639 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2640 | Py_ssize_t size, |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2641 | const char *errors) |
| 2642 | { |
| 2643 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2644 | Py_ssize_t startinpos; |
| 2645 | Py_ssize_t endinpos; |
| 2646 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2647 | PyUnicodeObject *v; |
| 2648 | Py_UNICODE *p; |
| 2649 | const char *end; |
| 2650 | const char *reason; |
| 2651 | PyObject *errorHandler = NULL; |
| 2652 | PyObject *exc = NULL; |
| 2653 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 2654 | #ifdef Py_UNICODE_WIDE |
| 2655 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 2656 | #endif |
| 2657 | |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 2658 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2659 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 2660 | if (v == NULL) |
| 2661 | goto onError; |
| 2662 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
| 2663 | return (PyObject *)v; |
| 2664 | p = PyUnicode_AS_UNICODE(v); |
| 2665 | end = s + size; |
| 2666 | |
| 2667 | while (s < end) { |
Neal Norwitz | 1004a53 | 2006-05-15 07:17:23 +0000 | [diff] [blame] | 2668 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2669 | /* We have to sanity check the raw data, otherwise doom looms for |
| 2670 | some malformed UCS-4 data. */ |
| 2671 | if ( |
| 2672 | #ifdef Py_UNICODE_WIDE |
| 2673 | *p > unimax || *p < 0 || |
| 2674 | #endif |
| 2675 | end-s < Py_UNICODE_SIZE |
| 2676 | ) |
| 2677 | { |
| 2678 | startinpos = s - starts; |
| 2679 | if (end-s < Py_UNICODE_SIZE) { |
| 2680 | endinpos = end-starts; |
| 2681 | reason = "truncated input"; |
| 2682 | } |
| 2683 | else { |
| 2684 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 2685 | reason = "illegal code point (> 0x10FFFF)"; |
| 2686 | } |
| 2687 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 2688 | if (unicode_decode_call_errorhandler( |
| 2689 | errors, &errorHandler, |
| 2690 | "unicode_internal", reason, |
| 2691 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2692 | (PyObject **)&v, &outpos, &p)) { |
| 2693 | goto onError; |
| 2694 | } |
| 2695 | } |
| 2696 | else { |
| 2697 | p++; |
| 2698 | s += Py_UNICODE_SIZE; |
| 2699 | } |
| 2700 | } |
| 2701 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2702 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2703 | goto onError; |
| 2704 | Py_XDECREF(errorHandler); |
| 2705 | Py_XDECREF(exc); |
| 2706 | return (PyObject *)v; |
| 2707 | |
| 2708 | onError: |
| 2709 | Py_XDECREF(v); |
| 2710 | Py_XDECREF(errorHandler); |
| 2711 | Py_XDECREF(exc); |
| 2712 | return NULL; |
| 2713 | } |
| 2714 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2715 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 2716 | |
| 2717 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2718 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2719 | const char *errors) |
| 2720 | { |
| 2721 | PyUnicodeObject *v; |
| 2722 | Py_UNICODE *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2723 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2724 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2725 | if (size == 1) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 2726 | Py_UNICODE r = *(unsigned char*)s; |
| 2727 | return PyUnicode_FromUnicode(&r, 1); |
| 2728 | } |
| 2729 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2730 | v = _PyUnicode_New(size); |
| 2731 | if (v == NULL) |
| 2732 | goto onError; |
| 2733 | if (size == 0) |
| 2734 | return (PyObject *)v; |
| 2735 | p = PyUnicode_AS_UNICODE(v); |
| 2736 | while (size-- > 0) |
| 2737 | *p++ = (unsigned char)*s++; |
| 2738 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2739 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2740 | onError: |
| 2741 | Py_XDECREF(v); |
| 2742 | return NULL; |
| 2743 | } |
| 2744 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2745 | /* create or adjust a UnicodeEncodeError */ |
| 2746 | static void make_encode_exception(PyObject **exceptionObject, |
| 2747 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2748 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 2749 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2750 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2751 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2752 | if (*exceptionObject == NULL) { |
| 2753 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 2754 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2755 | } |
| 2756 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2757 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 2758 | goto onError; |
| 2759 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 2760 | goto onError; |
| 2761 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 2762 | goto onError; |
| 2763 | return; |
| 2764 | onError: |
| 2765 | Py_DECREF(*exceptionObject); |
| 2766 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2767 | } |
| 2768 | } |
| 2769 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2770 | /* raises a UnicodeEncodeError */ |
| 2771 | static void raise_encode_exception(PyObject **exceptionObject, |
| 2772 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2773 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 2774 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2775 | const char *reason) |
| 2776 | { |
| 2777 | make_encode_exception(exceptionObject, |
| 2778 | encoding, unicode, size, startpos, endpos, reason); |
| 2779 | if (*exceptionObject != NULL) |
| 2780 | PyCodec_StrictErrors(*exceptionObject); |
| 2781 | } |
| 2782 | |
| 2783 | /* error handling callback helper: |
| 2784 | build arguments, call the callback and check the arguments, |
| 2785 | put the result into newpos and return the replacement string, which |
| 2786 | has to be freed by the caller */ |
| 2787 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
| 2788 | PyObject **errorHandler, |
| 2789 | const char *encoding, const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2790 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 2791 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 2792 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2793 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2794 | 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] | 2795 | |
| 2796 | PyObject *restuple; |
| 2797 | PyObject *resunicode; |
| 2798 | |
| 2799 | if (*errorHandler == NULL) { |
| 2800 | *errorHandler = PyCodec_LookupError(errors); |
| 2801 | if (*errorHandler == NULL) |
| 2802 | return NULL; |
| 2803 | } |
| 2804 | |
| 2805 | make_encode_exception(exceptionObject, |
| 2806 | encoding, unicode, size, startpos, endpos, reason); |
| 2807 | if (*exceptionObject == NULL) |
| 2808 | return NULL; |
| 2809 | |
| 2810 | restuple = PyObject_CallFunctionObjArgs( |
| 2811 | *errorHandler, *exceptionObject, NULL); |
| 2812 | if (restuple == NULL) |
| 2813 | return NULL; |
| 2814 | if (!PyTuple_Check(restuple)) { |
| 2815 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 2816 | Py_DECREF(restuple); |
| 2817 | return NULL; |
| 2818 | } |
| 2819 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
| 2820 | &resunicode, newpos)) { |
| 2821 | Py_DECREF(restuple); |
| 2822 | return NULL; |
| 2823 | } |
| 2824 | if (*newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 2825 | *newpos = size+*newpos; |
| 2826 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 2827 | 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] | 2828 | Py_DECREF(restuple); |
| 2829 | return NULL; |
| 2830 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2831 | Py_INCREF(resunicode); |
| 2832 | Py_DECREF(restuple); |
| 2833 | return resunicode; |
| 2834 | } |
| 2835 | |
| 2836 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2837 | Py_ssize_t size, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2838 | const char *errors, |
| 2839 | int limit) |
| 2840 | { |
| 2841 | /* output object */ |
| 2842 | PyObject *res; |
| 2843 | /* pointers to the beginning and end+1 of input */ |
| 2844 | const Py_UNICODE *startp = p; |
| 2845 | const Py_UNICODE *endp = p + size; |
| 2846 | /* pointer to the beginning of the unencodable characters */ |
| 2847 | /* const Py_UNICODE *badp = NULL; */ |
| 2848 | /* pointer into the output */ |
| 2849 | char *str; |
| 2850 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2851 | Py_ssize_t respos = 0; |
| 2852 | Py_ssize_t ressize; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2853 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 2854 | 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] | 2855 | PyObject *errorHandler = NULL; |
| 2856 | PyObject *exc = NULL; |
| 2857 | /* the following variable is used for caching string comparisons |
| 2858 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 2859 | int known_errorHandler = -1; |
| 2860 | |
| 2861 | /* allocate enough for a simple encoding without |
| 2862 | replacements, if we need more, we'll resize */ |
| 2863 | res = PyString_FromStringAndSize(NULL, size); |
| 2864 | if (res == NULL) |
| 2865 | goto onError; |
| 2866 | if (size == 0) |
| 2867 | return res; |
| 2868 | str = PyString_AS_STRING(res); |
| 2869 | ressize = size; |
| 2870 | |
| 2871 | while (p<endp) { |
| 2872 | Py_UNICODE c = *p; |
| 2873 | |
| 2874 | /* can we encode this? */ |
| 2875 | if (c<limit) { |
| 2876 | /* no overflow check, because we know that the space is enough */ |
| 2877 | *str++ = (char)c; |
| 2878 | ++p; |
| 2879 | } |
| 2880 | else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2881 | Py_ssize_t unicodepos = p-startp; |
| 2882 | Py_ssize_t requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2883 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2884 | Py_ssize_t repsize; |
| 2885 | Py_ssize_t newpos; |
| 2886 | Py_ssize_t respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2887 | Py_UNICODE *uni2; |
| 2888 | /* startpos for collecting unencodable chars */ |
| 2889 | const Py_UNICODE *collstart = p; |
| 2890 | const Py_UNICODE *collend = p; |
| 2891 | /* find all unecodable characters */ |
| 2892 | while ((collend < endp) && ((*collend)>=limit)) |
| 2893 | ++collend; |
| 2894 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 2895 | if (known_errorHandler==-1) { |
| 2896 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 2897 | known_errorHandler = 1; |
| 2898 | else if (!strcmp(errors, "replace")) |
| 2899 | known_errorHandler = 2; |
| 2900 | else if (!strcmp(errors, "ignore")) |
| 2901 | known_errorHandler = 3; |
| 2902 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 2903 | known_errorHandler = 4; |
| 2904 | else |
| 2905 | known_errorHandler = 0; |
| 2906 | } |
| 2907 | switch (known_errorHandler) { |
| 2908 | case 1: /* strict */ |
| 2909 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 2910 | goto onError; |
| 2911 | case 2: /* replace */ |
| 2912 | while (collstart++<collend) |
| 2913 | *str++ = '?'; /* fall through */ |
| 2914 | case 3: /* ignore */ |
| 2915 | p = collend; |
| 2916 | break; |
| 2917 | case 4: /* xmlcharrefreplace */ |
| 2918 | respos = str-PyString_AS_STRING(res); |
| 2919 | /* determine replacement size (temporarily (mis)uses p) */ |
| 2920 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 2921 | if (*p<10) |
| 2922 | repsize += 2+1+1; |
| 2923 | else if (*p<100) |
| 2924 | repsize += 2+2+1; |
| 2925 | else if (*p<1000) |
| 2926 | repsize += 2+3+1; |
| 2927 | else if (*p<10000) |
| 2928 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 2929 | #ifndef Py_UNICODE_WIDE |
| 2930 | else |
| 2931 | repsize += 2+5+1; |
| 2932 | #else |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2933 | else if (*p<100000) |
| 2934 | repsize += 2+5+1; |
| 2935 | else if (*p<1000000) |
| 2936 | repsize += 2+6+1; |
| 2937 | else |
| 2938 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2939 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2940 | } |
| 2941 | requiredsize = respos+repsize+(endp-collend); |
| 2942 | if (requiredsize > ressize) { |
| 2943 | if (requiredsize<2*ressize) |
| 2944 | requiredsize = 2*ressize; |
| 2945 | if (_PyString_Resize(&res, requiredsize)) |
| 2946 | goto onError; |
| 2947 | str = PyString_AS_STRING(res) + respos; |
| 2948 | ressize = requiredsize; |
| 2949 | } |
| 2950 | /* generate replacement (temporarily (mis)uses p) */ |
| 2951 | for (p = collstart; p < collend; ++p) { |
| 2952 | str += sprintf(str, "&#%d;", (int)*p); |
| 2953 | } |
| 2954 | p = collend; |
| 2955 | break; |
| 2956 | default: |
| 2957 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 2958 | encoding, reason, startp, size, &exc, |
| 2959 | collstart-startp, collend-startp, &newpos); |
| 2960 | if (repunicode == NULL) |
| 2961 | goto onError; |
| 2962 | /* need more space? (at least enough for what we |
| 2963 | have+the replacement+the rest of the string, so |
| 2964 | we won't have to check space for encodable characters) */ |
| 2965 | respos = str-PyString_AS_STRING(res); |
| 2966 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 2967 | requiredsize = respos+repsize+(endp-collend); |
| 2968 | if (requiredsize > ressize) { |
| 2969 | if (requiredsize<2*ressize) |
| 2970 | requiredsize = 2*ressize; |
| 2971 | if (_PyString_Resize(&res, requiredsize)) { |
| 2972 | Py_DECREF(repunicode); |
| 2973 | goto onError; |
| 2974 | } |
| 2975 | str = PyString_AS_STRING(res) + respos; |
| 2976 | ressize = requiredsize; |
| 2977 | } |
| 2978 | /* check if there is anything unencodable in the replacement |
| 2979 | and copy it to the output */ |
| 2980 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 2981 | c = *uni2; |
| 2982 | if (c >= limit) { |
| 2983 | raise_encode_exception(&exc, encoding, startp, size, |
| 2984 | unicodepos, unicodepos+1, reason); |
| 2985 | Py_DECREF(repunicode); |
| 2986 | goto onError; |
| 2987 | } |
| 2988 | *str = (char)c; |
| 2989 | } |
| 2990 | p = startp + newpos; |
| 2991 | Py_DECREF(repunicode); |
| 2992 | } |
| 2993 | } |
| 2994 | } |
| 2995 | /* Resize if we allocated to much */ |
| 2996 | respos = str-PyString_AS_STRING(res); |
| 2997 | if (respos<ressize) |
| 2998 | /* If this falls res will be NULL */ |
| 2999 | _PyString_Resize(&res, respos); |
| 3000 | Py_XDECREF(errorHandler); |
| 3001 | Py_XDECREF(exc); |
| 3002 | return res; |
| 3003 | |
| 3004 | onError: |
| 3005 | Py_XDECREF(res); |
| 3006 | Py_XDECREF(errorHandler); |
| 3007 | Py_XDECREF(exc); |
| 3008 | return NULL; |
| 3009 | } |
| 3010 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3011 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3012 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3013 | const char *errors) |
| 3014 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3015 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3016 | } |
| 3017 | |
| 3018 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 3019 | { |
| 3020 | if (!PyUnicode_Check(unicode)) { |
| 3021 | PyErr_BadArgument(); |
| 3022 | return NULL; |
| 3023 | } |
| 3024 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 3025 | PyUnicode_GET_SIZE(unicode), |
| 3026 | NULL); |
| 3027 | } |
| 3028 | |
| 3029 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 3030 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3031 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3032 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3033 | const char *errors) |
| 3034 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3035 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3036 | PyUnicodeObject *v; |
| 3037 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3038 | Py_ssize_t startinpos; |
| 3039 | Py_ssize_t endinpos; |
| 3040 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3041 | const char *e; |
| 3042 | PyObject *errorHandler = NULL; |
| 3043 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3044 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3045 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3046 | if (size == 1 && *(unsigned char*)s < 128) { |
| 3047 | Py_UNICODE r = *(unsigned char*)s; |
| 3048 | return PyUnicode_FromUnicode(&r, 1); |
| 3049 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3050 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3051 | v = _PyUnicode_New(size); |
| 3052 | if (v == NULL) |
| 3053 | goto onError; |
| 3054 | if (size == 0) |
| 3055 | return (PyObject *)v; |
| 3056 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3057 | e = s + size; |
| 3058 | while (s < e) { |
| 3059 | register unsigned char c = (unsigned char)*s; |
| 3060 | if (c < 128) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3061 | *p++ = c; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3062 | ++s; |
| 3063 | } |
| 3064 | else { |
| 3065 | startinpos = s-starts; |
| 3066 | endinpos = startinpos + 1; |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 3067 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3068 | if (unicode_decode_call_errorhandler( |
| 3069 | errors, &errorHandler, |
| 3070 | "ascii", "ordinal not in range(128)", |
| 3071 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 3072 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3073 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3074 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3075 | } |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3076 | if (p - PyUnicode_AS_UNICODE(v) < PyString_GET_SIZE(v)) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 3077 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3078 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3079 | Py_XDECREF(errorHandler); |
| 3080 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3081 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3082 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3083 | onError: |
| 3084 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3085 | Py_XDECREF(errorHandler); |
| 3086 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3087 | return NULL; |
| 3088 | } |
| 3089 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3090 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3091 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3092 | const char *errors) |
| 3093 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3094 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3095 | } |
| 3096 | |
| 3097 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 3098 | { |
| 3099 | if (!PyUnicode_Check(unicode)) { |
| 3100 | PyErr_BadArgument(); |
| 3101 | return NULL; |
| 3102 | } |
| 3103 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 3104 | PyUnicode_GET_SIZE(unicode), |
| 3105 | NULL); |
| 3106 | } |
| 3107 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3108 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3109 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3110 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3111 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3112 | #if SIZEOF_INT < SIZEOF_SSIZE_T |
| 3113 | #define NEED_RETRY |
| 3114 | #endif |
| 3115 | |
| 3116 | /* XXX This code is limited to "true" double-byte encodings, as |
| 3117 | a) it assumes an incomplete character consists of a single byte, and |
| 3118 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
| 3119 | encodings, see IsDBCSLeadByteEx documentation. */ |
| 3120 | |
| 3121 | static int is_dbcs_lead_byte(const char *s, int offset) |
| 3122 | { |
| 3123 | const char *curr = s + offset; |
| 3124 | |
| 3125 | if (IsDBCSLeadByte(*curr)) { |
| 3126 | const char *prev = CharPrev(s, curr); |
| 3127 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
| 3128 | } |
| 3129 | return 0; |
| 3130 | } |
| 3131 | |
| 3132 | /* |
| 3133 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 3134 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 3135 | */ |
| 3136 | static int decode_mbcs(PyUnicodeObject **v, |
| 3137 | const char *s, /* MBCS string */ |
| 3138 | int size, /* sizeof MBCS string */ |
| 3139 | int final) |
| 3140 | { |
| 3141 | Py_UNICODE *p; |
| 3142 | Py_ssize_t n = 0; |
| 3143 | int usize = 0; |
| 3144 | |
| 3145 | assert(size >= 0); |
| 3146 | |
| 3147 | /* Skip trailing lead-byte unless 'final' is set */ |
| 3148 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
| 3149 | --size; |
| 3150 | |
| 3151 | /* First get the size of the result */ |
| 3152 | if (size > 0) { |
| 3153 | usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); |
| 3154 | if (usize == 0) { |
| 3155 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3156 | return -1; |
| 3157 | } |
| 3158 | } |
| 3159 | |
| 3160 | if (*v == NULL) { |
| 3161 | /* Create unicode object */ |
| 3162 | *v = _PyUnicode_New(usize); |
| 3163 | if (*v == NULL) |
| 3164 | return -1; |
| 3165 | } |
| 3166 | else { |
| 3167 | /* Extend unicode object */ |
| 3168 | n = PyUnicode_GET_SIZE(*v); |
| 3169 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 3170 | return -1; |
| 3171 | } |
| 3172 | |
| 3173 | /* Do the conversion */ |
| 3174 | if (size > 0) { |
| 3175 | p = PyUnicode_AS_UNICODE(*v) + n; |
| 3176 | if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { |
| 3177 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3178 | return -1; |
| 3179 | } |
| 3180 | } |
| 3181 | |
| 3182 | return size; |
| 3183 | } |
| 3184 | |
| 3185 | PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
| 3186 | Py_ssize_t size, |
| 3187 | const char *errors, |
| 3188 | Py_ssize_t *consumed) |
| 3189 | { |
| 3190 | PyUnicodeObject *v = NULL; |
| 3191 | int done; |
| 3192 | |
| 3193 | if (consumed) |
| 3194 | *consumed = 0; |
| 3195 | |
| 3196 | #ifdef NEED_RETRY |
| 3197 | retry: |
| 3198 | if (size > INT_MAX) |
| 3199 | done = decode_mbcs(&v, s, INT_MAX, 0); |
| 3200 | else |
| 3201 | #endif |
| 3202 | done = decode_mbcs(&v, s, (int)size, !consumed); |
| 3203 | |
| 3204 | if (done < 0) { |
| 3205 | Py_XDECREF(v); |
| 3206 | return NULL; |
| 3207 | } |
| 3208 | |
| 3209 | if (consumed) |
| 3210 | *consumed += done; |
| 3211 | |
| 3212 | #ifdef NEED_RETRY |
| 3213 | if (size > INT_MAX) { |
| 3214 | s += done; |
| 3215 | size -= done; |
| 3216 | goto retry; |
| 3217 | } |
| 3218 | #endif |
| 3219 | |
| 3220 | return (PyObject *)v; |
| 3221 | } |
| 3222 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3223 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3224 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3225 | const char *errors) |
| 3226 | { |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3227 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 3228 | } |
| 3229 | |
| 3230 | /* |
| 3231 | * Convert unicode into string object (MBCS). |
| 3232 | * Returns 0 if succeed, -1 otherwise. |
| 3233 | */ |
| 3234 | static int encode_mbcs(PyObject **repr, |
| 3235 | const Py_UNICODE *p, /* unicode */ |
| 3236 | int size) /* size of unicode */ |
| 3237 | { |
| 3238 | int mbcssize = 0; |
| 3239 | Py_ssize_t n = 0; |
| 3240 | |
| 3241 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3242 | |
| 3243 | /* First get the size of the result */ |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3244 | if (size > 0) { |
| 3245 | mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); |
| 3246 | if (mbcssize == 0) { |
| 3247 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3248 | return -1; |
| 3249 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3250 | } |
| 3251 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3252 | if (*repr == NULL) { |
| 3253 | /* Create string object */ |
| 3254 | *repr = PyString_FromStringAndSize(NULL, mbcssize); |
| 3255 | if (*repr == NULL) |
| 3256 | return -1; |
| 3257 | } |
| 3258 | else { |
| 3259 | /* Extend string object */ |
| 3260 | n = PyString_Size(*repr); |
| 3261 | if (_PyString_Resize(repr, n + mbcssize) < 0) |
| 3262 | return -1; |
| 3263 | } |
| 3264 | |
| 3265 | /* Do the conversion */ |
| 3266 | if (size > 0) { |
| 3267 | char *s = PyString_AS_STRING(*repr) + n; |
| 3268 | if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { |
| 3269 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3270 | return -1; |
| 3271 | } |
| 3272 | } |
| 3273 | |
| 3274 | return 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3275 | } |
| 3276 | |
| 3277 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3278 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3279 | const char *errors) |
| 3280 | { |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3281 | PyObject *repr = NULL; |
| 3282 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 3283 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3284 | #ifdef NEED_RETRY |
| 3285 | retry: |
| 3286 | if (size > INT_MAX) |
| 3287 | ret = encode_mbcs(&repr, p, INT_MAX); |
| 3288 | else |
| 3289 | #endif |
| 3290 | ret = encode_mbcs(&repr, p, (int)size); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3291 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3292 | if (ret < 0) { |
| 3293 | Py_XDECREF(repr); |
| 3294 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3295 | } |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3296 | |
| 3297 | #ifdef NEED_RETRY |
| 3298 | if (size > INT_MAX) { |
| 3299 | p += INT_MAX; |
| 3300 | size -= INT_MAX; |
| 3301 | goto retry; |
| 3302 | } |
| 3303 | #endif |
| 3304 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3305 | return repr; |
| 3306 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3307 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3308 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 3309 | { |
| 3310 | if (!PyUnicode_Check(unicode)) { |
| 3311 | PyErr_BadArgument(); |
| 3312 | return NULL; |
| 3313 | } |
| 3314 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 3315 | PyUnicode_GET_SIZE(unicode), |
| 3316 | NULL); |
| 3317 | } |
| 3318 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3319 | #undef NEED_RETRY |
| 3320 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3321 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3322 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3323 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 3324 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3325 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3326 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3327 | PyObject *mapping, |
| 3328 | const char *errors) |
| 3329 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3330 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3331 | Py_ssize_t startinpos; |
| 3332 | Py_ssize_t endinpos; |
| 3333 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3334 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3335 | PyUnicodeObject *v; |
| 3336 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3337 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3338 | PyObject *errorHandler = NULL; |
| 3339 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3340 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3341 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3342 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3343 | /* Default to Latin-1 */ |
| 3344 | if (mapping == NULL) |
| 3345 | return PyUnicode_DecodeLatin1(s, size, errors); |
| 3346 | |
| 3347 | v = _PyUnicode_New(size); |
| 3348 | if (v == NULL) |
| 3349 | goto onError; |
| 3350 | if (size == 0) |
| 3351 | return (PyObject *)v; |
| 3352 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3353 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3354 | if (PyUnicode_CheckExact(mapping)) { |
| 3355 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 3356 | maplen = PyUnicode_GET_SIZE(mapping); |
| 3357 | while (s < e) { |
| 3358 | unsigned char ch = *s; |
| 3359 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3360 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3361 | if (ch < maplen) |
| 3362 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3363 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3364 | if (x == 0xfffe) { |
| 3365 | /* undefined mapping */ |
| 3366 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3367 | startinpos = s-starts; |
| 3368 | endinpos = startinpos+1; |
| 3369 | if (unicode_decode_call_errorhandler( |
| 3370 | errors, &errorHandler, |
| 3371 | "charmap", "character maps to <undefined>", |
| 3372 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 3373 | (PyObject **)&v, &outpos, &p)) { |
| 3374 | goto onError; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 3375 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3376 | continue; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 3377 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3378 | *p++ = x; |
| 3379 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3380 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3381 | } |
| 3382 | else { |
| 3383 | while (s < e) { |
| 3384 | unsigned char ch = *s; |
| 3385 | PyObject *w, *x; |
| 3386 | |
| 3387 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 3388 | w = PyInt_FromLong((long)ch); |
| 3389 | if (w == NULL) |
| 3390 | goto onError; |
| 3391 | x = PyObject_GetItem(mapping, w); |
| 3392 | Py_DECREF(w); |
| 3393 | if (x == NULL) { |
| 3394 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 3395 | /* No mapping found means: mapping is undefined. */ |
| 3396 | PyErr_Clear(); |
| 3397 | x = Py_None; |
| 3398 | Py_INCREF(x); |
| 3399 | } else |
| 3400 | goto onError; |
| 3401 | } |
| 3402 | |
| 3403 | /* Apply mapping */ |
| 3404 | if (PyInt_Check(x)) { |
| 3405 | long value = PyInt_AS_LONG(x); |
| 3406 | if (value < 0 || value > 65535) { |
| 3407 | PyErr_SetString(PyExc_TypeError, |
| 3408 | "character mapping must be in range(65536)"); |
| 3409 | Py_DECREF(x); |
| 3410 | goto onError; |
| 3411 | } |
| 3412 | *p++ = (Py_UNICODE)value; |
| 3413 | } |
| 3414 | else if (x == Py_None) { |
| 3415 | /* undefined mapping */ |
| 3416 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3417 | startinpos = s-starts; |
| 3418 | endinpos = startinpos+1; |
| 3419 | if (unicode_decode_call_errorhandler( |
| 3420 | errors, &errorHandler, |
| 3421 | "charmap", "character maps to <undefined>", |
| 3422 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 3423 | (PyObject **)&v, &outpos, &p)) { |
| 3424 | Py_DECREF(x); |
| 3425 | goto onError; |
| 3426 | } |
Walter Dörwald | d4fff17 | 2005-11-28 22:15:56 +0000 | [diff] [blame] | 3427 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3428 | continue; |
| 3429 | } |
| 3430 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3431 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3432 | |
| 3433 | if (targetsize == 1) |
| 3434 | /* 1-1 mapping */ |
| 3435 | *p++ = *PyUnicode_AS_UNICODE(x); |
| 3436 | |
| 3437 | else if (targetsize > 1) { |
| 3438 | /* 1-n mapping */ |
| 3439 | if (targetsize > extrachars) { |
| 3440 | /* resize first */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3441 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 3442 | Py_ssize_t needed = (targetsize - extrachars) + \ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3443 | (targetsize << 2); |
| 3444 | extrachars += needed; |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 3445 | /* XXX overflow detection missing */ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3446 | if (_PyUnicode_Resize(&v, |
| 3447 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 3448 | Py_DECREF(x); |
| 3449 | goto onError; |
| 3450 | } |
| 3451 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 3452 | } |
| 3453 | Py_UNICODE_COPY(p, |
| 3454 | PyUnicode_AS_UNICODE(x), |
| 3455 | targetsize); |
| 3456 | p += targetsize; |
| 3457 | extrachars -= targetsize; |
| 3458 | } |
| 3459 | /* 1-0 mapping: skip the character */ |
| 3460 | } |
| 3461 | else { |
| 3462 | /* wrong return value */ |
| 3463 | PyErr_SetString(PyExc_TypeError, |
| 3464 | "character mapping must return integer, None or unicode"); |
| 3465 | Py_DECREF(x); |
| 3466 | goto onError; |
| 3467 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3468 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3469 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3470 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3471 | } |
| 3472 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 3473 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3474 | goto 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 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3478 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3479 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3480 | Py_XDECREF(errorHandler); |
| 3481 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3482 | Py_XDECREF(v); |
| 3483 | return NULL; |
| 3484 | } |
| 3485 | |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3486 | /* Charmap encoding: the lookup table */ |
| 3487 | |
| 3488 | struct encoding_map{ |
| 3489 | PyObject_HEAD |
| 3490 | unsigned char level1[32]; |
| 3491 | int count2, count3; |
| 3492 | unsigned char level23[1]; |
| 3493 | }; |
| 3494 | |
| 3495 | static PyObject* |
| 3496 | encoding_map_size(PyObject *obj, PyObject* args) |
| 3497 | { |
| 3498 | struct encoding_map *map = (struct encoding_map*)obj; |
| 3499 | return PyInt_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
| 3500 | 128*map->count3); |
| 3501 | } |
| 3502 | |
| 3503 | static PyMethodDef encoding_map_methods[] = { |
| 3504 | {"size", encoding_map_size, METH_NOARGS, |
| 3505 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 3506 | { 0 } |
| 3507 | }; |
| 3508 | |
| 3509 | static void |
| 3510 | encoding_map_dealloc(PyObject* o) |
| 3511 | { |
| 3512 | PyObject_FREE(o); |
| 3513 | } |
| 3514 | |
| 3515 | static PyTypeObject EncodingMapType = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 3516 | PyVarObject_HEAD_INIT(NULL, 0) |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3517 | "EncodingMap", /*tp_name*/ |
| 3518 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 3519 | 0, /*tp_itemsize*/ |
| 3520 | /* methods */ |
| 3521 | encoding_map_dealloc, /*tp_dealloc*/ |
| 3522 | 0, /*tp_print*/ |
| 3523 | 0, /*tp_getattr*/ |
| 3524 | 0, /*tp_setattr*/ |
| 3525 | 0, /*tp_compare*/ |
| 3526 | 0, /*tp_repr*/ |
| 3527 | 0, /*tp_as_number*/ |
| 3528 | 0, /*tp_as_sequence*/ |
| 3529 | 0, /*tp_as_mapping*/ |
| 3530 | 0, /*tp_hash*/ |
| 3531 | 0, /*tp_call*/ |
| 3532 | 0, /*tp_str*/ |
| 3533 | 0, /*tp_getattro*/ |
| 3534 | 0, /*tp_setattro*/ |
| 3535 | 0, /*tp_as_buffer*/ |
| 3536 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 3537 | 0, /*tp_doc*/ |
| 3538 | 0, /*tp_traverse*/ |
| 3539 | 0, /*tp_clear*/ |
| 3540 | 0, /*tp_richcompare*/ |
| 3541 | 0, /*tp_weaklistoffset*/ |
| 3542 | 0, /*tp_iter*/ |
| 3543 | 0, /*tp_iternext*/ |
| 3544 | encoding_map_methods, /*tp_methods*/ |
| 3545 | 0, /*tp_members*/ |
| 3546 | 0, /*tp_getset*/ |
| 3547 | 0, /*tp_base*/ |
| 3548 | 0, /*tp_dict*/ |
| 3549 | 0, /*tp_descr_get*/ |
| 3550 | 0, /*tp_descr_set*/ |
| 3551 | 0, /*tp_dictoffset*/ |
| 3552 | 0, /*tp_init*/ |
| 3553 | 0, /*tp_alloc*/ |
| 3554 | 0, /*tp_new*/ |
| 3555 | 0, /*tp_free*/ |
| 3556 | 0, /*tp_is_gc*/ |
| 3557 | }; |
| 3558 | |
| 3559 | PyObject* |
| 3560 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 3561 | { |
| 3562 | Py_UNICODE *decode; |
| 3563 | PyObject *result; |
| 3564 | struct encoding_map *mresult; |
| 3565 | int i; |
| 3566 | int need_dict = 0; |
| 3567 | unsigned char level1[32]; |
| 3568 | unsigned char level2[512]; |
| 3569 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 3570 | int count2 = 0, count3 = 0; |
| 3571 | |
| 3572 | if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { |
| 3573 | PyErr_BadArgument(); |
| 3574 | return NULL; |
| 3575 | } |
| 3576 | decode = PyUnicode_AS_UNICODE(string); |
| 3577 | memset(level1, 0xFF, sizeof level1); |
| 3578 | memset(level2, 0xFF, sizeof level2); |
| 3579 | |
| 3580 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 3581 | or if there are non-BMP characters, we need to use |
| 3582 | a mapping dictionary. */ |
| 3583 | if (decode[0] != 0) |
| 3584 | need_dict = 1; |
| 3585 | for (i = 1; i < 256; i++) { |
| 3586 | int l1, l2; |
| 3587 | if (decode[i] == 0 |
| 3588 | #ifdef Py_UNICODE_WIDE |
| 3589 | || decode[i] > 0xFFFF |
| 3590 | #endif |
| 3591 | ) { |
| 3592 | need_dict = 1; |
| 3593 | break; |
| 3594 | } |
| 3595 | if (decode[i] == 0xFFFE) |
| 3596 | /* unmapped character */ |
| 3597 | continue; |
| 3598 | l1 = decode[i] >> 11; |
| 3599 | l2 = decode[i] >> 7; |
| 3600 | if (level1[l1] == 0xFF) |
| 3601 | level1[l1] = count2++; |
| 3602 | if (level2[l2] == 0xFF) |
| 3603 | level2[l2] = count3++; |
| 3604 | } |
| 3605 | |
| 3606 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 3607 | need_dict = 1; |
| 3608 | |
| 3609 | if (need_dict) { |
| 3610 | PyObject *result = PyDict_New(); |
| 3611 | PyObject *key, *value; |
| 3612 | if (!result) |
| 3613 | return NULL; |
| 3614 | for (i = 0; i < 256; i++) { |
| 3615 | key = value = NULL; |
| 3616 | key = PyInt_FromLong(decode[i]); |
| 3617 | value = PyInt_FromLong(i); |
| 3618 | if (!key || !value) |
| 3619 | goto failed1; |
| 3620 | if (PyDict_SetItem(result, key, value) == -1) |
| 3621 | goto failed1; |
Georg Brandl | 9f16760 | 2006-06-04 21:46:16 +0000 | [diff] [blame] | 3622 | Py_DECREF(key); |
| 3623 | Py_DECREF(value); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3624 | } |
| 3625 | return result; |
| 3626 | failed1: |
| 3627 | Py_XDECREF(key); |
| 3628 | Py_XDECREF(value); |
| 3629 | Py_DECREF(result); |
| 3630 | return NULL; |
| 3631 | } |
| 3632 | |
| 3633 | /* Create a three-level trie */ |
| 3634 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 3635 | 16*count2 + 128*count3 - 1); |
| 3636 | if (!result) |
| 3637 | return PyErr_NoMemory(); |
| 3638 | PyObject_Init(result, &EncodingMapType); |
| 3639 | mresult = (struct encoding_map*)result; |
| 3640 | mresult->count2 = count2; |
| 3641 | mresult->count3 = count3; |
| 3642 | mlevel1 = mresult->level1; |
| 3643 | mlevel2 = mresult->level23; |
| 3644 | mlevel3 = mresult->level23 + 16*count2; |
| 3645 | memcpy(mlevel1, level1, 32); |
| 3646 | memset(mlevel2, 0xFF, 16*count2); |
| 3647 | memset(mlevel3, 0, 128*count3); |
| 3648 | count3 = 0; |
| 3649 | for (i = 1; i < 256; i++) { |
| 3650 | int o1, o2, o3, i2, i3; |
| 3651 | if (decode[i] == 0xFFFE) |
| 3652 | /* unmapped character */ |
| 3653 | continue; |
| 3654 | o1 = decode[i]>>11; |
| 3655 | o2 = (decode[i]>>7) & 0xF; |
| 3656 | i2 = 16*mlevel1[o1] + o2; |
| 3657 | if (mlevel2[i2] == 0xFF) |
| 3658 | mlevel2[i2] = count3++; |
| 3659 | o3 = decode[i] & 0x7F; |
| 3660 | i3 = 128*mlevel2[i2] + o3; |
| 3661 | mlevel3[i3] = i; |
| 3662 | } |
| 3663 | return result; |
| 3664 | } |
| 3665 | |
| 3666 | static int |
| 3667 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 3668 | { |
| 3669 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 3670 | int l1 = c>>11; |
| 3671 | int l2 = (c>>7) & 0xF; |
| 3672 | int l3 = c & 0x7F; |
| 3673 | int i; |
| 3674 | |
| 3675 | #ifdef Py_UNICODE_WIDE |
| 3676 | if (c > 0xFFFF) { |
| 3677 | return -1; |
| 3678 | } |
| 3679 | #endif |
| 3680 | if (c == 0) |
| 3681 | return 0; |
| 3682 | /* level 1*/ |
| 3683 | i = map->level1[l1]; |
| 3684 | if (i == 0xFF) { |
| 3685 | return -1; |
| 3686 | } |
| 3687 | /* level 2*/ |
| 3688 | i = map->level23[16*i+l2]; |
| 3689 | if (i == 0xFF) { |
| 3690 | return -1; |
| 3691 | } |
| 3692 | /* level 3 */ |
| 3693 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 3694 | if (i == 0) { |
| 3695 | return -1; |
| 3696 | } |
| 3697 | return i; |
| 3698 | } |
| 3699 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3700 | /* Lookup the character ch in the mapping. If the character |
| 3701 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3702 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3703 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3704 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3705 | PyObject *w = PyInt_FromLong((long)c); |
| 3706 | PyObject *x; |
| 3707 | |
| 3708 | if (w == NULL) |
| 3709 | return NULL; |
| 3710 | x = PyObject_GetItem(mapping, w); |
| 3711 | Py_DECREF(w); |
| 3712 | if (x == NULL) { |
| 3713 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 3714 | /* No mapping found means: mapping is undefined. */ |
| 3715 | PyErr_Clear(); |
| 3716 | x = Py_None; |
| 3717 | Py_INCREF(x); |
| 3718 | return x; |
| 3719 | } else |
| 3720 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3721 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 3722 | else if (x == Py_None) |
| 3723 | return x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3724 | else if (PyInt_Check(x)) { |
| 3725 | long value = PyInt_AS_LONG(x); |
| 3726 | if (value < 0 || value > 255) { |
| 3727 | PyErr_SetString(PyExc_TypeError, |
| 3728 | "character mapping must be in range(256)"); |
| 3729 | Py_DECREF(x); |
| 3730 | return NULL; |
| 3731 | } |
| 3732 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3733 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3734 | else if (PyString_Check(x)) |
| 3735 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3736 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3737 | /* wrong return value */ |
| 3738 | PyErr_SetString(PyExc_TypeError, |
| 3739 | "character mapping must return integer, None or str"); |
| 3740 | Py_DECREF(x); |
| 3741 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3742 | } |
| 3743 | } |
| 3744 | |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3745 | static int |
| 3746 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
| 3747 | { |
| 3748 | Py_ssize_t outsize = PyString_GET_SIZE(*outobj); |
| 3749 | /* exponentially overallocate to minimize reallocations */ |
| 3750 | if (requiredsize < 2*outsize) |
| 3751 | requiredsize = 2*outsize; |
| 3752 | if (_PyString_Resize(outobj, requiredsize)) { |
| 3753 | return 0; |
| 3754 | } |
| 3755 | return 1; |
| 3756 | } |
| 3757 | |
| 3758 | typedef enum charmapencode_result { |
| 3759 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
| 3760 | }charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3761 | /* lookup the character, put the result in the output string and adjust |
| 3762 | various state variables. Reallocate the output string if not enough |
| 3763 | space is available. Return a new reference to the object that |
| 3764 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 3765 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 3766 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3767 | static |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3768 | charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3769 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3770 | { |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3771 | PyObject *rep; |
| 3772 | char *outstart; |
| 3773 | Py_ssize_t outsize = PyString_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3774 | |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 3775 | if (Py_Type(mapping) == &EncodingMapType) { |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3776 | int res = encoding_map_lookup(c, mapping); |
| 3777 | Py_ssize_t requiredsize = *outpos+1; |
| 3778 | if (res == -1) |
| 3779 | return enc_FAILED; |
| 3780 | if (outsize<requiredsize) |
| 3781 | if (!charmapencode_resize(outobj, outpos, requiredsize)) |
| 3782 | return enc_EXCEPTION; |
| 3783 | outstart = PyString_AS_STRING(*outobj); |
| 3784 | outstart[(*outpos)++] = (char)res; |
| 3785 | return enc_SUCCESS; |
| 3786 | } |
| 3787 | |
| 3788 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3789 | if (rep==NULL) |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3790 | return enc_EXCEPTION; |
| 3791 | else if (rep==Py_None) { |
| 3792 | Py_DECREF(rep); |
| 3793 | return enc_FAILED; |
| 3794 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3795 | if (PyInt_Check(rep)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3796 | Py_ssize_t requiredsize = *outpos+1; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3797 | if (outsize<requiredsize) |
| 3798 | if (!charmapencode_resize(outobj, outpos, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3799 | Py_DECREF(rep); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3800 | return enc_EXCEPTION; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3801 | } |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3802 | outstart = PyString_AS_STRING(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3803 | outstart[(*outpos)++] = (char)PyInt_AS_LONG(rep); |
| 3804 | } |
| 3805 | else { |
| 3806 | const char *repchars = PyString_AS_STRING(rep); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3807 | Py_ssize_t repsize = PyString_GET_SIZE(rep); |
| 3808 | Py_ssize_t requiredsize = *outpos+repsize; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3809 | if (outsize<requiredsize) |
| 3810 | if (!charmapencode_resize(outobj, outpos, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3811 | Py_DECREF(rep); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3812 | return enc_EXCEPTION; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3813 | } |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3814 | outstart = PyString_AS_STRING(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3815 | memcpy(outstart + *outpos, repchars, repsize); |
| 3816 | *outpos += repsize; |
| 3817 | } |
| 3818 | } |
Georg Brandl | 9f16760 | 2006-06-04 21:46:16 +0000 | [diff] [blame] | 3819 | Py_DECREF(rep); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3820 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3821 | } |
| 3822 | |
| 3823 | /* handle an error in PyUnicode_EncodeCharmap |
| 3824 | Return 0 on success, -1 on error */ |
| 3825 | static |
| 3826 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3827 | 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] | 3828 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 3829 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3830 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3831 | { |
| 3832 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3833 | Py_ssize_t repsize; |
| 3834 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3835 | Py_UNICODE *uni2; |
| 3836 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3837 | Py_ssize_t collstartpos = *inpos; |
| 3838 | Py_ssize_t collendpos = *inpos+1; |
| 3839 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3840 | char *encoding = "charmap"; |
| 3841 | char *reason = "character maps to <undefined>"; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3842 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3843 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3844 | /* find all unencodable characters */ |
| 3845 | while (collendpos < size) { |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3846 | PyObject *rep; |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 3847 | if (Py_Type(mapping) == &EncodingMapType) { |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3848 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 3849 | if (res != -1) |
| 3850 | break; |
| 3851 | ++collendpos; |
| 3852 | continue; |
| 3853 | } |
| 3854 | |
| 3855 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 3856 | if (rep==NULL) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3857 | return -1; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3858 | else if (rep!=Py_None) { |
| 3859 | Py_DECREF(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3860 | break; |
| 3861 | } |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3862 | Py_DECREF(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3863 | ++collendpos; |
| 3864 | } |
| 3865 | /* cache callback name lookup |
| 3866 | * (if not done yet, i.e. it's the first error) */ |
| 3867 | if (*known_errorHandler==-1) { |
| 3868 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3869 | *known_errorHandler = 1; |
| 3870 | else if (!strcmp(errors, "replace")) |
| 3871 | *known_errorHandler = 2; |
| 3872 | else if (!strcmp(errors, "ignore")) |
| 3873 | *known_errorHandler = 3; |
| 3874 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3875 | *known_errorHandler = 4; |
| 3876 | else |
| 3877 | *known_errorHandler = 0; |
| 3878 | } |
| 3879 | switch (*known_errorHandler) { |
| 3880 | case 1: /* strict */ |
| 3881 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3882 | return -1; |
| 3883 | case 2: /* replace */ |
| 3884 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
| 3885 | x = charmapencode_output('?', mapping, res, respos); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3886 | if (x==enc_EXCEPTION) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3887 | return -1; |
| 3888 | } |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3889 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3890 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3891 | return -1; |
| 3892 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3893 | } |
| 3894 | /* fall through */ |
| 3895 | case 3: /* ignore */ |
| 3896 | *inpos = collendpos; |
| 3897 | break; |
| 3898 | case 4: /* xmlcharrefreplace */ |
| 3899 | /* generate replacement (temporarily (mis)uses p) */ |
| 3900 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
| 3901 | char buffer[2+29+1+1]; |
| 3902 | char *cp; |
| 3903 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 3904 | for (cp = buffer; *cp; ++cp) { |
| 3905 | x = charmapencode_output(*cp, mapping, res, respos); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3906 | if (x==enc_EXCEPTION) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3907 | return -1; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3908 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3909 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3910 | return -1; |
| 3911 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3912 | } |
| 3913 | } |
| 3914 | *inpos = collendpos; |
| 3915 | break; |
| 3916 | default: |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 3917 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3918 | encoding, reason, p, size, exceptionObject, |
| 3919 | collstartpos, collendpos, &newpos); |
| 3920 | if (repunicode == NULL) |
| 3921 | return -1; |
| 3922 | /* generate replacement */ |
| 3923 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3924 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 3925 | x = charmapencode_output(*uni2, mapping, res, respos); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3926 | if (x==enc_EXCEPTION) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3927 | return -1; |
| 3928 | } |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3929 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3930 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3931 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3932 | return -1; |
| 3933 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3934 | } |
| 3935 | *inpos = newpos; |
| 3936 | Py_DECREF(repunicode); |
| 3937 | } |
| 3938 | return 0; |
| 3939 | } |
| 3940 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3941 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3942 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3943 | PyObject *mapping, |
| 3944 | const char *errors) |
| 3945 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3946 | /* output object */ |
| 3947 | PyObject *res = NULL; |
| 3948 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3949 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3950 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3951 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3952 | PyObject *errorHandler = NULL; |
| 3953 | PyObject *exc = NULL; |
| 3954 | /* the following variable is used for caching string comparisons |
| 3955 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 3956 | * 3=ignore, 4=xmlcharrefreplace */ |
| 3957 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3958 | |
| 3959 | /* Default to Latin-1 */ |
| 3960 | if (mapping == NULL) |
| 3961 | return PyUnicode_EncodeLatin1(p, size, errors); |
| 3962 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3963 | /* allocate enough for a simple encoding without |
| 3964 | replacements, if we need more, we'll resize */ |
| 3965 | res = PyString_FromStringAndSize(NULL, size); |
| 3966 | if (res == NULL) |
| 3967 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3968 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3969 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3970 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3971 | while (inpos<size) { |
| 3972 | /* try to encode it */ |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3973 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 3974 | if (x==enc_EXCEPTION) /* error */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3975 | goto onError; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3976 | if (x==enc_FAILED) { /* unencodable character */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3977 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 3978 | &exc, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 3979 | &known_errorHandler, &errorHandler, errors, |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 3980 | &res, &respos)) { |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 3981 | goto onError; |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +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 | else |
| 3985 | /* done with this character => adjust input position */ |
| 3986 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3987 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3988 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3989 | /* Resize if we allocated to much */ |
| 3990 | if (respos<PyString_GET_SIZE(res)) { |
| 3991 | if (_PyString_Resize(&res, respos)) |
| 3992 | goto onError; |
| 3993 | } |
| 3994 | Py_XDECREF(exc); |
| 3995 | Py_XDECREF(errorHandler); |
| 3996 | return res; |
| 3997 | |
| 3998 | onError: |
| 3999 | Py_XDECREF(res); |
| 4000 | Py_XDECREF(exc); |
| 4001 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4002 | return NULL; |
| 4003 | } |
| 4004 | |
| 4005 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
| 4006 | PyObject *mapping) |
| 4007 | { |
| 4008 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
| 4009 | PyErr_BadArgument(); |
| 4010 | return NULL; |
| 4011 | } |
| 4012 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
| 4013 | PyUnicode_GET_SIZE(unicode), |
| 4014 | mapping, |
| 4015 | NULL); |
| 4016 | } |
| 4017 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4018 | /* create or adjust a UnicodeTranslateError */ |
| 4019 | static void make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4020 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4021 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4022 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4023 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4024 | if (*exceptionObject == NULL) { |
| 4025 | *exceptionObject = PyUnicodeTranslateError_Create( |
| 4026 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4027 | } |
| 4028 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4029 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 4030 | goto onError; |
| 4031 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 4032 | goto onError; |
| 4033 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 4034 | goto onError; |
| 4035 | return; |
| 4036 | onError: |
| 4037 | Py_DECREF(*exceptionObject); |
| 4038 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4039 | } |
| 4040 | } |
| 4041 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4042 | /* raises a UnicodeTranslateError */ |
| 4043 | static void raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4044 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4045 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4046 | const char *reason) |
| 4047 | { |
| 4048 | make_translate_exception(exceptionObject, |
| 4049 | unicode, size, startpos, endpos, reason); |
| 4050 | if (*exceptionObject != NULL) |
| 4051 | PyCodec_StrictErrors(*exceptionObject); |
| 4052 | } |
| 4053 | |
| 4054 | /* error handling callback helper: |
| 4055 | build arguments, call the callback and check the arguments, |
| 4056 | put the result into newpos and return the replacement string, which |
| 4057 | has to be freed by the caller */ |
| 4058 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
| 4059 | PyObject **errorHandler, |
| 4060 | const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4061 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 4062 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4063 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4064 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4065 | 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] | 4066 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4067 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4068 | PyObject *restuple; |
| 4069 | PyObject *resunicode; |
| 4070 | |
| 4071 | if (*errorHandler == NULL) { |
| 4072 | *errorHandler = PyCodec_LookupError(errors); |
| 4073 | if (*errorHandler == NULL) |
| 4074 | return NULL; |
| 4075 | } |
| 4076 | |
| 4077 | make_translate_exception(exceptionObject, |
| 4078 | unicode, size, startpos, endpos, reason); |
| 4079 | if (*exceptionObject == NULL) |
| 4080 | return NULL; |
| 4081 | |
| 4082 | restuple = PyObject_CallFunctionObjArgs( |
| 4083 | *errorHandler, *exceptionObject, NULL); |
| 4084 | if (restuple == NULL) |
| 4085 | return NULL; |
| 4086 | if (!PyTuple_Check(restuple)) { |
| 4087 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 4088 | Py_DECREF(restuple); |
| 4089 | return NULL; |
| 4090 | } |
| 4091 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4092 | &resunicode, &i_newpos)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4093 | Py_DECREF(restuple); |
| 4094 | return NULL; |
| 4095 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4096 | if (i_newpos<0) |
| 4097 | *newpos = size+i_newpos; |
| 4098 | else |
| 4099 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4100 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 4101 | 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] | 4102 | Py_DECREF(restuple); |
| 4103 | return NULL; |
| 4104 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4105 | Py_INCREF(resunicode); |
| 4106 | Py_DECREF(restuple); |
| 4107 | return resunicode; |
| 4108 | } |
| 4109 | |
| 4110 | /* Lookup the character ch in the mapping and put the result in result, |
| 4111 | which must be decrefed by the caller. |
| 4112 | Return 0 on success, -1 on error */ |
| 4113 | static |
| 4114 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 4115 | { |
| 4116 | PyObject *w = PyInt_FromLong((long)c); |
| 4117 | PyObject *x; |
| 4118 | |
| 4119 | if (w == NULL) |
| 4120 | return -1; |
| 4121 | x = PyObject_GetItem(mapping, w); |
| 4122 | Py_DECREF(w); |
| 4123 | if (x == NULL) { |
| 4124 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4125 | /* No mapping found means: use 1:1 mapping. */ |
| 4126 | PyErr_Clear(); |
| 4127 | *result = NULL; |
| 4128 | return 0; |
| 4129 | } else |
| 4130 | return -1; |
| 4131 | } |
| 4132 | else if (x == Py_None) { |
| 4133 | *result = x; |
| 4134 | return 0; |
| 4135 | } |
| 4136 | else if (PyInt_Check(x)) { |
| 4137 | long value = PyInt_AS_LONG(x); |
| 4138 | long max = PyUnicode_GetMax(); |
| 4139 | if (value < 0 || value > max) { |
| 4140 | PyErr_Format(PyExc_TypeError, |
| 4141 | "character mapping must be in range(0x%lx)", max+1); |
| 4142 | Py_DECREF(x); |
| 4143 | return -1; |
| 4144 | } |
| 4145 | *result = x; |
| 4146 | return 0; |
| 4147 | } |
| 4148 | else if (PyUnicode_Check(x)) { |
| 4149 | *result = x; |
| 4150 | return 0; |
| 4151 | } |
| 4152 | else { |
| 4153 | /* wrong return value */ |
| 4154 | PyErr_SetString(PyExc_TypeError, |
| 4155 | "character mapping must return integer, None or unicode"); |
Walter Dörwald | 150523e | 2003-08-15 16:52:19 +0000 | [diff] [blame] | 4156 | Py_DECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4157 | return -1; |
| 4158 | } |
| 4159 | } |
| 4160 | /* ensure that *outobj is at least requiredsize characters long, |
| 4161 | if not reallocate and adjust various state variables. |
| 4162 | Return 0 on success, -1 on error */ |
| 4163 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4164 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4165 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4166 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4167 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4168 | if (requiredsize > oldsize) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4169 | /* remember old output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4170 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4171 | /* exponentially overallocate to minimize reallocations */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4172 | if (requiredsize < 2 * oldsize) |
| 4173 | requiredsize = 2 * oldsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 4174 | if (_PyUnicode_Resize(outobj, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4175 | return -1; |
| 4176 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4177 | } |
| 4178 | return 0; |
| 4179 | } |
| 4180 | /* lookup the character, put the result in the output string and adjust |
| 4181 | various state variables. Return a new reference to the object that |
| 4182 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 4183 | undefined (in which case no character was written). |
| 4184 | The called must decref result. |
| 4185 | Return 0 on success, -1 on error. */ |
| 4186 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4187 | 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] | 4188 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4189 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4190 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4191 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4192 | return -1; |
| 4193 | if (*res==NULL) { |
| 4194 | /* not found => default to 1:1 mapping */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4195 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4196 | } |
| 4197 | else if (*res==Py_None) |
| 4198 | ; |
| 4199 | else if (PyInt_Check(*res)) { |
| 4200 | /* no overflow check, because we know that the space is enough */ |
| 4201 | *(*outp)++ = (Py_UNICODE)PyInt_AS_LONG(*res); |
| 4202 | } |
| 4203 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4204 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4205 | if (repsize==1) { |
| 4206 | /* no overflow check, because we know that the space is enough */ |
| 4207 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 4208 | } |
| 4209 | else if (repsize!=0) { |
| 4210 | /* more than one character */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4211 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
Walter Dörwald | cd736e7 | 2004-02-05 17:36:00 +0000 | [diff] [blame] | 4212 | (insize - (curinp-startinp)) + |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4213 | repsize - 1; |
| 4214 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4215 | return -1; |
| 4216 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 4217 | *outp += repsize; |
| 4218 | } |
| 4219 | } |
| 4220 | else |
| 4221 | return -1; |
| 4222 | return 0; |
| 4223 | } |
| 4224 | |
| 4225 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4226 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4227 | PyObject *mapping, |
| 4228 | const char *errors) |
| 4229 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4230 | /* output object */ |
| 4231 | PyObject *res = NULL; |
| 4232 | /* pointers to the beginning and end+1 of input */ |
| 4233 | const Py_UNICODE *startp = p; |
| 4234 | const Py_UNICODE *endp = p + size; |
| 4235 | /* pointer into the output */ |
| 4236 | Py_UNICODE *str; |
| 4237 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4238 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4239 | char *reason = "character maps to <undefined>"; |
| 4240 | PyObject *errorHandler = NULL; |
| 4241 | PyObject *exc = NULL; |
| 4242 | /* the following variable is used for caching string comparisons |
| 4243 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 4244 | * 3=ignore, 4=xmlcharrefreplace */ |
| 4245 | int known_errorHandler = -1; |
| 4246 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4247 | if (mapping == NULL) { |
| 4248 | PyErr_BadArgument(); |
| 4249 | return NULL; |
| 4250 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4251 | |
| 4252 | /* allocate enough for a simple 1:1 translation without |
| 4253 | replacements, if we need more, we'll resize */ |
| 4254 | res = PyUnicode_FromUnicode(NULL, size); |
| 4255 | if (res == NULL) |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4256 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4257 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4258 | return res; |
| 4259 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4260 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4261 | while (p<endp) { |
| 4262 | /* try to encode it */ |
| 4263 | PyObject *x = NULL; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4264 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4265 | Py_XDECREF(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4266 | goto onError; |
| 4267 | } |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 4268 | Py_XDECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4269 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 4270 | ++p; |
| 4271 | else { /* untranslatable character */ |
| 4272 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4273 | Py_ssize_t repsize; |
| 4274 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4275 | Py_UNICODE *uni2; |
| 4276 | /* startpos for collecting untranslatable chars */ |
| 4277 | const Py_UNICODE *collstart = p; |
| 4278 | const Py_UNICODE *collend = p+1; |
| 4279 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4280 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4281 | /* find all untranslatable characters */ |
| 4282 | while (collend < endp) { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4283 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4284 | goto onError; |
| 4285 | Py_XDECREF(x); |
| 4286 | if (x!=Py_None) |
| 4287 | break; |
| 4288 | ++collend; |
| 4289 | } |
| 4290 | /* cache callback name lookup |
| 4291 | * (if not done yet, i.e. it's the first error) */ |
| 4292 | if (known_errorHandler==-1) { |
| 4293 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4294 | known_errorHandler = 1; |
| 4295 | else if (!strcmp(errors, "replace")) |
| 4296 | known_errorHandler = 2; |
| 4297 | else if (!strcmp(errors, "ignore")) |
| 4298 | known_errorHandler = 3; |
| 4299 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4300 | known_errorHandler = 4; |
| 4301 | else |
| 4302 | known_errorHandler = 0; |
| 4303 | } |
| 4304 | switch (known_errorHandler) { |
| 4305 | case 1: /* strict */ |
| 4306 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
| 4307 | goto onError; |
| 4308 | case 2: /* replace */ |
| 4309 | /* No need to check for space, this is a 1:1 replacement */ |
| 4310 | for (coll = collstart; coll<collend; ++coll) |
| 4311 | *str++ = '?'; |
| 4312 | /* fall through */ |
| 4313 | case 3: /* ignore */ |
| 4314 | p = collend; |
| 4315 | break; |
| 4316 | case 4: /* xmlcharrefreplace */ |
| 4317 | /* generate replacement (temporarily (mis)uses p) */ |
| 4318 | for (p = collstart; p < collend; ++p) { |
| 4319 | char buffer[2+29+1+1]; |
| 4320 | char *cp; |
| 4321 | sprintf(buffer, "&#%d;", (int)*p); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4322 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4323 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 4324 | goto onError; |
| 4325 | for (cp = buffer; *cp; ++cp) |
| 4326 | *str++ = *cp; |
| 4327 | } |
| 4328 | p = collend; |
| 4329 | break; |
| 4330 | default: |
| 4331 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 4332 | reason, startp, size, &exc, |
| 4333 | collstart-startp, collend-startp, &newpos); |
| 4334 | if (repunicode == NULL) |
| 4335 | goto onError; |
| 4336 | /* generate replacement */ |
| 4337 | repsize = PyUnicode_GET_SIZE(repunicode); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4338 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4339 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 4340 | Py_DECREF(repunicode); |
| 4341 | goto onError; |
| 4342 | } |
| 4343 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 4344 | *str++ = *uni2; |
| 4345 | p = startp + newpos; |
| 4346 | Py_DECREF(repunicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4347 | } |
| 4348 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4349 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4350 | /* Resize if we allocated to much */ |
| 4351 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4352 | if (respos<PyUnicode_GET_SIZE(res)) { |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 4353 | if (_PyUnicode_Resize(&res, respos) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 4354 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4355 | } |
| 4356 | Py_XDECREF(exc); |
| 4357 | Py_XDECREF(errorHandler); |
| 4358 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4359 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4360 | onError: |
| 4361 | Py_XDECREF(res); |
| 4362 | Py_XDECREF(exc); |
| 4363 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4364 | return NULL; |
| 4365 | } |
| 4366 | |
| 4367 | PyObject *PyUnicode_Translate(PyObject *str, |
| 4368 | PyObject *mapping, |
| 4369 | const char *errors) |
| 4370 | { |
| 4371 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4372 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4373 | str = PyUnicode_FromObject(str); |
| 4374 | if (str == NULL) |
| 4375 | goto onError; |
| 4376 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
| 4377 | PyUnicode_GET_SIZE(str), |
| 4378 | mapping, |
| 4379 | errors); |
| 4380 | Py_DECREF(str); |
| 4381 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4382 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4383 | onError: |
| 4384 | Py_XDECREF(str); |
| 4385 | return NULL; |
| 4386 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4387 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4388 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 4389 | |
| 4390 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4391 | Py_ssize_t length, |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4392 | char *output, |
| 4393 | const char *errors) |
| 4394 | { |
| 4395 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4396 | PyObject *errorHandler = NULL; |
| 4397 | PyObject *exc = NULL; |
| 4398 | const char *encoding = "decimal"; |
| 4399 | const char *reason = "invalid decimal Unicode string"; |
| 4400 | /* the following variable is used for caching string comparisons |
| 4401 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 4402 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4403 | |
| 4404 | if (output == NULL) { |
| 4405 | PyErr_BadArgument(); |
| 4406 | return -1; |
| 4407 | } |
| 4408 | |
| 4409 | p = s; |
| 4410 | end = s + length; |
| 4411 | while (p < end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4412 | register Py_UNICODE ch = *p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4413 | int decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4414 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4415 | Py_ssize_t repsize; |
| 4416 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4417 | Py_UNICODE *uni2; |
| 4418 | Py_UNICODE *collstart; |
| 4419 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4420 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4421 | if (Py_UNICODE_ISSPACE(ch)) { |
| 4422 | *output++ = ' '; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4423 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4424 | continue; |
| 4425 | } |
| 4426 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 4427 | if (decimal >= 0) { |
| 4428 | *output++ = '0' + decimal; |
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 | } |
Guido van Rossum | ba47704 | 2000-04-06 18:18:10 +0000 | [diff] [blame] | 4432 | if (0 < ch && ch < 256) { |
Guido van Rossum | 42c29aa | 2000-05-03 23:58:29 +0000 | [diff] [blame] | 4433 | *output++ = (char)ch; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4434 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4435 | continue; |
| 4436 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4437 | /* All other characters are considered unencodable */ |
| 4438 | collstart = p; |
| 4439 | collend = p+1; |
| 4440 | while (collend < end) { |
| 4441 | if ((0 < *collend && *collend < 256) || |
| 4442 | !Py_UNICODE_ISSPACE(*collend) || |
| 4443 | Py_UNICODE_TODECIMAL(*collend)) |
| 4444 | break; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4445 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4446 | /* cache callback name lookup |
| 4447 | * (if not done yet, i.e. it's the first error) */ |
| 4448 | if (known_errorHandler==-1) { |
| 4449 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4450 | known_errorHandler = 1; |
| 4451 | else if (!strcmp(errors, "replace")) |
| 4452 | known_errorHandler = 2; |
| 4453 | else if (!strcmp(errors, "ignore")) |
| 4454 | known_errorHandler = 3; |
| 4455 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4456 | known_errorHandler = 4; |
| 4457 | else |
| 4458 | known_errorHandler = 0; |
| 4459 | } |
| 4460 | switch (known_errorHandler) { |
| 4461 | case 1: /* strict */ |
| 4462 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 4463 | goto onError; |
| 4464 | case 2: /* replace */ |
| 4465 | for (p = collstart; p < collend; ++p) |
| 4466 | *output++ = '?'; |
| 4467 | /* fall through */ |
| 4468 | case 3: /* ignore */ |
| 4469 | p = collend; |
| 4470 | break; |
| 4471 | case 4: /* xmlcharrefreplace */ |
| 4472 | /* generate replacement (temporarily (mis)uses p) */ |
| 4473 | for (p = collstart; p < collend; ++p) |
| 4474 | output += sprintf(output, "&#%d;", (int)*p); |
| 4475 | p = collend; |
| 4476 | break; |
| 4477 | default: |
| 4478 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 4479 | encoding, reason, s, length, &exc, |
| 4480 | collstart-s, collend-s, &newpos); |
| 4481 | if (repunicode == NULL) |
| 4482 | goto onError; |
| 4483 | /* generate replacement */ |
| 4484 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4485 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 4486 | Py_UNICODE ch = *uni2; |
| 4487 | if (Py_UNICODE_ISSPACE(ch)) |
| 4488 | *output++ = ' '; |
| 4489 | else { |
| 4490 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 4491 | if (decimal >= 0) |
| 4492 | *output++ = '0' + decimal; |
| 4493 | else if (0 < ch && ch < 256) |
| 4494 | *output++ = (char)ch; |
| 4495 | else { |
| 4496 | Py_DECREF(repunicode); |
| 4497 | raise_encode_exception(&exc, encoding, |
| 4498 | s, length, collstart-s, collend-s, reason); |
| 4499 | goto onError; |
| 4500 | } |
| 4501 | } |
| 4502 | } |
| 4503 | p = s + newpos; |
| 4504 | Py_DECREF(repunicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4505 | } |
| 4506 | } |
| 4507 | /* 0-terminate the output string */ |
| 4508 | *output++ = '\0'; |
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 0; |
| 4512 | |
| 4513 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4514 | Py_XDECREF(exc); |
| 4515 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4516 | return -1; |
| 4517 | } |
| 4518 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4519 | /* --- Helpers ------------------------------------------------------------ */ |
| 4520 | |
Fredrik Lundh | a50d201 | 2006-05-26 17:04:58 +0000 | [diff] [blame] | 4521 | #define STRINGLIB_CHAR Py_UNICODE |
Fredrik Lundh | 6471ee4 | 2006-05-24 14:28:11 +0000 | [diff] [blame] | 4522 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 4523 | #define STRINGLIB_LEN PyUnicode_GET_SIZE |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 4524 | #define STRINGLIB_NEW PyUnicode_FromUnicode |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 4525 | #define STRINGLIB_STR PyUnicode_AS_UNICODE |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 4526 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 4527 | Py_LOCAL_INLINE(int) |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 4528 | STRINGLIB_CMP(const Py_UNICODE* str, const Py_UNICODE* other, Py_ssize_t len) |
| 4529 | { |
Fredrik Lundh | 9c0e9c0 | 2006-05-26 18:24:15 +0000 | [diff] [blame] | 4530 | if (str[0] != other[0]) |
| 4531 | return 1; |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 4532 | return memcmp((void*) str, (void*) other, len * sizeof(Py_UNICODE)); |
| 4533 | } |
| 4534 | |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 4535 | #define STRINGLIB_EMPTY unicode_empty |
| 4536 | |
Fredrik Lundh | a50d201 | 2006-05-26 17:04:58 +0000 | [diff] [blame] | 4537 | #include "stringlib/fastsearch.h" |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 4538 | |
| 4539 | #include "stringlib/count.h" |
| 4540 | #include "stringlib/find.h" |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 4541 | #include "stringlib/partition.h" |
| 4542 | |
Fredrik Lundh | c816281 | 2006-05-26 19:33:03 +0000 | [diff] [blame] | 4543 | /* helper macro to fixup start/end slice values */ |
| 4544 | #define FIX_START_END(obj) \ |
| 4545 | if (start < 0) \ |
| 4546 | start += (obj)->length; \ |
| 4547 | if (start < 0) \ |
| 4548 | start = 0; \ |
| 4549 | if (end > (obj)->length) \ |
| 4550 | end = (obj)->length; \ |
| 4551 | if (end < 0) \ |
| 4552 | end += (obj)->length; \ |
| 4553 | if (end < 0) \ |
| 4554 | end = 0; |
| 4555 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4556 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 4557 | PyObject *substr, |
| 4558 | Py_ssize_t start, |
| 4559 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4560 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4561 | Py_ssize_t result; |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 4562 | PyUnicodeObject* str_obj; |
| 4563 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4564 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 4565 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 4566 | if (!str_obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4567 | return -1; |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 4568 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 4569 | if (!sub_obj) { |
| 4570 | Py_DECREF(str_obj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4571 | return -1; |
| 4572 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4573 | |
Fredrik Lundh | c816281 | 2006-05-26 19:33:03 +0000 | [diff] [blame] | 4574 | FIX_START_END(str_obj); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4575 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 4576 | result = stringlib_count( |
| 4577 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length |
| 4578 | ); |
| 4579 | |
| 4580 | Py_DECREF(sub_obj); |
| 4581 | Py_DECREF(str_obj); |
| 4582 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4583 | return result; |
| 4584 | } |
| 4585 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4586 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 4587 | PyObject *sub, |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 4588 | Py_ssize_t start, |
| 4589 | Py_ssize_t end, |
| 4590 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4591 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4592 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4593 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 4594 | str = PyUnicode_FromObject(str); |
| 4595 | if (!str) |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 4596 | return -2; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 4597 | sub = PyUnicode_FromObject(sub); |
| 4598 | if (!sub) { |
| 4599 | Py_DECREF(str); |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 4600 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4601 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4602 | |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 4603 | if (direction > 0) |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 4604 | result = stringlib_find_slice( |
| 4605 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 4606 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 4607 | start, end |
| 4608 | ); |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 4609 | else |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 4610 | result = stringlib_rfind_slice( |
| 4611 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 4612 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 4613 | start, end |
| 4614 | ); |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 4615 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 4616 | Py_DECREF(str); |
| 4617 | Py_DECREF(sub); |
| 4618 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4619 | return result; |
| 4620 | } |
| 4621 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4622 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4623 | int tailmatch(PyUnicodeObject *self, |
| 4624 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4625 | Py_ssize_t start, |
| 4626 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4627 | int direction) |
| 4628 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4629 | if (substring->length == 0) |
| 4630 | return 1; |
| 4631 | |
Fredrik Lundh | c816281 | 2006-05-26 19:33:03 +0000 | [diff] [blame] | 4632 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4633 | |
| 4634 | end -= substring->length; |
| 4635 | if (end < start) |
| 4636 | return 0; |
| 4637 | |
| 4638 | if (direction > 0) { |
| 4639 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 4640 | return 1; |
| 4641 | } else { |
| 4642 | if (Py_UNICODE_MATCH(self, start, substring)) |
| 4643 | return 1; |
| 4644 | } |
| 4645 | |
| 4646 | return 0; |
| 4647 | } |
| 4648 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4649 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4650 | PyObject *substr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4651 | Py_ssize_t start, |
| 4652 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4653 | int direction) |
| 4654 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4655 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4656 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4657 | str = PyUnicode_FromObject(str); |
| 4658 | if (str == NULL) |
| 4659 | return -1; |
| 4660 | substr = PyUnicode_FromObject(substr); |
| 4661 | if (substr == NULL) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 4662 | Py_DECREF(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4663 | return -1; |
| 4664 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4665 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4666 | result = tailmatch((PyUnicodeObject *)str, |
| 4667 | (PyUnicodeObject *)substr, |
| 4668 | start, end, direction); |
| 4669 | Py_DECREF(str); |
| 4670 | Py_DECREF(substr); |
| 4671 | return result; |
| 4672 | } |
| 4673 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4674 | /* Apply fixfct filter to the Unicode object self and return a |
| 4675 | reference to the modified object */ |
| 4676 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4677 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4678 | PyObject *fixup(PyUnicodeObject *self, |
| 4679 | int (*fixfct)(PyUnicodeObject *s)) |
| 4680 | { |
| 4681 | |
| 4682 | PyUnicodeObject *u; |
| 4683 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4684 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4685 | if (u == NULL) |
| 4686 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4687 | |
| 4688 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 4689 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4690 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4691 | /* fixfct should return TRUE if it modified the buffer. If |
| 4692 | FALSE, return a reference to the original buffer instead |
| 4693 | (to save space, not time) */ |
| 4694 | Py_INCREF(self); |
| 4695 | Py_DECREF(u); |
| 4696 | return (PyObject*) self; |
| 4697 | } |
| 4698 | return (PyObject*) u; |
| 4699 | } |
| 4700 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4701 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4702 | int fixupper(PyUnicodeObject *self) |
| 4703 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4704 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4705 | Py_UNICODE *s = self->str; |
| 4706 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4707 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4708 | while (len-- > 0) { |
| 4709 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4710 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4711 | ch = Py_UNICODE_TOUPPER(*s); |
| 4712 | if (ch != *s) { |
| 4713 | status = 1; |
| 4714 | *s = ch; |
| 4715 | } |
| 4716 | s++; |
| 4717 | } |
| 4718 | |
| 4719 | return status; |
| 4720 | } |
| 4721 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4722 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4723 | int fixlower(PyUnicodeObject *self) |
| 4724 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4725 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4726 | Py_UNICODE *s = self->str; |
| 4727 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4728 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4729 | while (len-- > 0) { |
| 4730 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4731 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4732 | ch = Py_UNICODE_TOLOWER(*s); |
| 4733 | if (ch != *s) { |
| 4734 | status = 1; |
| 4735 | *s = ch; |
| 4736 | } |
| 4737 | s++; |
| 4738 | } |
| 4739 | |
| 4740 | return status; |
| 4741 | } |
| 4742 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4743 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4744 | int fixswapcase(PyUnicodeObject *self) |
| 4745 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4746 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4747 | Py_UNICODE *s = self->str; |
| 4748 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4749 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4750 | while (len-- > 0) { |
| 4751 | if (Py_UNICODE_ISUPPER(*s)) { |
| 4752 | *s = Py_UNICODE_TOLOWER(*s); |
| 4753 | status = 1; |
| 4754 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 4755 | *s = Py_UNICODE_TOUPPER(*s); |
| 4756 | status = 1; |
| 4757 | } |
| 4758 | s++; |
| 4759 | } |
| 4760 | |
| 4761 | return status; |
| 4762 | } |
| 4763 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4764 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4765 | int fixcapitalize(PyUnicodeObject *self) |
| 4766 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4767 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 4768 | Py_UNICODE *s = self->str; |
| 4769 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4770 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 4771 | if (len == 0) |
| 4772 | return 0; |
| 4773 | if (Py_UNICODE_ISLOWER(*s)) { |
| 4774 | *s = Py_UNICODE_TOUPPER(*s); |
| 4775 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4776 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 4777 | s++; |
| 4778 | while (--len > 0) { |
| 4779 | if (Py_UNICODE_ISUPPER(*s)) { |
| 4780 | *s = Py_UNICODE_TOLOWER(*s); |
| 4781 | status = 1; |
| 4782 | } |
| 4783 | s++; |
| 4784 | } |
| 4785 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4786 | } |
| 4787 | |
| 4788 | static |
| 4789 | int fixtitle(PyUnicodeObject *self) |
| 4790 | { |
| 4791 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 4792 | register Py_UNICODE *e; |
| 4793 | int previous_is_cased; |
| 4794 | |
| 4795 | /* Shortcut for single character strings */ |
| 4796 | if (PyUnicode_GET_SIZE(self) == 1) { |
| 4797 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 4798 | if (*p != ch) { |
| 4799 | *p = ch; |
| 4800 | return 1; |
| 4801 | } |
| 4802 | else |
| 4803 | return 0; |
| 4804 | } |
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 | e = p + PyUnicode_GET_SIZE(self); |
| 4807 | previous_is_cased = 0; |
| 4808 | for (; p < e; p++) { |
| 4809 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4810 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4811 | if (previous_is_cased) |
| 4812 | *p = Py_UNICODE_TOLOWER(ch); |
| 4813 | else |
| 4814 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4815 | |
| 4816 | if (Py_UNICODE_ISLOWER(ch) || |
| 4817 | Py_UNICODE_ISUPPER(ch) || |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4818 | Py_UNICODE_ISTITLE(ch)) |
| 4819 | previous_is_cased = 1; |
| 4820 | else |
| 4821 | previous_is_cased = 0; |
| 4822 | } |
| 4823 | return 1; |
| 4824 | } |
| 4825 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4826 | PyObject * |
| 4827 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4828 | { |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4829 | PyObject *internal_separator = NULL; |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 4830 | const Py_UNICODE blank = ' '; |
| 4831 | const Py_UNICODE *sep = ␣ |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 4832 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4833 | PyUnicodeObject *res = NULL; /* the result */ |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 4834 | Py_ssize_t res_alloc = 100; /* # allocated bytes for string in res */ |
| 4835 | Py_ssize_t res_used; /* # used bytes */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4836 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 4837 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4838 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4839 | PyObject *item; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4840 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4841 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4842 | fseq = PySequence_Fast(seq, ""); |
| 4843 | if (fseq == NULL) { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4844 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4845 | } |
| 4846 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 4847 | /* Grrrr. A codec may be invoked to convert str objects to |
| 4848 | * Unicode, and so it's possible to call back into Python code |
| 4849 | * during PyUnicode_FromObject(), and so it's possible for a sick |
| 4850 | * codec to change the size of fseq (if seq is a list). Therefore |
| 4851 | * we have to keep refetching the size -- can't assume seqlen |
| 4852 | * is invariant. |
| 4853 | */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4854 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 4855 | /* If empty sequence, return u"". */ |
| 4856 | if (seqlen == 0) { |
| 4857 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 4858 | goto Done; |
| 4859 | } |
| 4860 | /* If singleton sequence with an exact Unicode, return that. */ |
| 4861 | if (seqlen == 1) { |
| 4862 | item = PySequence_Fast_GET_ITEM(fseq, 0); |
| 4863 | if (PyUnicode_CheckExact(item)) { |
| 4864 | Py_INCREF(item); |
| 4865 | res = (PyUnicodeObject *)item; |
| 4866 | goto Done; |
| 4867 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4868 | } |
| 4869 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4870 | /* At least two items to join, or one that isn't exact Unicode. */ |
| 4871 | if (seqlen > 1) { |
| 4872 | /* Set up sep and seplen -- they're needed. */ |
| 4873 | if (separator == NULL) { |
| 4874 | sep = ␣ |
| 4875 | seplen = 1; |
| 4876 | } |
| 4877 | else { |
| 4878 | internal_separator = PyUnicode_FromObject(separator); |
| 4879 | if (internal_separator == NULL) |
| 4880 | goto onError; |
| 4881 | sep = PyUnicode_AS_UNICODE(internal_separator); |
| 4882 | seplen = PyUnicode_GET_SIZE(internal_separator); |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 4883 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 4884 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4885 | } |
| 4886 | } |
| 4887 | |
| 4888 | /* Get space. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4889 | res = _PyUnicode_New(res_alloc); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4890 | if (res == NULL) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4891 | goto onError; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4892 | res_p = PyUnicode_AS_UNICODE(res); |
| 4893 | res_used = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4894 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4895 | for (i = 0; i < seqlen; ++i) { |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 4896 | Py_ssize_t itemlen; |
| 4897 | Py_ssize_t new_res_used; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4898 | |
| 4899 | item = PySequence_Fast_GET_ITEM(fseq, i); |
| 4900 | /* Convert item to Unicode. */ |
| 4901 | if (! PyUnicode_Check(item) && ! PyString_Check(item)) { |
| 4902 | PyErr_Format(PyExc_TypeError, |
Thomas Wouters | 715a4cd | 2006-04-16 22:04:49 +0000 | [diff] [blame] | 4903 | "sequence item %zd: expected string or Unicode," |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4904 | " %.80s found", |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 4905 | i, Py_Type(item)->tp_name); |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 4906 | goto onError; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4907 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4908 | item = PyUnicode_FromObject(item); |
| 4909 | if (item == NULL) |
| 4910 | goto onError; |
| 4911 | /* We own a reference to item from here on. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4912 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 4913 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 4914 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 4915 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4916 | /* 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] | 4917 | itemlen = PyUnicode_GET_SIZE(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4918 | new_res_used = res_used + itemlen; |
Georg Brandl | 90e27d3 | 2006-06-10 06:40:50 +0000 | [diff] [blame] | 4919 | if (new_res_used < 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4920 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4921 | if (i < seqlen - 1) { |
| 4922 | new_res_used += seplen; |
Georg Brandl | 90e27d3 | 2006-06-10 06:40:50 +0000 | [diff] [blame] | 4923 | if (new_res_used < 0) |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4924 | goto Overflow; |
| 4925 | } |
| 4926 | if (new_res_used > res_alloc) { |
| 4927 | /* double allocated size until it's big enough */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4928 | do { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4929 | res_alloc += res_alloc; |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 4930 | if (res_alloc <= 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4931 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4932 | } while (new_res_used > res_alloc); |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4933 | if (_PyUnicode_Resize(&res, res_alloc) < 0) { |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 4934 | Py_DECREF(item); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4935 | goto onError; |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 4936 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4937 | res_p = PyUnicode_AS_UNICODE(res) + res_used; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4938 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4939 | |
| 4940 | /* Copy item, and maybe the separator. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4941 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4942 | res_p += itemlen; |
| 4943 | if (i < seqlen - 1) { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4944 | Py_UNICODE_COPY(res_p, sep, seplen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4945 | res_p += seplen; |
| 4946 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4947 | Py_DECREF(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4948 | res_used = new_res_used; |
| 4949 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4950 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4951 | /* Shrink res to match the used area; this probably can't fail, |
| 4952 | * but it's cheap to check. |
| 4953 | */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4954 | if (_PyUnicode_Resize(&res, res_used) < 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4955 | goto onError; |
| 4956 | |
| 4957 | Done: |
| 4958 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4959 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4960 | return (PyObject *)res; |
| 4961 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4962 | Overflow: |
| 4963 | PyErr_SetString(PyExc_OverflowError, |
Georg Brandl | 90e27d3 | 2006-06-10 06:40:50 +0000 | [diff] [blame] | 4964 | "join() result is too long for a Python string"); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4965 | Py_DECREF(item); |
| 4966 | /* fall through */ |
| 4967 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4968 | onError: |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4969 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4970 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4971 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4972 | return NULL; |
| 4973 | } |
| 4974 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4975 | static |
| 4976 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4977 | Py_ssize_t left, |
| 4978 | Py_ssize_t right, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4979 | Py_UNICODE fill) |
| 4980 | { |
| 4981 | PyUnicodeObject *u; |
| 4982 | |
| 4983 | if (left < 0) |
| 4984 | left = 0; |
| 4985 | if (right < 0) |
| 4986 | right = 0; |
| 4987 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4988 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4989 | Py_INCREF(self); |
| 4990 | return self; |
| 4991 | } |
| 4992 | |
| 4993 | u = _PyUnicode_New(left + self->length + right); |
| 4994 | if (u) { |
| 4995 | if (left) |
| 4996 | Py_UNICODE_FILL(u->str, fill, left); |
| 4997 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 4998 | if (right) |
| 4999 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 5000 | } |
| 5001 | |
| 5002 | return u; |
| 5003 | } |
| 5004 | |
| 5005 | #define SPLIT_APPEND(data, left, right) \ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5006 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5007 | if (!str) \ |
| 5008 | goto onError; \ |
| 5009 | if (PyList_Append(list, str)) { \ |
| 5010 | Py_DECREF(str); \ |
| 5011 | goto onError; \ |
| 5012 | } \ |
| 5013 | else \ |
| 5014 | Py_DECREF(str); |
| 5015 | |
| 5016 | static |
| 5017 | PyObject *split_whitespace(PyUnicodeObject *self, |
| 5018 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5019 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5020 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5021 | register Py_ssize_t i; |
| 5022 | register Py_ssize_t j; |
| 5023 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5024 | PyObject *str; |
| 5025 | |
| 5026 | for (i = j = 0; i < len; ) { |
| 5027 | /* find a token */ |
| 5028 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 5029 | i++; |
| 5030 | j = i; |
| 5031 | while (i < len && !Py_UNICODE_ISSPACE(self->str[i])) |
| 5032 | i++; |
| 5033 | if (j < i) { |
| 5034 | if (maxcount-- <= 0) |
| 5035 | break; |
| 5036 | SPLIT_APPEND(self->str, j, i); |
| 5037 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 5038 | i++; |
| 5039 | j = i; |
| 5040 | } |
| 5041 | } |
| 5042 | if (j < len) { |
| 5043 | SPLIT_APPEND(self->str, j, len); |
| 5044 | } |
| 5045 | return list; |
| 5046 | |
| 5047 | onError: |
| 5048 | Py_DECREF(list); |
| 5049 | return NULL; |
| 5050 | } |
| 5051 | |
| 5052 | PyObject *PyUnicode_Splitlines(PyObject *string, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5053 | int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5054 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5055 | register Py_ssize_t i; |
| 5056 | register Py_ssize_t j; |
| 5057 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5058 | PyObject *list; |
| 5059 | PyObject *str; |
| 5060 | Py_UNICODE *data; |
| 5061 | |
| 5062 | string = PyUnicode_FromObject(string); |
| 5063 | if (string == NULL) |
| 5064 | return NULL; |
| 5065 | data = PyUnicode_AS_UNICODE(string); |
| 5066 | len = PyUnicode_GET_SIZE(string); |
| 5067 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5068 | list = PyList_New(0); |
| 5069 | if (!list) |
| 5070 | goto onError; |
| 5071 | |
| 5072 | for (i = j = 0; i < len; ) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5073 | Py_ssize_t eol; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5074 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5075 | /* Find a line and append it */ |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5076 | while (i < len && !BLOOM_LINEBREAK(data[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5077 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5078 | |
| 5079 | /* Skip the line break reading CRLF as one line break */ |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5080 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5081 | if (i < len) { |
| 5082 | if (data[i] == '\r' && i + 1 < len && |
| 5083 | data[i+1] == '\n') |
| 5084 | i += 2; |
| 5085 | else |
| 5086 | i++; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5087 | if (keepends) |
| 5088 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5089 | } |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5090 | SPLIT_APPEND(data, j, eol); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5091 | j = i; |
| 5092 | } |
| 5093 | if (j < len) { |
| 5094 | SPLIT_APPEND(data, j, len); |
| 5095 | } |
| 5096 | |
| 5097 | Py_DECREF(string); |
| 5098 | return list; |
| 5099 | |
| 5100 | onError: |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5101 | Py_XDECREF(list); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5102 | Py_DECREF(string); |
| 5103 | return NULL; |
| 5104 | } |
| 5105 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5106 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5107 | PyObject *split_char(PyUnicodeObject *self, |
| 5108 | PyObject *list, |
| 5109 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5110 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5111 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5112 | register Py_ssize_t i; |
| 5113 | register Py_ssize_t j; |
| 5114 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5115 | PyObject *str; |
| 5116 | |
| 5117 | for (i = j = 0; i < len; ) { |
| 5118 | if (self->str[i] == ch) { |
| 5119 | if (maxcount-- <= 0) |
| 5120 | break; |
| 5121 | SPLIT_APPEND(self->str, j, i); |
| 5122 | i = j = i + 1; |
| 5123 | } else |
| 5124 | i++; |
| 5125 | } |
| 5126 | if (j <= len) { |
| 5127 | SPLIT_APPEND(self->str, j, len); |
| 5128 | } |
| 5129 | return list; |
| 5130 | |
| 5131 | onError: |
| 5132 | Py_DECREF(list); |
| 5133 | return NULL; |
| 5134 | } |
| 5135 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5136 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5137 | PyObject *split_substring(PyUnicodeObject *self, |
| 5138 | PyObject *list, |
| 5139 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5140 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5141 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5142 | register Py_ssize_t i; |
| 5143 | register Py_ssize_t j; |
| 5144 | Py_ssize_t len = self->length; |
| 5145 | Py_ssize_t sublen = substring->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5146 | PyObject *str; |
| 5147 | |
Guido van Rossum | cda4f9a | 2000-12-19 02:23:19 +0000 | [diff] [blame] | 5148 | for (i = j = 0; i <= len - sublen; ) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5149 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 5150 | if (maxcount-- <= 0) |
| 5151 | break; |
| 5152 | SPLIT_APPEND(self->str, j, i); |
| 5153 | i = j = i + sublen; |
| 5154 | } else |
| 5155 | i++; |
| 5156 | } |
| 5157 | if (j <= len) { |
| 5158 | SPLIT_APPEND(self->str, j, len); |
| 5159 | } |
| 5160 | return list; |
| 5161 | |
| 5162 | onError: |
| 5163 | Py_DECREF(list); |
| 5164 | return NULL; |
| 5165 | } |
| 5166 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5167 | static |
| 5168 | PyObject *rsplit_whitespace(PyUnicodeObject *self, |
| 5169 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5170 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5171 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5172 | register Py_ssize_t i; |
| 5173 | register Py_ssize_t j; |
| 5174 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5175 | PyObject *str; |
| 5176 | |
| 5177 | for (i = j = len - 1; i >= 0; ) { |
| 5178 | /* find a token */ |
| 5179 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 5180 | i--; |
| 5181 | j = i; |
| 5182 | while (i >= 0 && !Py_UNICODE_ISSPACE(self->str[i])) |
| 5183 | i--; |
| 5184 | if (j > i) { |
| 5185 | if (maxcount-- <= 0) |
| 5186 | break; |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5187 | SPLIT_APPEND(self->str, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5188 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 5189 | i--; |
| 5190 | j = i; |
| 5191 | } |
| 5192 | } |
| 5193 | if (j >= 0) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5194 | SPLIT_APPEND(self->str, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5195 | } |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5196 | if (PyList_Reverse(list) < 0) |
| 5197 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5198 | return list; |
| 5199 | |
| 5200 | onError: |
| 5201 | Py_DECREF(list); |
| 5202 | return NULL; |
| 5203 | } |
| 5204 | |
| 5205 | static |
| 5206 | PyObject *rsplit_char(PyUnicodeObject *self, |
| 5207 | PyObject *list, |
| 5208 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5209 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5210 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5211 | register Py_ssize_t i; |
| 5212 | register Py_ssize_t j; |
| 5213 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5214 | PyObject *str; |
| 5215 | |
| 5216 | for (i = j = len - 1; i >= 0; ) { |
| 5217 | if (self->str[i] == ch) { |
| 5218 | if (maxcount-- <= 0) |
| 5219 | break; |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5220 | SPLIT_APPEND(self->str, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5221 | j = i = i - 1; |
| 5222 | } else |
| 5223 | i--; |
| 5224 | } |
Hye-Shik Chang | 7fc4cf5 | 2003-12-23 09:10:16 +0000 | [diff] [blame] | 5225 | if (j >= -1) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5226 | SPLIT_APPEND(self->str, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5227 | } |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5228 | if (PyList_Reverse(list) < 0) |
| 5229 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5230 | return list; |
| 5231 | |
| 5232 | onError: |
| 5233 | Py_DECREF(list); |
| 5234 | return NULL; |
| 5235 | } |
| 5236 | |
| 5237 | static |
| 5238 | PyObject *rsplit_substring(PyUnicodeObject *self, |
| 5239 | PyObject *list, |
| 5240 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5241 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5242 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5243 | register Py_ssize_t i; |
| 5244 | register Py_ssize_t j; |
| 5245 | Py_ssize_t len = self->length; |
| 5246 | Py_ssize_t sublen = substring->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5247 | PyObject *str; |
| 5248 | |
| 5249 | for (i = len - sublen, j = len; i >= 0; ) { |
| 5250 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 5251 | if (maxcount-- <= 0) |
| 5252 | break; |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5253 | SPLIT_APPEND(self->str, i + sublen, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5254 | j = i; |
| 5255 | i -= sublen; |
| 5256 | } else |
| 5257 | i--; |
| 5258 | } |
| 5259 | if (j >= 0) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5260 | SPLIT_APPEND(self->str, 0, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5261 | } |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5262 | if (PyList_Reverse(list) < 0) |
| 5263 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5264 | return list; |
| 5265 | |
| 5266 | onError: |
| 5267 | Py_DECREF(list); |
| 5268 | return NULL; |
| 5269 | } |
| 5270 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5271 | #undef SPLIT_APPEND |
| 5272 | |
| 5273 | static |
| 5274 | PyObject *split(PyUnicodeObject *self, |
| 5275 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5276 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5277 | { |
| 5278 | PyObject *list; |
| 5279 | |
| 5280 | if (maxcount < 0) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5281 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5282 | |
| 5283 | list = PyList_New(0); |
| 5284 | if (!list) |
| 5285 | return NULL; |
| 5286 | |
| 5287 | if (substring == NULL) |
| 5288 | return split_whitespace(self,list,maxcount); |
| 5289 | |
| 5290 | else if (substring->length == 1) |
| 5291 | return split_char(self,list,substring->str[0],maxcount); |
| 5292 | |
| 5293 | else if (substring->length == 0) { |
| 5294 | Py_DECREF(list); |
| 5295 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 5296 | return NULL; |
| 5297 | } |
| 5298 | else |
| 5299 | return split_substring(self,list,substring,maxcount); |
| 5300 | } |
| 5301 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5302 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5303 | PyObject *rsplit(PyUnicodeObject *self, |
| 5304 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5305 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5306 | { |
| 5307 | PyObject *list; |
| 5308 | |
| 5309 | if (maxcount < 0) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5310 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5311 | |
| 5312 | list = PyList_New(0); |
| 5313 | if (!list) |
| 5314 | return NULL; |
| 5315 | |
| 5316 | if (substring == NULL) |
| 5317 | return rsplit_whitespace(self,list,maxcount); |
| 5318 | |
| 5319 | else if (substring->length == 1) |
| 5320 | return rsplit_char(self,list,substring->str[0],maxcount); |
| 5321 | |
| 5322 | else if (substring->length == 0) { |
| 5323 | Py_DECREF(list); |
| 5324 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 5325 | return NULL; |
| 5326 | } |
| 5327 | else |
| 5328 | return rsplit_substring(self,list,substring,maxcount); |
| 5329 | } |
| 5330 | |
| 5331 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5332 | PyObject *replace(PyUnicodeObject *self, |
| 5333 | PyUnicodeObject *str1, |
| 5334 | PyUnicodeObject *str2, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5335 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5336 | { |
| 5337 | PyUnicodeObject *u; |
| 5338 | |
| 5339 | if (maxcount < 0) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5340 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5341 | |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5342 | if (str1->length == str2->length) { |
| 5343 | /* same length */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5344 | Py_ssize_t i; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5345 | if (str1->length == 1) { |
| 5346 | /* replace characters */ |
| 5347 | Py_UNICODE u1, u2; |
| 5348 | if (!findchar(self->str, self->length, str1->str[0])) |
| 5349 | goto nothing; |
| 5350 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 5351 | if (!u) |
| 5352 | return NULL; |
| 5353 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5354 | u1 = str1->str[0]; |
| 5355 | u2 = str2->str[0]; |
| 5356 | for (i = 0; i < u->length; i++) |
| 5357 | if (u->str[i] == u1) { |
| 5358 | if (--maxcount < 0) |
| 5359 | break; |
| 5360 | u->str[i] = u2; |
| 5361 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5362 | } else { |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5363 | i = fastsearch( |
| 5364 | self->str, self->length, str1->str, str1->length, FAST_SEARCH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5365 | ); |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5366 | if (i < 0) |
| 5367 | goto nothing; |
| 5368 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 5369 | if (!u) |
| 5370 | return NULL; |
| 5371 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5372 | while (i <= self->length - str1->length) |
| 5373 | if (Py_UNICODE_MATCH(self, i, str1)) { |
| 5374 | if (--maxcount < 0) |
| 5375 | break; |
| 5376 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 5377 | i += str1->length; |
| 5378 | } else |
| 5379 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5380 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5381 | } else { |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5382 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 5383 | Py_ssize_t n, i, j, e; |
Fredrik Lundh | 0c71f88 | 2006-05-25 16:46:54 +0000 | [diff] [blame] | 5384 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5385 | Py_UNICODE *p; |
| 5386 | |
| 5387 | /* replace strings */ |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5388 | n = stringlib_count(self->str, self->length, str1->str, str1->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5389 | if (n > maxcount) |
| 5390 | n = maxcount; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5391 | if (n == 0) |
| 5392 | goto nothing; |
Fredrik Lundh | 0c71f88 | 2006-05-25 16:46:54 +0000 | [diff] [blame] | 5393 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 5394 | delta = (str2->length - str1->length); |
| 5395 | if (delta == 0) { |
| 5396 | new_size = self->length; |
| 5397 | } else { |
| 5398 | product = n * (str2->length - str1->length); |
| 5399 | if ((product / (str2->length - str1->length)) != n) { |
| 5400 | PyErr_SetString(PyExc_OverflowError, |
| 5401 | "replace string is too long"); |
| 5402 | return NULL; |
| 5403 | } |
| 5404 | new_size = self->length + product; |
| 5405 | if (new_size < 0) { |
| 5406 | PyErr_SetString(PyExc_OverflowError, |
| 5407 | "replace string is too long"); |
| 5408 | return NULL; |
| 5409 | } |
| 5410 | } |
| 5411 | u = _PyUnicode_New(new_size); |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5412 | if (!u) |
| 5413 | return NULL; |
| 5414 | i = 0; |
| 5415 | p = u->str; |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 5416 | e = self->length - str1->length; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5417 | if (str1->length > 0) { |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 5418 | while (n-- > 0) { |
| 5419 | /* look for next match */ |
| 5420 | j = i; |
| 5421 | while (j <= e) { |
| 5422 | if (Py_UNICODE_MATCH(self, j, str1)) |
| 5423 | break; |
| 5424 | j++; |
| 5425 | } |
| 5426 | if (j > i) { |
| 5427 | if (j > e) |
| 5428 | break; |
| 5429 | /* copy unchanged part [i:j] */ |
| 5430 | Py_UNICODE_COPY(p, self->str+i, j-i); |
| 5431 | p += j - i; |
| 5432 | } |
| 5433 | /* copy substitution string */ |
| 5434 | if (str2->length > 0) { |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5435 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 5436 | p += str2->length; |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 5437 | } |
| 5438 | i = j + str1->length; |
| 5439 | } |
| 5440 | if (i < self->length) |
| 5441 | /* copy tail [i:] */ |
| 5442 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5443 | } else { |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 5444 | /* interleave */ |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5445 | while (n > 0) { |
| 5446 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 5447 | p += str2->length; |
| 5448 | if (--n <= 0) |
| 5449 | break; |
| 5450 | *p++ = self->str[i++]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5451 | } |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5452 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5453 | } |
| 5454 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5455 | return (PyObject *) u; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5456 | |
| 5457 | nothing: |
| 5458 | /* nothing to replace; return original string (when possible) */ |
| 5459 | if (PyUnicode_CheckExact(self)) { |
| 5460 | Py_INCREF(self); |
| 5461 | return (PyObject *) self; |
| 5462 | } |
| 5463 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5464 | } |
| 5465 | |
| 5466 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 5467 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5468 | PyDoc_STRVAR(title__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5469 | "S.title() -> unicode\n\ |
| 5470 | \n\ |
| 5471 | 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] | 5472 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5473 | |
| 5474 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5475 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5476 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5477 | return fixup(self, fixtitle); |
| 5478 | } |
| 5479 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5480 | PyDoc_STRVAR(capitalize__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5481 | "S.capitalize() -> unicode\n\ |
| 5482 | \n\ |
| 5483 | 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] | 5484 | have upper case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5485 | |
| 5486 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5487 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5488 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5489 | return fixup(self, fixcapitalize); |
| 5490 | } |
| 5491 | |
| 5492 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5493 | PyDoc_STRVAR(capwords__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5494 | "S.capwords() -> unicode\n\ |
| 5495 | \n\ |
| 5496 | 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] | 5497 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5498 | |
| 5499 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5500 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5501 | { |
| 5502 | PyObject *list; |
| 5503 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5504 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5505 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5506 | /* Split into words */ |
| 5507 | list = split(self, NULL, -1); |
| 5508 | if (!list) |
| 5509 | return NULL; |
| 5510 | |
| 5511 | /* Capitalize each word */ |
| 5512 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 5513 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
| 5514 | fixcapitalize); |
| 5515 | if (item == NULL) |
| 5516 | goto onError; |
| 5517 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 5518 | PyList_SET_ITEM(list, i, item); |
| 5519 | } |
| 5520 | |
| 5521 | /* Join the words to form a new string */ |
| 5522 | item = PyUnicode_Join(NULL, list); |
| 5523 | |
| 5524 | onError: |
| 5525 | Py_DECREF(list); |
| 5526 | return (PyObject *)item; |
| 5527 | } |
| 5528 | #endif |
| 5529 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5530 | /* Argument converter. Coerces to a single unicode character */ |
| 5531 | |
| 5532 | static int |
| 5533 | convert_uc(PyObject *obj, void *addr) |
| 5534 | { |
| 5535 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 5536 | PyObject *uniobj; |
| 5537 | Py_UNICODE *unistr; |
| 5538 | |
| 5539 | uniobj = PyUnicode_FromObject(obj); |
| 5540 | if (uniobj == NULL) { |
| 5541 | PyErr_SetString(PyExc_TypeError, |
| 5542 | "The fill character cannot be converted to Unicode"); |
| 5543 | return 0; |
| 5544 | } |
| 5545 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 5546 | PyErr_SetString(PyExc_TypeError, |
| 5547 | "The fill character must be exactly one character long"); |
| 5548 | Py_DECREF(uniobj); |
| 5549 | return 0; |
| 5550 | } |
| 5551 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 5552 | *fillcharloc = unistr[0]; |
| 5553 | Py_DECREF(uniobj); |
| 5554 | return 1; |
| 5555 | } |
| 5556 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5557 | PyDoc_STRVAR(center__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5558 | "S.center(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5559 | \n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5560 | Return S centered in a Unicode string of length width. Padding is\n\ |
| 5561 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5562 | |
| 5563 | static PyObject * |
| 5564 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 5565 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5566 | Py_ssize_t marg, left; |
| 5567 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5568 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5569 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 5570 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5571 | return NULL; |
| 5572 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5573 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5574 | Py_INCREF(self); |
| 5575 | return (PyObject*) self; |
| 5576 | } |
| 5577 | |
| 5578 | marg = width - self->length; |
| 5579 | left = marg / 2 + (marg & width & 1); |
| 5580 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5581 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5582 | } |
| 5583 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5584 | #if 0 |
| 5585 | |
| 5586 | /* This code should go into some future Unicode collation support |
| 5587 | module. The basic comparison should compare ordinals on a naive |
Trent Mick | 20abf57 | 2000-08-12 22:14:34 +0000 | [diff] [blame] | 5588 | basis (this is what Java does and thus JPython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5589 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5590 | /* speedy UTF-16 code point order comparison */ |
| 5591 | /* gleaned from: */ |
| 5592 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 5593 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 5594 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5595 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5596 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5597 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 5598 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 5599 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5600 | }; |
| 5601 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5602 | static int |
| 5603 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 5604 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5605 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5606 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5607 | Py_UNICODE *s1 = str1->str; |
| 5608 | Py_UNICODE *s2 = str2->str; |
| 5609 | |
| 5610 | len1 = str1->length; |
| 5611 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5612 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5613 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5614 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5615 | |
| 5616 | c1 = *s1++; |
| 5617 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 5618 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5619 | if (c1 > (1<<11) * 26) |
| 5620 | c1 += utf16Fixup[c1>>11]; |
| 5621 | if (c2 > (1<<11) * 26) |
| 5622 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5623 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 5624 | |
| 5625 | if (c1 != c2) |
| 5626 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5627 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5628 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5629 | } |
| 5630 | |
| 5631 | return (len1 < len2) ? -1 : (len1 != len2); |
| 5632 | } |
| 5633 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5634 | #else |
| 5635 | |
| 5636 | static int |
| 5637 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 5638 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5639 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5640 | |
| 5641 | Py_UNICODE *s1 = str1->str; |
| 5642 | Py_UNICODE *s2 = str2->str; |
| 5643 | |
| 5644 | len1 = str1->length; |
| 5645 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5646 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5647 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5648 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5649 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 5650 | c1 = *s1++; |
| 5651 | c2 = *s2++; |
| 5652 | |
| 5653 | if (c1 != c2) |
| 5654 | return (c1 < c2) ? -1 : 1; |
| 5655 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5656 | len1--; len2--; |
| 5657 | } |
| 5658 | |
| 5659 | return (len1 < len2) ? -1 : (len1 != len2); |
| 5660 | } |
| 5661 | |
| 5662 | #endif |
| 5663 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5664 | int PyUnicode_Compare(PyObject *left, |
| 5665 | PyObject *right) |
| 5666 | { |
| 5667 | PyUnicodeObject *u = NULL, *v = NULL; |
| 5668 | int result; |
| 5669 | |
| 5670 | /* Coerce the two arguments */ |
| 5671 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 5672 | if (u == NULL) |
| 5673 | goto onError; |
| 5674 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 5675 | if (v == NULL) |
| 5676 | goto onError; |
| 5677 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5678 | /* Shortcut for empty or interned objects */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5679 | if (v == u) { |
| 5680 | Py_DECREF(u); |
| 5681 | Py_DECREF(v); |
| 5682 | return 0; |
| 5683 | } |
| 5684 | |
| 5685 | result = unicode_compare(u, v); |
| 5686 | |
| 5687 | Py_DECREF(u); |
| 5688 | Py_DECREF(v); |
| 5689 | return result; |
| 5690 | |
| 5691 | onError: |
| 5692 | Py_XDECREF(u); |
| 5693 | Py_XDECREF(v); |
| 5694 | return -1; |
| 5695 | } |
| 5696 | |
Marc-André Lemburg | 040f76b | 2006-08-14 10:55:19 +0000 | [diff] [blame] | 5697 | PyObject *PyUnicode_RichCompare(PyObject *left, |
| 5698 | PyObject *right, |
| 5699 | int op) |
| 5700 | { |
| 5701 | int result; |
| 5702 | |
| 5703 | result = PyUnicode_Compare(left, right); |
| 5704 | if (result == -1 && PyErr_Occurred()) |
| 5705 | goto onError; |
| 5706 | |
| 5707 | /* Convert the return value to a Boolean */ |
| 5708 | switch (op) { |
| 5709 | case Py_EQ: |
| 5710 | result = (result == 0); |
| 5711 | break; |
| 5712 | case Py_NE: |
| 5713 | result = (result != 0); |
| 5714 | break; |
| 5715 | case Py_LE: |
| 5716 | result = (result <= 0); |
| 5717 | break; |
| 5718 | case Py_GE: |
| 5719 | result = (result >= 0); |
| 5720 | break; |
| 5721 | case Py_LT: |
| 5722 | result = (result == -1); |
| 5723 | break; |
| 5724 | case Py_GT: |
| 5725 | result = (result == 1); |
| 5726 | break; |
| 5727 | } |
| 5728 | return PyBool_FromLong(result); |
| 5729 | |
| 5730 | onError: |
| 5731 | |
| 5732 | /* Standard case |
| 5733 | |
| 5734 | Type errors mean that PyUnicode_FromObject() could not convert |
| 5735 | one of the arguments (usually the right hand side) to Unicode, |
| 5736 | ie. we can't handle the comparison request. However, it is |
| 5737 | possible that the other object knows a comparison method, which |
| 5738 | is why we return Py_NotImplemented to give the other object a |
| 5739 | chance. |
| 5740 | |
| 5741 | */ |
| 5742 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 5743 | PyErr_Clear(); |
| 5744 | Py_INCREF(Py_NotImplemented); |
| 5745 | return Py_NotImplemented; |
| 5746 | } |
| 5747 | if (op != Py_EQ && op != Py_NE) |
| 5748 | return NULL; |
| 5749 | |
| 5750 | /* Equality comparison. |
| 5751 | |
| 5752 | This is a special case: we silence any PyExc_UnicodeDecodeError |
| 5753 | and instead turn it into a PyErr_UnicodeWarning. |
| 5754 | |
| 5755 | */ |
| 5756 | if (!PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) |
| 5757 | return NULL; |
| 5758 | PyErr_Clear(); |
| 5759 | if (PyErr_Warn(PyExc_UnicodeWarning, |
| 5760 | (op == Py_EQ) ? |
| 5761 | "Unicode equal comparison " |
| 5762 | "failed to convert both arguments to Unicode - " |
| 5763 | "interpreting them as being unequal" : |
| 5764 | "Unicode unequal comparison " |
| 5765 | "failed to convert both arguments to Unicode - " |
| 5766 | "interpreting them as being unequal" |
| 5767 | ) < 0) |
| 5768 | return NULL; |
| 5769 | result = (op == Py_NE); |
| 5770 | return PyBool_FromLong(result); |
| 5771 | } |
| 5772 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5773 | int PyUnicode_Contains(PyObject *container, |
| 5774 | PyObject *element) |
| 5775 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5776 | PyObject *str, *sub; |
| 5777 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5778 | |
| 5779 | /* Coerce the two arguments */ |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5780 | sub = PyUnicode_FromObject(element); |
| 5781 | if (!sub) { |
Marc-André Lemburg | 7c01468 | 2000-06-28 08:11:47 +0000 | [diff] [blame] | 5782 | PyErr_SetString(PyExc_TypeError, |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 5783 | "'in <string>' requires string as left operand"); |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 5784 | return -1; |
Marc-André Lemburg | 7c01468 | 2000-06-28 08:11:47 +0000 | [diff] [blame] | 5785 | } |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 5786 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5787 | str = PyUnicode_FromObject(container); |
| 5788 | if (!str) { |
| 5789 | Py_DECREF(sub); |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 5790 | return -1; |
| 5791 | } |
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 | result = stringlib_contains_obj(str, sub); |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 5794 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5795 | Py_DECREF(str); |
| 5796 | Py_DECREF(sub); |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5797 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5798 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5799 | } |
| 5800 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5801 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 5802 | |
| 5803 | PyObject *PyUnicode_Concat(PyObject *left, |
| 5804 | PyObject *right) |
| 5805 | { |
| 5806 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 5807 | |
| 5808 | /* Coerce the two arguments */ |
| 5809 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 5810 | if (u == NULL) |
| 5811 | goto onError; |
| 5812 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 5813 | if (v == NULL) |
| 5814 | goto onError; |
| 5815 | |
| 5816 | /* Shortcuts */ |
| 5817 | if (v == unicode_empty) { |
| 5818 | Py_DECREF(v); |
| 5819 | return (PyObject *)u; |
| 5820 | } |
| 5821 | if (u == unicode_empty) { |
| 5822 | Py_DECREF(u); |
| 5823 | return (PyObject *)v; |
| 5824 | } |
| 5825 | |
| 5826 | /* Concat the two Unicode strings */ |
| 5827 | w = _PyUnicode_New(u->length + v->length); |
| 5828 | if (w == NULL) |
| 5829 | goto onError; |
| 5830 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 5831 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 5832 | |
| 5833 | Py_DECREF(u); |
| 5834 | Py_DECREF(v); |
| 5835 | return (PyObject *)w; |
| 5836 | |
| 5837 | onError: |
| 5838 | Py_XDECREF(u); |
| 5839 | Py_XDECREF(v); |
| 5840 | return NULL; |
| 5841 | } |
| 5842 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5843 | PyDoc_STRVAR(count__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5844 | "S.count(sub[, start[, end]]) -> int\n\ |
| 5845 | \n\ |
Fredrik Lundh | 763b50f | 2006-05-22 15:35:12 +0000 | [diff] [blame] | 5846 | Return the number of non-overlapping occurrences of substring sub in\n\ |
| 5847 | 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] | 5848 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5849 | |
| 5850 | static PyObject * |
| 5851 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 5852 | { |
| 5853 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5854 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5855 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5856 | PyObject *result; |
| 5857 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 5858 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
| 5859 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5860 | return NULL; |
| 5861 | |
| 5862 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5863 | (PyObject *)substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5864 | if (substring == NULL) |
| 5865 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5866 | |
Fredrik Lundh | c816281 | 2006-05-26 19:33:03 +0000 | [diff] [blame] | 5867 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5868 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5869 | result = PyInt_FromSsize_t( |
| 5870 | stringlib_count(self->str + start, end - start, |
| 5871 | substring->str, substring->length) |
| 5872 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5873 | |
| 5874 | Py_DECREF(substring); |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5875 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5876 | return result; |
| 5877 | } |
| 5878 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5879 | PyDoc_STRVAR(encode__doc__, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5880 | "S.encode([encoding[,errors]]) -> string or unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5881 | \n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5882 | Encodes S using the codec registered for encoding. encoding defaults\n\ |
| 5883 | 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] | 5884 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5885 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 5886 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 5887 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5888 | |
| 5889 | static PyObject * |
| 5890 | unicode_encode(PyUnicodeObject *self, PyObject *args) |
| 5891 | { |
| 5892 | char *encoding = NULL; |
| 5893 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5894 | PyObject *v; |
| 5895 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5896 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 5897 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5898 | v = PyUnicode_AsEncodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5899 | if (v == NULL) |
| 5900 | goto onError; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5901 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
| 5902 | PyErr_Format(PyExc_TypeError, |
| 5903 | "encoder did not return a string/unicode object " |
| 5904 | "(type=%.400s)", |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 5905 | Py_Type(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5906 | Py_DECREF(v); |
| 5907 | return NULL; |
| 5908 | } |
| 5909 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5910 | |
| 5911 | onError: |
| 5912 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5913 | } |
| 5914 | |
| 5915 | PyDoc_STRVAR(decode__doc__, |
| 5916 | "S.decode([encoding[,errors]]) -> string or unicode\n\ |
| 5917 | \n\ |
| 5918 | Decodes S using the codec registered for encoding. encoding defaults\n\ |
| 5919 | to the default encoding. errors may be given to set a different error\n\ |
| 5920 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
| 5921 | a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ |
| 5922 | as well as any other name registerd with codecs.register_error that is\n\ |
| 5923 | able to handle UnicodeDecodeErrors."); |
| 5924 | |
| 5925 | static PyObject * |
Marc-André Lemburg | 126b44c | 2004-07-10 12:04:20 +0000 | [diff] [blame] | 5926 | unicode_decode(PyUnicodeObject *self, PyObject *args) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5927 | { |
| 5928 | char *encoding = NULL; |
| 5929 | char *errors = NULL; |
| 5930 | PyObject *v; |
| 5931 | |
| 5932 | if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors)) |
| 5933 | return NULL; |
| 5934 | v = PyUnicode_AsDecodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5935 | if (v == NULL) |
| 5936 | goto onError; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5937 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
| 5938 | PyErr_Format(PyExc_TypeError, |
| 5939 | "decoder did not return a string/unicode object " |
| 5940 | "(type=%.400s)", |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 5941 | Py_Type(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5942 | Py_DECREF(v); |
| 5943 | return NULL; |
| 5944 | } |
| 5945 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5946 | |
| 5947 | onError: |
| 5948 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5949 | } |
| 5950 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5951 | PyDoc_STRVAR(expandtabs__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5952 | "S.expandtabs([tabsize]) -> unicode\n\ |
| 5953 | \n\ |
| 5954 | 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] | 5955 | 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] | 5956 | |
| 5957 | static PyObject* |
| 5958 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 5959 | { |
| 5960 | Py_UNICODE *e; |
| 5961 | Py_UNICODE *p; |
| 5962 | Py_UNICODE *q; |
Neal Norwitz | 7dbd2a3 | 2007-06-09 03:36:34 +0000 | [diff] [blame] | 5963 | Py_ssize_t i, j, old_j; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5964 | PyUnicodeObject *u; |
| 5965 | int tabsize = 8; |
| 5966 | |
| 5967 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
| 5968 | return NULL; |
| 5969 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5970 | /* First pass: determine size of output string */ |
Neal Norwitz | 7dbd2a3 | 2007-06-09 03:36:34 +0000 | [diff] [blame] | 5971 | i = j = old_j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5972 | e = self->str + self->length; |
| 5973 | for (p = self->str; p < e; p++) |
| 5974 | if (*p == '\t') { |
Neal Norwitz | 7dbd2a3 | 2007-06-09 03:36:34 +0000 | [diff] [blame] | 5975 | if (tabsize > 0) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5976 | j += tabsize - (j % tabsize); |
Neal Norwitz | 7dbd2a3 | 2007-06-09 03:36:34 +0000 | [diff] [blame] | 5977 | if (old_j > j) { |
Neal Norwitz | 5c9a81a | 2007-06-11 02:16:10 +0000 | [diff] [blame] | 5978 | PyErr_SetString(PyExc_OverflowError, |
| 5979 | "new string is too long"); |
Neal Norwitz | 7dbd2a3 | 2007-06-09 03:36:34 +0000 | [diff] [blame] | 5980 | return NULL; |
| 5981 | } |
| 5982 | old_j = j; |
| 5983 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5984 | } |
| 5985 | else { |
| 5986 | j++; |
| 5987 | if (*p == '\n' || *p == '\r') { |
| 5988 | i += j; |
Neal Norwitz | 5c9a81a | 2007-06-11 02:16:10 +0000 | [diff] [blame] | 5989 | old_j = j = 0; |
| 5990 | if (i < 0) { |
| 5991 | PyErr_SetString(PyExc_OverflowError, |
| 5992 | "new string is too long"); |
| 5993 | return NULL; |
| 5994 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5995 | } |
| 5996 | } |
| 5997 | |
Neal Norwitz | 7dbd2a3 | 2007-06-09 03:36:34 +0000 | [diff] [blame] | 5998 | if ((i + j) < 0) { |
| 5999 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 6000 | return NULL; |
| 6001 | } |
| 6002 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6003 | /* Second pass: create output string and fill it */ |
| 6004 | u = _PyUnicode_New(i + j); |
| 6005 | if (!u) |
| 6006 | return NULL; |
| 6007 | |
| 6008 | j = 0; |
| 6009 | q = u->str; |
| 6010 | |
| 6011 | for (p = self->str; p < e; p++) |
| 6012 | if (*p == '\t') { |
| 6013 | if (tabsize > 0) { |
| 6014 | i = tabsize - (j % tabsize); |
| 6015 | j += i; |
| 6016 | while (i--) |
| 6017 | *q++ = ' '; |
| 6018 | } |
| 6019 | } |
| 6020 | else { |
| 6021 | j++; |
| 6022 | *q++ = *p; |
| 6023 | if (*p == '\n' || *p == '\r') |
| 6024 | j = 0; |
| 6025 | } |
| 6026 | |
| 6027 | return (PyObject*) u; |
| 6028 | } |
| 6029 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6030 | PyDoc_STRVAR(find__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6031 | "S.find(sub [,start [,end]]) -> int\n\ |
| 6032 | \n\ |
| 6033 | Return the lowest index in S where substring sub is found,\n\ |
Georg Brandl | 9efd9b6 | 2007-07-29 17:38:35 +0000 | [diff] [blame] | 6034 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6035 | arguments start and end are interpreted as in slice notation.\n\ |
| 6036 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6037 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6038 | |
| 6039 | static PyObject * |
| 6040 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 6041 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6042 | PyObject *substring; |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame^] | 6043 | Py_ssize_t start; |
| 6044 | Py_ssize_t end; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6045 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6046 | |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame^] | 6047 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6048 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6049 | |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 6050 | result = stringlib_find_slice( |
| 6051 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6052 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6053 | start, end |
| 6054 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6055 | |
| 6056 | Py_DECREF(substring); |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6057 | |
| 6058 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6059 | } |
| 6060 | |
| 6061 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6062 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6063 | { |
| 6064 | if (index < 0 || index >= self->length) { |
| 6065 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 6066 | return NULL; |
| 6067 | } |
| 6068 | |
| 6069 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 6070 | } |
| 6071 | |
| 6072 | static long |
| 6073 | unicode_hash(PyUnicodeObject *self) |
| 6074 | { |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 6075 | /* Since Unicode objects compare equal to their ASCII string |
| 6076 | counterparts, they should use the individual character values |
| 6077 | as basis for their hash value. This is needed to assure that |
| 6078 | strings and Unicode objects behave in the same way as |
| 6079 | dictionary keys. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6080 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6081 | register Py_ssize_t len; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 6082 | register Py_UNICODE *p; |
| 6083 | register long x; |
| 6084 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6085 | if (self->hash != -1) |
| 6086 | return self->hash; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 6087 | len = PyUnicode_GET_SIZE(self); |
| 6088 | p = PyUnicode_AS_UNICODE(self); |
| 6089 | x = *p << 7; |
| 6090 | while (--len >= 0) |
| 6091 | x = (1000003*x) ^ *p++; |
| 6092 | x ^= PyUnicode_GET_SIZE(self); |
| 6093 | if (x == -1) |
| 6094 | x = -2; |
| 6095 | self->hash = x; |
| 6096 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6097 | } |
| 6098 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6099 | PyDoc_STRVAR(index__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6100 | "S.index(sub [,start [,end]]) -> int\n\ |
| 6101 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6102 | 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] | 6103 | |
| 6104 | static PyObject * |
| 6105 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 6106 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6107 | Py_ssize_t result; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6108 | PyObject *substring; |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame^] | 6109 | Py_ssize_t start; |
| 6110 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6111 | |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame^] | 6112 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6113 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6114 | |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 6115 | result = stringlib_find_slice( |
| 6116 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6117 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6118 | start, end |
| 6119 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6120 | |
| 6121 | Py_DECREF(substring); |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6122 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6123 | if (result < 0) { |
| 6124 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 6125 | return NULL; |
| 6126 | } |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6127 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6128 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6129 | } |
| 6130 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6131 | PyDoc_STRVAR(islower__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6132 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6133 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6134 | 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] | 6135 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6136 | |
| 6137 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6138 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6139 | { |
| 6140 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6141 | register const Py_UNICODE *e; |
| 6142 | int cased; |
| 6143 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6144 | /* Shortcut for single character strings */ |
| 6145 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6146 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6147 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6148 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6149 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6150 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6151 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6152 | e = p + PyUnicode_GET_SIZE(self); |
| 6153 | cased = 0; |
| 6154 | for (; p < e; p++) { |
| 6155 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6156 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6157 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6158 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6159 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 6160 | cased = 1; |
| 6161 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6162 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6163 | } |
| 6164 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6165 | PyDoc_STRVAR(isupper__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6166 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6167 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6168 | 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] | 6169 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6170 | |
| 6171 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6172 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6173 | { |
| 6174 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6175 | register const Py_UNICODE *e; |
| 6176 | int cased; |
| 6177 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6178 | /* Shortcut for single character strings */ |
| 6179 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6180 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6181 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6182 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6183 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6184 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6185 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6186 | e = p + PyUnicode_GET_SIZE(self); |
| 6187 | cased = 0; |
| 6188 | for (; p < e; p++) { |
| 6189 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6190 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6191 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6192 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6193 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 6194 | cased = 1; |
| 6195 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6196 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6197 | } |
| 6198 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6199 | PyDoc_STRVAR(istitle__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6200 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6201 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6202 | Return True if S is a titlecased string and there is at least one\n\ |
| 6203 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 6204 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 6205 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6206 | |
| 6207 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6208 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6209 | { |
| 6210 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6211 | register const Py_UNICODE *e; |
| 6212 | int cased, previous_is_cased; |
| 6213 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6214 | /* Shortcut for single character strings */ |
| 6215 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6216 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 6217 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6218 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6219 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6220 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6221 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6222 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6223 | e = p + PyUnicode_GET_SIZE(self); |
| 6224 | cased = 0; |
| 6225 | previous_is_cased = 0; |
| 6226 | for (; p < e; p++) { |
| 6227 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6228 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6229 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 6230 | if (previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6231 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6232 | previous_is_cased = 1; |
| 6233 | cased = 1; |
| 6234 | } |
| 6235 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 6236 | if (!previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6237 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6238 | previous_is_cased = 1; |
| 6239 | cased = 1; |
| 6240 | } |
| 6241 | else |
| 6242 | previous_is_cased = 0; |
| 6243 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6244 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6245 | } |
| 6246 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6247 | PyDoc_STRVAR(isspace__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6248 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6249 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6250 | Return True if all characters in S are whitespace\n\ |
| 6251 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6252 | |
| 6253 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6254 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6255 | { |
| 6256 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6257 | register const Py_UNICODE *e; |
| 6258 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6259 | /* Shortcut for single character strings */ |
| 6260 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6261 | Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6262 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6263 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6264 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6265 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6266 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6267 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6268 | e = p + PyUnicode_GET_SIZE(self); |
| 6269 | for (; p < e; p++) { |
| 6270 | if (!Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6271 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6272 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6273 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6274 | } |
| 6275 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6276 | PyDoc_STRVAR(isalpha__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6277 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6278 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6279 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6280 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6281 | |
| 6282 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6283 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6284 | { |
| 6285 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6286 | register const Py_UNICODE *e; |
| 6287 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6288 | /* Shortcut for single character strings */ |
| 6289 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6290 | Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6291 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6292 | |
| 6293 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6294 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6295 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6296 | |
| 6297 | e = p + PyUnicode_GET_SIZE(self); |
| 6298 | for (; p < e; p++) { |
| 6299 | if (!Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6300 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6301 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6302 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6303 | } |
| 6304 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6305 | PyDoc_STRVAR(isalnum__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6306 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6307 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6308 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6309 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6310 | |
| 6311 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6312 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6313 | { |
| 6314 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6315 | register const Py_UNICODE *e; |
| 6316 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6317 | /* Shortcut for single character strings */ |
| 6318 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6319 | Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6320 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6321 | |
| 6322 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6323 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6324 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6325 | |
| 6326 | e = p + PyUnicode_GET_SIZE(self); |
| 6327 | for (; p < e; p++) { |
| 6328 | if (!Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6329 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6330 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6331 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6332 | } |
| 6333 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6334 | PyDoc_STRVAR(isdecimal__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6335 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6336 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6337 | 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] | 6338 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6339 | |
| 6340 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6341 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6342 | { |
| 6343 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6344 | register const Py_UNICODE *e; |
| 6345 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6346 | /* Shortcut for single character strings */ |
| 6347 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6348 | Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6349 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6350 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6351 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6352 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6353 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6354 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6355 | e = p + PyUnicode_GET_SIZE(self); |
| 6356 | for (; p < e; p++) { |
| 6357 | if (!Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6358 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6359 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6360 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6361 | } |
| 6362 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6363 | PyDoc_STRVAR(isdigit__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6364 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6365 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6366 | Return True if all characters in S are digits\n\ |
| 6367 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6368 | |
| 6369 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6370 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6371 | { |
| 6372 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6373 | register const Py_UNICODE *e; |
| 6374 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6375 | /* Shortcut for single character strings */ |
| 6376 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6377 | Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6378 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6379 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6380 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6381 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6382 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6383 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6384 | e = p + PyUnicode_GET_SIZE(self); |
| 6385 | for (; p < e; p++) { |
| 6386 | if (!Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6387 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6388 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6389 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6390 | } |
| 6391 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6392 | PyDoc_STRVAR(isnumeric__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6393 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6394 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6395 | 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] | 6396 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6397 | |
| 6398 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6399 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6400 | { |
| 6401 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6402 | register const Py_UNICODE *e; |
| 6403 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6404 | /* Shortcut for single character strings */ |
| 6405 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6406 | Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6407 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6408 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6409 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6410 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6411 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6412 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6413 | e = p + PyUnicode_GET_SIZE(self); |
| 6414 | for (; p < e; p++) { |
| 6415 | if (!Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6416 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6417 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6418 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6419 | } |
| 6420 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6421 | PyDoc_STRVAR(join__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6422 | "S.join(sequence) -> unicode\n\ |
| 6423 | \n\ |
| 6424 | 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] | 6425 | sequence. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6426 | |
| 6427 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6428 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6429 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6430 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6431 | } |
| 6432 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6433 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6434 | unicode_length(PyUnicodeObject *self) |
| 6435 | { |
| 6436 | return self->length; |
| 6437 | } |
| 6438 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6439 | PyDoc_STRVAR(ljust__doc__, |
Hye-Shik Chang | 974ed7c | 2004-06-02 16:49:17 +0000 | [diff] [blame] | 6440 | "S.ljust(width[, fillchar]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6441 | \n\ |
| 6442 | 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] | 6443 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6444 | |
| 6445 | static PyObject * |
| 6446 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 6447 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6448 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6449 | Py_UNICODE fillchar = ' '; |
| 6450 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6451 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6452 | return NULL; |
| 6453 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6454 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6455 | Py_INCREF(self); |
| 6456 | return (PyObject*) self; |
| 6457 | } |
| 6458 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6459 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6460 | } |
| 6461 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6462 | PyDoc_STRVAR(lower__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6463 | "S.lower() -> unicode\n\ |
| 6464 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6465 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6466 | |
| 6467 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6468 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6469 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6470 | return fixup(self, fixlower); |
| 6471 | } |
| 6472 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6473 | #define LEFTSTRIP 0 |
| 6474 | #define RIGHTSTRIP 1 |
| 6475 | #define BOTHSTRIP 2 |
| 6476 | |
| 6477 | /* Arrays indexed by above */ |
| 6478 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 6479 | |
| 6480 | #define STRIPNAME(i) (stripformat[i]+3) |
| 6481 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6482 | /* externally visible for str.strip(unicode) */ |
| 6483 | PyObject * |
| 6484 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 6485 | { |
| 6486 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6487 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6488 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6489 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 6490 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6491 | |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 6492 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
| 6493 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6494 | i = 0; |
| 6495 | if (striptype != RIGHTSTRIP) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 6496 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 6497 | i++; |
| 6498 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6499 | } |
| 6500 | |
| 6501 | j = len; |
| 6502 | if (striptype != LEFTSTRIP) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 6503 | do { |
| 6504 | j--; |
| 6505 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 6506 | j++; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6507 | } |
| 6508 | |
| 6509 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 6510 | Py_INCREF(self); |
| 6511 | return (PyObject*)self; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6512 | } |
| 6513 | else |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 6514 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6515 | } |
| 6516 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6517 | |
| 6518 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6519 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6520 | { |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6521 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6522 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6523 | |
| 6524 | i = 0; |
| 6525 | if (striptype != RIGHTSTRIP) { |
| 6526 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 6527 | i++; |
| 6528 | } |
| 6529 | } |
| 6530 | |
| 6531 | j = len; |
| 6532 | if (striptype != LEFTSTRIP) { |
| 6533 | do { |
| 6534 | j--; |
| 6535 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 6536 | j++; |
| 6537 | } |
| 6538 | |
| 6539 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 6540 | Py_INCREF(self); |
| 6541 | return (PyObject*)self; |
| 6542 | } |
| 6543 | else |
| 6544 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6545 | } |
| 6546 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6547 | |
| 6548 | static PyObject * |
| 6549 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 6550 | { |
| 6551 | PyObject *sep = NULL; |
| 6552 | |
| 6553 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 6554 | return NULL; |
| 6555 | |
| 6556 | if (sep != NULL && sep != Py_None) { |
| 6557 | if (PyUnicode_Check(sep)) |
| 6558 | return _PyUnicode_XStrip(self, striptype, sep); |
| 6559 | else if (PyString_Check(sep)) { |
| 6560 | PyObject *res; |
| 6561 | sep = PyUnicode_FromObject(sep); |
| 6562 | if (sep==NULL) |
| 6563 | return NULL; |
| 6564 | res = _PyUnicode_XStrip(self, striptype, sep); |
| 6565 | Py_DECREF(sep); |
| 6566 | return res; |
| 6567 | } |
| 6568 | else { |
| 6569 | PyErr_Format(PyExc_TypeError, |
| 6570 | "%s arg must be None, unicode or str", |
| 6571 | STRIPNAME(striptype)); |
| 6572 | return NULL; |
| 6573 | } |
| 6574 | } |
| 6575 | |
| 6576 | return do_strip(self, striptype); |
| 6577 | } |
| 6578 | |
| 6579 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6580 | PyDoc_STRVAR(strip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6581 | "S.strip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6582 | \n\ |
| 6583 | Return a copy of the string S with leading and trailing\n\ |
| 6584 | whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6585 | If chars is given and not None, remove characters in chars instead.\n\ |
| 6586 | 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] | 6587 | |
| 6588 | static PyObject * |
| 6589 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 6590 | { |
| 6591 | if (PyTuple_GET_SIZE(args) == 0) |
| 6592 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 6593 | else |
| 6594 | return do_argstrip(self, BOTHSTRIP, args); |
| 6595 | } |
| 6596 | |
| 6597 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6598 | PyDoc_STRVAR(lstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6599 | "S.lstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6600 | \n\ |
| 6601 | Return a copy of the string S with leading whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6602 | If chars is given and not None, remove characters in chars instead.\n\ |
| 6603 | 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] | 6604 | |
| 6605 | static PyObject * |
| 6606 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 6607 | { |
| 6608 | if (PyTuple_GET_SIZE(args) == 0) |
| 6609 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 6610 | else |
| 6611 | return do_argstrip(self, LEFTSTRIP, args); |
| 6612 | } |
| 6613 | |
| 6614 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6615 | PyDoc_STRVAR(rstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6616 | "S.rstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6617 | \n\ |
| 6618 | Return a copy of the string S with trailing whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6619 | If chars is given and not None, remove characters in chars instead.\n\ |
| 6620 | 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] | 6621 | |
| 6622 | static PyObject * |
| 6623 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 6624 | { |
| 6625 | if (PyTuple_GET_SIZE(args) == 0) |
| 6626 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 6627 | else |
| 6628 | return do_argstrip(self, RIGHTSTRIP, args); |
| 6629 | } |
| 6630 | |
| 6631 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6632 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6633 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6634 | { |
| 6635 | PyUnicodeObject *u; |
| 6636 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6637 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 6638 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6639 | |
| 6640 | if (len < 0) |
| 6641 | len = 0; |
| 6642 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6643 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6644 | /* no repeat, return original string */ |
| 6645 | Py_INCREF(str); |
| 6646 | return (PyObject*) str; |
| 6647 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 6648 | |
| 6649 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 6650 | * needed doesn't overflow size_t |
| 6651 | */ |
| 6652 | nchars = len * str->length; |
| 6653 | if (len && nchars / len != str->length) { |
| 6654 | PyErr_SetString(PyExc_OverflowError, |
| 6655 | "repeated string is too long"); |
| 6656 | return NULL; |
| 6657 | } |
| 6658 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 6659 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 6660 | PyErr_SetString(PyExc_OverflowError, |
| 6661 | "repeated string is too long"); |
| 6662 | return NULL; |
| 6663 | } |
| 6664 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6665 | if (!u) |
| 6666 | return NULL; |
| 6667 | |
| 6668 | p = u->str; |
| 6669 | |
Fredrik Lundh | f1d60a5 | 2006-05-22 16:29:30 +0000 | [diff] [blame] | 6670 | if (str->length == 1 && len > 0) { |
| 6671 | Py_UNICODE_FILL(p, str->str[0], len); |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 6672 | } else { |
Tim Peters | 1bacc64 | 2006-05-23 05:47:16 +0000 | [diff] [blame] | 6673 | Py_ssize_t done = 0; /* number of characters copied this far */ |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 6674 | if (done < nchars) { |
Fredrik Lundh | f1d60a5 | 2006-05-22 16:29:30 +0000 | [diff] [blame] | 6675 | Py_UNICODE_COPY(p, str->str, str->length); |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 6676 | done = str->length; |
| 6677 | } |
| 6678 | while (done < nchars) { |
| 6679 | int n = (done <= nchars-done) ? done : nchars-done; |
| 6680 | Py_UNICODE_COPY(p+done, p, n); |
| 6681 | done += n; |
| 6682 | } |
| 6683 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6684 | |
| 6685 | return (PyObject*) u; |
| 6686 | } |
| 6687 | |
| 6688 | PyObject *PyUnicode_Replace(PyObject *obj, |
| 6689 | PyObject *subobj, |
| 6690 | PyObject *replobj, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6691 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6692 | { |
| 6693 | PyObject *self; |
| 6694 | PyObject *str1; |
| 6695 | PyObject *str2; |
| 6696 | PyObject *result; |
| 6697 | |
| 6698 | self = PyUnicode_FromObject(obj); |
| 6699 | if (self == NULL) |
| 6700 | return NULL; |
| 6701 | str1 = PyUnicode_FromObject(subobj); |
| 6702 | if (str1 == NULL) { |
| 6703 | Py_DECREF(self); |
| 6704 | return NULL; |
| 6705 | } |
| 6706 | str2 = PyUnicode_FromObject(replobj); |
| 6707 | if (str2 == NULL) { |
| 6708 | Py_DECREF(self); |
| 6709 | Py_DECREF(str1); |
| 6710 | return NULL; |
| 6711 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6712 | result = replace((PyUnicodeObject *)self, |
| 6713 | (PyUnicodeObject *)str1, |
| 6714 | (PyUnicodeObject *)str2, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6715 | maxcount); |
| 6716 | Py_DECREF(self); |
| 6717 | Py_DECREF(str1); |
| 6718 | Py_DECREF(str2); |
| 6719 | return result; |
| 6720 | } |
| 6721 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6722 | PyDoc_STRVAR(replace__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6723 | "S.replace (old, new[, maxsplit]) -> unicode\n\ |
| 6724 | \n\ |
| 6725 | Return a copy of S with all occurrences of substring\n\ |
| 6726 | 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] | 6727 | given, only the first maxsplit occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6728 | |
| 6729 | static PyObject* |
| 6730 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 6731 | { |
| 6732 | PyUnicodeObject *str1; |
| 6733 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6734 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6735 | PyObject *result; |
| 6736 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6737 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6738 | return NULL; |
| 6739 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 6740 | if (str1 == NULL) |
| 6741 | return NULL; |
| 6742 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 6743 | if (str2 == NULL) { |
| 6744 | Py_DECREF(str1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6745 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 6746 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6747 | |
| 6748 | result = replace(self, str1, str2, maxcount); |
| 6749 | |
| 6750 | Py_DECREF(str1); |
| 6751 | Py_DECREF(str2); |
| 6752 | return result; |
| 6753 | } |
| 6754 | |
| 6755 | static |
| 6756 | PyObject *unicode_repr(PyObject *unicode) |
| 6757 | { |
| 6758 | return unicodeescape_string(PyUnicode_AS_UNICODE(unicode), |
| 6759 | PyUnicode_GET_SIZE(unicode), |
| 6760 | 1); |
| 6761 | } |
| 6762 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6763 | PyDoc_STRVAR(rfind__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6764 | "S.rfind(sub [,start [,end]]) -> int\n\ |
| 6765 | \n\ |
| 6766 | Return the highest index in S where substring sub is found,\n\ |
Georg Brandl | 9efd9b6 | 2007-07-29 17:38:35 +0000 | [diff] [blame] | 6767 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6768 | arguments start and end are interpreted as in slice notation.\n\ |
| 6769 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6770 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6771 | |
| 6772 | static PyObject * |
| 6773 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 6774 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6775 | PyObject *substring; |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame^] | 6776 | Py_ssize_t start; |
| 6777 | Py_ssize_t end; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6778 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6779 | |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame^] | 6780 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
| 6781 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6782 | |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 6783 | result = stringlib_rfind_slice( |
| 6784 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6785 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6786 | start, end |
| 6787 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6788 | |
| 6789 | Py_DECREF(substring); |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6790 | |
| 6791 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6792 | } |
| 6793 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6794 | PyDoc_STRVAR(rindex__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6795 | "S.rindex(sub [,start [,end]]) -> int\n\ |
| 6796 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6797 | 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] | 6798 | |
| 6799 | static PyObject * |
| 6800 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 6801 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6802 | PyObject *substring; |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame^] | 6803 | Py_ssize_t start; |
| 6804 | Py_ssize_t end; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6805 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6806 | |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame^] | 6807 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
| 6808 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6809 | |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 6810 | result = stringlib_rfind_slice( |
| 6811 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6812 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6813 | start, end |
| 6814 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6815 | |
| 6816 | Py_DECREF(substring); |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6817 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6818 | if (result < 0) { |
| 6819 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 6820 | return NULL; |
| 6821 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6822 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6823 | } |
| 6824 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6825 | PyDoc_STRVAR(rjust__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6826 | "S.rjust(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6827 | \n\ |
| 6828 | 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] | 6829 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6830 | |
| 6831 | static PyObject * |
| 6832 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 6833 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6834 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6835 | Py_UNICODE fillchar = ' '; |
| 6836 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6837 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6838 | return NULL; |
| 6839 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6840 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6841 | Py_INCREF(self); |
| 6842 | return (PyObject*) self; |
| 6843 | } |
| 6844 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6845 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6846 | } |
| 6847 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6848 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6849 | 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] | 6850 | { |
| 6851 | /* standard clamping */ |
| 6852 | if (start < 0) |
| 6853 | start = 0; |
| 6854 | if (end < 0) |
| 6855 | end = 0; |
| 6856 | if (end > self->length) |
| 6857 | end = self->length; |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6858 | if (start == 0 && end == self->length && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6859 | /* full slice, return original string */ |
| 6860 | Py_INCREF(self); |
| 6861 | return (PyObject*) self; |
| 6862 | } |
| 6863 | if (start > end) |
| 6864 | start = end; |
| 6865 | /* copy slice */ |
| 6866 | return (PyObject*) PyUnicode_FromUnicode(self->str + start, |
| 6867 | end - start); |
| 6868 | } |
| 6869 | |
| 6870 | PyObject *PyUnicode_Split(PyObject *s, |
| 6871 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6872 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6873 | { |
| 6874 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6875 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6876 | s = PyUnicode_FromObject(s); |
| 6877 | if (s == NULL) |
| 6878 | return NULL; |
| 6879 | if (sep != NULL) { |
| 6880 | sep = PyUnicode_FromObject(sep); |
| 6881 | if (sep == NULL) { |
| 6882 | Py_DECREF(s); |
| 6883 | return NULL; |
| 6884 | } |
| 6885 | } |
| 6886 | |
| 6887 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 6888 | |
| 6889 | Py_DECREF(s); |
| 6890 | Py_XDECREF(sep); |
| 6891 | return result; |
| 6892 | } |
| 6893 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6894 | PyDoc_STRVAR(split__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6895 | "S.split([sep [,maxsplit]]) -> list of strings\n\ |
| 6896 | \n\ |
| 6897 | Return a list of the words in S, using sep as the\n\ |
| 6898 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Thomas Heller | ca0d2cb | 2004-09-15 11:41:32 +0000 | [diff] [blame] | 6899 | 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] | 6900 | any whitespace string is a separator."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6901 | |
| 6902 | static PyObject* |
| 6903 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 6904 | { |
| 6905 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6906 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6907 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6908 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6909 | return NULL; |
| 6910 | |
| 6911 | if (substring == Py_None) |
| 6912 | return split(self, NULL, maxcount); |
| 6913 | else if (PyUnicode_Check(substring)) |
| 6914 | return split(self, (PyUnicodeObject *)substring, maxcount); |
| 6915 | else |
| 6916 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
| 6917 | } |
| 6918 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6919 | PyObject * |
| 6920 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 6921 | { |
| 6922 | PyObject* str_obj; |
| 6923 | PyObject* sep_obj; |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6924 | PyObject* out; |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 6925 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6926 | str_obj = PyUnicode_FromObject(str_in); |
| 6927 | if (!str_obj) |
| 6928 | return NULL; |
| 6929 | sep_obj = PyUnicode_FromObject(sep_in); |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 6930 | if (!sep_obj) { |
| 6931 | Py_DECREF(str_obj); |
| 6932 | return NULL; |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6933 | } |
| 6934 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 6935 | out = stringlib_partition( |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 6936 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 6937 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 6938 | ); |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6939 | |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 6940 | Py_DECREF(sep_obj); |
| 6941 | Py_DECREF(str_obj); |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6942 | |
| 6943 | return out; |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6944 | } |
| 6945 | |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 6946 | |
| 6947 | PyObject * |
| 6948 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 6949 | { |
| 6950 | PyObject* str_obj; |
| 6951 | PyObject* sep_obj; |
| 6952 | PyObject* out; |
| 6953 | |
| 6954 | str_obj = PyUnicode_FromObject(str_in); |
| 6955 | if (!str_obj) |
| 6956 | return NULL; |
| 6957 | sep_obj = PyUnicode_FromObject(sep_in); |
| 6958 | if (!sep_obj) { |
| 6959 | Py_DECREF(str_obj); |
| 6960 | return NULL; |
| 6961 | } |
| 6962 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 6963 | out = stringlib_rpartition( |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 6964 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 6965 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 6966 | ); |
| 6967 | |
| 6968 | Py_DECREF(sep_obj); |
| 6969 | Py_DECREF(str_obj); |
| 6970 | |
| 6971 | return out; |
| 6972 | } |
| 6973 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6974 | PyDoc_STRVAR(partition__doc__, |
| 6975 | "S.partition(sep) -> (head, sep, tail)\n\ |
| 6976 | \n\ |
| 6977 | Searches for the separator sep in S, and returns the part before it,\n\ |
| 6978 | the separator itself, and the part after it. If the separator is not\n\ |
| 6979 | found, returns S and two empty strings."); |
| 6980 | |
| 6981 | static PyObject* |
Fredrik Lundh | 450277f | 2006-05-26 09:46:59 +0000 | [diff] [blame] | 6982 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6983 | { |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6984 | return PyUnicode_Partition((PyObject *)self, separator); |
| 6985 | } |
| 6986 | |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 6987 | PyDoc_STRVAR(rpartition__doc__, |
Raymond Hettinger | a0c95fa | 2006-09-04 15:32:48 +0000 | [diff] [blame] | 6988 | "S.rpartition(sep) -> (tail, sep, head)\n\ |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 6989 | \n\ |
| 6990 | Searches for the separator sep in S, starting at the end of S, and returns\n\ |
| 6991 | 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] | 6992 | separator is not found, returns two empty strings and S."); |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 6993 | |
| 6994 | static PyObject* |
| 6995 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 6996 | { |
| 6997 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 6998 | } |
| 6999 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7000 | PyObject *PyUnicode_RSplit(PyObject *s, |
| 7001 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7002 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7003 | { |
| 7004 | PyObject *result; |
| 7005 | |
| 7006 | s = PyUnicode_FromObject(s); |
| 7007 | if (s == NULL) |
| 7008 | return NULL; |
| 7009 | if (sep != NULL) { |
| 7010 | sep = PyUnicode_FromObject(sep); |
| 7011 | if (sep == NULL) { |
| 7012 | Py_DECREF(s); |
| 7013 | return NULL; |
| 7014 | } |
| 7015 | } |
| 7016 | |
| 7017 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 7018 | |
| 7019 | Py_DECREF(s); |
| 7020 | Py_XDECREF(sep); |
| 7021 | return result; |
| 7022 | } |
| 7023 | |
| 7024 | PyDoc_STRVAR(rsplit__doc__, |
| 7025 | "S.rsplit([sep [,maxsplit]]) -> list of strings\n\ |
| 7026 | \n\ |
| 7027 | Return a list of the words in S, using sep as the\n\ |
| 7028 | delimiter string, starting at the end of the string and\n\ |
| 7029 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 7030 | splits are done. If sep is not specified, any whitespace string\n\ |
| 7031 | is a separator."); |
| 7032 | |
| 7033 | static PyObject* |
| 7034 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 7035 | { |
| 7036 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7037 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7038 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7039 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7040 | return NULL; |
| 7041 | |
| 7042 | if (substring == Py_None) |
| 7043 | return rsplit(self, NULL, maxcount); |
| 7044 | else if (PyUnicode_Check(substring)) |
| 7045 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
| 7046 | else |
| 7047 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
| 7048 | } |
| 7049 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7050 | PyDoc_STRVAR(splitlines__doc__, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7051 | "S.splitlines([keepends]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7052 | \n\ |
| 7053 | 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] | 7054 | 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] | 7055 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7056 | |
| 7057 | static PyObject* |
| 7058 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 7059 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7060 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7061 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7062 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7063 | return NULL; |
| 7064 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7065 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7066 | } |
| 7067 | |
| 7068 | static |
| 7069 | PyObject *unicode_str(PyUnicodeObject *self) |
| 7070 | { |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7071 | return PyUnicode_AsEncodedString((PyObject *)self, NULL, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7072 | } |
| 7073 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7074 | PyDoc_STRVAR(swapcase__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7075 | "S.swapcase() -> unicode\n\ |
| 7076 | \n\ |
| 7077 | 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] | 7078 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7079 | |
| 7080 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7081 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7082 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7083 | return fixup(self, fixswapcase); |
| 7084 | } |
| 7085 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7086 | PyDoc_STRVAR(translate__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7087 | "S.translate(table) -> unicode\n\ |
| 7088 | \n\ |
| 7089 | Return a copy of the string S, where all characters have been mapped\n\ |
| 7090 | 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] | 7091 | Unicode ordinals to Unicode ordinals, Unicode strings or None.\n\ |
| 7092 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 7093 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7094 | |
| 7095 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7096 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7097 | { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7098 | return PyUnicode_TranslateCharmap(self->str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7099 | self->length, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7100 | table, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7101 | "ignore"); |
| 7102 | } |
| 7103 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7104 | PyDoc_STRVAR(upper__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7105 | "S.upper() -> unicode\n\ |
| 7106 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7107 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7108 | |
| 7109 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7110 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7111 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7112 | return fixup(self, fixupper); |
| 7113 | } |
| 7114 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7115 | PyDoc_STRVAR(zfill__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7116 | "S.zfill(width) -> unicode\n\ |
| 7117 | \n\ |
| 7118 | 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] | 7119 | of the specified width. The string x is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7120 | |
| 7121 | static PyObject * |
| 7122 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 7123 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7124 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7125 | PyUnicodeObject *u; |
| 7126 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7127 | Py_ssize_t width; |
| 7128 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7129 | return NULL; |
| 7130 | |
| 7131 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 7132 | if (PyUnicode_CheckExact(self)) { |
| 7133 | Py_INCREF(self); |
| 7134 | return (PyObject*) self; |
| 7135 | } |
| 7136 | else |
| 7137 | return PyUnicode_FromUnicode( |
| 7138 | PyUnicode_AS_UNICODE(self), |
| 7139 | PyUnicode_GET_SIZE(self) |
| 7140 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7141 | } |
| 7142 | |
| 7143 | fill = width - self->length; |
| 7144 | |
| 7145 | u = pad(self, fill, 0, '0'); |
| 7146 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 7147 | if (u == NULL) |
| 7148 | return NULL; |
| 7149 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7150 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 7151 | /* move sign to beginning of string */ |
| 7152 | u->str[0] = u->str[fill]; |
| 7153 | u->str[fill] = '0'; |
| 7154 | } |
| 7155 | |
| 7156 | return (PyObject*) u; |
| 7157 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7158 | |
| 7159 | #if 0 |
| 7160 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7161 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7162 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7163 | return PyInt_FromLong(unicode_freelist_size); |
| 7164 | } |
| 7165 | #endif |
| 7166 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7167 | PyDoc_STRVAR(startswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7168 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7169 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 7170 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 7171 | With optional start, test S beginning at that position.\n\ |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7172 | With optional end, stop comparing S at that position.\n\ |
| 7173 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7174 | |
| 7175 | static PyObject * |
| 7176 | unicode_startswith(PyUnicodeObject *self, |
| 7177 | PyObject *args) |
| 7178 | { |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7179 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7180 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7181 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7182 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7183 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7184 | |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7185 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 7186 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7187 | return NULL; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7188 | if (PyTuple_Check(subobj)) { |
| 7189 | Py_ssize_t i; |
| 7190 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 7191 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 7192 | PyTuple_GET_ITEM(subobj, i)); |
| 7193 | if (substring == NULL) |
| 7194 | return NULL; |
| 7195 | result = tailmatch(self, substring, start, end, -1); |
| 7196 | Py_DECREF(substring); |
| 7197 | if (result) { |
| 7198 | Py_RETURN_TRUE; |
| 7199 | } |
| 7200 | } |
| 7201 | /* nothing matched */ |
| 7202 | Py_RETURN_FALSE; |
| 7203 | } |
| 7204 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7205 | if (substring == NULL) |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7206 | return NULL; |
| 7207 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7208 | Py_DECREF(substring); |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7209 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7210 | } |
| 7211 | |
| 7212 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7213 | PyDoc_STRVAR(endswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7214 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7215 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 7216 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 7217 | With optional start, test S beginning at that position.\n\ |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7218 | With optional end, stop comparing S at that position.\n\ |
| 7219 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7220 | |
| 7221 | static PyObject * |
| 7222 | unicode_endswith(PyUnicodeObject *self, |
| 7223 | PyObject *args) |
| 7224 | { |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7225 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7226 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7227 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7228 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7229 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7230 | |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7231 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
| 7232 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7233 | return NULL; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7234 | if (PyTuple_Check(subobj)) { |
| 7235 | Py_ssize_t i; |
| 7236 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 7237 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 7238 | PyTuple_GET_ITEM(subobj, i)); |
| 7239 | if (substring == NULL) |
| 7240 | return NULL; |
| 7241 | result = tailmatch(self, substring, start, end, +1); |
| 7242 | Py_DECREF(substring); |
| 7243 | if (result) { |
| 7244 | Py_RETURN_TRUE; |
| 7245 | } |
| 7246 | } |
| 7247 | Py_RETURN_FALSE; |
| 7248 | } |
| 7249 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7250 | if (substring == NULL) |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7251 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7252 | |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7253 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7254 | Py_DECREF(substring); |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7255 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7256 | } |
| 7257 | |
| 7258 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 7259 | |
| 7260 | static PyObject * |
| 7261 | unicode_getnewargs(PyUnicodeObject *v) |
| 7262 | { |
| 7263 | return Py_BuildValue("(u#)", v->str, v->length); |
| 7264 | } |
| 7265 | |
| 7266 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7267 | static PyMethodDef unicode_methods[] = { |
| 7268 | |
| 7269 | /* Order is according to common usage: often used methods should |
| 7270 | appear first, since lookup is done sequentially. */ |
| 7271 | |
Georg Brandl | ecdc0a9 | 2006-03-30 12:19:07 +0000 | [diff] [blame] | 7272 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7273 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 7274 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7275 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7276 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 7277 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 7278 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 7279 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 7280 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 7281 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 7282 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Fredrik Lundh | 450277f | 2006-05-26 09:46:59 +0000 | [diff] [blame] | 7283 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7284 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 7285 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 7286 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7287 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7288 | {"decode", (PyCFunction) unicode_decode, METH_VARARGS, decode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7289 | /* {"maketrans", (PyCFunction) unicode_maketrans, METH_VARARGS, maketrans__doc__}, */ |
| 7290 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 7291 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 7292 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7293 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7294 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7295 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7296 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7297 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 7298 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 7299 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 7300 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 7301 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 7302 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 7303 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 7304 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 7305 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 7306 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 7307 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 7308 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 7309 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 7310 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7311 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 7312 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7313 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7314 | #endif |
| 7315 | |
| 7316 | #if 0 |
| 7317 | /* This one is just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7318 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7319 | #endif |
| 7320 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 7321 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7322 | {NULL, NULL} |
| 7323 | }; |
| 7324 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 7325 | static PyObject * |
| 7326 | unicode_mod(PyObject *v, PyObject *w) |
| 7327 | { |
| 7328 | if (!PyUnicode_Check(v)) { |
| 7329 | Py_INCREF(Py_NotImplemented); |
| 7330 | return Py_NotImplemented; |
| 7331 | } |
| 7332 | return PyUnicode_Format(v, w); |
| 7333 | } |
| 7334 | |
| 7335 | static PyNumberMethods unicode_as_number = { |
| 7336 | 0, /*nb_add*/ |
| 7337 | 0, /*nb_subtract*/ |
| 7338 | 0, /*nb_multiply*/ |
| 7339 | 0, /*nb_divide*/ |
| 7340 | unicode_mod, /*nb_remainder*/ |
| 7341 | }; |
| 7342 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7343 | static PySequenceMethods unicode_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7344 | (lenfunc) unicode_length, /* sq_length */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 7345 | PyUnicode_Concat, /* sq_concat */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7346 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 7347 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 7348 | (ssizessizeargfunc) unicode_slice, /* sq_slice */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7349 | 0, /* sq_ass_item */ |
| 7350 | 0, /* sq_ass_slice */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 7351 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7352 | }; |
| 7353 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7354 | static PyObject* |
| 7355 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 7356 | { |
Marc-André Lemburg | 3a45779 | 2006-08-14 12:57:27 +0000 | [diff] [blame] | 7357 | if (PyIndex_Check(item)) { |
| 7358 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7359 | if (i == -1 && PyErr_Occurred()) |
| 7360 | return NULL; |
| 7361 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7362 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7363 | return unicode_getitem(self, i); |
| 7364 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7365 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7366 | Py_UNICODE* source_buf; |
| 7367 | Py_UNICODE* result_buf; |
| 7368 | PyObject* result; |
| 7369 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7370 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7371 | &start, &stop, &step, &slicelength) < 0) { |
| 7372 | return NULL; |
| 7373 | } |
| 7374 | |
| 7375 | if (slicelength <= 0) { |
| 7376 | return PyUnicode_FromUnicode(NULL, 0); |
Thomas Wouters | 3ccec68 | 2007-08-28 15:28:19 +0000 | [diff] [blame] | 7377 | } else if (start == 0 && step == 1 && slicelength == self->length && |
| 7378 | PyUnicode_CheckExact(self)) { |
| 7379 | Py_INCREF(self); |
| 7380 | return (PyObject *)self; |
| 7381 | } else if (step == 1) { |
| 7382 | return PyUnicode_FromUnicode(self->str + start, slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7383 | } else { |
| 7384 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 7385 | result_buf = (Py_UNICODE *)PyMem_MALLOC(slicelength* |
| 7386 | sizeof(Py_UNICODE)); |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7387 | |
| 7388 | if (result_buf == NULL) |
| 7389 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7390 | |
| 7391 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 7392 | result_buf[i] = source_buf[cur]; |
| 7393 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7394 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7395 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
| 7396 | PyMem_FREE(result_buf); |
| 7397 | return result; |
| 7398 | } |
| 7399 | } else { |
| 7400 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 7401 | return NULL; |
| 7402 | } |
| 7403 | } |
| 7404 | |
| 7405 | static PyMappingMethods unicode_as_mapping = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7406 | (lenfunc)unicode_length, /* mp_length */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7407 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 7408 | (objobjargproc)0, /* mp_ass_subscript */ |
| 7409 | }; |
| 7410 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7411 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7412 | unicode_buffer_getreadbuf(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7413 | Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7414 | const void **ptr) |
| 7415 | { |
| 7416 | if (index != 0) { |
| 7417 | PyErr_SetString(PyExc_SystemError, |
| 7418 | "accessing non-existent unicode segment"); |
| 7419 | return -1; |
| 7420 | } |
| 7421 | *ptr = (void *) self->str; |
| 7422 | return PyUnicode_GET_DATA_SIZE(self); |
| 7423 | } |
| 7424 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7425 | static Py_ssize_t |
| 7426 | unicode_buffer_getwritebuf(PyUnicodeObject *self, Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7427 | const void **ptr) |
| 7428 | { |
| 7429 | PyErr_SetString(PyExc_TypeError, |
Neal Norwitz | 20e7213 | 2002-06-13 21:25:17 +0000 | [diff] [blame] | 7430 | "cannot use unicode as modifiable buffer"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7431 | return -1; |
| 7432 | } |
| 7433 | |
| 7434 | static int |
| 7435 | unicode_buffer_getsegcount(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7436 | Py_ssize_t *lenp) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7437 | { |
| 7438 | if (lenp) |
| 7439 | *lenp = PyUnicode_GET_DATA_SIZE(self); |
| 7440 | return 1; |
| 7441 | } |
| 7442 | |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 7443 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7444 | unicode_buffer_getcharbuf(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7445 | Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7446 | const void **ptr) |
| 7447 | { |
| 7448 | PyObject *str; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7449 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7450 | if (index != 0) { |
| 7451 | PyErr_SetString(PyExc_SystemError, |
| 7452 | "accessing non-existent unicode segment"); |
| 7453 | return -1; |
| 7454 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 7455 | str = _PyUnicode_AsDefaultEncodedString((PyObject *)self, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7456 | if (str == NULL) |
| 7457 | return -1; |
| 7458 | *ptr = (void *) PyString_AS_STRING(str); |
| 7459 | return PyString_GET_SIZE(str); |
| 7460 | } |
| 7461 | |
| 7462 | /* Helpers for PyUnicode_Format() */ |
| 7463 | |
| 7464 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7465 | 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] | 7466 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7467 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7468 | if (argidx < arglen) { |
| 7469 | (*p_argidx)++; |
| 7470 | if (arglen < 0) |
| 7471 | return args; |
| 7472 | else |
| 7473 | return PyTuple_GetItem(args, argidx); |
| 7474 | } |
| 7475 | PyErr_SetString(PyExc_TypeError, |
| 7476 | "not enough arguments for format string"); |
| 7477 | return NULL; |
| 7478 | } |
| 7479 | |
| 7480 | #define F_LJUST (1<<0) |
| 7481 | #define F_SIGN (1<<1) |
| 7482 | #define F_BLANK (1<<2) |
| 7483 | #define F_ALT (1<<3) |
| 7484 | #define F_ZERO (1<<4) |
| 7485 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7486 | static Py_ssize_t |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7487 | strtounicode(Py_UNICODE *buffer, const char *charbuffer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7488 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7489 | register Py_ssize_t i; |
| 7490 | Py_ssize_t len = strlen(charbuffer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7491 | for (i = len - 1; i >= 0; i--) |
| 7492 | buffer[i] = (Py_UNICODE) charbuffer[i]; |
| 7493 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7494 | return len; |
| 7495 | } |
| 7496 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7497 | static int |
| 7498 | doubletounicode(Py_UNICODE *buffer, size_t len, const char *format, double x) |
| 7499 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 7500 | Py_ssize_t result; |
| 7501 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7502 | PyOS_ascii_formatd((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 7503 | result = strtounicode(buffer, (char *)buffer); |
| 7504 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7505 | } |
| 7506 | |
| 7507 | static int |
| 7508 | longtounicode(Py_UNICODE *buffer, size_t len, const char *format, long x) |
| 7509 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 7510 | Py_ssize_t result; |
| 7511 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7512 | PyOS_snprintf((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 7513 | result = strtounicode(buffer, (char *)buffer); |
| 7514 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7515 | } |
| 7516 | |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 7517 | /* XXX To save some code duplication, formatfloat/long/int could have been |
| 7518 | shared with stringobject.c, converting from 8-bit to Unicode after the |
| 7519 | formatting is done. */ |
| 7520 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7521 | static int |
| 7522 | formatfloat(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7523 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7524 | int flags, |
| 7525 | int prec, |
| 7526 | int type, |
| 7527 | PyObject *v) |
| 7528 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7529 | /* fmt = '%#.' + `prec` + `type` |
| 7530 | 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] | 7531 | char fmt[20]; |
| 7532 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7533 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7534 | x = PyFloat_AsDouble(v); |
| 7535 | if (x == -1.0 && PyErr_Occurred()) |
| 7536 | return -1; |
| 7537 | if (prec < 0) |
| 7538 | prec = 6; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7539 | if (type == 'f' && (fabs(x) / 1e25) >= 1e25) |
| 7540 | type = 'g'; |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 7541 | /* Worst case length calc to ensure no buffer overrun: |
| 7542 | |
| 7543 | 'g' formats: |
| 7544 | fmt = %#.<prec>g |
| 7545 | buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
| 7546 | for any double rep.) |
| 7547 | len = 1 + prec + 1 + 2 + 5 = 9 + prec |
| 7548 | |
| 7549 | 'f' formats: |
| 7550 | buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
| 7551 | len = 1 + 50 + 1 + prec = 52 + prec |
| 7552 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7553 | If prec=0 the effective precision is 1 (the leading digit is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7554 | always given), therefore increase the length by one. |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 7555 | |
| 7556 | */ |
Georg Brandl | 7c3b50d | 2007-07-12 08:38:00 +0000 | [diff] [blame] | 7557 | if (((type == 'g' || type == 'G') && |
| 7558 | buflen <= (size_t)10 + (size_t)prec) || |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 7559 | (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7560 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 7561 | "formatted float is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7562 | return -1; |
| 7563 | } |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 7564 | PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
| 7565 | (flags&F_ALT) ? "#" : "", |
| 7566 | prec, type); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7567 | return doubletounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7568 | } |
| 7569 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7570 | static PyObject* |
| 7571 | formatlong(PyObject *val, int flags, int prec, int type) |
| 7572 | { |
| 7573 | char *buf; |
| 7574 | int i, len; |
| 7575 | PyObject *str; /* temporary string object. */ |
| 7576 | PyUnicodeObject *result; |
| 7577 | |
| 7578 | str = _PyString_FormatLong(val, flags, prec, type, &buf, &len); |
| 7579 | if (!str) |
| 7580 | return NULL; |
| 7581 | result = _PyUnicode_New(len); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 7582 | if (!result) { |
| 7583 | Py_DECREF(str); |
| 7584 | return NULL; |
| 7585 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7586 | for (i = 0; i < len; i++) |
| 7587 | result->str[i] = buf[i]; |
| 7588 | result->str[len] = 0; |
| 7589 | Py_DECREF(str); |
| 7590 | return (PyObject*)result; |
| 7591 | } |
| 7592 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7593 | static int |
| 7594 | formatint(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7595 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7596 | int flags, |
| 7597 | int prec, |
| 7598 | int type, |
| 7599 | PyObject *v) |
| 7600 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7601 | /* fmt = '%#.' + `prec` + 'l' + `type` |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7602 | * worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
| 7603 | * + 1 + 1 |
| 7604 | * = 24 |
| 7605 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7606 | char fmt[64]; /* plenty big enough! */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7607 | char *sign; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7608 | long x; |
| 7609 | |
| 7610 | x = PyInt_AsLong(v); |
| 7611 | if (x == -1 && PyErr_Occurred()) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7612 | return -1; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7613 | if (x < 0 && type == 'u') { |
| 7614 | type = 'd'; |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 7615 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7616 | if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) |
| 7617 | sign = "-"; |
| 7618 | else |
| 7619 | sign = ""; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7620 | if (prec < 0) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7621 | prec = 1; |
| 7622 | |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7623 | /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) |
| 7624 | * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7625 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7626 | if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7627 | PyErr_SetString(PyExc_OverflowError, |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7628 | "formatted integer is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7629 | return -1; |
| 7630 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7631 | |
| 7632 | if ((flags & F_ALT) && |
| 7633 | (type == 'x' || type == 'X')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7634 | /* When converting under %#x or %#X, there are a number |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7635 | * of issues that cause pain: |
| 7636 | * - when 0 is being converted, the C standard leaves off |
| 7637 | * the '0x' or '0X', which is inconsistent with other |
| 7638 | * %#x/%#X conversions and inconsistent with Python's |
| 7639 | * hex() function |
| 7640 | * - there are platforms that violate the standard and |
| 7641 | * convert 0 with the '0x' or '0X' |
| 7642 | * (Metrowerks, Compaq Tru64) |
| 7643 | * - there are platforms that give '0x' when converting |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7644 | * under %#X, but convert 0 in accordance with the |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7645 | * standard (OS/2 EMX) |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7646 | * |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7647 | * We can achieve the desired consistency by inserting our |
| 7648 | * own '0x' or '0X' prefix, and substituting %x/%X in place |
| 7649 | * of %#x/%#X. |
| 7650 | * |
| 7651 | * Note that this is the same approach as used in |
| 7652 | * formatint() in stringobject.c |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 7653 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7654 | PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", |
| 7655 | sign, type, prec, type); |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 7656 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7657 | else { |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7658 | PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", |
| 7659 | sign, (flags&F_ALT) ? "#" : "", |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7660 | prec, type); |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 7661 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7662 | if (sign[0]) |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7663 | return longtounicode(buf, buflen, fmt, -x); |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7664 | else |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7665 | return longtounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7666 | } |
| 7667 | |
| 7668 | static int |
| 7669 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7670 | size_t buflen, |
| 7671 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7672 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7673 | /* presume that the buffer is at least 2 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7674 | if (PyUnicode_Check(v)) { |
| 7675 | if (PyUnicode_GET_SIZE(v) != 1) |
| 7676 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7677 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7678 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7679 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7680 | else if (PyString_Check(v)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7681 | if (PyString_GET_SIZE(v) != 1) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7682 | goto onError; |
| 7683 | buf[0] = (Py_UNICODE)PyString_AS_STRING(v)[0]; |
| 7684 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7685 | |
| 7686 | else { |
| 7687 | /* Integer input truncated to a character */ |
| 7688 | long x; |
| 7689 | x = PyInt_AsLong(v); |
| 7690 | if (x == -1 && PyErr_Occurred()) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7691 | goto onError; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 7692 | #ifdef Py_UNICODE_WIDE |
| 7693 | if (x < 0 || x > 0x10ffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 7694 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 7695 | "%c arg not in range(0x110000) " |
| 7696 | "(wide Python build)"); |
| 7697 | return -1; |
| 7698 | } |
| 7699 | #else |
| 7700 | if (x < 0 || x > 0xffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 7701 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 7702 | "%c arg not in range(0x10000) " |
| 7703 | "(narrow Python build)"); |
| 7704 | return -1; |
| 7705 | } |
| 7706 | #endif |
| 7707 | buf[0] = (Py_UNICODE) x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7708 | } |
| 7709 | buf[1] = '\0'; |
| 7710 | return 1; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7711 | |
| 7712 | onError: |
| 7713 | PyErr_SetString(PyExc_TypeError, |
| 7714 | "%c requires int or char"); |
| 7715 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7716 | } |
| 7717 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7718 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 7719 | |
| 7720 | FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
| 7721 | chars are formatted. XXX This is a magic number. Each formatting |
| 7722 | routine does bounds checking to ensure no overflow, but a better |
| 7723 | solution may be to malloc a buffer of appropriate size for each |
| 7724 | format. For now, the current solution is sufficient. |
| 7725 | */ |
| 7726 | #define FORMATBUFLEN (size_t)120 |
| 7727 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7728 | PyObject *PyUnicode_Format(PyObject *format, |
| 7729 | PyObject *args) |
| 7730 | { |
| 7731 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7732 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7733 | int args_owned = 0; |
| 7734 | PyUnicodeObject *result = NULL; |
| 7735 | PyObject *dict = NULL; |
| 7736 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7737 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7738 | if (format == NULL || args == NULL) { |
| 7739 | PyErr_BadInternalCall(); |
| 7740 | return NULL; |
| 7741 | } |
| 7742 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7743 | if (uformat == NULL) |
| 7744 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7745 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 7746 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 7747 | |
| 7748 | reslen = rescnt = fmtcnt + 100; |
| 7749 | result = _PyUnicode_New(reslen); |
| 7750 | if (result == NULL) |
| 7751 | goto onError; |
| 7752 | res = PyUnicode_AS_UNICODE(result); |
| 7753 | |
| 7754 | if (PyTuple_Check(args)) { |
| 7755 | arglen = PyTuple_Size(args); |
| 7756 | argidx = 0; |
| 7757 | } |
| 7758 | else { |
| 7759 | arglen = -1; |
| 7760 | argidx = -2; |
| 7761 | } |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 7762 | if (Py_Type(args)->tp_as_mapping && !PyTuple_Check(args) && |
Neal Norwitz | 80a1bf4 | 2002-11-12 23:01:12 +0000 | [diff] [blame] | 7763 | !PyObject_TypeCheck(args, &PyBaseString_Type)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7764 | dict = args; |
| 7765 | |
| 7766 | while (--fmtcnt >= 0) { |
| 7767 | if (*fmt != '%') { |
| 7768 | if (--rescnt < 0) { |
| 7769 | rescnt = fmtcnt + 100; |
| 7770 | reslen += rescnt; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7771 | if (_PyUnicode_Resize(&result, reslen) < 0) |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 7772 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7773 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 7774 | --rescnt; |
| 7775 | } |
| 7776 | *res++ = *fmt++; |
| 7777 | } |
| 7778 | else { |
| 7779 | /* Got a format specifier */ |
| 7780 | int flags = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7781 | Py_ssize_t width = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7782 | int prec = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7783 | Py_UNICODE c = '\0'; |
| 7784 | Py_UNICODE fill; |
| 7785 | PyObject *v = NULL; |
| 7786 | PyObject *temp = NULL; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7787 | Py_UNICODE *pbuf; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7788 | Py_UNICODE sign; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7789 | Py_ssize_t len; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7790 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7791 | |
| 7792 | fmt++; |
| 7793 | if (*fmt == '(') { |
| 7794 | Py_UNICODE *keystart; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7795 | Py_ssize_t keylen; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7796 | PyObject *key; |
| 7797 | int pcount = 1; |
| 7798 | |
| 7799 | if (dict == NULL) { |
| 7800 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7801 | "format requires a mapping"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7802 | goto onError; |
| 7803 | } |
| 7804 | ++fmt; |
| 7805 | --fmtcnt; |
| 7806 | keystart = fmt; |
| 7807 | /* Skip over balanced parentheses */ |
| 7808 | while (pcount > 0 && --fmtcnt >= 0) { |
| 7809 | if (*fmt == ')') |
| 7810 | --pcount; |
| 7811 | else if (*fmt == '(') |
| 7812 | ++pcount; |
| 7813 | fmt++; |
| 7814 | } |
| 7815 | keylen = fmt - keystart - 1; |
| 7816 | if (fmtcnt < 0 || pcount > 0) { |
| 7817 | PyErr_SetString(PyExc_ValueError, |
| 7818 | "incomplete format key"); |
| 7819 | goto onError; |
| 7820 | } |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 7821 | #if 0 |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7822 | /* keys are converted to strings using UTF-8 and |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7823 | then looked up since Python uses strings to hold |
| 7824 | variables names etc. in its namespaces and we |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7825 | wouldn't want to break common idioms. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7826 | key = PyUnicode_EncodeUTF8(keystart, |
| 7827 | keylen, |
| 7828 | NULL); |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 7829 | #else |
| 7830 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 7831 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7832 | if (key == NULL) |
| 7833 | goto onError; |
| 7834 | if (args_owned) { |
| 7835 | Py_DECREF(args); |
| 7836 | args_owned = 0; |
| 7837 | } |
| 7838 | args = PyObject_GetItem(dict, key); |
| 7839 | Py_DECREF(key); |
| 7840 | if (args == NULL) { |
| 7841 | goto onError; |
| 7842 | } |
| 7843 | args_owned = 1; |
| 7844 | arglen = -1; |
| 7845 | argidx = -2; |
| 7846 | } |
| 7847 | while (--fmtcnt >= 0) { |
| 7848 | switch (c = *fmt++) { |
| 7849 | case '-': flags |= F_LJUST; continue; |
| 7850 | case '+': flags |= F_SIGN; continue; |
| 7851 | case ' ': flags |= F_BLANK; continue; |
| 7852 | case '#': flags |= F_ALT; continue; |
| 7853 | case '0': flags |= F_ZERO; continue; |
| 7854 | } |
| 7855 | break; |
| 7856 | } |
| 7857 | if (c == '*') { |
| 7858 | v = getnextarg(args, arglen, &argidx); |
| 7859 | if (v == NULL) |
| 7860 | goto onError; |
| 7861 | if (!PyInt_Check(v)) { |
| 7862 | PyErr_SetString(PyExc_TypeError, |
| 7863 | "* wants int"); |
| 7864 | goto onError; |
| 7865 | } |
| 7866 | width = PyInt_AsLong(v); |
| 7867 | if (width < 0) { |
| 7868 | flags |= F_LJUST; |
| 7869 | width = -width; |
| 7870 | } |
| 7871 | if (--fmtcnt >= 0) |
| 7872 | c = *fmt++; |
| 7873 | } |
| 7874 | else if (c >= '0' && c <= '9') { |
| 7875 | width = c - '0'; |
| 7876 | while (--fmtcnt >= 0) { |
| 7877 | c = *fmt++; |
| 7878 | if (c < '0' || c > '9') |
| 7879 | break; |
| 7880 | if ((width*10) / 10 != width) { |
| 7881 | PyErr_SetString(PyExc_ValueError, |
| 7882 | "width too big"); |
| 7883 | goto onError; |
| 7884 | } |
| 7885 | width = width*10 + (c - '0'); |
| 7886 | } |
| 7887 | } |
| 7888 | if (c == '.') { |
| 7889 | prec = 0; |
| 7890 | if (--fmtcnt >= 0) |
| 7891 | c = *fmt++; |
| 7892 | if (c == '*') { |
| 7893 | v = getnextarg(args, arglen, &argidx); |
| 7894 | if (v == NULL) |
| 7895 | goto onError; |
| 7896 | if (!PyInt_Check(v)) { |
| 7897 | PyErr_SetString(PyExc_TypeError, |
| 7898 | "* wants int"); |
| 7899 | goto onError; |
| 7900 | } |
| 7901 | prec = PyInt_AsLong(v); |
| 7902 | if (prec < 0) |
| 7903 | prec = 0; |
| 7904 | if (--fmtcnt >= 0) |
| 7905 | c = *fmt++; |
| 7906 | } |
| 7907 | else if (c >= '0' && c <= '9') { |
| 7908 | prec = c - '0'; |
| 7909 | while (--fmtcnt >= 0) { |
| 7910 | c = Py_CHARMASK(*fmt++); |
| 7911 | if (c < '0' || c > '9') |
| 7912 | break; |
| 7913 | if ((prec*10) / 10 != prec) { |
| 7914 | PyErr_SetString(PyExc_ValueError, |
| 7915 | "prec too big"); |
| 7916 | goto onError; |
| 7917 | } |
| 7918 | prec = prec*10 + (c - '0'); |
| 7919 | } |
| 7920 | } |
| 7921 | } /* prec */ |
| 7922 | if (fmtcnt >= 0) { |
| 7923 | if (c == 'h' || c == 'l' || c == 'L') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7924 | if (--fmtcnt >= 0) |
| 7925 | c = *fmt++; |
| 7926 | } |
| 7927 | } |
| 7928 | if (fmtcnt < 0) { |
| 7929 | PyErr_SetString(PyExc_ValueError, |
| 7930 | "incomplete format"); |
| 7931 | goto onError; |
| 7932 | } |
| 7933 | if (c != '%') { |
| 7934 | v = getnextarg(args, arglen, &argidx); |
| 7935 | if (v == NULL) |
| 7936 | goto onError; |
| 7937 | } |
| 7938 | sign = 0; |
| 7939 | fill = ' '; |
| 7940 | switch (c) { |
| 7941 | |
| 7942 | case '%': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7943 | pbuf = formatbuf; |
| 7944 | /* presume that buffer length is at least 1 */ |
| 7945 | pbuf[0] = '%'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7946 | len = 1; |
| 7947 | break; |
| 7948 | |
| 7949 | case 's': |
| 7950 | case 'r': |
| 7951 | if (PyUnicode_Check(v) && c == 's') { |
| 7952 | temp = v; |
| 7953 | Py_INCREF(temp); |
| 7954 | } |
| 7955 | else { |
| 7956 | PyObject *unicode; |
| 7957 | if (c == 's') |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 7958 | temp = PyObject_Unicode(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7959 | else |
| 7960 | temp = PyObject_Repr(v); |
| 7961 | if (temp == NULL) |
| 7962 | goto onError; |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 7963 | if (PyUnicode_Check(temp)) |
| 7964 | /* nothing to do */; |
| 7965 | else if (PyString_Check(temp)) { |
| 7966 | /* convert to string to Unicode */ |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7967 | unicode = PyUnicode_Decode(PyString_AS_STRING(temp), |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7968 | PyString_GET_SIZE(temp), |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7969 | NULL, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7970 | "strict"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7971 | Py_DECREF(temp); |
| 7972 | temp = unicode; |
| 7973 | if (temp == NULL) |
| 7974 | goto onError; |
| 7975 | } |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 7976 | else { |
| 7977 | Py_DECREF(temp); |
| 7978 | PyErr_SetString(PyExc_TypeError, |
| 7979 | "%s argument has non-string str()"); |
| 7980 | goto onError; |
| 7981 | } |
| 7982 | } |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7983 | pbuf = PyUnicode_AS_UNICODE(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7984 | len = PyUnicode_GET_SIZE(temp); |
| 7985 | if (prec >= 0 && len > prec) |
| 7986 | len = prec; |
| 7987 | break; |
| 7988 | |
| 7989 | case 'i': |
| 7990 | case 'd': |
| 7991 | case 'u': |
| 7992 | case 'o': |
| 7993 | case 'x': |
| 7994 | case 'X': |
| 7995 | if (c == 'i') |
| 7996 | c = 'd'; |
Tim Peters | a3a3a03 | 2000-11-30 05:22:44 +0000 | [diff] [blame] | 7997 | if (PyLong_Check(v)) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7998 | temp = formatlong(v, flags, prec, c); |
| 7999 | if (!temp) |
| 8000 | goto onError; |
| 8001 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 8002 | len = PyUnicode_GET_SIZE(temp); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8003 | sign = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8004 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8005 | else { |
| 8006 | pbuf = formatbuf; |
| 8007 | len = formatint(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 8008 | flags, prec, c, v); |
| 8009 | if (len < 0) |
| 8010 | goto onError; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8011 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8012 | } |
| 8013 | if (flags & F_ZERO) |
| 8014 | fill = '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8015 | break; |
| 8016 | |
| 8017 | case 'e': |
| 8018 | case 'E': |
| 8019 | case 'f': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 8020 | case 'F': |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8021 | case 'g': |
| 8022 | case 'G': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 8023 | if (c == 'F') |
| 8024 | c = 'f'; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8025 | pbuf = formatbuf; |
| 8026 | len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 8027 | flags, prec, c, v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8028 | if (len < 0) |
| 8029 | goto onError; |
| 8030 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8031 | if (flags & F_ZERO) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8032 | fill = '0'; |
| 8033 | break; |
| 8034 | |
| 8035 | case 'c': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8036 | pbuf = formatbuf; |
| 8037 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8038 | if (len < 0) |
| 8039 | goto onError; |
| 8040 | break; |
| 8041 | |
| 8042 | default: |
| 8043 | PyErr_Format(PyExc_ValueError, |
Andrew M. Kuchling | 6ca8917 | 2000-12-15 13:07:46 +0000 | [diff] [blame] | 8044 | "unsupported format character '%c' (0x%x) " |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 8045 | "at index %zd", |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8046 | (31<=c && c<=126) ? (char)c : '?', |
Marc-André Lemburg | 24e53b6 | 2002-09-24 09:32:14 +0000 | [diff] [blame] | 8047 | (int)c, |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 8048 | (Py_ssize_t)(fmt - 1 - |
| 8049 | PyUnicode_AS_UNICODE(uformat))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8050 | goto onError; |
| 8051 | } |
| 8052 | if (sign) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8053 | if (*pbuf == '-' || *pbuf == '+') { |
| 8054 | sign = *pbuf++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8055 | len--; |
| 8056 | } |
| 8057 | else if (flags & F_SIGN) |
| 8058 | sign = '+'; |
| 8059 | else if (flags & F_BLANK) |
| 8060 | sign = ' '; |
| 8061 | else |
| 8062 | sign = 0; |
| 8063 | } |
| 8064 | if (width < len) |
| 8065 | width = len; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8066 | if (rescnt - (sign != 0) < width) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8067 | reslen -= rescnt; |
| 8068 | rescnt = width + fmtcnt + 100; |
| 8069 | reslen += rescnt; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8070 | if (reslen < 0) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 8071 | Py_XDECREF(temp); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8072 | PyErr_NoMemory(); |
| 8073 | goto onError; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8074 | } |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8075 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 8076 | Py_XDECREF(temp); |
| 8077 | goto onError; |
| 8078 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8079 | res = PyUnicode_AS_UNICODE(result) |
| 8080 | + reslen - rescnt; |
| 8081 | } |
| 8082 | if (sign) { |
| 8083 | if (fill != ' ') |
| 8084 | *res++ = sign; |
| 8085 | rescnt--; |
| 8086 | if (width > len) |
| 8087 | width--; |
| 8088 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8089 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
| 8090 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8091 | assert(pbuf[1] == c); |
| 8092 | if (fill != ' ') { |
| 8093 | *res++ = *pbuf++; |
| 8094 | *res++ = *pbuf++; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8095 | } |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8096 | rescnt -= 2; |
| 8097 | width -= 2; |
| 8098 | if (width < 0) |
| 8099 | width = 0; |
| 8100 | len -= 2; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8101 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8102 | if (width > len && !(flags & F_LJUST)) { |
| 8103 | do { |
| 8104 | --rescnt; |
| 8105 | *res++ = fill; |
| 8106 | } while (--width > len); |
| 8107 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8108 | if (fill == ' ') { |
| 8109 | if (sign) |
| 8110 | *res++ = sign; |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8111 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8112 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8113 | assert(pbuf[1] == c); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8114 | *res++ = *pbuf++; |
| 8115 | *res++ = *pbuf++; |
| 8116 | } |
| 8117 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8118 | Py_UNICODE_COPY(res, pbuf, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8119 | res += len; |
| 8120 | rescnt -= len; |
| 8121 | while (--width >= len) { |
| 8122 | --rescnt; |
| 8123 | *res++ = ' '; |
| 8124 | } |
| 8125 | if (dict && (argidx < arglen) && c != '%') { |
| 8126 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 8127 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8128 | Py_XDECREF(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8129 | goto onError; |
| 8130 | } |
| 8131 | Py_XDECREF(temp); |
| 8132 | } /* '%' */ |
| 8133 | } /* until end */ |
| 8134 | if (argidx < arglen && !dict) { |
| 8135 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 8136 | "not all arguments converted during string formatting"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8137 | goto onError; |
| 8138 | } |
| 8139 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8140 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
| 8141 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8142 | if (args_owned) { |
| 8143 | Py_DECREF(args); |
| 8144 | } |
| 8145 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8146 | return (PyObject *)result; |
| 8147 | |
| 8148 | onError: |
| 8149 | Py_XDECREF(result); |
| 8150 | Py_DECREF(uformat); |
| 8151 | if (args_owned) { |
| 8152 | Py_DECREF(args); |
| 8153 | } |
| 8154 | return NULL; |
| 8155 | } |
| 8156 | |
| 8157 | static PyBufferProcs unicode_as_buffer = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8158 | (readbufferproc) unicode_buffer_getreadbuf, |
| 8159 | (writebufferproc) unicode_buffer_getwritebuf, |
| 8160 | (segcountproc) unicode_buffer_getsegcount, |
| 8161 | (charbufferproc) unicode_buffer_getcharbuf, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8162 | }; |
| 8163 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 8164 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8165 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 8166 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8167 | static PyObject * |
| 8168 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 8169 | { |
| 8170 | PyObject *x = NULL; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 8171 | static char *kwlist[] = {"string", "encoding", "errors", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8172 | char *encoding = NULL; |
| 8173 | char *errors = NULL; |
| 8174 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8175 | if (type != &PyUnicode_Type) |
| 8176 | return unicode_subtype_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8177 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:unicode", |
| 8178 | kwlist, &x, &encoding, &errors)) |
| 8179 | return NULL; |
| 8180 | if (x == NULL) |
| 8181 | return (PyObject *)_PyUnicode_New(0); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 8182 | if (encoding == NULL && errors == NULL) |
| 8183 | return PyObject_Unicode(x); |
| 8184 | else |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8185 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
| 8186 | } |
| 8187 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8188 | static PyObject * |
| 8189 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 8190 | { |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8191 | PyUnicodeObject *tmp, *pnew; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8192 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8193 | |
| 8194 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 8195 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 8196 | if (tmp == NULL) |
| 8197 | return NULL; |
| 8198 | assert(PyUnicode_Check(tmp)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8199 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 8200 | if (pnew == NULL) { |
| 8201 | Py_DECREF(tmp); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8202 | return NULL; |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 8203 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8204 | pnew->str = PyMem_NEW(Py_UNICODE, n+1); |
| 8205 | if (pnew->str == NULL) { |
| 8206 | _Py_ForgetReference((PyObject *)pnew); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 8207 | PyObject_Del(pnew); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 8208 | Py_DECREF(tmp); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 8209 | return PyErr_NoMemory(); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8210 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8211 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 8212 | pnew->length = n; |
| 8213 | pnew->hash = tmp->hash; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8214 | Py_DECREF(tmp); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8215 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8216 | } |
| 8217 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8218 | PyDoc_STRVAR(unicode_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8219 | "unicode(string [, encoding[, errors]]) -> object\n\ |
| 8220 | \n\ |
| 8221 | Create a new Unicode object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 8222 | encoding defaults to the current default string encoding.\n\ |
| 8223 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8224 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8225 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 8226 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8227 | "unicode", /* tp_name */ |
| 8228 | sizeof(PyUnicodeObject), /* tp_size */ |
| 8229 | 0, /* tp_itemsize */ |
| 8230 | /* Slots */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 8231 | (destructor)unicode_dealloc, /* tp_dealloc */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8232 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8233 | 0, /* tp_getattr */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8234 | 0, /* tp_setattr */ |
Marc-André Lemburg | 040f76b | 2006-08-14 10:55:19 +0000 | [diff] [blame] | 8235 | 0, /* tp_compare */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 8236 | unicode_repr, /* tp_repr */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8237 | &unicode_as_number, /* tp_as_number */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8238 | &unicode_as_sequence, /* tp_as_sequence */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8239 | &unicode_as_mapping, /* tp_as_mapping */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8240 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 8241 | 0, /* tp_call*/ |
| 8242 | (reprfunc) unicode_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8243 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 8244 | 0, /* tp_setattro */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8245 | &unicode_as_buffer, /* tp_as_buffer */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8246 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
Neal Norwitz | ee3a1b5 | 2007-02-25 19:44:48 +0000 | [diff] [blame] | 8247 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8248 | unicode_doc, /* tp_doc */ |
| 8249 | 0, /* tp_traverse */ |
| 8250 | 0, /* tp_clear */ |
Marc-André Lemburg | 040f76b | 2006-08-14 10:55:19 +0000 | [diff] [blame] | 8251 | PyUnicode_RichCompare, /* tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8252 | 0, /* tp_weaklistoffset */ |
| 8253 | 0, /* tp_iter */ |
| 8254 | 0, /* tp_iternext */ |
| 8255 | unicode_methods, /* tp_methods */ |
| 8256 | 0, /* tp_members */ |
| 8257 | 0, /* tp_getset */ |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 8258 | &PyBaseString_Type, /* tp_base */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8259 | 0, /* tp_dict */ |
| 8260 | 0, /* tp_descr_get */ |
| 8261 | 0, /* tp_descr_set */ |
| 8262 | 0, /* tp_dictoffset */ |
| 8263 | 0, /* tp_init */ |
| 8264 | 0, /* tp_alloc */ |
| 8265 | unicode_new, /* tp_new */ |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 8266 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8267 | }; |
| 8268 | |
| 8269 | /* Initialize the Unicode implementation */ |
| 8270 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 8271 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8272 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8273 | int i; |
| 8274 | |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 8275 | /* XXX - move this array to unicodectype.c ? */ |
| 8276 | Py_UNICODE linebreak[] = { |
| 8277 | 0x000A, /* LINE FEED */ |
| 8278 | 0x000D, /* CARRIAGE RETURN */ |
| 8279 | 0x001C, /* FILE SEPARATOR */ |
| 8280 | 0x001D, /* GROUP SEPARATOR */ |
| 8281 | 0x001E, /* RECORD SEPARATOR */ |
| 8282 | 0x0085, /* NEXT LINE */ |
| 8283 | 0x2028, /* LINE SEPARATOR */ |
| 8284 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 8285 | }; |
| 8286 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8287 | /* Init the implementation */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8288 | unicode_freelist = NULL; |
| 8289 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8290 | unicode_empty = _PyUnicode_New(0); |
Neal Norwitz | e1fdb32 | 2006-07-21 05:32:28 +0000 | [diff] [blame] | 8291 | if (!unicode_empty) |
| 8292 | return; |
| 8293 | |
Marc-André Lemburg | 90e8147 | 2000-06-07 09:13:21 +0000 | [diff] [blame] | 8294 | strcpy(unicode_default_encoding, "ascii"); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8295 | for (i = 0; i < 256; i++) |
| 8296 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 8297 | if (PyType_Ready(&PyUnicode_Type) < 0) |
| 8298 | Py_FatalError("Can't initialize 'unicode'"); |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 8299 | |
| 8300 | /* initialize the linebreak bloom filter */ |
| 8301 | bloom_linebreak = make_bloom_mask( |
| 8302 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 8303 | ); |
Neal Norwitz | de4c78a | 2006-06-13 08:28:19 +0000 | [diff] [blame] | 8304 | |
| 8305 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8306 | } |
| 8307 | |
| 8308 | /* Finalize the Unicode implementation */ |
| 8309 | |
| 8310 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 8311 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8312 | { |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 8313 | PyUnicodeObject *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8314 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8315 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 8316 | Py_XDECREF(unicode_empty); |
| 8317 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 8318 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8319 | for (i = 0; i < 256; i++) { |
| 8320 | if (unicode_latin1[i]) { |
| 8321 | Py_DECREF(unicode_latin1[i]); |
| 8322 | unicode_latin1[i] = NULL; |
| 8323 | } |
| 8324 | } |
| 8325 | |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 8326 | for (u = unicode_freelist; u != NULL;) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8327 | PyUnicodeObject *v = u; |
| 8328 | u = *(PyUnicodeObject **)u; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 8329 | if (v->str) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 8330 | PyMem_DEL(v->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 8331 | Py_XDECREF(v->defenc); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 8332 | PyObject_Del(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8333 | } |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8334 | unicode_freelist = NULL; |
| 8335 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8336 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 8337 | |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 8338 | #ifdef __cplusplus |
| 8339 | } |
| 8340 | #endif |
| 8341 | |
| 8342 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 8343 | /* |
| 8344 | Local variables: |
| 8345 | c-basic-offset: 4 |
| 8346 | indent-tabs-mode: nil |
| 8347 | End: |
| 8348 | */ |