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 | 95e2a91 | 2006-05-26 11:38:15 +0000 | [diff] [blame] | 144 | Py_LOCAL(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 | 95e2a91 | 2006-05-26 11:38:15 +0000 | [diff] [blame] | 158 | Py_LOCAL(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 |
| 199 | safe to look at str[length] (without makeing any assumptions about what |
| 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 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 236 | /* Optimization fo 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); |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 315 | unicode->ob_type->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; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 329 | if (v == NULL || !PyUnicode_Check(v) || v->ob_refcnt != 1 || length < 0) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 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", |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 557 | obj->ob_type->tp_name); |
| 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)", |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 607 | unicode->ob_type->tp_name); |
| 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)", |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 717 | v->ob_type->tp_name); |
| 718 | Py_DECREF(v); |
| 719 | goto onError; |
| 720 | } |
| 721 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 722 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 723 | onError: |
| 724 | return NULL; |
| 725 | } |
| 726 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 727 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
| 728 | const char *errors) |
| 729 | { |
| 730 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
| 731 | |
| 732 | if (v) |
| 733 | return v; |
| 734 | v = PyUnicode_AsEncodedString(unicode, NULL, errors); |
| 735 | if (v && errors == NULL) |
| 736 | ((PyUnicodeObject *)unicode)->defenc = v; |
| 737 | return v; |
| 738 | } |
| 739 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 740 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 741 | { |
| 742 | if (!PyUnicode_Check(unicode)) { |
| 743 | PyErr_BadArgument(); |
| 744 | goto onError; |
| 745 | } |
| 746 | return PyUnicode_AS_UNICODE(unicode); |
| 747 | |
| 748 | onError: |
| 749 | return NULL; |
| 750 | } |
| 751 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 752 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 753 | { |
| 754 | if (!PyUnicode_Check(unicode)) { |
| 755 | PyErr_BadArgument(); |
| 756 | goto onError; |
| 757 | } |
| 758 | return PyUnicode_GET_SIZE(unicode); |
| 759 | |
| 760 | onError: |
| 761 | return -1; |
| 762 | } |
| 763 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 764 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 765 | { |
| 766 | return unicode_default_encoding; |
| 767 | } |
| 768 | |
| 769 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 770 | { |
| 771 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 772 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 773 | /* Make sure the encoding is valid. As side effect, this also |
| 774 | loads the encoding into the codec registry cache. */ |
| 775 | v = _PyCodec_Lookup(encoding); |
| 776 | if (v == NULL) |
| 777 | goto onError; |
| 778 | Py_DECREF(v); |
| 779 | strncpy(unicode_default_encoding, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 780 | encoding, |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 781 | sizeof(unicode_default_encoding)); |
| 782 | return 0; |
| 783 | |
| 784 | onError: |
| 785 | return -1; |
| 786 | } |
| 787 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 788 | /* error handling callback helper: |
| 789 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 790 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 791 | and adjust various state variables. |
| 792 | return 0 on success, -1 on error |
| 793 | */ |
| 794 | |
| 795 | static |
| 796 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
| 797 | const char *encoding, const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 798 | const char *input, Py_ssize_t insize, Py_ssize_t *startinpos, Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 799 | PyObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 800 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 801 | static char *argparse = "O!n;decoding error handler must return (unicode, int) tuple"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 802 | |
| 803 | PyObject *restuple = NULL; |
| 804 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 805 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
| 806 | Py_ssize_t requiredsize; |
| 807 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 808 | Py_UNICODE *repptr; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 809 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 810 | int res = -1; |
| 811 | |
| 812 | if (*errorHandler == NULL) { |
| 813 | *errorHandler = PyCodec_LookupError(errors); |
| 814 | if (*errorHandler == NULL) |
| 815 | goto onError; |
| 816 | } |
| 817 | |
| 818 | if (*exceptionObject == NULL) { |
| 819 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 820 | encoding, input, insize, *startinpos, *endinpos, reason); |
| 821 | if (*exceptionObject == NULL) |
| 822 | goto onError; |
| 823 | } |
| 824 | else { |
| 825 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 826 | goto onError; |
| 827 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
| 828 | goto onError; |
| 829 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 830 | goto onError; |
| 831 | } |
| 832 | |
| 833 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 834 | if (restuple == NULL) |
| 835 | goto onError; |
| 836 | if (!PyTuple_Check(restuple)) { |
| 837 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 838 | goto onError; |
| 839 | } |
| 840 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
| 841 | goto onError; |
| 842 | if (newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 843 | newpos = insize+newpos; |
| 844 | if (newpos<0 || newpos>insize) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 845 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 846 | goto onError; |
| 847 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 848 | |
| 849 | /* need more space? (at least enough for what we |
| 850 | have+the replacement+the rest of the string (starting |
| 851 | at the new input position), so we won't have to check space |
| 852 | when there are no errors in the rest of the string) */ |
| 853 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 854 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 855 | requiredsize = *outpos + repsize + insize-newpos; |
| 856 | if (requiredsize > outsize) { |
| 857 | if (requiredsize<2*outsize) |
| 858 | requiredsize = 2*outsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 859 | if (PyUnicode_Resize(output, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 860 | goto onError; |
| 861 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
| 862 | } |
| 863 | *endinpos = newpos; |
| 864 | *inptr = input + newpos; |
| 865 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 866 | *outptr += repsize; |
| 867 | *outpos += repsize; |
| 868 | /* we made it! */ |
| 869 | res = 0; |
| 870 | |
| 871 | onError: |
| 872 | Py_XDECREF(restuple); |
| 873 | return res; |
| 874 | } |
| 875 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 876 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 877 | |
| 878 | /* see RFC2152 for details */ |
| 879 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 880 | static |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 881 | char utf7_special[128] = { |
| 882 | /* indicate whether a UTF-7 character is special i.e. cannot be directly |
| 883 | encoded: |
| 884 | 0 - not special |
| 885 | 1 - special |
| 886 | 2 - whitespace (optional) |
| 887 | 3 - RFC2152 Set O (optional) */ |
| 888 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, |
| 889 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 890 | 2, 3, 3, 3, 3, 3, 3, 0, 0, 0, 3, 1, 0, 0, 0, 1, |
| 891 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, |
| 892 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 893 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, |
| 894 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 895 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 1, 1, |
| 896 | |
| 897 | }; |
| 898 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 899 | /* Note: The comparison (c) <= 0 is a trick to work-around gcc |
| 900 | warnings about the comparison always being false; since |
| 901 | utf7_special[0] is 1, we can safely make that one comparison |
| 902 | true */ |
| 903 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 904 | #define SPECIAL(c, encodeO, encodeWS) \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 905 | ((c) > 127 || (c) <= 0 || utf7_special[(c)] == 1 || \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 906 | (encodeWS && (utf7_special[(c)] == 2)) || \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 907 | (encodeO && (utf7_special[(c)] == 3))) |
| 908 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 909 | #define B64(n) \ |
| 910 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 911 | #define B64CHAR(c) \ |
| 912 | (isalnum(c) || (c) == '+' || (c) == '/') |
| 913 | #define UB64(c) \ |
| 914 | ((c) == '+' ? 62 : (c) == '/' ? 63 : (c) >= 'a' ? \ |
| 915 | (c) - 71 : (c) >= 'A' ? (c) - 65 : (c) + 4 ) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 916 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 917 | #define ENCODE(out, ch, bits) \ |
| 918 | while (bits >= 6) { \ |
| 919 | *out++ = B64(ch >> (bits-6)); \ |
| 920 | bits -= 6; \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 921 | } |
| 922 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 923 | #define DECODE(out, ch, bits, surrogate) \ |
| 924 | while (bits >= 16) { \ |
| 925 | Py_UNICODE outCh = (Py_UNICODE) ((ch >> (bits-16)) & 0xffff); \ |
| 926 | bits -= 16; \ |
| 927 | if (surrogate) { \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 928 | /* We have already generated an error for the high surrogate \ |
| 929 | so let's not bother seeing if the low surrogate is correct or not */ \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 930 | surrogate = 0; \ |
| 931 | } else if (0xDC00 <= outCh && outCh <= 0xDFFF) { \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 932 | /* This is a surrogate pair. Unfortunately we can't represent \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 933 | it in a 16-bit character */ \ |
| 934 | surrogate = 1; \ |
| 935 | errmsg = "code pairs are not supported"; \ |
| 936 | goto utf7Error; \ |
| 937 | } else { \ |
| 938 | *out++ = outCh; \ |
| 939 | } \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 940 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 941 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 942 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 943 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 944 | const char *errors) |
| 945 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 946 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 947 | Py_ssize_t startinpos; |
| 948 | Py_ssize_t endinpos; |
| 949 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 950 | const char *e; |
| 951 | PyUnicodeObject *unicode; |
| 952 | Py_UNICODE *p; |
| 953 | const char *errmsg = ""; |
| 954 | int inShift = 0; |
| 955 | unsigned int bitsleft = 0; |
| 956 | unsigned long charsleft = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 957 | int surrogate = 0; |
| 958 | PyObject *errorHandler = NULL; |
| 959 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 960 | |
| 961 | unicode = _PyUnicode_New(size); |
| 962 | if (!unicode) |
| 963 | return NULL; |
| 964 | if (size == 0) |
| 965 | return (PyObject *)unicode; |
| 966 | |
| 967 | p = unicode->str; |
| 968 | e = s + size; |
| 969 | |
| 970 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 971 | Py_UNICODE ch; |
| 972 | restart: |
| 973 | ch = *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 974 | |
| 975 | if (inShift) { |
| 976 | if ((ch == '-') || !B64CHAR(ch)) { |
| 977 | inShift = 0; |
| 978 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 979 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 980 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 981 | if (bitsleft >= 6) { |
| 982 | /* The shift sequence has a partial character in it. If |
| 983 | bitsleft < 6 then we could just classify it as padding |
| 984 | but that is not the case here */ |
| 985 | |
| 986 | errmsg = "partial character in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 987 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 988 | } |
| 989 | /* According to RFC2152 the remaining bits should be zero. We |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 990 | choose to signal an error/insert a replacement character |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 991 | here so indicate the potential of a misencoded character. */ |
| 992 | |
| 993 | /* On x86, a << b == a << (b%32) so make sure that bitsleft != 0 */ |
| 994 | if (bitsleft && charsleft << (sizeof(charsleft) * 8 - bitsleft)) { |
| 995 | errmsg = "non-zero padding bits in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 996 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | if (ch == '-') { |
| 1000 | if ((s < e) && (*(s) == '-')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1001 | *p++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1002 | inShift = 1; |
| 1003 | } |
| 1004 | } else if (SPECIAL(ch,0,0)) { |
| 1005 | errmsg = "unexpected special character"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1006 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1007 | } else { |
| 1008 | *p++ = ch; |
| 1009 | } |
| 1010 | } else { |
| 1011 | charsleft = (charsleft << 6) | UB64(ch); |
| 1012 | bitsleft += 6; |
| 1013 | s++; |
| 1014 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1015 | } |
| 1016 | } |
| 1017 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1018 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1019 | s++; |
| 1020 | if (s < e && *s == '-') { |
| 1021 | s++; |
| 1022 | *p++ = '+'; |
| 1023 | } else |
| 1024 | { |
| 1025 | inShift = 1; |
| 1026 | bitsleft = 0; |
| 1027 | } |
| 1028 | } |
| 1029 | else if (SPECIAL(ch,0,0)) { |
| 1030 | errmsg = "unexpected special character"; |
| 1031 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1032 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1033 | } |
| 1034 | else { |
| 1035 | *p++ = ch; |
| 1036 | s++; |
| 1037 | } |
| 1038 | continue; |
| 1039 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1040 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1041 | endinpos = s-starts; |
| 1042 | if (unicode_decode_call_errorhandler( |
| 1043 | errors, &errorHandler, |
| 1044 | "utf7", errmsg, |
| 1045 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1046 | (PyObject **)&unicode, &outpos, &p)) |
| 1047 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | if (inShift) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1051 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1052 | endinpos = size; |
| 1053 | if (unicode_decode_call_errorhandler( |
| 1054 | errors, &errorHandler, |
| 1055 | "utf7", "unterminated shift sequence", |
| 1056 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1057 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1058 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1059 | if (s < e) |
| 1060 | goto restart; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1063 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1064 | goto onError; |
| 1065 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1066 | Py_XDECREF(errorHandler); |
| 1067 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1068 | return (PyObject *)unicode; |
| 1069 | |
| 1070 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1071 | Py_XDECREF(errorHandler); |
| 1072 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1073 | Py_DECREF(unicode); |
| 1074 | return NULL; |
| 1075 | } |
| 1076 | |
| 1077 | |
| 1078 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1079 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1080 | int encodeSetO, |
| 1081 | int encodeWhiteSpace, |
| 1082 | const char *errors) |
| 1083 | { |
| 1084 | PyObject *v; |
| 1085 | /* It might be possible to tighten this worst case */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1086 | Py_ssize_t cbAllocated = 5 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1087 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1088 | Py_ssize_t i = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1089 | unsigned int bitsleft = 0; |
| 1090 | unsigned long charsleft = 0; |
| 1091 | char * out; |
| 1092 | char * start; |
| 1093 | |
| 1094 | if (size == 0) |
| 1095 | return PyString_FromStringAndSize(NULL, 0); |
| 1096 | |
| 1097 | v = PyString_FromStringAndSize(NULL, cbAllocated); |
| 1098 | if (v == NULL) |
| 1099 | return NULL; |
| 1100 | |
| 1101 | start = out = PyString_AS_STRING(v); |
| 1102 | for (;i < size; ++i) { |
| 1103 | Py_UNICODE ch = s[i]; |
| 1104 | |
| 1105 | if (!inShift) { |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1106 | if (ch == '+') { |
| 1107 | *out++ = '+'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1108 | *out++ = '-'; |
| 1109 | } else if (SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1110 | charsleft = ch; |
| 1111 | bitsleft = 16; |
| 1112 | *out++ = '+'; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1113 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1114 | inShift = bitsleft > 0; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1115 | } else { |
| 1116 | *out++ = (char) ch; |
| 1117 | } |
| 1118 | } else { |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1119 | if (!SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1120 | *out++ = B64(charsleft << (6-bitsleft)); |
| 1121 | charsleft = 0; |
| 1122 | bitsleft = 0; |
| 1123 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 1124 | so no '-' is required, except if the character is itself a '-' */ |
| 1125 | if (B64CHAR(ch) || ch == '-') { |
| 1126 | *out++ = '-'; |
| 1127 | } |
| 1128 | inShift = 0; |
| 1129 | *out++ = (char) ch; |
| 1130 | } else { |
| 1131 | bitsleft += 16; |
| 1132 | charsleft = (charsleft << 16) | ch; |
| 1133 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
| 1134 | |
| 1135 | /* If the next character is special then we dont' need to terminate |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1136 | the shift sequence. If the next character is not a BASE64 character |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1137 | or '-' then the shift sequence will be terminated implicitly and we |
| 1138 | don't have to insert a '-'. */ |
| 1139 | |
| 1140 | if (bitsleft == 0) { |
| 1141 | if (i + 1 < size) { |
| 1142 | Py_UNICODE ch2 = s[i+1]; |
| 1143 | |
| 1144 | if (SPECIAL(ch2, encodeSetO, encodeWhiteSpace)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1145 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1146 | } else if (B64CHAR(ch2) || ch2 == '-') { |
| 1147 | *out++ = '-'; |
| 1148 | inShift = 0; |
| 1149 | } else { |
| 1150 | inShift = 0; |
| 1151 | } |
| 1152 | |
| 1153 | } |
| 1154 | else { |
| 1155 | *out++ = '-'; |
| 1156 | inShift = 0; |
| 1157 | } |
| 1158 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1159 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1160 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1161 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1162 | if (bitsleft) { |
| 1163 | *out++= B64(charsleft << (6-bitsleft) ); |
| 1164 | *out++ = '-'; |
| 1165 | } |
| 1166 | |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 1167 | _PyString_Resize(&v, out - start); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1168 | return v; |
| 1169 | } |
| 1170 | |
| 1171 | #undef SPECIAL |
| 1172 | #undef B64 |
| 1173 | #undef B64CHAR |
| 1174 | #undef UB64 |
| 1175 | #undef ENCODE |
| 1176 | #undef DECODE |
| 1177 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1178 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 1179 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1180 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1181 | char utf8_code_length[256] = { |
| 1182 | /* Map UTF-8 encoded prefix byte to sequence length. zero means |
| 1183 | illegal prefix. see RFC 2279 for details */ |
| 1184 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1185 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1186 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1187 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1188 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1189 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1190 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1191 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1192 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1193 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1194 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1195 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1196 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1197 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1198 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 1199 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 |
| 1200 | }; |
| 1201 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1202 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1203 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1204 | const char *errors) |
| 1205 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1206 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 1207 | } |
| 1208 | |
| 1209 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1210 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1211 | const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1212 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1213 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1214 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1215 | int n; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1216 | Py_ssize_t startinpos; |
| 1217 | Py_ssize_t endinpos; |
| 1218 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1219 | const char *e; |
| 1220 | PyUnicodeObject *unicode; |
| 1221 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1222 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1223 | PyObject *errorHandler = NULL; |
| 1224 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1225 | |
| 1226 | /* Note: size will always be longer than the resulting Unicode |
| 1227 | character count */ |
| 1228 | unicode = _PyUnicode_New(size); |
| 1229 | if (!unicode) |
| 1230 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1231 | if (size == 0) { |
| 1232 | if (consumed) |
| 1233 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1234 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1235 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1236 | |
| 1237 | /* Unpack UTF-8 encoded data */ |
| 1238 | p = unicode->str; |
| 1239 | e = s + size; |
| 1240 | |
| 1241 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1242 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1243 | |
| 1244 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1245 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1246 | s++; |
| 1247 | continue; |
| 1248 | } |
| 1249 | |
| 1250 | n = utf8_code_length[ch]; |
| 1251 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1252 | if (s + n > e) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1253 | if (consumed) |
| 1254 | break; |
| 1255 | else { |
| 1256 | errmsg = "unexpected end of data"; |
| 1257 | startinpos = s-starts; |
| 1258 | endinpos = size; |
| 1259 | goto utf8Error; |
| 1260 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1261 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1262 | |
| 1263 | switch (n) { |
| 1264 | |
| 1265 | case 0: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1266 | errmsg = "unexpected code byte"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1267 | startinpos = s-starts; |
| 1268 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1269 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1270 | |
| 1271 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1272 | errmsg = "internal error"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1273 | startinpos = s-starts; |
| 1274 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1275 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1276 | |
| 1277 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1278 | if ((s[1] & 0xc0) != 0x80) { |
| 1279 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1280 | startinpos = s-starts; |
| 1281 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1282 | goto utf8Error; |
| 1283 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1284 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1285 | if (ch < 0x80) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1286 | startinpos = s-starts; |
| 1287 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1288 | errmsg = "illegal encoding"; |
| 1289 | goto utf8Error; |
| 1290 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1291 | else |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1292 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1293 | break; |
| 1294 | |
| 1295 | case 3: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1296 | if ((s[1] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1297 | (s[2] & 0xc0) != 0x80) { |
| 1298 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1299 | startinpos = s-starts; |
| 1300 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1301 | goto utf8Error; |
| 1302 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1303 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1304 | if (ch < 0x0800) { |
| 1305 | /* Note: UTF-8 encodings of surrogates are considered |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1306 | legal UTF-8 sequences; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1307 | |
| 1308 | XXX For wide builds (UCS-4) we should probably try |
| 1309 | to recombine the surrogates into a single code |
| 1310 | unit. |
| 1311 | */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1312 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1313 | startinpos = s-starts; |
| 1314 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1315 | goto utf8Error; |
| 1316 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1317 | else |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1318 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1319 | break; |
| 1320 | |
| 1321 | case 4: |
| 1322 | if ((s[1] & 0xc0) != 0x80 || |
| 1323 | (s[2] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1324 | (s[3] & 0xc0) != 0x80) { |
| 1325 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1326 | startinpos = s-starts; |
| 1327 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1328 | goto utf8Error; |
| 1329 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1330 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 1331 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 1332 | /* validate and convert to UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1333 | if ((ch < 0x10000) /* minimum value allowed for 4 |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1334 | byte encoding */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1335 | || (ch > 0x10ffff)) /* maximum value allowed for |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1336 | UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1337 | { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1338 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1339 | startinpos = s-starts; |
| 1340 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1341 | goto utf8Error; |
| 1342 | } |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1343 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1344 | *p++ = (Py_UNICODE)ch; |
| 1345 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1346 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1347 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1348 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 1349 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1350 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1351 | /* high surrogate = top 10 bits added to D800 */ |
| 1352 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1353 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1354 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 1355 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1356 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1357 | break; |
| 1358 | |
| 1359 | default: |
| 1360 | /* Other sizes are only needed for UCS-4 */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1361 | errmsg = "unsupported Unicode code range"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1362 | startinpos = s-starts; |
| 1363 | endinpos = startinpos+n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1364 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1365 | } |
| 1366 | s += n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1367 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1368 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1369 | utf8Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1370 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1371 | if (unicode_decode_call_errorhandler( |
| 1372 | errors, &errorHandler, |
| 1373 | "utf8", errmsg, |
| 1374 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1375 | (PyObject **)&unicode, &outpos, &p)) |
| 1376 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1377 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1378 | if (consumed) |
| 1379 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1380 | |
| 1381 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1382 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1383 | goto onError; |
| 1384 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1385 | Py_XDECREF(errorHandler); |
| 1386 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1387 | return (PyObject *)unicode; |
| 1388 | |
| 1389 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1390 | Py_XDECREF(errorHandler); |
| 1391 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1392 | Py_DECREF(unicode); |
| 1393 | return NULL; |
| 1394 | } |
| 1395 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1396 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 1397 | and allocate exactly as much space needed at the end. Else allocate the |
| 1398 | maximum possible needed (4 result bytes per Unicode character), and return |
| 1399 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1400 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1401 | PyObject * |
| 1402 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1403 | Py_ssize_t size, |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1404 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1405 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1406 | #define MAX_SHORT_UNICHARS 300 /* largest size we'll do on the stack */ |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 1407 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1408 | Py_ssize_t i; /* index into s of next input byte */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1409 | PyObject *v; /* result string object */ |
| 1410 | char *p; /* next free byte in output buffer */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1411 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 1412 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1413 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1414 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1415 | assert(s != NULL); |
| 1416 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1417 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1418 | if (size <= MAX_SHORT_UNICHARS) { |
| 1419 | /* Write into the stack buffer; nallocated can't overflow. |
| 1420 | * At the end, we'll allocate exactly as much heap space as it |
| 1421 | * turns out we need. |
| 1422 | */ |
| 1423 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
| 1424 | v = NULL; /* will allocate after we're done */ |
| 1425 | p = stackbuf; |
| 1426 | } |
| 1427 | else { |
| 1428 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 1429 | nallocated = size * 4; |
| 1430 | if (nallocated / 4 != size) /* overflow! */ |
| 1431 | return PyErr_NoMemory(); |
| 1432 | v = PyString_FromStringAndSize(NULL, nallocated); |
| 1433 | if (v == NULL) |
| 1434 | return NULL; |
| 1435 | p = PyString_AS_STRING(v); |
| 1436 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1437 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1438 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1439 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1440 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1441 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1442 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1443 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1444 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1445 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1446 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 1447 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 1448 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1449 | } |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1450 | else { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1451 | /* Encode UCS2 Unicode ordinals */ |
| 1452 | if (ch < 0x10000) { |
| 1453 | /* Special case: check for high surrogate */ |
| 1454 | if (0xD800 <= ch && ch <= 0xDBFF && i != size) { |
| 1455 | Py_UCS4 ch2 = s[i]; |
| 1456 | /* Check for low surrogate and combine the two to |
| 1457 | form a UCS4 value */ |
| 1458 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1459 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1460 | i++; |
| 1461 | goto encodeUCS4; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1462 | } |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1463 | /* Fall through: handles isolated high surrogates */ |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1464 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1465 | *p++ = (char)(0xe0 | (ch >> 12)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1466 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 1467 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 1468 | continue; |
| 1469 | } |
| 1470 | encodeUCS4: |
| 1471 | /* Encode UCS4 Unicode ordinals */ |
| 1472 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 1473 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 1474 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 1475 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 1476 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1477 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 1478 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1479 | if (v == NULL) { |
| 1480 | /* This was stack allocated. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 1481 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1482 | assert(nneeded <= nallocated); |
| 1483 | v = PyString_FromStringAndSize(stackbuf, nneeded); |
| 1484 | } |
| 1485 | else { |
| 1486 | /* Cut back to size actually needed. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 1487 | nneeded = p - PyString_AS_STRING(v); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1488 | assert(nneeded <= nallocated); |
| 1489 | _PyString_Resize(&v, nneeded); |
| 1490 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1491 | return v; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1492 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1493 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1494 | } |
| 1495 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1496 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 1497 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1498 | if (!PyUnicode_Check(unicode)) { |
| 1499 | PyErr_BadArgument(); |
| 1500 | return NULL; |
| 1501 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 1502 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 1503 | PyUnicode_GET_SIZE(unicode), |
| 1504 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 1508 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1509 | PyObject * |
| 1510 | PyUnicode_DecodeUTF16(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1511 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1512 | const char *errors, |
| 1513 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1514 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1515 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 1516 | } |
| 1517 | |
| 1518 | PyObject * |
| 1519 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1520 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1521 | const char *errors, |
| 1522 | int *byteorder, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1523 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1524 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1525 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1526 | Py_ssize_t startinpos; |
| 1527 | Py_ssize_t endinpos; |
| 1528 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1529 | PyUnicodeObject *unicode; |
| 1530 | Py_UNICODE *p; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1531 | const unsigned char *q, *e; |
| 1532 | int bo = 0; /* assume native ordering by default */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1533 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1534 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 1535 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 1536 | int ihi = 1, ilo = 0; |
| 1537 | #else |
| 1538 | int ihi = 0, ilo = 1; |
| 1539 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1540 | PyObject *errorHandler = NULL; |
| 1541 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1542 | |
| 1543 | /* Note: size will always be longer than the resulting Unicode |
| 1544 | character count */ |
| 1545 | unicode = _PyUnicode_New(size); |
| 1546 | if (!unicode) |
| 1547 | return NULL; |
| 1548 | if (size == 0) |
| 1549 | return (PyObject *)unicode; |
| 1550 | |
| 1551 | /* Unpack UTF-16 encoded data */ |
| 1552 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1553 | q = (unsigned char *)s; |
| 1554 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1555 | |
| 1556 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1557 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1558 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1559 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 1560 | byte order setting accordingly. In native mode, the leading BOM |
| 1561 | mark is skipped, in all other modes, it is copied to the output |
| 1562 | stream as-is (giving a ZWNBSP character). */ |
| 1563 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1564 | if (size >= 2) { |
| 1565 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1566 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1567 | if (bom == 0xFEFF) { |
| 1568 | q += 2; |
| 1569 | bo = -1; |
| 1570 | } |
| 1571 | else if (bom == 0xFFFE) { |
| 1572 | q += 2; |
| 1573 | bo = 1; |
| 1574 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1575 | #else |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1576 | if (bom == 0xFEFF) { |
| 1577 | q += 2; |
| 1578 | bo = 1; |
| 1579 | } |
| 1580 | else if (bom == 0xFFFE) { |
| 1581 | q += 2; |
| 1582 | bo = -1; |
| 1583 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1584 | #endif |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1585 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1586 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1587 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1588 | if (bo == -1) { |
| 1589 | /* force LE */ |
| 1590 | ihi = 1; |
| 1591 | ilo = 0; |
| 1592 | } |
| 1593 | else if (bo == 1) { |
| 1594 | /* force BE */ |
| 1595 | ihi = 0; |
| 1596 | ilo = 1; |
| 1597 | } |
| 1598 | |
| 1599 | while (q < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1600 | Py_UNICODE ch; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1601 | /* remaining bytes at the end? (size should be even) */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1602 | if (e-q<2) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1603 | if (consumed) |
| 1604 | break; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1605 | errmsg = "truncated data"; |
| 1606 | startinpos = ((const char *)q)-starts; |
| 1607 | endinpos = ((const char *)e)-starts; |
| 1608 | goto utf16Error; |
| 1609 | /* The remaining input chars are ignored if the callback |
| 1610 | chooses to skip the input */ |
| 1611 | } |
| 1612 | ch = (q[ihi] << 8) | q[ilo]; |
| 1613 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1614 | q += 2; |
| 1615 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1616 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 1617 | *p++ = ch; |
| 1618 | continue; |
| 1619 | } |
| 1620 | |
| 1621 | /* UTF-16 code pair: */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1622 | if (q >= e) { |
| 1623 | errmsg = "unexpected end of data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1624 | startinpos = (((const char *)q)-2)-starts; |
| 1625 | endinpos = ((const char *)e)-starts; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1626 | goto utf16Error; |
| 1627 | } |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 1628 | if (0xD800 <= ch && ch <= 0xDBFF) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1629 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 1630 | q += 2; |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 1631 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1632 | #ifndef Py_UNICODE_WIDE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1633 | *p++ = ch; |
| 1634 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1635 | #else |
| 1636 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1637 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1638 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1639 | } |
| 1640 | else { |
| 1641 | errmsg = "illegal UTF-16 surrogate"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1642 | startinpos = (((const char *)q)-4)-starts; |
| 1643 | endinpos = startinpos+2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1644 | goto utf16Error; |
| 1645 | } |
| 1646 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1647 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1648 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1649 | startinpos = (((const char *)q)-2)-starts; |
| 1650 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1651 | /* Fall through to report the error */ |
| 1652 | |
| 1653 | utf16Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1654 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1655 | if (unicode_decode_call_errorhandler( |
| 1656 | errors, &errorHandler, |
| 1657 | "utf16", errmsg, |
| 1658 | starts, size, &startinpos, &endinpos, &exc, (const char **)&q, |
| 1659 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1660 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1661 | } |
| 1662 | |
| 1663 | if (byteorder) |
| 1664 | *byteorder = bo; |
| 1665 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1666 | if (consumed) |
| 1667 | *consumed = (const char *)q-starts; |
| 1668 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1669 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1670 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1671 | goto onError; |
| 1672 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1673 | Py_XDECREF(errorHandler); |
| 1674 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1675 | return (PyObject *)unicode; |
| 1676 | |
| 1677 | onError: |
| 1678 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1679 | Py_XDECREF(errorHandler); |
| 1680 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1681 | return NULL; |
| 1682 | } |
| 1683 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1684 | PyObject * |
| 1685 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1686 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1687 | const char *errors, |
| 1688 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1689 | { |
| 1690 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1691 | unsigned char *p; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1692 | #ifdef Py_UNICODE_WIDE |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1693 | int i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1694 | #else |
| 1695 | const int pairs = 0; |
| 1696 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1697 | /* Offsets from p for storing byte pairs in the right order. */ |
| 1698 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 1699 | int ihi = 1, ilo = 0; |
| 1700 | #else |
| 1701 | int ihi = 0, ilo = 1; |
| 1702 | #endif |
| 1703 | |
| 1704 | #define STORECHAR(CH) \ |
| 1705 | do { \ |
| 1706 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 1707 | p[ilo] = (CH) & 0xff; \ |
| 1708 | p += 2; \ |
| 1709 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1710 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1711 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1712 | for (i = pairs = 0; i < size; i++) |
| 1713 | if (s[i] >= 0x10000) |
| 1714 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1715 | #endif |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1716 | v = PyString_FromStringAndSize(NULL, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1717 | 2 * (size + pairs + (byteorder == 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1718 | if (v == NULL) |
| 1719 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1720 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1721 | p = (unsigned char *)PyString_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1722 | if (byteorder == 0) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1723 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 1724 | if (size == 0) |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 1725 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1726 | |
| 1727 | if (byteorder == -1) { |
| 1728 | /* force LE */ |
| 1729 | ihi = 1; |
| 1730 | ilo = 0; |
| 1731 | } |
| 1732 | else if (byteorder == 1) { |
| 1733 | /* force BE */ |
| 1734 | ihi = 0; |
| 1735 | ilo = 1; |
| 1736 | } |
| 1737 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1738 | while (size-- > 0) { |
| 1739 | Py_UNICODE ch = *s++; |
| 1740 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1741 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1742 | if (ch >= 0x10000) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1743 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 1744 | ch = 0xD800 | ((ch-0x10000) >> 10); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1745 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1746 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1747 | STORECHAR(ch); |
| 1748 | if (ch2) |
| 1749 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1750 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1751 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1752 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1753 | } |
| 1754 | |
| 1755 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 1756 | { |
| 1757 | if (!PyUnicode_Check(unicode)) { |
| 1758 | PyErr_BadArgument(); |
| 1759 | return NULL; |
| 1760 | } |
| 1761 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
| 1762 | PyUnicode_GET_SIZE(unicode), |
| 1763 | NULL, |
| 1764 | 0); |
| 1765 | } |
| 1766 | |
| 1767 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 1768 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1769 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 1770 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1771 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1772 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1773 | const char *errors) |
| 1774 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1775 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1776 | Py_ssize_t startinpos; |
| 1777 | Py_ssize_t endinpos; |
| 1778 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1779 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1780 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1781 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1782 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1783 | char* message; |
| 1784 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1785 | PyObject *errorHandler = NULL; |
| 1786 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1787 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1788 | /* Escaped strings will always be longer than the resulting |
| 1789 | 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] | 1790 | length after conversion to the true value. |
| 1791 | (but if the error callback returns a long replacement string |
| 1792 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1793 | v = _PyUnicode_New(size); |
| 1794 | if (v == NULL) |
| 1795 | goto onError; |
| 1796 | if (size == 0) |
| 1797 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1798 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1799 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1800 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1801 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1802 | while (s < end) { |
| 1803 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 1804 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1805 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1806 | |
| 1807 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 1808 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1809 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1810 | continue; |
| 1811 | } |
| 1812 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1813 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1814 | /* \ - Escapes */ |
| 1815 | s++; |
| 1816 | switch (*s++) { |
| 1817 | |
| 1818 | /* \x escapes */ |
| 1819 | case '\n': break; |
| 1820 | case '\\': *p++ = '\\'; break; |
| 1821 | case '\'': *p++ = '\''; break; |
| 1822 | case '\"': *p++ = '\"'; break; |
| 1823 | case 'b': *p++ = '\b'; break; |
| 1824 | case 'f': *p++ = '\014'; break; /* FF */ |
| 1825 | case 't': *p++ = '\t'; break; |
| 1826 | case 'n': *p++ = '\n'; break; |
| 1827 | case 'r': *p++ = '\r'; break; |
| 1828 | case 'v': *p++ = '\013'; break; /* VT */ |
| 1829 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 1830 | |
| 1831 | /* \OOO (octal) escapes */ |
| 1832 | case '0': case '1': case '2': case '3': |
| 1833 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1834 | x = s[-1] - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1835 | if ('0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1836 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1837 | if ('0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1838 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1839 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1840 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1841 | break; |
| 1842 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1843 | /* hex escapes */ |
| 1844 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1845 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1846 | digits = 2; |
| 1847 | message = "truncated \\xXX escape"; |
| 1848 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1849 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1850 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1851 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1852 | digits = 4; |
| 1853 | message = "truncated \\uXXXX escape"; |
| 1854 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1855 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1856 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1857 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1858 | digits = 8; |
| 1859 | message = "truncated \\UXXXXXXXX escape"; |
| 1860 | hexescape: |
| 1861 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1862 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1863 | if (s+digits>end) { |
| 1864 | endinpos = size; |
| 1865 | if (unicode_decode_call_errorhandler( |
| 1866 | errors, &errorHandler, |
| 1867 | "unicodeescape", "end of string in escape sequence", |
| 1868 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1869 | (PyObject **)&v, &outpos, &p)) |
| 1870 | goto onError; |
| 1871 | goto nextByte; |
| 1872 | } |
| 1873 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1874 | c = (unsigned char) s[i]; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1875 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1876 | endinpos = (s+i+1)-starts; |
| 1877 | if (unicode_decode_call_errorhandler( |
| 1878 | errors, &errorHandler, |
| 1879 | "unicodeescape", message, |
| 1880 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1881 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1882 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1883 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1884 | } |
| 1885 | chr = (chr<<4) & ~0xF; |
| 1886 | if (c >= '0' && c <= '9') |
| 1887 | chr += c - '0'; |
| 1888 | else if (c >= 'a' && c <= 'f') |
| 1889 | chr += 10 + c - 'a'; |
| 1890 | else |
| 1891 | chr += 10 + c - 'A'; |
| 1892 | } |
| 1893 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 1894 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1895 | /* _decoding_error will have already written into the |
| 1896 | target buffer. */ |
| 1897 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1898 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1899 | /* when we get here, chr is a 32-bit unicode character */ |
| 1900 | if (chr <= 0xffff) |
| 1901 | /* UCS-2 character */ |
| 1902 | *p++ = (Py_UNICODE) chr; |
| 1903 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1904 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1905 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1906 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1907 | *p++ = chr; |
| 1908 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1909 | chr -= 0x10000L; |
| 1910 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 1911 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1912 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1913 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1914 | endinpos = s-starts; |
| 1915 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1916 | if (unicode_decode_call_errorhandler( |
| 1917 | errors, &errorHandler, |
| 1918 | "unicodeescape", "illegal Unicode character", |
| 1919 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1920 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1921 | goto onError; |
| 1922 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1923 | break; |
| 1924 | |
| 1925 | /* \N{name} */ |
| 1926 | case 'N': |
| 1927 | message = "malformed \\N character escape"; |
| 1928 | if (ucnhash_CAPI == NULL) { |
| 1929 | /* load the unicode data module */ |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 1930 | PyObject *m, *api; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1931 | m = PyImport_ImportModule("unicodedata"); |
| 1932 | if (m == NULL) |
| 1933 | goto ucnhashError; |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 1934 | api = PyObject_GetAttrString(m, "ucnhash_CAPI"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1935 | Py_DECREF(m); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 1936 | if (api == NULL) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1937 | goto ucnhashError; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 1938 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCObject_AsVoidPtr(api); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 1939 | Py_DECREF(api); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1940 | if (ucnhash_CAPI == NULL) |
| 1941 | goto ucnhashError; |
| 1942 | } |
| 1943 | if (*s == '{') { |
| 1944 | const char *start = s+1; |
| 1945 | /* look for the closing brace */ |
| 1946 | while (*s != '}' && s < end) |
| 1947 | s++; |
| 1948 | if (s > start && s < end && *s == '}') { |
| 1949 | /* found a name. look it up in the unicode database */ |
| 1950 | message = "unknown Unicode character name"; |
| 1951 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 1952 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1953 | goto store; |
| 1954 | } |
| 1955 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1956 | endinpos = s-starts; |
| 1957 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1958 | if (unicode_decode_call_errorhandler( |
| 1959 | errors, &errorHandler, |
| 1960 | "unicodeescape", message, |
| 1961 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1962 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1963 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1964 | break; |
| 1965 | |
| 1966 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1967 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1968 | message = "\\ at end of string"; |
| 1969 | s--; |
| 1970 | endinpos = s-starts; |
| 1971 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1972 | if (unicode_decode_call_errorhandler( |
| 1973 | errors, &errorHandler, |
| 1974 | "unicodeescape", message, |
| 1975 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1976 | (PyObject **)&v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1977 | goto onError; |
| 1978 | } |
| 1979 | else { |
| 1980 | *p++ = '\\'; |
| 1981 | *p++ = (unsigned char)s[-1]; |
| 1982 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1983 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1984 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1985 | nextByte: |
| 1986 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1987 | } |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 1988 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1989 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 1990 | Py_XDECREF(errorHandler); |
| 1991 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1992 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1993 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1994 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1995 | PyErr_SetString( |
| 1996 | PyExc_UnicodeError, |
| 1997 | "\\N escapes not supported (can't load unicodedata module)" |
| 1998 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 1999 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2000 | Py_XDECREF(errorHandler); |
| 2001 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 2002 | return NULL; |
| 2003 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2004 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2005 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2006 | Py_XDECREF(errorHandler); |
| 2007 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2008 | return NULL; |
| 2009 | } |
| 2010 | |
| 2011 | /* Return a Unicode-Escape string version of the Unicode object. |
| 2012 | |
| 2013 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 2014 | appropriate. |
| 2015 | |
| 2016 | */ |
| 2017 | |
Fredrik Lundh | 95e2a91 | 2006-05-26 11:38:15 +0000 | [diff] [blame] | 2018 | Py_LOCAL(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
| 2019 | Py_ssize_t size, |
| 2020 | Py_UNICODE ch) |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 2021 | { |
| 2022 | /* like wcschr, but doesn't stop at NULL characters */ |
| 2023 | |
| 2024 | while (size-- > 0) { |
| 2025 | if (*s == ch) |
| 2026 | return s; |
| 2027 | s++; |
| 2028 | } |
| 2029 | |
| 2030 | return NULL; |
| 2031 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 2032 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2033 | static |
| 2034 | PyObject *unicodeescape_string(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2035 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2036 | int quotes) |
| 2037 | { |
| 2038 | PyObject *repr; |
| 2039 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2040 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2041 | static const char *hexdigit = "0123456789abcdef"; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2042 | |
| 2043 | repr = PyString_FromStringAndSize(NULL, 2 + 6*size + 1); |
| 2044 | if (repr == NULL) |
| 2045 | return NULL; |
| 2046 | |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2047 | p = PyString_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2048 | |
| 2049 | if (quotes) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2050 | *p++ = 'u'; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2051 | *p++ = (findchar(s, size, '\'') && |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2052 | !findchar(s, size, '"')) ? '"' : '\''; |
| 2053 | } |
| 2054 | while (size-- > 0) { |
| 2055 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2056 | |
Hye-Shik Chang | 835b243 | 2005-12-17 04:38:31 +0000 | [diff] [blame] | 2057 | /* Escape quotes and backslashes */ |
| 2058 | if ((quotes && |
| 2059 | ch == (Py_UNICODE) PyString_AS_STRING(repr)[1]) || ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2060 | *p++ = '\\'; |
| 2061 | *p++ = (char) ch; |
Guido van Rossum | ad9744a | 2001-09-21 15:38:17 +0000 | [diff] [blame] | 2062 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2063 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2064 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 2065 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2066 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 2067 | else if (ch >= 0x10000) { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2068 | Py_ssize_t offset = p - PyString_AS_STRING(repr); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2069 | |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2070 | /* Resize the string if necessary */ |
| 2071 | if (offset + 12 > PyString_GET_SIZE(repr)) { |
| 2072 | if (_PyString_Resize(&repr, PyString_GET_SIZE(repr) + 100)) |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 2073 | return NULL; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2074 | p = PyString_AS_STRING(repr) + offset; |
| 2075 | } |
| 2076 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2077 | *p++ = '\\'; |
| 2078 | *p++ = 'U'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2079 | *p++ = hexdigit[(ch >> 28) & 0x0000000F]; |
| 2080 | *p++ = hexdigit[(ch >> 24) & 0x0000000F]; |
| 2081 | *p++ = hexdigit[(ch >> 20) & 0x0000000F]; |
| 2082 | *p++ = hexdigit[(ch >> 16) & 0x0000000F]; |
| 2083 | *p++ = hexdigit[(ch >> 12) & 0x0000000F]; |
| 2084 | *p++ = hexdigit[(ch >> 8) & 0x0000000F]; |
| 2085 | *p++ = hexdigit[(ch >> 4) & 0x0000000F]; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2086 | *p++ = hexdigit[ch & 0x0000000F]; |
| 2087 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2088 | } |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 2089 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2090 | /* Map UTF-16 surrogate pairs to Unicode \UXXXXXXXX escapes */ |
| 2091 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 2092 | Py_UNICODE ch2; |
| 2093 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2094 | |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2095 | ch2 = *s++; |
| 2096 | size--; |
| 2097 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 2098 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 2099 | *p++ = '\\'; |
| 2100 | *p++ = 'U'; |
| 2101 | *p++ = hexdigit[(ucs >> 28) & 0x0000000F]; |
| 2102 | *p++ = hexdigit[(ucs >> 24) & 0x0000000F]; |
| 2103 | *p++ = hexdigit[(ucs >> 20) & 0x0000000F]; |
| 2104 | *p++ = hexdigit[(ucs >> 16) & 0x0000000F]; |
| 2105 | *p++ = hexdigit[(ucs >> 12) & 0x0000000F]; |
| 2106 | *p++ = hexdigit[(ucs >> 8) & 0x0000000F]; |
| 2107 | *p++ = hexdigit[(ucs >> 4) & 0x0000000F]; |
| 2108 | *p++ = hexdigit[ucs & 0x0000000F]; |
| 2109 | continue; |
| 2110 | } |
| 2111 | /* Fall through: isolated surrogates are copied as-is */ |
| 2112 | s--; |
| 2113 | size++; |
| 2114 | } |
| 2115 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2116 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2117 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2118 | *p++ = '\\'; |
| 2119 | *p++ = 'u'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2120 | *p++ = hexdigit[(ch >> 12) & 0x000F]; |
| 2121 | *p++ = hexdigit[(ch >> 8) & 0x000F]; |
| 2122 | *p++ = hexdigit[(ch >> 4) & 0x000F]; |
| 2123 | *p++ = hexdigit[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2124 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2125 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2126 | /* Map special whitespace to '\t', \n', '\r' */ |
| 2127 | else if (ch == '\t') { |
| 2128 | *p++ = '\\'; |
| 2129 | *p++ = 't'; |
| 2130 | } |
| 2131 | else if (ch == '\n') { |
| 2132 | *p++ = '\\'; |
| 2133 | *p++ = 'n'; |
| 2134 | } |
| 2135 | else if (ch == '\r') { |
| 2136 | *p++ = '\\'; |
| 2137 | *p++ = 'r'; |
| 2138 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2139 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2140 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 2141 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2142 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2143 | *p++ = 'x'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2144 | *p++ = hexdigit[(ch >> 4) & 0x000F]; |
| 2145 | *p++ = hexdigit[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2146 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2147 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2148 | /* Copy everything else as-is */ |
| 2149 | else |
| 2150 | *p++ = (char) ch; |
| 2151 | } |
| 2152 | if (quotes) |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2153 | *p++ = PyString_AS_STRING(repr)[1]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2154 | |
| 2155 | *p = '\0'; |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 2156 | _PyString_Resize(&repr, p - PyString_AS_STRING(repr)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2157 | return repr; |
| 2158 | } |
| 2159 | |
| 2160 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2161 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2162 | { |
| 2163 | return unicodeescape_string(s, size, 0); |
| 2164 | } |
| 2165 | |
| 2166 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
| 2167 | { |
| 2168 | if (!PyUnicode_Check(unicode)) { |
| 2169 | PyErr_BadArgument(); |
| 2170 | return NULL; |
| 2171 | } |
| 2172 | return PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 2173 | PyUnicode_GET_SIZE(unicode)); |
| 2174 | } |
| 2175 | |
| 2176 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 2177 | |
| 2178 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2179 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2180 | const char *errors) |
| 2181 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2182 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2183 | Py_ssize_t startinpos; |
| 2184 | Py_ssize_t endinpos; |
| 2185 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2186 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2187 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2188 | const char *end; |
| 2189 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2190 | PyObject *errorHandler = NULL; |
| 2191 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2192 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2193 | /* Escaped strings will always be longer than the resulting |
| 2194 | 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] | 2195 | length after conversion to the true value. (But decoding error |
| 2196 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2197 | v = _PyUnicode_New(size); |
| 2198 | if (v == NULL) |
| 2199 | goto onError; |
| 2200 | if (size == 0) |
| 2201 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2202 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2203 | end = s + size; |
| 2204 | while (s < end) { |
| 2205 | unsigned char c; |
Martin v. Löwis | 047c05e | 2002-03-21 08:55:28 +0000 | [diff] [blame] | 2206 | Py_UCS4 x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2207 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2208 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2209 | |
| 2210 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 2211 | if (*s != '\\') { |
| 2212 | *p++ = (unsigned char)*s++; |
| 2213 | continue; |
| 2214 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2215 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2216 | |
| 2217 | /* \u-escapes are only interpreted iff the number of leading |
| 2218 | backslashes if odd */ |
| 2219 | bs = s; |
| 2220 | for (;s < end;) { |
| 2221 | if (*s != '\\') |
| 2222 | break; |
| 2223 | *p++ = (unsigned char)*s++; |
| 2224 | } |
| 2225 | if (((s - bs) & 1) == 0 || |
| 2226 | s >= end || |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2227 | (*s != 'u' && *s != 'U')) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2228 | continue; |
| 2229 | } |
| 2230 | p--; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2231 | count = *s=='u' ? 4 : 8; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2232 | s++; |
| 2233 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2234 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2235 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2236 | for (x = 0, i = 0; i < count; ++i, ++s) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2237 | c = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2238 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2239 | endinpos = s-starts; |
| 2240 | if (unicode_decode_call_errorhandler( |
| 2241 | errors, &errorHandler, |
| 2242 | "rawunicodeescape", "truncated \\uXXXX", |
| 2243 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2244 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2245 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2246 | goto nextByte; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2247 | } |
| 2248 | x = (x<<4) & ~0xF; |
| 2249 | if (c >= '0' && c <= '9') |
| 2250 | x += c - '0'; |
| 2251 | else if (c >= 'a' && c <= 'f') |
| 2252 | x += 10 + c - 'a'; |
| 2253 | else |
| 2254 | x += 10 + c - 'A'; |
| 2255 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2256 | #ifndef Py_UNICODE_WIDE |
| 2257 | if (x > 0x10000) { |
| 2258 | if (unicode_decode_call_errorhandler( |
| 2259 | errors, &errorHandler, |
| 2260 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
| 2261 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2262 | (PyObject **)&v, &outpos, &p)) |
| 2263 | goto onError; |
| 2264 | } |
| 2265 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2266 | *p++ = x; |
| 2267 | nextByte: |
| 2268 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2269 | } |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2270 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 2271 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2272 | Py_XDECREF(errorHandler); |
| 2273 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2274 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2275 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2276 | onError: |
| 2277 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2278 | Py_XDECREF(errorHandler); |
| 2279 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2280 | return NULL; |
| 2281 | } |
| 2282 | |
| 2283 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2284 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2285 | { |
| 2286 | PyObject *repr; |
| 2287 | char *p; |
| 2288 | char *q; |
| 2289 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2290 | static const char *hexdigit = "0123456789abcdef"; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2291 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2292 | #ifdef Py_UNICODE_WIDE |
| 2293 | repr = PyString_FromStringAndSize(NULL, 10 * size); |
| 2294 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2295 | repr = PyString_FromStringAndSize(NULL, 6 * size); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2296 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2297 | if (repr == NULL) |
| 2298 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 2299 | if (size == 0) |
| 2300 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2301 | |
| 2302 | p = q = PyString_AS_STRING(repr); |
| 2303 | while (size-- > 0) { |
| 2304 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2305 | #ifdef Py_UNICODE_WIDE |
| 2306 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 2307 | if (ch >= 0x10000) { |
| 2308 | *p++ = '\\'; |
| 2309 | *p++ = 'U'; |
| 2310 | *p++ = hexdigit[(ch >> 28) & 0xf]; |
| 2311 | *p++ = hexdigit[(ch >> 24) & 0xf]; |
| 2312 | *p++ = hexdigit[(ch >> 20) & 0xf]; |
| 2313 | *p++ = hexdigit[(ch >> 16) & 0xf]; |
| 2314 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 2315 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 2316 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 2317 | *p++ = hexdigit[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2318 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2319 | else |
| 2320 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2321 | /* Map 16-bit characters to '\uxxxx' */ |
| 2322 | if (ch >= 256) { |
| 2323 | *p++ = '\\'; |
| 2324 | *p++ = 'u'; |
| 2325 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 2326 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 2327 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 2328 | *p++ = hexdigit[ch & 15]; |
| 2329 | } |
| 2330 | /* Copy everything else as-is */ |
| 2331 | else |
| 2332 | *p++ = (char) ch; |
| 2333 | } |
| 2334 | *p = '\0'; |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 2335 | _PyString_Resize(&repr, p - q); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2336 | return repr; |
| 2337 | } |
| 2338 | |
| 2339 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 2340 | { |
| 2341 | if (!PyUnicode_Check(unicode)) { |
| 2342 | PyErr_BadArgument(); |
| 2343 | return NULL; |
| 2344 | } |
| 2345 | return PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 2346 | PyUnicode_GET_SIZE(unicode)); |
| 2347 | } |
| 2348 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2349 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 2350 | |
| 2351 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2352 | Py_ssize_t size, |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2353 | const char *errors) |
| 2354 | { |
| 2355 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2356 | Py_ssize_t startinpos; |
| 2357 | Py_ssize_t endinpos; |
| 2358 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2359 | PyUnicodeObject *v; |
| 2360 | Py_UNICODE *p; |
| 2361 | const char *end; |
| 2362 | const char *reason; |
| 2363 | PyObject *errorHandler = NULL; |
| 2364 | PyObject *exc = NULL; |
| 2365 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 2366 | #ifdef Py_UNICODE_WIDE |
| 2367 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 2368 | #endif |
| 2369 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2370 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 2371 | if (v == NULL) |
| 2372 | goto onError; |
| 2373 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
| 2374 | return (PyObject *)v; |
| 2375 | p = PyUnicode_AS_UNICODE(v); |
| 2376 | end = s + size; |
| 2377 | |
| 2378 | while (s < end) { |
Neal Norwitz | 1004a53 | 2006-05-15 07:17:23 +0000 | [diff] [blame] | 2379 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2380 | /* We have to sanity check the raw data, otherwise doom looms for |
| 2381 | some malformed UCS-4 data. */ |
| 2382 | if ( |
| 2383 | #ifdef Py_UNICODE_WIDE |
| 2384 | *p > unimax || *p < 0 || |
| 2385 | #endif |
| 2386 | end-s < Py_UNICODE_SIZE |
| 2387 | ) |
| 2388 | { |
| 2389 | startinpos = s - starts; |
| 2390 | if (end-s < Py_UNICODE_SIZE) { |
| 2391 | endinpos = end-starts; |
| 2392 | reason = "truncated input"; |
| 2393 | } |
| 2394 | else { |
| 2395 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 2396 | reason = "illegal code point (> 0x10FFFF)"; |
| 2397 | } |
| 2398 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 2399 | if (unicode_decode_call_errorhandler( |
| 2400 | errors, &errorHandler, |
| 2401 | "unicode_internal", reason, |
| 2402 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2403 | (PyObject **)&v, &outpos, &p)) { |
| 2404 | goto onError; |
| 2405 | } |
| 2406 | } |
| 2407 | else { |
| 2408 | p++; |
| 2409 | s += Py_UNICODE_SIZE; |
| 2410 | } |
| 2411 | } |
| 2412 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2413 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2414 | goto onError; |
| 2415 | Py_XDECREF(errorHandler); |
| 2416 | Py_XDECREF(exc); |
| 2417 | return (PyObject *)v; |
| 2418 | |
| 2419 | onError: |
| 2420 | Py_XDECREF(v); |
| 2421 | Py_XDECREF(errorHandler); |
| 2422 | Py_XDECREF(exc); |
| 2423 | return NULL; |
| 2424 | } |
| 2425 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2426 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 2427 | |
| 2428 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2429 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2430 | const char *errors) |
| 2431 | { |
| 2432 | PyUnicodeObject *v; |
| 2433 | Py_UNICODE *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2434 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2435 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2436 | if (size == 1) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 2437 | Py_UNICODE r = *(unsigned char*)s; |
| 2438 | return PyUnicode_FromUnicode(&r, 1); |
| 2439 | } |
| 2440 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2441 | v = _PyUnicode_New(size); |
| 2442 | if (v == NULL) |
| 2443 | goto onError; |
| 2444 | if (size == 0) |
| 2445 | return (PyObject *)v; |
| 2446 | p = PyUnicode_AS_UNICODE(v); |
| 2447 | while (size-- > 0) |
| 2448 | *p++ = (unsigned char)*s++; |
| 2449 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2450 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2451 | onError: |
| 2452 | Py_XDECREF(v); |
| 2453 | return NULL; |
| 2454 | } |
| 2455 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2456 | /* create or adjust a UnicodeEncodeError */ |
| 2457 | static void make_encode_exception(PyObject **exceptionObject, |
| 2458 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2459 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 2460 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2461 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2462 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2463 | if (*exceptionObject == NULL) { |
| 2464 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 2465 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2466 | } |
| 2467 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2468 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 2469 | goto onError; |
| 2470 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 2471 | goto onError; |
| 2472 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 2473 | goto onError; |
| 2474 | return; |
| 2475 | onError: |
| 2476 | Py_DECREF(*exceptionObject); |
| 2477 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2478 | } |
| 2479 | } |
| 2480 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2481 | /* raises a UnicodeEncodeError */ |
| 2482 | static void raise_encode_exception(PyObject **exceptionObject, |
| 2483 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2484 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 2485 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2486 | const char *reason) |
| 2487 | { |
| 2488 | make_encode_exception(exceptionObject, |
| 2489 | encoding, unicode, size, startpos, endpos, reason); |
| 2490 | if (*exceptionObject != NULL) |
| 2491 | PyCodec_StrictErrors(*exceptionObject); |
| 2492 | } |
| 2493 | |
| 2494 | /* error handling callback helper: |
| 2495 | build arguments, call the callback and check the arguments, |
| 2496 | put the result into newpos and return the replacement string, which |
| 2497 | has to be freed by the caller */ |
| 2498 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
| 2499 | PyObject **errorHandler, |
| 2500 | const char *encoding, const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2501 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 2502 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 2503 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2504 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2505 | 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] | 2506 | |
| 2507 | PyObject *restuple; |
| 2508 | PyObject *resunicode; |
| 2509 | |
| 2510 | if (*errorHandler == NULL) { |
| 2511 | *errorHandler = PyCodec_LookupError(errors); |
| 2512 | if (*errorHandler == NULL) |
| 2513 | return NULL; |
| 2514 | } |
| 2515 | |
| 2516 | make_encode_exception(exceptionObject, |
| 2517 | encoding, unicode, size, startpos, endpos, reason); |
| 2518 | if (*exceptionObject == NULL) |
| 2519 | return NULL; |
| 2520 | |
| 2521 | restuple = PyObject_CallFunctionObjArgs( |
| 2522 | *errorHandler, *exceptionObject, NULL); |
| 2523 | if (restuple == NULL) |
| 2524 | return NULL; |
| 2525 | if (!PyTuple_Check(restuple)) { |
| 2526 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 2527 | Py_DECREF(restuple); |
| 2528 | return NULL; |
| 2529 | } |
| 2530 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
| 2531 | &resunicode, newpos)) { |
| 2532 | Py_DECREF(restuple); |
| 2533 | return NULL; |
| 2534 | } |
| 2535 | if (*newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 2536 | *newpos = size+*newpos; |
| 2537 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 2538 | 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] | 2539 | Py_DECREF(restuple); |
| 2540 | return NULL; |
| 2541 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2542 | Py_INCREF(resunicode); |
| 2543 | Py_DECREF(restuple); |
| 2544 | return resunicode; |
| 2545 | } |
| 2546 | |
| 2547 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2548 | Py_ssize_t size, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2549 | const char *errors, |
| 2550 | int limit) |
| 2551 | { |
| 2552 | /* output object */ |
| 2553 | PyObject *res; |
| 2554 | /* pointers to the beginning and end+1 of input */ |
| 2555 | const Py_UNICODE *startp = p; |
| 2556 | const Py_UNICODE *endp = p + size; |
| 2557 | /* pointer to the beginning of the unencodable characters */ |
| 2558 | /* const Py_UNICODE *badp = NULL; */ |
| 2559 | /* pointer into the output */ |
| 2560 | char *str; |
| 2561 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2562 | Py_ssize_t respos = 0; |
| 2563 | Py_ssize_t ressize; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2564 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 2565 | 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] | 2566 | PyObject *errorHandler = NULL; |
| 2567 | PyObject *exc = NULL; |
| 2568 | /* the following variable is used for caching string comparisons |
| 2569 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 2570 | int known_errorHandler = -1; |
| 2571 | |
| 2572 | /* allocate enough for a simple encoding without |
| 2573 | replacements, if we need more, we'll resize */ |
| 2574 | res = PyString_FromStringAndSize(NULL, size); |
| 2575 | if (res == NULL) |
| 2576 | goto onError; |
| 2577 | if (size == 0) |
| 2578 | return res; |
| 2579 | str = PyString_AS_STRING(res); |
| 2580 | ressize = size; |
| 2581 | |
| 2582 | while (p<endp) { |
| 2583 | Py_UNICODE c = *p; |
| 2584 | |
| 2585 | /* can we encode this? */ |
| 2586 | if (c<limit) { |
| 2587 | /* no overflow check, because we know that the space is enough */ |
| 2588 | *str++ = (char)c; |
| 2589 | ++p; |
| 2590 | } |
| 2591 | else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2592 | Py_ssize_t unicodepos = p-startp; |
| 2593 | Py_ssize_t requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2594 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2595 | Py_ssize_t repsize; |
| 2596 | Py_ssize_t newpos; |
| 2597 | Py_ssize_t respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2598 | Py_UNICODE *uni2; |
| 2599 | /* startpos for collecting unencodable chars */ |
| 2600 | const Py_UNICODE *collstart = p; |
| 2601 | const Py_UNICODE *collend = p; |
| 2602 | /* find all unecodable characters */ |
| 2603 | while ((collend < endp) && ((*collend)>=limit)) |
| 2604 | ++collend; |
| 2605 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 2606 | if (known_errorHandler==-1) { |
| 2607 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 2608 | known_errorHandler = 1; |
| 2609 | else if (!strcmp(errors, "replace")) |
| 2610 | known_errorHandler = 2; |
| 2611 | else if (!strcmp(errors, "ignore")) |
| 2612 | known_errorHandler = 3; |
| 2613 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 2614 | known_errorHandler = 4; |
| 2615 | else |
| 2616 | known_errorHandler = 0; |
| 2617 | } |
| 2618 | switch (known_errorHandler) { |
| 2619 | case 1: /* strict */ |
| 2620 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 2621 | goto onError; |
| 2622 | case 2: /* replace */ |
| 2623 | while (collstart++<collend) |
| 2624 | *str++ = '?'; /* fall through */ |
| 2625 | case 3: /* ignore */ |
| 2626 | p = collend; |
| 2627 | break; |
| 2628 | case 4: /* xmlcharrefreplace */ |
| 2629 | respos = str-PyString_AS_STRING(res); |
| 2630 | /* determine replacement size (temporarily (mis)uses p) */ |
| 2631 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 2632 | if (*p<10) |
| 2633 | repsize += 2+1+1; |
| 2634 | else if (*p<100) |
| 2635 | repsize += 2+2+1; |
| 2636 | else if (*p<1000) |
| 2637 | repsize += 2+3+1; |
| 2638 | else if (*p<10000) |
| 2639 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 2640 | #ifndef Py_UNICODE_WIDE |
| 2641 | else |
| 2642 | repsize += 2+5+1; |
| 2643 | #else |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2644 | else if (*p<100000) |
| 2645 | repsize += 2+5+1; |
| 2646 | else if (*p<1000000) |
| 2647 | repsize += 2+6+1; |
| 2648 | else |
| 2649 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2650 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2651 | } |
| 2652 | requiredsize = respos+repsize+(endp-collend); |
| 2653 | if (requiredsize > ressize) { |
| 2654 | if (requiredsize<2*ressize) |
| 2655 | requiredsize = 2*ressize; |
| 2656 | if (_PyString_Resize(&res, requiredsize)) |
| 2657 | goto onError; |
| 2658 | str = PyString_AS_STRING(res) + respos; |
| 2659 | ressize = requiredsize; |
| 2660 | } |
| 2661 | /* generate replacement (temporarily (mis)uses p) */ |
| 2662 | for (p = collstart; p < collend; ++p) { |
| 2663 | str += sprintf(str, "&#%d;", (int)*p); |
| 2664 | } |
| 2665 | p = collend; |
| 2666 | break; |
| 2667 | default: |
| 2668 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 2669 | encoding, reason, startp, size, &exc, |
| 2670 | collstart-startp, collend-startp, &newpos); |
| 2671 | if (repunicode == NULL) |
| 2672 | goto onError; |
| 2673 | /* need more space? (at least enough for what we |
| 2674 | have+the replacement+the rest of the string, so |
| 2675 | we won't have to check space for encodable characters) */ |
| 2676 | respos = str-PyString_AS_STRING(res); |
| 2677 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 2678 | requiredsize = respos+repsize+(endp-collend); |
| 2679 | if (requiredsize > ressize) { |
| 2680 | if (requiredsize<2*ressize) |
| 2681 | requiredsize = 2*ressize; |
| 2682 | if (_PyString_Resize(&res, requiredsize)) { |
| 2683 | Py_DECREF(repunicode); |
| 2684 | goto onError; |
| 2685 | } |
| 2686 | str = PyString_AS_STRING(res) + respos; |
| 2687 | ressize = requiredsize; |
| 2688 | } |
| 2689 | /* check if there is anything unencodable in the replacement |
| 2690 | and copy it to the output */ |
| 2691 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 2692 | c = *uni2; |
| 2693 | if (c >= limit) { |
| 2694 | raise_encode_exception(&exc, encoding, startp, size, |
| 2695 | unicodepos, unicodepos+1, reason); |
| 2696 | Py_DECREF(repunicode); |
| 2697 | goto onError; |
| 2698 | } |
| 2699 | *str = (char)c; |
| 2700 | } |
| 2701 | p = startp + newpos; |
| 2702 | Py_DECREF(repunicode); |
| 2703 | } |
| 2704 | } |
| 2705 | } |
| 2706 | /* Resize if we allocated to much */ |
| 2707 | respos = str-PyString_AS_STRING(res); |
| 2708 | if (respos<ressize) |
| 2709 | /* If this falls res will be NULL */ |
| 2710 | _PyString_Resize(&res, respos); |
| 2711 | Py_XDECREF(errorHandler); |
| 2712 | Py_XDECREF(exc); |
| 2713 | return res; |
| 2714 | |
| 2715 | onError: |
| 2716 | Py_XDECREF(res); |
| 2717 | Py_XDECREF(errorHandler); |
| 2718 | Py_XDECREF(exc); |
| 2719 | return NULL; |
| 2720 | } |
| 2721 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2722 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2723 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2724 | const char *errors) |
| 2725 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2726 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2727 | } |
| 2728 | |
| 2729 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 2730 | { |
| 2731 | if (!PyUnicode_Check(unicode)) { |
| 2732 | PyErr_BadArgument(); |
| 2733 | return NULL; |
| 2734 | } |
| 2735 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 2736 | PyUnicode_GET_SIZE(unicode), |
| 2737 | NULL); |
| 2738 | } |
| 2739 | |
| 2740 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 2741 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2742 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2743 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2744 | const char *errors) |
| 2745 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2746 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2747 | PyUnicodeObject *v; |
| 2748 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2749 | Py_ssize_t startinpos; |
| 2750 | Py_ssize_t endinpos; |
| 2751 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2752 | const char *e; |
| 2753 | PyObject *errorHandler = NULL; |
| 2754 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2755 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2756 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 2757 | if (size == 1 && *(unsigned char*)s < 128) { |
| 2758 | Py_UNICODE r = *(unsigned char*)s; |
| 2759 | return PyUnicode_FromUnicode(&r, 1); |
| 2760 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2761 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2762 | v = _PyUnicode_New(size); |
| 2763 | if (v == NULL) |
| 2764 | goto onError; |
| 2765 | if (size == 0) |
| 2766 | return (PyObject *)v; |
| 2767 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2768 | e = s + size; |
| 2769 | while (s < e) { |
| 2770 | register unsigned char c = (unsigned char)*s; |
| 2771 | if (c < 128) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2772 | *p++ = c; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2773 | ++s; |
| 2774 | } |
| 2775 | else { |
| 2776 | startinpos = s-starts; |
| 2777 | endinpos = startinpos + 1; |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 2778 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2779 | if (unicode_decode_call_errorhandler( |
| 2780 | errors, &errorHandler, |
| 2781 | "ascii", "ordinal not in range(128)", |
| 2782 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2783 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2784 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2785 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2786 | } |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 2787 | if (p - PyUnicode_AS_UNICODE(v) < PyString_GET_SIZE(v)) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2788 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 2789 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2790 | Py_XDECREF(errorHandler); |
| 2791 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2792 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2793 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2794 | onError: |
| 2795 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2796 | Py_XDECREF(errorHandler); |
| 2797 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2798 | return NULL; |
| 2799 | } |
| 2800 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2801 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2802 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2803 | const char *errors) |
| 2804 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2805 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2806 | } |
| 2807 | |
| 2808 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 2809 | { |
| 2810 | if (!PyUnicode_Check(unicode)) { |
| 2811 | PyErr_BadArgument(); |
| 2812 | return NULL; |
| 2813 | } |
| 2814 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 2815 | PyUnicode_GET_SIZE(unicode), |
| 2816 | NULL); |
| 2817 | } |
| 2818 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2819 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 2820 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2821 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 2822 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2823 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2824 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2825 | const char *errors) |
| 2826 | { |
| 2827 | PyUnicodeObject *v; |
| 2828 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2829 | DWORD usize; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2830 | |
| 2831 | /* First get the size of the result */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2832 | assert(size < INT_MAX); |
| 2833 | usize = MultiByteToWideChar(CP_ACP, 0, s, (int)size, NULL, 0); |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 2834 | if (size > 0 && usize==0) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2835 | return PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2836 | |
| 2837 | v = _PyUnicode_New(usize); |
| 2838 | if (v == NULL) |
| 2839 | return NULL; |
| 2840 | if (usize == 0) |
| 2841 | return (PyObject *)v; |
| 2842 | p = PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2843 | if (0 == MultiByteToWideChar(CP_ACP, 0, s, (int)size, p, usize)) { |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2844 | Py_DECREF(v); |
| 2845 | return PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2846 | } |
| 2847 | |
| 2848 | return (PyObject *)v; |
| 2849 | } |
| 2850 | |
| 2851 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2852 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2853 | const char *errors) |
| 2854 | { |
| 2855 | PyObject *repr; |
| 2856 | char *s; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 2857 | DWORD mbcssize; |
| 2858 | |
| 2859 | /* If there are no characters, bail now! */ |
| 2860 | if (size==0) |
| 2861 | return PyString_FromString(""); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2862 | |
| 2863 | /* First get the size of the result */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2864 | assert(size<INT_MAX); |
| 2865 | mbcssize = WideCharToMultiByte(CP_ACP, 0, p, (int)size, NULL, 0, NULL, NULL); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2866 | if (mbcssize==0) |
| 2867 | return PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2868 | |
| 2869 | repr = PyString_FromStringAndSize(NULL, mbcssize); |
| 2870 | if (repr == NULL) |
| 2871 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 2872 | if (mbcssize == 0) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2873 | return repr; |
| 2874 | |
| 2875 | /* Do the conversion */ |
| 2876 | s = PyString_AS_STRING(repr); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2877 | assert(size < INT_MAX); |
| 2878 | if (0 == WideCharToMultiByte(CP_ACP, 0, p, (int)size, s, mbcssize, NULL, NULL)) { |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2879 | Py_DECREF(repr); |
| 2880 | return PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2881 | } |
| 2882 | return repr; |
| 2883 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 2884 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 2885 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 2886 | { |
| 2887 | if (!PyUnicode_Check(unicode)) { |
| 2888 | PyErr_BadArgument(); |
| 2889 | return NULL; |
| 2890 | } |
| 2891 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 2892 | PyUnicode_GET_SIZE(unicode), |
| 2893 | NULL); |
| 2894 | } |
| 2895 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2896 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2897 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2898 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 2899 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2900 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2901 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2902 | PyObject *mapping, |
| 2903 | const char *errors) |
| 2904 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2905 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2906 | Py_ssize_t startinpos; |
| 2907 | Py_ssize_t endinpos; |
| 2908 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2909 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2910 | PyUnicodeObject *v; |
| 2911 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2912 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2913 | PyObject *errorHandler = NULL; |
| 2914 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2915 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2916 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2917 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2918 | /* Default to Latin-1 */ |
| 2919 | if (mapping == NULL) |
| 2920 | return PyUnicode_DecodeLatin1(s, size, errors); |
| 2921 | |
| 2922 | v = _PyUnicode_New(size); |
| 2923 | if (v == NULL) |
| 2924 | goto onError; |
| 2925 | if (size == 0) |
| 2926 | return (PyObject *)v; |
| 2927 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2928 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2929 | if (PyUnicode_CheckExact(mapping)) { |
| 2930 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 2931 | maplen = PyUnicode_GET_SIZE(mapping); |
| 2932 | while (s < e) { |
| 2933 | unsigned char ch = *s; |
| 2934 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2935 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2936 | if (ch < maplen) |
| 2937 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2938 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2939 | if (x == 0xfffe) { |
| 2940 | /* undefined mapping */ |
| 2941 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2942 | startinpos = s-starts; |
| 2943 | endinpos = startinpos+1; |
| 2944 | if (unicode_decode_call_errorhandler( |
| 2945 | errors, &errorHandler, |
| 2946 | "charmap", "character maps to <undefined>", |
| 2947 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2948 | (PyObject **)&v, &outpos, &p)) { |
| 2949 | goto onError; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 2950 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2951 | continue; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 2952 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2953 | *p++ = x; |
| 2954 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2955 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2956 | } |
| 2957 | else { |
| 2958 | while (s < e) { |
| 2959 | unsigned char ch = *s; |
| 2960 | PyObject *w, *x; |
| 2961 | |
| 2962 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 2963 | w = PyInt_FromLong((long)ch); |
| 2964 | if (w == NULL) |
| 2965 | goto onError; |
| 2966 | x = PyObject_GetItem(mapping, w); |
| 2967 | Py_DECREF(w); |
| 2968 | if (x == NULL) { |
| 2969 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 2970 | /* No mapping found means: mapping is undefined. */ |
| 2971 | PyErr_Clear(); |
| 2972 | x = Py_None; |
| 2973 | Py_INCREF(x); |
| 2974 | } else |
| 2975 | goto onError; |
| 2976 | } |
| 2977 | |
| 2978 | /* Apply mapping */ |
| 2979 | if (PyInt_Check(x)) { |
| 2980 | long value = PyInt_AS_LONG(x); |
| 2981 | if (value < 0 || value > 65535) { |
| 2982 | PyErr_SetString(PyExc_TypeError, |
| 2983 | "character mapping must be in range(65536)"); |
| 2984 | Py_DECREF(x); |
| 2985 | goto onError; |
| 2986 | } |
| 2987 | *p++ = (Py_UNICODE)value; |
| 2988 | } |
| 2989 | else if (x == Py_None) { |
| 2990 | /* undefined mapping */ |
| 2991 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2992 | startinpos = s-starts; |
| 2993 | endinpos = startinpos+1; |
| 2994 | if (unicode_decode_call_errorhandler( |
| 2995 | errors, &errorHandler, |
| 2996 | "charmap", "character maps to <undefined>", |
| 2997 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2998 | (PyObject **)&v, &outpos, &p)) { |
| 2999 | Py_DECREF(x); |
| 3000 | goto onError; |
| 3001 | } |
Walter Dörwald | d4fff17 | 2005-11-28 22:15:56 +0000 | [diff] [blame] | 3002 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3003 | continue; |
| 3004 | } |
| 3005 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3006 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3007 | |
| 3008 | if (targetsize == 1) |
| 3009 | /* 1-1 mapping */ |
| 3010 | *p++ = *PyUnicode_AS_UNICODE(x); |
| 3011 | |
| 3012 | else if (targetsize > 1) { |
| 3013 | /* 1-n mapping */ |
| 3014 | if (targetsize > extrachars) { |
| 3015 | /* resize first */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3016 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 3017 | Py_ssize_t needed = (targetsize - extrachars) + \ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3018 | (targetsize << 2); |
| 3019 | extrachars += needed; |
| 3020 | if (_PyUnicode_Resize(&v, |
| 3021 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 3022 | Py_DECREF(x); |
| 3023 | goto onError; |
| 3024 | } |
| 3025 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 3026 | } |
| 3027 | Py_UNICODE_COPY(p, |
| 3028 | PyUnicode_AS_UNICODE(x), |
| 3029 | targetsize); |
| 3030 | p += targetsize; |
| 3031 | extrachars -= targetsize; |
| 3032 | } |
| 3033 | /* 1-0 mapping: skip the character */ |
| 3034 | } |
| 3035 | else { |
| 3036 | /* wrong return value */ |
| 3037 | PyErr_SetString(PyExc_TypeError, |
| 3038 | "character mapping must return integer, None or unicode"); |
| 3039 | Py_DECREF(x); |
| 3040 | goto onError; |
| 3041 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3042 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3043 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3044 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3045 | } |
| 3046 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 3047 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3048 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3049 | Py_XDECREF(errorHandler); |
| 3050 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3051 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3052 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3053 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3054 | Py_XDECREF(errorHandler); |
| 3055 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3056 | Py_XDECREF(v); |
| 3057 | return NULL; |
| 3058 | } |
| 3059 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3060 | /* Lookup the character ch in the mapping. If the character |
| 3061 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3062 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3063 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3064 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3065 | PyObject *w = PyInt_FromLong((long)c); |
| 3066 | PyObject *x; |
| 3067 | |
| 3068 | if (w == NULL) |
| 3069 | return NULL; |
| 3070 | x = PyObject_GetItem(mapping, w); |
| 3071 | Py_DECREF(w); |
| 3072 | if (x == NULL) { |
| 3073 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 3074 | /* No mapping found means: mapping is undefined. */ |
| 3075 | PyErr_Clear(); |
| 3076 | x = Py_None; |
| 3077 | Py_INCREF(x); |
| 3078 | return x; |
| 3079 | } else |
| 3080 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3081 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 3082 | else if (x == Py_None) |
| 3083 | return x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3084 | else if (PyInt_Check(x)) { |
| 3085 | long value = PyInt_AS_LONG(x); |
| 3086 | if (value < 0 || value > 255) { |
| 3087 | PyErr_SetString(PyExc_TypeError, |
| 3088 | "character mapping must be in range(256)"); |
| 3089 | Py_DECREF(x); |
| 3090 | return NULL; |
| 3091 | } |
| 3092 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3093 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3094 | else if (PyString_Check(x)) |
| 3095 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3096 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3097 | /* wrong return value */ |
| 3098 | PyErr_SetString(PyExc_TypeError, |
| 3099 | "character mapping must return integer, None or str"); |
| 3100 | Py_DECREF(x); |
| 3101 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3102 | } |
| 3103 | } |
| 3104 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3105 | /* lookup the character, put the result in the output string and adjust |
| 3106 | various state variables. Reallocate the output string if not enough |
| 3107 | space is available. Return a new reference to the object that |
| 3108 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 3109 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 3110 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3111 | static |
| 3112 | PyObject *charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3113 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3114 | { |
| 3115 | PyObject *rep = charmapencode_lookup(c, mapping); |
| 3116 | |
| 3117 | if (rep==NULL) |
| 3118 | return NULL; |
| 3119 | else if (rep==Py_None) |
| 3120 | return rep; |
| 3121 | else { |
| 3122 | char *outstart = PyString_AS_STRING(*outobj); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3123 | Py_ssize_t outsize = PyString_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3124 | if (PyInt_Check(rep)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3125 | Py_ssize_t requiredsize = *outpos+1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3126 | if (outsize<requiredsize) { |
| 3127 | /* exponentially overallocate to minimize reallocations */ |
| 3128 | if (requiredsize < 2*outsize) |
| 3129 | requiredsize = 2*outsize; |
| 3130 | if (_PyString_Resize(outobj, requiredsize)) { |
| 3131 | Py_DECREF(rep); |
| 3132 | return NULL; |
| 3133 | } |
| 3134 | outstart = PyString_AS_STRING(*outobj); |
| 3135 | } |
| 3136 | outstart[(*outpos)++] = (char)PyInt_AS_LONG(rep); |
| 3137 | } |
| 3138 | else { |
| 3139 | const char *repchars = PyString_AS_STRING(rep); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3140 | Py_ssize_t repsize = PyString_GET_SIZE(rep); |
| 3141 | Py_ssize_t requiredsize = *outpos+repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3142 | if (outsize<requiredsize) { |
| 3143 | /* exponentially overallocate to minimize reallocations */ |
| 3144 | if (requiredsize < 2*outsize) |
| 3145 | requiredsize = 2*outsize; |
| 3146 | if (_PyString_Resize(outobj, requiredsize)) { |
| 3147 | Py_DECREF(rep); |
| 3148 | return NULL; |
| 3149 | } |
| 3150 | outstart = PyString_AS_STRING(*outobj); |
| 3151 | } |
| 3152 | memcpy(outstart + *outpos, repchars, repsize); |
| 3153 | *outpos += repsize; |
| 3154 | } |
| 3155 | } |
| 3156 | return rep; |
| 3157 | } |
| 3158 | |
| 3159 | /* handle an error in PyUnicode_EncodeCharmap |
| 3160 | Return 0 on success, -1 on error */ |
| 3161 | static |
| 3162 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3163 | 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] | 3164 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 3165 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3166 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3167 | { |
| 3168 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3169 | Py_ssize_t repsize; |
| 3170 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3171 | Py_UNICODE *uni2; |
| 3172 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3173 | Py_ssize_t collstartpos = *inpos; |
| 3174 | Py_ssize_t collendpos = *inpos+1; |
| 3175 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3176 | char *encoding = "charmap"; |
| 3177 | char *reason = "character maps to <undefined>"; |
| 3178 | |
| 3179 | PyObject *x; |
| 3180 | /* find all unencodable characters */ |
| 3181 | while (collendpos < size) { |
| 3182 | x = charmapencode_lookup(p[collendpos], mapping); |
| 3183 | if (x==NULL) |
| 3184 | return -1; |
| 3185 | else if (x!=Py_None) { |
| 3186 | Py_DECREF(x); |
| 3187 | break; |
| 3188 | } |
| 3189 | Py_DECREF(x); |
| 3190 | ++collendpos; |
| 3191 | } |
| 3192 | /* cache callback name lookup |
| 3193 | * (if not done yet, i.e. it's the first error) */ |
| 3194 | if (*known_errorHandler==-1) { |
| 3195 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3196 | *known_errorHandler = 1; |
| 3197 | else if (!strcmp(errors, "replace")) |
| 3198 | *known_errorHandler = 2; |
| 3199 | else if (!strcmp(errors, "ignore")) |
| 3200 | *known_errorHandler = 3; |
| 3201 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3202 | *known_errorHandler = 4; |
| 3203 | else |
| 3204 | *known_errorHandler = 0; |
| 3205 | } |
| 3206 | switch (*known_errorHandler) { |
| 3207 | case 1: /* strict */ |
| 3208 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3209 | return -1; |
| 3210 | case 2: /* replace */ |
| 3211 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
| 3212 | x = charmapencode_output('?', mapping, res, respos); |
| 3213 | if (x==NULL) { |
| 3214 | return -1; |
| 3215 | } |
| 3216 | else if (x==Py_None) { |
| 3217 | Py_DECREF(x); |
| 3218 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3219 | return -1; |
| 3220 | } |
| 3221 | Py_DECREF(x); |
| 3222 | } |
| 3223 | /* fall through */ |
| 3224 | case 3: /* ignore */ |
| 3225 | *inpos = collendpos; |
| 3226 | break; |
| 3227 | case 4: /* xmlcharrefreplace */ |
| 3228 | /* generate replacement (temporarily (mis)uses p) */ |
| 3229 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
| 3230 | char buffer[2+29+1+1]; |
| 3231 | char *cp; |
| 3232 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 3233 | for (cp = buffer; *cp; ++cp) { |
| 3234 | x = charmapencode_output(*cp, mapping, res, respos); |
| 3235 | if (x==NULL) |
| 3236 | return -1; |
| 3237 | else if (x==Py_None) { |
| 3238 | Py_DECREF(x); |
| 3239 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3240 | return -1; |
| 3241 | } |
| 3242 | Py_DECREF(x); |
| 3243 | } |
| 3244 | } |
| 3245 | *inpos = collendpos; |
| 3246 | break; |
| 3247 | default: |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 3248 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3249 | encoding, reason, p, size, exceptionObject, |
| 3250 | collstartpos, collendpos, &newpos); |
| 3251 | if (repunicode == NULL) |
| 3252 | return -1; |
| 3253 | /* generate replacement */ |
| 3254 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3255 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 3256 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 3257 | if (x==NULL) { |
| 3258 | Py_DECREF(repunicode); |
| 3259 | return -1; |
| 3260 | } |
| 3261 | else if (x==Py_None) { |
| 3262 | Py_DECREF(repunicode); |
| 3263 | Py_DECREF(x); |
| 3264 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3265 | return -1; |
| 3266 | } |
| 3267 | Py_DECREF(x); |
| 3268 | } |
| 3269 | *inpos = newpos; |
| 3270 | Py_DECREF(repunicode); |
| 3271 | } |
| 3272 | return 0; |
| 3273 | } |
| 3274 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3275 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3276 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3277 | PyObject *mapping, |
| 3278 | const char *errors) |
| 3279 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3280 | /* output object */ |
| 3281 | PyObject *res = NULL; |
| 3282 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3283 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3284 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3285 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3286 | PyObject *errorHandler = NULL; |
| 3287 | PyObject *exc = NULL; |
| 3288 | /* the following variable is used for caching string comparisons |
| 3289 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 3290 | * 3=ignore, 4=xmlcharrefreplace */ |
| 3291 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3292 | |
| 3293 | /* Default to Latin-1 */ |
| 3294 | if (mapping == NULL) |
| 3295 | return PyUnicode_EncodeLatin1(p, size, errors); |
| 3296 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3297 | /* allocate enough for a simple encoding without |
| 3298 | replacements, if we need more, we'll resize */ |
| 3299 | res = PyString_FromStringAndSize(NULL, size); |
| 3300 | if (res == NULL) |
| 3301 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3302 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3303 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3304 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3305 | while (inpos<size) { |
| 3306 | /* try to encode it */ |
| 3307 | PyObject *x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 3308 | if (x==NULL) /* error */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3309 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3310 | if (x==Py_None) { /* unencodable character */ |
| 3311 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 3312 | &exc, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 3313 | &known_errorHandler, &errorHandler, errors, |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 3314 | &res, &respos)) { |
| 3315 | Py_DECREF(x); |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 3316 | goto onError; |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 3317 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3318 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3319 | else |
| 3320 | /* done with this character => adjust input position */ |
| 3321 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3322 | Py_DECREF(x); |
| 3323 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3324 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3325 | /* Resize if we allocated to much */ |
| 3326 | if (respos<PyString_GET_SIZE(res)) { |
| 3327 | if (_PyString_Resize(&res, respos)) |
| 3328 | goto onError; |
| 3329 | } |
| 3330 | Py_XDECREF(exc); |
| 3331 | Py_XDECREF(errorHandler); |
| 3332 | return res; |
| 3333 | |
| 3334 | onError: |
| 3335 | Py_XDECREF(res); |
| 3336 | Py_XDECREF(exc); |
| 3337 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3338 | return NULL; |
| 3339 | } |
| 3340 | |
| 3341 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
| 3342 | PyObject *mapping) |
| 3343 | { |
| 3344 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
| 3345 | PyErr_BadArgument(); |
| 3346 | return NULL; |
| 3347 | } |
| 3348 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
| 3349 | PyUnicode_GET_SIZE(unicode), |
| 3350 | mapping, |
| 3351 | NULL); |
| 3352 | } |
| 3353 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3354 | /* create or adjust a UnicodeTranslateError */ |
| 3355 | static void make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3356 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3357 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3358 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3359 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3360 | if (*exceptionObject == NULL) { |
| 3361 | *exceptionObject = PyUnicodeTranslateError_Create( |
| 3362 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3363 | } |
| 3364 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3365 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 3366 | goto onError; |
| 3367 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 3368 | goto onError; |
| 3369 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 3370 | goto onError; |
| 3371 | return; |
| 3372 | onError: |
| 3373 | Py_DECREF(*exceptionObject); |
| 3374 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3375 | } |
| 3376 | } |
| 3377 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3378 | /* raises a UnicodeTranslateError */ |
| 3379 | static void raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3380 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3381 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3382 | const char *reason) |
| 3383 | { |
| 3384 | make_translate_exception(exceptionObject, |
| 3385 | unicode, size, startpos, endpos, reason); |
| 3386 | if (*exceptionObject != NULL) |
| 3387 | PyCodec_StrictErrors(*exceptionObject); |
| 3388 | } |
| 3389 | |
| 3390 | /* error handling callback helper: |
| 3391 | build arguments, call the callback and check the arguments, |
| 3392 | put the result into newpos and return the replacement string, which |
| 3393 | has to be freed by the caller */ |
| 3394 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
| 3395 | PyObject **errorHandler, |
| 3396 | const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3397 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 3398 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3399 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3400 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 3401 | 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] | 3402 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 3403 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3404 | PyObject *restuple; |
| 3405 | PyObject *resunicode; |
| 3406 | |
| 3407 | if (*errorHandler == NULL) { |
| 3408 | *errorHandler = PyCodec_LookupError(errors); |
| 3409 | if (*errorHandler == NULL) |
| 3410 | return NULL; |
| 3411 | } |
| 3412 | |
| 3413 | make_translate_exception(exceptionObject, |
| 3414 | unicode, size, startpos, endpos, reason); |
| 3415 | if (*exceptionObject == NULL) |
| 3416 | return NULL; |
| 3417 | |
| 3418 | restuple = PyObject_CallFunctionObjArgs( |
| 3419 | *errorHandler, *exceptionObject, NULL); |
| 3420 | if (restuple == NULL) |
| 3421 | return NULL; |
| 3422 | if (!PyTuple_Check(restuple)) { |
| 3423 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 3424 | Py_DECREF(restuple); |
| 3425 | return NULL; |
| 3426 | } |
| 3427 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3428 | &resunicode, &i_newpos)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3429 | Py_DECREF(restuple); |
| 3430 | return NULL; |
| 3431 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3432 | if (i_newpos<0) |
| 3433 | *newpos = size+i_newpos; |
| 3434 | else |
| 3435 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3436 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 3437 | 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] | 3438 | Py_DECREF(restuple); |
| 3439 | return NULL; |
| 3440 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3441 | Py_INCREF(resunicode); |
| 3442 | Py_DECREF(restuple); |
| 3443 | return resunicode; |
| 3444 | } |
| 3445 | |
| 3446 | /* Lookup the character ch in the mapping and put the result in result, |
| 3447 | which must be decrefed by the caller. |
| 3448 | Return 0 on success, -1 on error */ |
| 3449 | static |
| 3450 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 3451 | { |
| 3452 | PyObject *w = PyInt_FromLong((long)c); |
| 3453 | PyObject *x; |
| 3454 | |
| 3455 | if (w == NULL) |
| 3456 | return -1; |
| 3457 | x = PyObject_GetItem(mapping, w); |
| 3458 | Py_DECREF(w); |
| 3459 | if (x == NULL) { |
| 3460 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 3461 | /* No mapping found means: use 1:1 mapping. */ |
| 3462 | PyErr_Clear(); |
| 3463 | *result = NULL; |
| 3464 | return 0; |
| 3465 | } else |
| 3466 | return -1; |
| 3467 | } |
| 3468 | else if (x == Py_None) { |
| 3469 | *result = x; |
| 3470 | return 0; |
| 3471 | } |
| 3472 | else if (PyInt_Check(x)) { |
| 3473 | long value = PyInt_AS_LONG(x); |
| 3474 | long max = PyUnicode_GetMax(); |
| 3475 | if (value < 0 || value > max) { |
| 3476 | PyErr_Format(PyExc_TypeError, |
| 3477 | "character mapping must be in range(0x%lx)", max+1); |
| 3478 | Py_DECREF(x); |
| 3479 | return -1; |
| 3480 | } |
| 3481 | *result = x; |
| 3482 | return 0; |
| 3483 | } |
| 3484 | else if (PyUnicode_Check(x)) { |
| 3485 | *result = x; |
| 3486 | return 0; |
| 3487 | } |
| 3488 | else { |
| 3489 | /* wrong return value */ |
| 3490 | PyErr_SetString(PyExc_TypeError, |
| 3491 | "character mapping must return integer, None or unicode"); |
Walter Dörwald | 150523e | 2003-08-15 16:52:19 +0000 | [diff] [blame] | 3492 | Py_DECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3493 | return -1; |
| 3494 | } |
| 3495 | } |
| 3496 | /* ensure that *outobj is at least requiredsize characters long, |
| 3497 | if not reallocate and adjust various state variables. |
| 3498 | Return 0 on success, -1 on error */ |
| 3499 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3500 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3501 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3502 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3503 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3504 | if (requiredsize > oldsize) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3505 | /* remember old output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3506 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3507 | /* exponentially overallocate to minimize reallocations */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3508 | if (requiredsize < 2 * oldsize) |
| 3509 | requiredsize = 2 * oldsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3510 | if (_PyUnicode_Resize(outobj, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3511 | return -1; |
| 3512 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3513 | } |
| 3514 | return 0; |
| 3515 | } |
| 3516 | /* lookup the character, put the result in the output string and adjust |
| 3517 | various state variables. Return a new reference to the object that |
| 3518 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 3519 | undefined (in which case no character was written). |
| 3520 | The called must decref result. |
| 3521 | Return 0 on success, -1 on error. */ |
| 3522 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3523 | 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] | 3524 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3525 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3526 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3527 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3528 | return -1; |
| 3529 | if (*res==NULL) { |
| 3530 | /* not found => default to 1:1 mapping */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3531 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3532 | } |
| 3533 | else if (*res==Py_None) |
| 3534 | ; |
| 3535 | else if (PyInt_Check(*res)) { |
| 3536 | /* no overflow check, because we know that the space is enough */ |
| 3537 | *(*outp)++ = (Py_UNICODE)PyInt_AS_LONG(*res); |
| 3538 | } |
| 3539 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3540 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3541 | if (repsize==1) { |
| 3542 | /* no overflow check, because we know that the space is enough */ |
| 3543 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 3544 | } |
| 3545 | else if (repsize!=0) { |
| 3546 | /* more than one character */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3547 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
Walter Dörwald | cd736e7 | 2004-02-05 17:36:00 +0000 | [diff] [blame] | 3548 | (insize - (curinp-startinp)) + |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3549 | repsize - 1; |
| 3550 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3551 | return -1; |
| 3552 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 3553 | *outp += repsize; |
| 3554 | } |
| 3555 | } |
| 3556 | else |
| 3557 | return -1; |
| 3558 | return 0; |
| 3559 | } |
| 3560 | |
| 3561 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3562 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3563 | PyObject *mapping, |
| 3564 | const char *errors) |
| 3565 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3566 | /* output object */ |
| 3567 | PyObject *res = NULL; |
| 3568 | /* pointers to the beginning and end+1 of input */ |
| 3569 | const Py_UNICODE *startp = p; |
| 3570 | const Py_UNICODE *endp = p + size; |
| 3571 | /* pointer into the output */ |
| 3572 | Py_UNICODE *str; |
| 3573 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3574 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3575 | char *reason = "character maps to <undefined>"; |
| 3576 | PyObject *errorHandler = NULL; |
| 3577 | PyObject *exc = NULL; |
| 3578 | /* the following variable is used for caching string comparisons |
| 3579 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 3580 | * 3=ignore, 4=xmlcharrefreplace */ |
| 3581 | int known_errorHandler = -1; |
| 3582 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3583 | if (mapping == NULL) { |
| 3584 | PyErr_BadArgument(); |
| 3585 | return NULL; |
| 3586 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3587 | |
| 3588 | /* allocate enough for a simple 1:1 translation without |
| 3589 | replacements, if we need more, we'll resize */ |
| 3590 | res = PyUnicode_FromUnicode(NULL, size); |
| 3591 | if (res == NULL) |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3592 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3593 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3594 | return res; |
| 3595 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3596 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3597 | while (p<endp) { |
| 3598 | /* try to encode it */ |
| 3599 | PyObject *x = NULL; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3600 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3601 | Py_XDECREF(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3602 | goto onError; |
| 3603 | } |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 3604 | Py_XDECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3605 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 3606 | ++p; |
| 3607 | else { /* untranslatable character */ |
| 3608 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3609 | Py_ssize_t repsize; |
| 3610 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3611 | Py_UNICODE *uni2; |
| 3612 | /* startpos for collecting untranslatable chars */ |
| 3613 | const Py_UNICODE *collstart = p; |
| 3614 | const Py_UNICODE *collend = p+1; |
| 3615 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3616 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3617 | /* find all untranslatable characters */ |
| 3618 | while (collend < endp) { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3619 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3620 | goto onError; |
| 3621 | Py_XDECREF(x); |
| 3622 | if (x!=Py_None) |
| 3623 | break; |
| 3624 | ++collend; |
| 3625 | } |
| 3626 | /* cache callback name lookup |
| 3627 | * (if not done yet, i.e. it's the first error) */ |
| 3628 | if (known_errorHandler==-1) { |
| 3629 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3630 | known_errorHandler = 1; |
| 3631 | else if (!strcmp(errors, "replace")) |
| 3632 | known_errorHandler = 2; |
| 3633 | else if (!strcmp(errors, "ignore")) |
| 3634 | known_errorHandler = 3; |
| 3635 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3636 | known_errorHandler = 4; |
| 3637 | else |
| 3638 | known_errorHandler = 0; |
| 3639 | } |
| 3640 | switch (known_errorHandler) { |
| 3641 | case 1: /* strict */ |
| 3642 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
| 3643 | goto onError; |
| 3644 | case 2: /* replace */ |
| 3645 | /* No need to check for space, this is a 1:1 replacement */ |
| 3646 | for (coll = collstart; coll<collend; ++coll) |
| 3647 | *str++ = '?'; |
| 3648 | /* fall through */ |
| 3649 | case 3: /* ignore */ |
| 3650 | p = collend; |
| 3651 | break; |
| 3652 | case 4: /* xmlcharrefreplace */ |
| 3653 | /* generate replacement (temporarily (mis)uses p) */ |
| 3654 | for (p = collstart; p < collend; ++p) { |
| 3655 | char buffer[2+29+1+1]; |
| 3656 | char *cp; |
| 3657 | sprintf(buffer, "&#%d;", (int)*p); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3658 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3659 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 3660 | goto onError; |
| 3661 | for (cp = buffer; *cp; ++cp) |
| 3662 | *str++ = *cp; |
| 3663 | } |
| 3664 | p = collend; |
| 3665 | break; |
| 3666 | default: |
| 3667 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 3668 | reason, startp, size, &exc, |
| 3669 | collstart-startp, collend-startp, &newpos); |
| 3670 | if (repunicode == NULL) |
| 3671 | goto onError; |
| 3672 | /* generate replacement */ |
| 3673 | repsize = PyUnicode_GET_SIZE(repunicode); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3674 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3675 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 3676 | Py_DECREF(repunicode); |
| 3677 | goto onError; |
| 3678 | } |
| 3679 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 3680 | *str++ = *uni2; |
| 3681 | p = startp + newpos; |
| 3682 | Py_DECREF(repunicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3683 | } |
| 3684 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3685 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3686 | /* Resize if we allocated to much */ |
| 3687 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3688 | if (respos<PyUnicode_GET_SIZE(res)) { |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3689 | if (_PyUnicode_Resize(&res, respos) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3690 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3691 | } |
| 3692 | Py_XDECREF(exc); |
| 3693 | Py_XDECREF(errorHandler); |
| 3694 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3695 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3696 | onError: |
| 3697 | Py_XDECREF(res); |
| 3698 | Py_XDECREF(exc); |
| 3699 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3700 | return NULL; |
| 3701 | } |
| 3702 | |
| 3703 | PyObject *PyUnicode_Translate(PyObject *str, |
| 3704 | PyObject *mapping, |
| 3705 | const char *errors) |
| 3706 | { |
| 3707 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3708 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3709 | str = PyUnicode_FromObject(str); |
| 3710 | if (str == NULL) |
| 3711 | goto onError; |
| 3712 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
| 3713 | PyUnicode_GET_SIZE(str), |
| 3714 | mapping, |
| 3715 | errors); |
| 3716 | Py_DECREF(str); |
| 3717 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3718 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3719 | onError: |
| 3720 | Py_XDECREF(str); |
| 3721 | return NULL; |
| 3722 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3723 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3724 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 3725 | |
| 3726 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3727 | Py_ssize_t length, |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3728 | char *output, |
| 3729 | const char *errors) |
| 3730 | { |
| 3731 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3732 | PyObject *errorHandler = NULL; |
| 3733 | PyObject *exc = NULL; |
| 3734 | const char *encoding = "decimal"; |
| 3735 | const char *reason = "invalid decimal Unicode string"; |
| 3736 | /* the following variable is used for caching string comparisons |
| 3737 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 3738 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3739 | |
| 3740 | if (output == NULL) { |
| 3741 | PyErr_BadArgument(); |
| 3742 | return -1; |
| 3743 | } |
| 3744 | |
| 3745 | p = s; |
| 3746 | end = s + length; |
| 3747 | while (p < end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3748 | register Py_UNICODE ch = *p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3749 | int decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3750 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3751 | Py_ssize_t repsize; |
| 3752 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3753 | Py_UNICODE *uni2; |
| 3754 | Py_UNICODE *collstart; |
| 3755 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3756 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3757 | if (Py_UNICODE_ISSPACE(ch)) { |
| 3758 | *output++ = ' '; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3759 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3760 | continue; |
| 3761 | } |
| 3762 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 3763 | if (decimal >= 0) { |
| 3764 | *output++ = '0' + decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3765 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3766 | continue; |
| 3767 | } |
Guido van Rossum | ba47704 | 2000-04-06 18:18:10 +0000 | [diff] [blame] | 3768 | if (0 < ch && ch < 256) { |
Guido van Rossum | 42c29aa | 2000-05-03 23:58:29 +0000 | [diff] [blame] | 3769 | *output++ = (char)ch; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3770 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3771 | continue; |
| 3772 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3773 | /* All other characters are considered unencodable */ |
| 3774 | collstart = p; |
| 3775 | collend = p+1; |
| 3776 | while (collend < end) { |
| 3777 | if ((0 < *collend && *collend < 256) || |
| 3778 | !Py_UNICODE_ISSPACE(*collend) || |
| 3779 | Py_UNICODE_TODECIMAL(*collend)) |
| 3780 | break; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3781 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3782 | /* cache callback name lookup |
| 3783 | * (if not done yet, i.e. it's the first error) */ |
| 3784 | if (known_errorHandler==-1) { |
| 3785 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3786 | known_errorHandler = 1; |
| 3787 | else if (!strcmp(errors, "replace")) |
| 3788 | known_errorHandler = 2; |
| 3789 | else if (!strcmp(errors, "ignore")) |
| 3790 | known_errorHandler = 3; |
| 3791 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3792 | known_errorHandler = 4; |
| 3793 | else |
| 3794 | known_errorHandler = 0; |
| 3795 | } |
| 3796 | switch (known_errorHandler) { |
| 3797 | case 1: /* strict */ |
| 3798 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 3799 | goto onError; |
| 3800 | case 2: /* replace */ |
| 3801 | for (p = collstart; p < collend; ++p) |
| 3802 | *output++ = '?'; |
| 3803 | /* fall through */ |
| 3804 | case 3: /* ignore */ |
| 3805 | p = collend; |
| 3806 | break; |
| 3807 | case 4: /* xmlcharrefreplace */ |
| 3808 | /* generate replacement (temporarily (mis)uses p) */ |
| 3809 | for (p = collstart; p < collend; ++p) |
| 3810 | output += sprintf(output, "&#%d;", (int)*p); |
| 3811 | p = collend; |
| 3812 | break; |
| 3813 | default: |
| 3814 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 3815 | encoding, reason, s, length, &exc, |
| 3816 | collstart-s, collend-s, &newpos); |
| 3817 | if (repunicode == NULL) |
| 3818 | goto onError; |
| 3819 | /* generate replacement */ |
| 3820 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3821 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 3822 | Py_UNICODE ch = *uni2; |
| 3823 | if (Py_UNICODE_ISSPACE(ch)) |
| 3824 | *output++ = ' '; |
| 3825 | else { |
| 3826 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 3827 | if (decimal >= 0) |
| 3828 | *output++ = '0' + decimal; |
| 3829 | else if (0 < ch && ch < 256) |
| 3830 | *output++ = (char)ch; |
| 3831 | else { |
| 3832 | Py_DECREF(repunicode); |
| 3833 | raise_encode_exception(&exc, encoding, |
| 3834 | s, length, collstart-s, collend-s, reason); |
| 3835 | goto onError; |
| 3836 | } |
| 3837 | } |
| 3838 | } |
| 3839 | p = s + newpos; |
| 3840 | Py_DECREF(repunicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3841 | } |
| 3842 | } |
| 3843 | /* 0-terminate the output string */ |
| 3844 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3845 | Py_XDECREF(exc); |
| 3846 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3847 | return 0; |
| 3848 | |
| 3849 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3850 | Py_XDECREF(exc); |
| 3851 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 3852 | return -1; |
| 3853 | } |
| 3854 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3855 | /* --- Helpers ------------------------------------------------------------ */ |
| 3856 | |
Fredrik Lundh | a50d201 | 2006-05-26 17:04:58 +0000 | [diff] [blame] | 3857 | #define STRINGLIB_CHAR Py_UNICODE |
Fredrik Lundh | 6471ee4 | 2006-05-24 14:28:11 +0000 | [diff] [blame] | 3858 | |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 3859 | #define STRINGLIB_NEW PyUnicode_FromUnicode |
| 3860 | |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 3861 | Py_LOCAL(int) |
| 3862 | STRINGLIB_CMP(const Py_UNICODE* str, const Py_UNICODE* other, Py_ssize_t len) |
| 3863 | { |
Fredrik Lundh | 9c0e9c0 | 2006-05-26 18:24:15 +0000 | [diff] [blame] | 3864 | if (str[0] != other[0]) |
| 3865 | return 1; |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 3866 | return memcmp((void*) str, (void*) other, len * sizeof(Py_UNICODE)); |
| 3867 | } |
| 3868 | |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 3869 | #define STRINGLIB_EMPTY unicode_empty |
| 3870 | |
Fredrik Lundh | a50d201 | 2006-05-26 17:04:58 +0000 | [diff] [blame] | 3871 | #include "stringlib/fastsearch.h" |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 3872 | |
| 3873 | #include "stringlib/count.h" |
| 3874 | #include "stringlib/find.h" |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 3875 | #include "stringlib/partition.h" |
| 3876 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3877 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 3878 | PyObject *substr, |
| 3879 | Py_ssize_t start, |
| 3880 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3881 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3882 | Py_ssize_t result; |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 3883 | PyUnicodeObject* str_obj; |
| 3884 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3885 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 3886 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 3887 | if (!str_obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3888 | return -1; |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 3889 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 3890 | if (!sub_obj) { |
| 3891 | Py_DECREF(str_obj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3892 | return -1; |
| 3893 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3894 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 3895 | if (start < 0) |
| 3896 | start += str_obj->length; |
| 3897 | if (start < 0) |
| 3898 | start = 0; |
| 3899 | if (end > str_obj->length) |
| 3900 | end = str_obj->length; |
| 3901 | if (end < 0) |
| 3902 | end += str_obj->length; |
| 3903 | if (end < 0) |
| 3904 | end = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3905 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 3906 | result = stringlib_count( |
| 3907 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length |
| 3908 | ); |
| 3909 | |
| 3910 | Py_DECREF(sub_obj); |
| 3911 | Py_DECREF(str_obj); |
| 3912 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3913 | return result; |
| 3914 | } |
| 3915 | |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 3916 | static Py_ssize_t findstring(PyUnicodeObject *self, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3917 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3918 | Py_ssize_t start, |
| 3919 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3920 | int direction) |
| 3921 | { |
| 3922 | if (start < 0) |
| 3923 | start += self->length; |
| 3924 | if (start < 0) |
| 3925 | start = 0; |
| 3926 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3927 | if (end > self->length) |
| 3928 | end = self->length; |
| 3929 | if (end < 0) |
| 3930 | end += self->length; |
| 3931 | if (end < 0) |
| 3932 | end = 0; |
| 3933 | |
Guido van Rossum | 76afbd9 | 2002-08-20 17:29:29 +0000 | [diff] [blame] | 3934 | if (substring->length == 0) |
| 3935 | return (direction > 0) ? start : end; |
| 3936 | |
Fredrik Lundh | 6471ee4 | 2006-05-24 14:28:11 +0000 | [diff] [blame] | 3937 | if (direction > 0) { |
| 3938 | Py_ssize_t pos = fastsearch( |
| 3939 | PyUnicode_AS_UNICODE(self) + start, end - start, |
| 3940 | substring->str, substring->length, FAST_SEARCH |
| 3941 | ); |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 3942 | if (pos >= 0) |
| 3943 | return pos + start; |
| 3944 | } else { |
| 3945 | end -= substring->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3946 | for (; end >= start; end--) |
| 3947 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 3948 | return end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3949 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3950 | return -1; |
| 3951 | } |
| 3952 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3953 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame^] | 3954 | PyObject *substr, |
| 3955 | Py_ssize_t start, |
| 3956 | Py_ssize_t end, |
| 3957 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3958 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3959 | Py_ssize_t result; |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame^] | 3960 | PyUnicodeObject* str_obj; |
| 3961 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3962 | |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame^] | 3963 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 3964 | if (!str) |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 3965 | return -2; |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame^] | 3966 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 3967 | if (!sub_obj) { |
| 3968 | Py_DECREF(str_obj); |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 3969 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3970 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3971 | |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame^] | 3972 | if (start < 0) |
| 3973 | start += str_obj->length; |
| 3974 | if (start < 0) |
| 3975 | start = 0; |
| 3976 | if (end > str_obj->length) |
| 3977 | end = str_obj->length; |
| 3978 | if (end < 0) |
| 3979 | end += str_obj->length; |
| 3980 | if (end < 0) |
| 3981 | end = 0; |
| 3982 | |
| 3983 | if (direction > 0) |
| 3984 | result = stringlib_find( |
| 3985 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length |
| 3986 | ); |
| 3987 | else |
| 3988 | result = stringlib_rfind( |
| 3989 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length |
| 3990 | ); |
| 3991 | |
| 3992 | if (result >= 0) |
| 3993 | result += start; |
| 3994 | |
| 3995 | Py_DECREF(str_obj); |
| 3996 | Py_DECREF(sub_obj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3997 | return result; |
| 3998 | } |
| 3999 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4000 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4001 | int tailmatch(PyUnicodeObject *self, |
| 4002 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4003 | Py_ssize_t start, |
| 4004 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4005 | int direction) |
| 4006 | { |
| 4007 | if (start < 0) |
| 4008 | start += self->length; |
| 4009 | if (start < 0) |
| 4010 | start = 0; |
| 4011 | |
| 4012 | if (substring->length == 0) |
| 4013 | return 1; |
| 4014 | |
| 4015 | if (end > self->length) |
| 4016 | end = self->length; |
| 4017 | if (end < 0) |
| 4018 | end += self->length; |
| 4019 | if (end < 0) |
| 4020 | end = 0; |
| 4021 | |
| 4022 | end -= substring->length; |
| 4023 | if (end < start) |
| 4024 | return 0; |
| 4025 | |
| 4026 | if (direction > 0) { |
| 4027 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 4028 | return 1; |
| 4029 | } else { |
| 4030 | if (Py_UNICODE_MATCH(self, start, substring)) |
| 4031 | return 1; |
| 4032 | } |
| 4033 | |
| 4034 | return 0; |
| 4035 | } |
| 4036 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4037 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4038 | PyObject *substr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4039 | Py_ssize_t start, |
| 4040 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4041 | int direction) |
| 4042 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4043 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4044 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4045 | str = PyUnicode_FromObject(str); |
| 4046 | if (str == NULL) |
| 4047 | return -1; |
| 4048 | substr = PyUnicode_FromObject(substr); |
| 4049 | if (substr == NULL) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 4050 | Py_DECREF(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4051 | return -1; |
| 4052 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4053 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4054 | result = tailmatch((PyUnicodeObject *)str, |
| 4055 | (PyUnicodeObject *)substr, |
| 4056 | start, end, direction); |
| 4057 | Py_DECREF(str); |
| 4058 | Py_DECREF(substr); |
| 4059 | return result; |
| 4060 | } |
| 4061 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4062 | /* Apply fixfct filter to the Unicode object self and return a |
| 4063 | reference to the modified object */ |
| 4064 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4065 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4066 | PyObject *fixup(PyUnicodeObject *self, |
| 4067 | int (*fixfct)(PyUnicodeObject *s)) |
| 4068 | { |
| 4069 | |
| 4070 | PyUnicodeObject *u; |
| 4071 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4072 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4073 | if (u == NULL) |
| 4074 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4075 | |
| 4076 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 4077 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4078 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4079 | /* fixfct should return TRUE if it modified the buffer. If |
| 4080 | FALSE, return a reference to the original buffer instead |
| 4081 | (to save space, not time) */ |
| 4082 | Py_INCREF(self); |
| 4083 | Py_DECREF(u); |
| 4084 | return (PyObject*) self; |
| 4085 | } |
| 4086 | return (PyObject*) u; |
| 4087 | } |
| 4088 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4089 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4090 | int fixupper(PyUnicodeObject *self) |
| 4091 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4092 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4093 | Py_UNICODE *s = self->str; |
| 4094 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4095 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4096 | while (len-- > 0) { |
| 4097 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4098 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4099 | ch = Py_UNICODE_TOUPPER(*s); |
| 4100 | if (ch != *s) { |
| 4101 | status = 1; |
| 4102 | *s = ch; |
| 4103 | } |
| 4104 | s++; |
| 4105 | } |
| 4106 | |
| 4107 | return status; |
| 4108 | } |
| 4109 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4110 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4111 | int fixlower(PyUnicodeObject *self) |
| 4112 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4113 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4114 | Py_UNICODE *s = self->str; |
| 4115 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4116 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4117 | while (len-- > 0) { |
| 4118 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4119 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4120 | ch = Py_UNICODE_TOLOWER(*s); |
| 4121 | if (ch != *s) { |
| 4122 | status = 1; |
| 4123 | *s = ch; |
| 4124 | } |
| 4125 | s++; |
| 4126 | } |
| 4127 | |
| 4128 | return status; |
| 4129 | } |
| 4130 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4131 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4132 | int fixswapcase(PyUnicodeObject *self) |
| 4133 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4134 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4135 | Py_UNICODE *s = self->str; |
| 4136 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4137 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4138 | while (len-- > 0) { |
| 4139 | if (Py_UNICODE_ISUPPER(*s)) { |
| 4140 | *s = Py_UNICODE_TOLOWER(*s); |
| 4141 | status = 1; |
| 4142 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 4143 | *s = Py_UNICODE_TOUPPER(*s); |
| 4144 | status = 1; |
| 4145 | } |
| 4146 | s++; |
| 4147 | } |
| 4148 | |
| 4149 | return status; |
| 4150 | } |
| 4151 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4152 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4153 | int fixcapitalize(PyUnicodeObject *self) |
| 4154 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4155 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 4156 | Py_UNICODE *s = self->str; |
| 4157 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4158 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 4159 | if (len == 0) |
| 4160 | return 0; |
| 4161 | if (Py_UNICODE_ISLOWER(*s)) { |
| 4162 | *s = Py_UNICODE_TOUPPER(*s); |
| 4163 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4164 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 4165 | s++; |
| 4166 | while (--len > 0) { |
| 4167 | if (Py_UNICODE_ISUPPER(*s)) { |
| 4168 | *s = Py_UNICODE_TOLOWER(*s); |
| 4169 | status = 1; |
| 4170 | } |
| 4171 | s++; |
| 4172 | } |
| 4173 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4174 | } |
| 4175 | |
| 4176 | static |
| 4177 | int fixtitle(PyUnicodeObject *self) |
| 4178 | { |
| 4179 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 4180 | register Py_UNICODE *e; |
| 4181 | int previous_is_cased; |
| 4182 | |
| 4183 | /* Shortcut for single character strings */ |
| 4184 | if (PyUnicode_GET_SIZE(self) == 1) { |
| 4185 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 4186 | if (*p != ch) { |
| 4187 | *p = ch; |
| 4188 | return 1; |
| 4189 | } |
| 4190 | else |
| 4191 | return 0; |
| 4192 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4193 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4194 | e = p + PyUnicode_GET_SIZE(self); |
| 4195 | previous_is_cased = 0; |
| 4196 | for (; p < e; p++) { |
| 4197 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4198 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4199 | if (previous_is_cased) |
| 4200 | *p = Py_UNICODE_TOLOWER(ch); |
| 4201 | else |
| 4202 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4203 | |
| 4204 | if (Py_UNICODE_ISLOWER(ch) || |
| 4205 | Py_UNICODE_ISUPPER(ch) || |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4206 | Py_UNICODE_ISTITLE(ch)) |
| 4207 | previous_is_cased = 1; |
| 4208 | else |
| 4209 | previous_is_cased = 0; |
| 4210 | } |
| 4211 | return 1; |
| 4212 | } |
| 4213 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4214 | PyObject * |
| 4215 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4216 | { |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4217 | PyObject *internal_separator = NULL; |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 4218 | const Py_UNICODE blank = ' '; |
| 4219 | const Py_UNICODE *sep = ␣ |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 4220 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4221 | PyUnicodeObject *res = NULL; /* the result */ |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 4222 | Py_ssize_t res_alloc = 100; /* # allocated bytes for string in res */ |
| 4223 | Py_ssize_t res_used; /* # used bytes */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4224 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 4225 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4226 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4227 | PyObject *item; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4228 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4229 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4230 | fseq = PySequence_Fast(seq, ""); |
| 4231 | if (fseq == NULL) { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4232 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4233 | } |
| 4234 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 4235 | /* Grrrr. A codec may be invoked to convert str objects to |
| 4236 | * Unicode, and so it's possible to call back into Python code |
| 4237 | * during PyUnicode_FromObject(), and so it's possible for a sick |
| 4238 | * codec to change the size of fseq (if seq is a list). Therefore |
| 4239 | * we have to keep refetching the size -- can't assume seqlen |
| 4240 | * is invariant. |
| 4241 | */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4242 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 4243 | /* If empty sequence, return u"". */ |
| 4244 | if (seqlen == 0) { |
| 4245 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 4246 | goto Done; |
| 4247 | } |
| 4248 | /* If singleton sequence with an exact Unicode, return that. */ |
| 4249 | if (seqlen == 1) { |
| 4250 | item = PySequence_Fast_GET_ITEM(fseq, 0); |
| 4251 | if (PyUnicode_CheckExact(item)) { |
| 4252 | Py_INCREF(item); |
| 4253 | res = (PyUnicodeObject *)item; |
| 4254 | goto Done; |
| 4255 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4256 | } |
| 4257 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4258 | /* At least two items to join, or one that isn't exact Unicode. */ |
| 4259 | if (seqlen > 1) { |
| 4260 | /* Set up sep and seplen -- they're needed. */ |
| 4261 | if (separator == NULL) { |
| 4262 | sep = ␣ |
| 4263 | seplen = 1; |
| 4264 | } |
| 4265 | else { |
| 4266 | internal_separator = PyUnicode_FromObject(separator); |
| 4267 | if (internal_separator == NULL) |
| 4268 | goto onError; |
| 4269 | sep = PyUnicode_AS_UNICODE(internal_separator); |
| 4270 | seplen = PyUnicode_GET_SIZE(internal_separator); |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 4271 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 4272 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4273 | } |
| 4274 | } |
| 4275 | |
| 4276 | /* Get space. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4277 | res = _PyUnicode_New(res_alloc); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4278 | if (res == NULL) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4279 | goto onError; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4280 | res_p = PyUnicode_AS_UNICODE(res); |
| 4281 | res_used = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4282 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4283 | for (i = 0; i < seqlen; ++i) { |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 4284 | Py_ssize_t itemlen; |
| 4285 | Py_ssize_t new_res_used; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4286 | |
| 4287 | item = PySequence_Fast_GET_ITEM(fseq, i); |
| 4288 | /* Convert item to Unicode. */ |
| 4289 | if (! PyUnicode_Check(item) && ! PyString_Check(item)) { |
| 4290 | PyErr_Format(PyExc_TypeError, |
Thomas Wouters | 715a4cd | 2006-04-16 22:04:49 +0000 | [diff] [blame] | 4291 | "sequence item %zd: expected string or Unicode," |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4292 | " %.80s found", |
| 4293 | i, item->ob_type->tp_name); |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 4294 | goto onError; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4295 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4296 | item = PyUnicode_FromObject(item); |
| 4297 | if (item == NULL) |
| 4298 | goto onError; |
| 4299 | /* We own a reference to item from here on. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4300 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 4301 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 4302 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 4303 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4304 | /* 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] | 4305 | itemlen = PyUnicode_GET_SIZE(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4306 | new_res_used = res_used + itemlen; |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 4307 | if (new_res_used <= 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4308 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4309 | if (i < seqlen - 1) { |
| 4310 | new_res_used += seplen; |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 4311 | if (new_res_used <= 0) |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4312 | goto Overflow; |
| 4313 | } |
| 4314 | if (new_res_used > res_alloc) { |
| 4315 | /* double allocated size until it's big enough */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4316 | do { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4317 | res_alloc += res_alloc; |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 4318 | if (res_alloc <= 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4319 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4320 | } while (new_res_used > res_alloc); |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4321 | if (_PyUnicode_Resize(&res, res_alloc) < 0) { |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 4322 | Py_DECREF(item); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4323 | goto onError; |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 4324 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4325 | res_p = PyUnicode_AS_UNICODE(res) + res_used; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4326 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4327 | |
| 4328 | /* Copy item, and maybe the separator. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4329 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4330 | res_p += itemlen; |
| 4331 | if (i < seqlen - 1) { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4332 | Py_UNICODE_COPY(res_p, sep, seplen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4333 | res_p += seplen; |
| 4334 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4335 | Py_DECREF(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4336 | res_used = new_res_used; |
| 4337 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4338 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4339 | /* Shrink res to match the used area; this probably can't fail, |
| 4340 | * but it's cheap to check. |
| 4341 | */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4342 | if (_PyUnicode_Resize(&res, res_used) < 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4343 | goto onError; |
| 4344 | |
| 4345 | Done: |
| 4346 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4347 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4348 | return (PyObject *)res; |
| 4349 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4350 | Overflow: |
| 4351 | PyErr_SetString(PyExc_OverflowError, |
| 4352 | "join() is too long for a Python string"); |
| 4353 | Py_DECREF(item); |
| 4354 | /* fall through */ |
| 4355 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4356 | onError: |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4357 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4358 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4359 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4360 | return NULL; |
| 4361 | } |
| 4362 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4363 | static |
| 4364 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4365 | Py_ssize_t left, |
| 4366 | Py_ssize_t right, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4367 | Py_UNICODE fill) |
| 4368 | { |
| 4369 | PyUnicodeObject *u; |
| 4370 | |
| 4371 | if (left < 0) |
| 4372 | left = 0; |
| 4373 | if (right < 0) |
| 4374 | right = 0; |
| 4375 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4376 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4377 | Py_INCREF(self); |
| 4378 | return self; |
| 4379 | } |
| 4380 | |
| 4381 | u = _PyUnicode_New(left + self->length + right); |
| 4382 | if (u) { |
| 4383 | if (left) |
| 4384 | Py_UNICODE_FILL(u->str, fill, left); |
| 4385 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 4386 | if (right) |
| 4387 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 4388 | } |
| 4389 | |
| 4390 | return u; |
| 4391 | } |
| 4392 | |
| 4393 | #define SPLIT_APPEND(data, left, right) \ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4394 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4395 | if (!str) \ |
| 4396 | goto onError; \ |
| 4397 | if (PyList_Append(list, str)) { \ |
| 4398 | Py_DECREF(str); \ |
| 4399 | goto onError; \ |
| 4400 | } \ |
| 4401 | else \ |
| 4402 | Py_DECREF(str); |
| 4403 | |
| 4404 | static |
| 4405 | PyObject *split_whitespace(PyUnicodeObject *self, |
| 4406 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4407 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4408 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4409 | register Py_ssize_t i; |
| 4410 | register Py_ssize_t j; |
| 4411 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4412 | PyObject *str; |
| 4413 | |
| 4414 | for (i = j = 0; i < len; ) { |
| 4415 | /* find a token */ |
| 4416 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 4417 | i++; |
| 4418 | j = i; |
| 4419 | while (i < len && !Py_UNICODE_ISSPACE(self->str[i])) |
| 4420 | i++; |
| 4421 | if (j < i) { |
| 4422 | if (maxcount-- <= 0) |
| 4423 | break; |
| 4424 | SPLIT_APPEND(self->str, j, i); |
| 4425 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 4426 | i++; |
| 4427 | j = i; |
| 4428 | } |
| 4429 | } |
| 4430 | if (j < len) { |
| 4431 | SPLIT_APPEND(self->str, j, len); |
| 4432 | } |
| 4433 | return list; |
| 4434 | |
| 4435 | onError: |
| 4436 | Py_DECREF(list); |
| 4437 | return NULL; |
| 4438 | } |
| 4439 | |
| 4440 | PyObject *PyUnicode_Splitlines(PyObject *string, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4441 | int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4442 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4443 | register Py_ssize_t i; |
| 4444 | register Py_ssize_t j; |
| 4445 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4446 | PyObject *list; |
| 4447 | PyObject *str; |
| 4448 | Py_UNICODE *data; |
| 4449 | |
| 4450 | string = PyUnicode_FromObject(string); |
| 4451 | if (string == NULL) |
| 4452 | return NULL; |
| 4453 | data = PyUnicode_AS_UNICODE(string); |
| 4454 | len = PyUnicode_GET_SIZE(string); |
| 4455 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4456 | list = PyList_New(0); |
| 4457 | if (!list) |
| 4458 | goto onError; |
| 4459 | |
| 4460 | for (i = j = 0; i < len; ) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4461 | Py_ssize_t eol; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4462 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4463 | /* Find a line and append it */ |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 4464 | while (i < len && !BLOOM_LINEBREAK(data[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4465 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4466 | |
| 4467 | /* Skip the line break reading CRLF as one line break */ |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4468 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4469 | if (i < len) { |
| 4470 | if (data[i] == '\r' && i + 1 < len && |
| 4471 | data[i+1] == '\n') |
| 4472 | i += 2; |
| 4473 | else |
| 4474 | i++; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4475 | if (keepends) |
| 4476 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4477 | } |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4478 | SPLIT_APPEND(data, j, eol); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4479 | j = i; |
| 4480 | } |
| 4481 | if (j < len) { |
| 4482 | SPLIT_APPEND(data, j, len); |
| 4483 | } |
| 4484 | |
| 4485 | Py_DECREF(string); |
| 4486 | return list; |
| 4487 | |
| 4488 | onError: |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 4489 | Py_XDECREF(list); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4490 | Py_DECREF(string); |
| 4491 | return NULL; |
| 4492 | } |
| 4493 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4494 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4495 | PyObject *split_char(PyUnicodeObject *self, |
| 4496 | PyObject *list, |
| 4497 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4498 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4499 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4500 | register Py_ssize_t i; |
| 4501 | register Py_ssize_t j; |
| 4502 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4503 | PyObject *str; |
| 4504 | |
| 4505 | for (i = j = 0; i < len; ) { |
| 4506 | if (self->str[i] == ch) { |
| 4507 | if (maxcount-- <= 0) |
| 4508 | break; |
| 4509 | SPLIT_APPEND(self->str, j, i); |
| 4510 | i = j = i + 1; |
| 4511 | } else |
| 4512 | i++; |
| 4513 | } |
| 4514 | if (j <= len) { |
| 4515 | SPLIT_APPEND(self->str, j, len); |
| 4516 | } |
| 4517 | return list; |
| 4518 | |
| 4519 | onError: |
| 4520 | Py_DECREF(list); |
| 4521 | return NULL; |
| 4522 | } |
| 4523 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4524 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4525 | PyObject *split_substring(PyUnicodeObject *self, |
| 4526 | PyObject *list, |
| 4527 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4528 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4529 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4530 | register Py_ssize_t i; |
| 4531 | register Py_ssize_t j; |
| 4532 | Py_ssize_t len = self->length; |
| 4533 | Py_ssize_t sublen = substring->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4534 | PyObject *str; |
| 4535 | |
Guido van Rossum | cda4f9a | 2000-12-19 02:23:19 +0000 | [diff] [blame] | 4536 | for (i = j = 0; i <= len - sublen; ) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4537 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 4538 | if (maxcount-- <= 0) |
| 4539 | break; |
| 4540 | SPLIT_APPEND(self->str, j, i); |
| 4541 | i = j = i + sublen; |
| 4542 | } else |
| 4543 | i++; |
| 4544 | } |
| 4545 | if (j <= len) { |
| 4546 | SPLIT_APPEND(self->str, j, len); |
| 4547 | } |
| 4548 | return list; |
| 4549 | |
| 4550 | onError: |
| 4551 | Py_DECREF(list); |
| 4552 | return NULL; |
| 4553 | } |
| 4554 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4555 | static |
| 4556 | PyObject *rsplit_whitespace(PyUnicodeObject *self, |
| 4557 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4558 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4559 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4560 | register Py_ssize_t i; |
| 4561 | register Py_ssize_t j; |
| 4562 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4563 | PyObject *str; |
| 4564 | |
| 4565 | for (i = j = len - 1; i >= 0; ) { |
| 4566 | /* find a token */ |
| 4567 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 4568 | i--; |
| 4569 | j = i; |
| 4570 | while (i >= 0 && !Py_UNICODE_ISSPACE(self->str[i])) |
| 4571 | i--; |
| 4572 | if (j > i) { |
| 4573 | if (maxcount-- <= 0) |
| 4574 | break; |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 4575 | SPLIT_APPEND(self->str, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4576 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 4577 | i--; |
| 4578 | j = i; |
| 4579 | } |
| 4580 | } |
| 4581 | if (j >= 0) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 4582 | SPLIT_APPEND(self->str, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4583 | } |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 4584 | if (PyList_Reverse(list) < 0) |
| 4585 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4586 | return list; |
| 4587 | |
| 4588 | onError: |
| 4589 | Py_DECREF(list); |
| 4590 | return NULL; |
| 4591 | } |
| 4592 | |
| 4593 | static |
| 4594 | PyObject *rsplit_char(PyUnicodeObject *self, |
| 4595 | PyObject *list, |
| 4596 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4597 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4598 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4599 | register Py_ssize_t i; |
| 4600 | register Py_ssize_t j; |
| 4601 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4602 | PyObject *str; |
| 4603 | |
| 4604 | for (i = j = len - 1; i >= 0; ) { |
| 4605 | if (self->str[i] == ch) { |
| 4606 | if (maxcount-- <= 0) |
| 4607 | break; |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 4608 | SPLIT_APPEND(self->str, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4609 | j = i = i - 1; |
| 4610 | } else |
| 4611 | i--; |
| 4612 | } |
Hye-Shik Chang | 7fc4cf5 | 2003-12-23 09:10:16 +0000 | [diff] [blame] | 4613 | if (j >= -1) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 4614 | SPLIT_APPEND(self->str, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4615 | } |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 4616 | if (PyList_Reverse(list) < 0) |
| 4617 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4618 | return list; |
| 4619 | |
| 4620 | onError: |
| 4621 | Py_DECREF(list); |
| 4622 | return NULL; |
| 4623 | } |
| 4624 | |
| 4625 | static |
| 4626 | PyObject *rsplit_substring(PyUnicodeObject *self, |
| 4627 | PyObject *list, |
| 4628 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4629 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4630 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4631 | register Py_ssize_t i; |
| 4632 | register Py_ssize_t j; |
| 4633 | Py_ssize_t len = self->length; |
| 4634 | Py_ssize_t sublen = substring->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4635 | PyObject *str; |
| 4636 | |
| 4637 | for (i = len - sublen, j = len; i >= 0; ) { |
| 4638 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 4639 | if (maxcount-- <= 0) |
| 4640 | break; |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 4641 | SPLIT_APPEND(self->str, i + sublen, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4642 | j = i; |
| 4643 | i -= sublen; |
| 4644 | } else |
| 4645 | i--; |
| 4646 | } |
| 4647 | if (j >= 0) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 4648 | SPLIT_APPEND(self->str, 0, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4649 | } |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 4650 | if (PyList_Reverse(list) < 0) |
| 4651 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4652 | return list; |
| 4653 | |
| 4654 | onError: |
| 4655 | Py_DECREF(list); |
| 4656 | return NULL; |
| 4657 | } |
| 4658 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4659 | #undef SPLIT_APPEND |
| 4660 | |
| 4661 | static |
| 4662 | PyObject *split(PyUnicodeObject *self, |
| 4663 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4664 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4665 | { |
| 4666 | PyObject *list; |
| 4667 | |
| 4668 | if (maxcount < 0) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4669 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4670 | |
| 4671 | list = PyList_New(0); |
| 4672 | if (!list) |
| 4673 | return NULL; |
| 4674 | |
| 4675 | if (substring == NULL) |
| 4676 | return split_whitespace(self,list,maxcount); |
| 4677 | |
| 4678 | else if (substring->length == 1) |
| 4679 | return split_char(self,list,substring->str[0],maxcount); |
| 4680 | |
| 4681 | else if (substring->length == 0) { |
| 4682 | Py_DECREF(list); |
| 4683 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 4684 | return NULL; |
| 4685 | } |
| 4686 | else |
| 4687 | return split_substring(self,list,substring,maxcount); |
| 4688 | } |
| 4689 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4690 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4691 | PyObject *rsplit(PyUnicodeObject *self, |
| 4692 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4693 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4694 | { |
| 4695 | PyObject *list; |
| 4696 | |
| 4697 | if (maxcount < 0) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4698 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4699 | |
| 4700 | list = PyList_New(0); |
| 4701 | if (!list) |
| 4702 | return NULL; |
| 4703 | |
| 4704 | if (substring == NULL) |
| 4705 | return rsplit_whitespace(self,list,maxcount); |
| 4706 | |
| 4707 | else if (substring->length == 1) |
| 4708 | return rsplit_char(self,list,substring->str[0],maxcount); |
| 4709 | |
| 4710 | else if (substring->length == 0) { |
| 4711 | Py_DECREF(list); |
| 4712 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 4713 | return NULL; |
| 4714 | } |
| 4715 | else |
| 4716 | return rsplit_substring(self,list,substring,maxcount); |
| 4717 | } |
| 4718 | |
| 4719 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4720 | PyObject *replace(PyUnicodeObject *self, |
| 4721 | PyUnicodeObject *str1, |
| 4722 | PyUnicodeObject *str2, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4723 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4724 | { |
| 4725 | PyUnicodeObject *u; |
| 4726 | |
| 4727 | if (maxcount < 0) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4728 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4729 | |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 4730 | if (str1->length == str2->length) { |
| 4731 | /* same length */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4732 | Py_ssize_t i; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 4733 | if (str1->length == 1) { |
| 4734 | /* replace characters */ |
| 4735 | Py_UNICODE u1, u2; |
| 4736 | if (!findchar(self->str, self->length, str1->str[0])) |
| 4737 | goto nothing; |
| 4738 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 4739 | if (!u) |
| 4740 | return NULL; |
| 4741 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 4742 | u1 = str1->str[0]; |
| 4743 | u2 = str2->str[0]; |
| 4744 | for (i = 0; i < u->length; i++) |
| 4745 | if (u->str[i] == u1) { |
| 4746 | if (--maxcount < 0) |
| 4747 | break; |
| 4748 | u->str[i] = u2; |
| 4749 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4750 | } else { |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 4751 | i = fastsearch( |
| 4752 | self->str, self->length, str1->str, str1->length, FAST_SEARCH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4753 | ); |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 4754 | if (i < 0) |
| 4755 | goto nothing; |
| 4756 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 4757 | if (!u) |
| 4758 | return NULL; |
| 4759 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 4760 | while (i <= self->length - str1->length) |
| 4761 | if (Py_UNICODE_MATCH(self, i, str1)) { |
| 4762 | if (--maxcount < 0) |
| 4763 | break; |
| 4764 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 4765 | i += str1->length; |
| 4766 | } else |
| 4767 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4768 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4769 | } else { |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 4770 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4771 | Py_ssize_t n, i; |
Fredrik Lundh | 0c71f88 | 2006-05-25 16:46:54 +0000 | [diff] [blame] | 4772 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4773 | Py_UNICODE *p; |
| 4774 | |
| 4775 | /* replace strings */ |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 4776 | n = stringlib_count(self->str, self->length, str1->str, str1->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4777 | if (n > maxcount) |
| 4778 | n = maxcount; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 4779 | if (n == 0) |
| 4780 | goto nothing; |
Fredrik Lundh | 0c71f88 | 2006-05-25 16:46:54 +0000 | [diff] [blame] | 4781 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 4782 | delta = (str2->length - str1->length); |
| 4783 | if (delta == 0) { |
| 4784 | new_size = self->length; |
| 4785 | } else { |
| 4786 | product = n * (str2->length - str1->length); |
| 4787 | if ((product / (str2->length - str1->length)) != n) { |
| 4788 | PyErr_SetString(PyExc_OverflowError, |
| 4789 | "replace string is too long"); |
| 4790 | return NULL; |
| 4791 | } |
| 4792 | new_size = self->length + product; |
| 4793 | if (new_size < 0) { |
| 4794 | PyErr_SetString(PyExc_OverflowError, |
| 4795 | "replace string is too long"); |
| 4796 | return NULL; |
| 4797 | } |
| 4798 | } |
| 4799 | u = _PyUnicode_New(new_size); |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 4800 | if (!u) |
| 4801 | return NULL; |
| 4802 | i = 0; |
| 4803 | p = u->str; |
| 4804 | if (str1->length > 0) { |
| 4805 | while (i <= self->length - str1->length) |
| 4806 | if (Py_UNICODE_MATCH(self, i, str1)) { |
| 4807 | /* replace string segment */ |
| 4808 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 4809 | p += str2->length; |
| 4810 | i += str1->length; |
| 4811 | if (--n <= 0) { |
| 4812 | /* copy remaining part */ |
| 4813 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 4814 | break; |
Guido van Rossum | 8b1a6d6 | 2002-08-23 18:21:28 +0000 | [diff] [blame] | 4815 | } |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 4816 | } else |
| 4817 | *p++ = self->str[i++]; |
| 4818 | } else { |
| 4819 | while (n > 0) { |
| 4820 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 4821 | p += str2->length; |
| 4822 | if (--n <= 0) |
| 4823 | break; |
| 4824 | *p++ = self->str[i++]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4825 | } |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 4826 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4827 | } |
| 4828 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4829 | return (PyObject *) u; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 4830 | |
| 4831 | nothing: |
| 4832 | /* nothing to replace; return original string (when possible) */ |
| 4833 | if (PyUnicode_CheckExact(self)) { |
| 4834 | Py_INCREF(self); |
| 4835 | return (PyObject *) self; |
| 4836 | } |
| 4837 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4838 | } |
| 4839 | |
| 4840 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 4841 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4842 | PyDoc_STRVAR(title__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4843 | "S.title() -> unicode\n\ |
| 4844 | \n\ |
| 4845 | 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] | 4846 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4847 | |
| 4848 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 4849 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4850 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4851 | return fixup(self, fixtitle); |
| 4852 | } |
| 4853 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4854 | PyDoc_STRVAR(capitalize__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4855 | "S.capitalize() -> unicode\n\ |
| 4856 | \n\ |
| 4857 | 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] | 4858 | have upper case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4859 | |
| 4860 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 4861 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4862 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4863 | return fixup(self, fixcapitalize); |
| 4864 | } |
| 4865 | |
| 4866 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4867 | PyDoc_STRVAR(capwords__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4868 | "S.capwords() -> unicode\n\ |
| 4869 | \n\ |
| 4870 | 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] | 4871 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4872 | |
| 4873 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 4874 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4875 | { |
| 4876 | PyObject *list; |
| 4877 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4878 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4879 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4880 | /* Split into words */ |
| 4881 | list = split(self, NULL, -1); |
| 4882 | if (!list) |
| 4883 | return NULL; |
| 4884 | |
| 4885 | /* Capitalize each word */ |
| 4886 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 4887 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
| 4888 | fixcapitalize); |
| 4889 | if (item == NULL) |
| 4890 | goto onError; |
| 4891 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 4892 | PyList_SET_ITEM(list, i, item); |
| 4893 | } |
| 4894 | |
| 4895 | /* Join the words to form a new string */ |
| 4896 | item = PyUnicode_Join(NULL, list); |
| 4897 | |
| 4898 | onError: |
| 4899 | Py_DECREF(list); |
| 4900 | return (PyObject *)item; |
| 4901 | } |
| 4902 | #endif |
| 4903 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4904 | /* Argument converter. Coerces to a single unicode character */ |
| 4905 | |
| 4906 | static int |
| 4907 | convert_uc(PyObject *obj, void *addr) |
| 4908 | { |
| 4909 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 4910 | PyObject *uniobj; |
| 4911 | Py_UNICODE *unistr; |
| 4912 | |
| 4913 | uniobj = PyUnicode_FromObject(obj); |
| 4914 | if (uniobj == NULL) { |
| 4915 | PyErr_SetString(PyExc_TypeError, |
| 4916 | "The fill character cannot be converted to Unicode"); |
| 4917 | return 0; |
| 4918 | } |
| 4919 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 4920 | PyErr_SetString(PyExc_TypeError, |
| 4921 | "The fill character must be exactly one character long"); |
| 4922 | Py_DECREF(uniobj); |
| 4923 | return 0; |
| 4924 | } |
| 4925 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 4926 | *fillcharloc = unistr[0]; |
| 4927 | Py_DECREF(uniobj); |
| 4928 | return 1; |
| 4929 | } |
| 4930 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4931 | PyDoc_STRVAR(center__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4932 | "S.center(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4933 | \n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4934 | Return S centered in a Unicode string of length width. Padding is\n\ |
| 4935 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4936 | |
| 4937 | static PyObject * |
| 4938 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 4939 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4940 | Py_ssize_t marg, left; |
| 4941 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4942 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4943 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 4944 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4945 | return NULL; |
| 4946 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4947 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4948 | Py_INCREF(self); |
| 4949 | return (PyObject*) self; |
| 4950 | } |
| 4951 | |
| 4952 | marg = width - self->length; |
| 4953 | left = marg / 2 + (marg & width & 1); |
| 4954 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 4955 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4956 | } |
| 4957 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4958 | #if 0 |
| 4959 | |
| 4960 | /* This code should go into some future Unicode collation support |
| 4961 | module. The basic comparison should compare ordinals on a naive |
Trent Mick | 20abf57 | 2000-08-12 22:14:34 +0000 | [diff] [blame] | 4962 | basis (this is what Java does and thus JPython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 4963 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4964 | /* speedy UTF-16 code point order comparison */ |
| 4965 | /* gleaned from: */ |
| 4966 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 4967 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4968 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4969 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4970 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4971 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 4972 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 4973 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4974 | }; |
| 4975 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4976 | static int |
| 4977 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 4978 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4979 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4980 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4981 | Py_UNICODE *s1 = str1->str; |
| 4982 | Py_UNICODE *s2 = str2->str; |
| 4983 | |
| 4984 | len1 = str1->length; |
| 4985 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4986 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4987 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4988 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4989 | |
| 4990 | c1 = *s1++; |
| 4991 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 4992 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4993 | if (c1 > (1<<11) * 26) |
| 4994 | c1 += utf16Fixup[c1>>11]; |
| 4995 | if (c2 > (1<<11) * 26) |
| 4996 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 4997 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 4998 | |
| 4999 | if (c1 != c2) |
| 5000 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5001 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5002 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5003 | } |
| 5004 | |
| 5005 | return (len1 < len2) ? -1 : (len1 != len2); |
| 5006 | } |
| 5007 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5008 | #else |
| 5009 | |
| 5010 | static int |
| 5011 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 5012 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5013 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5014 | |
| 5015 | Py_UNICODE *s1 = str1->str; |
| 5016 | Py_UNICODE *s2 = str2->str; |
| 5017 | |
| 5018 | len1 = str1->length; |
| 5019 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5020 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5021 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5022 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5023 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 5024 | c1 = *s1++; |
| 5025 | c2 = *s2++; |
| 5026 | |
| 5027 | if (c1 != c2) |
| 5028 | return (c1 < c2) ? -1 : 1; |
| 5029 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5030 | len1--; len2--; |
| 5031 | } |
| 5032 | |
| 5033 | return (len1 < len2) ? -1 : (len1 != len2); |
| 5034 | } |
| 5035 | |
| 5036 | #endif |
| 5037 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5038 | int PyUnicode_Compare(PyObject *left, |
| 5039 | PyObject *right) |
| 5040 | { |
| 5041 | PyUnicodeObject *u = NULL, *v = NULL; |
| 5042 | int result; |
| 5043 | |
| 5044 | /* Coerce the two arguments */ |
| 5045 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 5046 | if (u == NULL) |
| 5047 | goto onError; |
| 5048 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 5049 | if (v == NULL) |
| 5050 | goto onError; |
| 5051 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5052 | /* Shortcut for empty or interned objects */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5053 | if (v == u) { |
| 5054 | Py_DECREF(u); |
| 5055 | Py_DECREF(v); |
| 5056 | return 0; |
| 5057 | } |
| 5058 | |
| 5059 | result = unicode_compare(u, v); |
| 5060 | |
| 5061 | Py_DECREF(u); |
| 5062 | Py_DECREF(v); |
| 5063 | return result; |
| 5064 | |
| 5065 | onError: |
| 5066 | Py_XDECREF(u); |
| 5067 | Py_XDECREF(v); |
| 5068 | return -1; |
| 5069 | } |
| 5070 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5071 | int PyUnicode_Contains(PyObject *container, |
| 5072 | PyObject *element) |
| 5073 | { |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 5074 | PyUnicodeObject *u, *v; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5075 | Py_ssize_t size; |
Fredrik Lundh | d5e0dc5 | 2006-05-24 15:11:01 +0000 | [diff] [blame] | 5076 | Py_ssize_t pos; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5077 | |
| 5078 | /* Coerce the two arguments */ |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 5079 | v = (PyUnicodeObject *) PyUnicode_FromObject(element); |
| 5080 | if (!v) { |
Marc-André Lemburg | 7c01468 | 2000-06-28 08:11:47 +0000 | [diff] [blame] | 5081 | PyErr_SetString(PyExc_TypeError, |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 5082 | "'in <string>' requires string as left operand"); |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 5083 | return -1; |
Marc-André Lemburg | 7c01468 | 2000-06-28 08:11:47 +0000 | [diff] [blame] | 5084 | } |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 5085 | |
| 5086 | u = (PyUnicodeObject *) PyUnicode_FromObject(container); |
| 5087 | if (!u) { |
| 5088 | Py_DECREF(v); |
| 5089 | return -1; |
| 5090 | } |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5091 | |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 5092 | size = PyUnicode_GET_SIZE(v); |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 5093 | if (!size) { |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 5094 | pos = 0; |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 5095 | goto done; |
| 5096 | } |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 5097 | |
Fredrik Lundh | d5e0dc5 | 2006-05-24 15:11:01 +0000 | [diff] [blame] | 5098 | pos = fastsearch( |
| 5099 | PyUnicode_AS_UNICODE(u), PyUnicode_GET_SIZE(u), |
| 5100 | PyUnicode_AS_UNICODE(v), size, FAST_SEARCH |
| 5101 | ); |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5102 | |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 5103 | done: |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5104 | Py_DECREF(u); |
| 5105 | Py_DECREF(v); |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 5106 | return (pos != -1); |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5107 | } |
| 5108 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5109 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 5110 | |
| 5111 | PyObject *PyUnicode_Concat(PyObject *left, |
| 5112 | PyObject *right) |
| 5113 | { |
| 5114 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 5115 | |
| 5116 | /* Coerce the two arguments */ |
| 5117 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 5118 | if (u == NULL) |
| 5119 | goto onError; |
| 5120 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 5121 | if (v == NULL) |
| 5122 | goto onError; |
| 5123 | |
| 5124 | /* Shortcuts */ |
| 5125 | if (v == unicode_empty) { |
| 5126 | Py_DECREF(v); |
| 5127 | return (PyObject *)u; |
| 5128 | } |
| 5129 | if (u == unicode_empty) { |
| 5130 | Py_DECREF(u); |
| 5131 | return (PyObject *)v; |
| 5132 | } |
| 5133 | |
| 5134 | /* Concat the two Unicode strings */ |
| 5135 | w = _PyUnicode_New(u->length + v->length); |
| 5136 | if (w == NULL) |
| 5137 | goto onError; |
| 5138 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 5139 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 5140 | |
| 5141 | Py_DECREF(u); |
| 5142 | Py_DECREF(v); |
| 5143 | return (PyObject *)w; |
| 5144 | |
| 5145 | onError: |
| 5146 | Py_XDECREF(u); |
| 5147 | Py_XDECREF(v); |
| 5148 | return NULL; |
| 5149 | } |
| 5150 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5151 | PyDoc_STRVAR(count__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5152 | "S.count(sub[, start[, end]]) -> int\n\ |
| 5153 | \n\ |
Fredrik Lundh | 763b50f | 2006-05-22 15:35:12 +0000 | [diff] [blame] | 5154 | Return the number of non-overlapping occurrences of substring sub in\n\ |
| 5155 | 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] | 5156 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5157 | |
| 5158 | static PyObject * |
| 5159 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 5160 | { |
| 5161 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5162 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5163 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5164 | PyObject *result; |
| 5165 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 5166 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
| 5167 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5168 | return NULL; |
| 5169 | |
| 5170 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5171 | (PyObject *)substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5172 | if (substring == NULL) |
| 5173 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5174 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5175 | if (start < 0) |
| 5176 | start += self->length; |
| 5177 | if (start < 0) |
| 5178 | start = 0; |
| 5179 | if (end > self->length) |
| 5180 | end = self->length; |
| 5181 | if (end < 0) |
| 5182 | end += self->length; |
| 5183 | if (end < 0) |
| 5184 | end = 0; |
| 5185 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5186 | result = PyInt_FromSsize_t( |
| 5187 | stringlib_count(self->str + start, end - start, |
| 5188 | substring->str, substring->length) |
| 5189 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5190 | |
| 5191 | Py_DECREF(substring); |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5192 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5193 | return result; |
| 5194 | } |
| 5195 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5196 | PyDoc_STRVAR(encode__doc__, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5197 | "S.encode([encoding[,errors]]) -> string or unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5198 | \n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5199 | Encodes S using the codec registered for encoding. encoding defaults\n\ |
| 5200 | 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] | 5201 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5202 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 5203 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 5204 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5205 | |
| 5206 | static PyObject * |
| 5207 | unicode_encode(PyUnicodeObject *self, PyObject *args) |
| 5208 | { |
| 5209 | char *encoding = NULL; |
| 5210 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5211 | PyObject *v; |
| 5212 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5213 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 5214 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5215 | v = PyUnicode_AsEncodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5216 | if (v == NULL) |
| 5217 | goto onError; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5218 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
| 5219 | PyErr_Format(PyExc_TypeError, |
| 5220 | "encoder did not return a string/unicode object " |
| 5221 | "(type=%.400s)", |
| 5222 | v->ob_type->tp_name); |
| 5223 | Py_DECREF(v); |
| 5224 | return NULL; |
| 5225 | } |
| 5226 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5227 | |
| 5228 | onError: |
| 5229 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5230 | } |
| 5231 | |
| 5232 | PyDoc_STRVAR(decode__doc__, |
| 5233 | "S.decode([encoding[,errors]]) -> string or unicode\n\ |
| 5234 | \n\ |
| 5235 | Decodes S using the codec registered for encoding. encoding defaults\n\ |
| 5236 | to the default encoding. errors may be given to set a different error\n\ |
| 5237 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
| 5238 | a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ |
| 5239 | as well as any other name registerd with codecs.register_error that is\n\ |
| 5240 | able to handle UnicodeDecodeErrors."); |
| 5241 | |
| 5242 | static PyObject * |
Marc-André Lemburg | 126b44c | 2004-07-10 12:04:20 +0000 | [diff] [blame] | 5243 | unicode_decode(PyUnicodeObject *self, PyObject *args) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5244 | { |
| 5245 | char *encoding = NULL; |
| 5246 | char *errors = NULL; |
| 5247 | PyObject *v; |
| 5248 | |
| 5249 | if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors)) |
| 5250 | return NULL; |
| 5251 | v = PyUnicode_AsDecodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5252 | if (v == NULL) |
| 5253 | goto onError; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5254 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
| 5255 | PyErr_Format(PyExc_TypeError, |
| 5256 | "decoder did not return a string/unicode object " |
| 5257 | "(type=%.400s)", |
| 5258 | v->ob_type->tp_name); |
| 5259 | Py_DECREF(v); |
| 5260 | return NULL; |
| 5261 | } |
| 5262 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5263 | |
| 5264 | onError: |
| 5265 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5266 | } |
| 5267 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5268 | PyDoc_STRVAR(expandtabs__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5269 | "S.expandtabs([tabsize]) -> unicode\n\ |
| 5270 | \n\ |
| 5271 | 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] | 5272 | 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] | 5273 | |
| 5274 | static PyObject* |
| 5275 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 5276 | { |
| 5277 | Py_UNICODE *e; |
| 5278 | Py_UNICODE *p; |
| 5279 | Py_UNICODE *q; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5280 | Py_ssize_t i, j; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5281 | PyUnicodeObject *u; |
| 5282 | int tabsize = 8; |
| 5283 | |
| 5284 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
| 5285 | return NULL; |
| 5286 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5287 | /* First pass: determine size of output string */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5288 | i = j = 0; |
| 5289 | e = self->str + self->length; |
| 5290 | for (p = self->str; p < e; p++) |
| 5291 | if (*p == '\t') { |
| 5292 | if (tabsize > 0) |
| 5293 | j += tabsize - (j % tabsize); |
| 5294 | } |
| 5295 | else { |
| 5296 | j++; |
| 5297 | if (*p == '\n' || *p == '\r') { |
| 5298 | i += j; |
| 5299 | j = 0; |
| 5300 | } |
| 5301 | } |
| 5302 | |
| 5303 | /* Second pass: create output string and fill it */ |
| 5304 | u = _PyUnicode_New(i + j); |
| 5305 | if (!u) |
| 5306 | return NULL; |
| 5307 | |
| 5308 | j = 0; |
| 5309 | q = u->str; |
| 5310 | |
| 5311 | for (p = self->str; p < e; p++) |
| 5312 | if (*p == '\t') { |
| 5313 | if (tabsize > 0) { |
| 5314 | i = tabsize - (j % tabsize); |
| 5315 | j += i; |
| 5316 | while (i--) |
| 5317 | *q++ = ' '; |
| 5318 | } |
| 5319 | } |
| 5320 | else { |
| 5321 | j++; |
| 5322 | *q++ = *p; |
| 5323 | if (*p == '\n' || *p == '\r') |
| 5324 | j = 0; |
| 5325 | } |
| 5326 | |
| 5327 | return (PyObject*) u; |
| 5328 | } |
| 5329 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5330 | PyDoc_STRVAR(find__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5331 | "S.find(sub [,start [,end]]) -> int\n\ |
| 5332 | \n\ |
| 5333 | Return the lowest index in S where substring sub is found,\n\ |
| 5334 | such that sub is contained within s[start,end]. Optional\n\ |
| 5335 | arguments start and end are interpreted as in slice notation.\n\ |
| 5336 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5337 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5338 | |
| 5339 | static PyObject * |
| 5340 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 5341 | { |
| 5342 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5343 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5344 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5345 | PyObject *result; |
| 5346 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 5347 | if (!PyArg_ParseTuple(args, "O|O&O&:find", &substring, |
| 5348 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5349 | return NULL; |
| 5350 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 5351 | (PyObject *)substring); |
| 5352 | if (substring == NULL) |
| 5353 | return NULL; |
| 5354 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5355 | result = PyInt_FromSsize_t(findstring(self, substring, start, end, 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5356 | |
| 5357 | Py_DECREF(substring); |
| 5358 | return result; |
| 5359 | } |
| 5360 | |
| 5361 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5362 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5363 | { |
| 5364 | if (index < 0 || index >= self->length) { |
| 5365 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 5366 | return NULL; |
| 5367 | } |
| 5368 | |
| 5369 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 5370 | } |
| 5371 | |
| 5372 | static long |
| 5373 | unicode_hash(PyUnicodeObject *self) |
| 5374 | { |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 5375 | /* Since Unicode objects compare equal to their ASCII string |
| 5376 | counterparts, they should use the individual character values |
| 5377 | as basis for their hash value. This is needed to assure that |
| 5378 | strings and Unicode objects behave in the same way as |
| 5379 | dictionary keys. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5380 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5381 | register Py_ssize_t len; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 5382 | register Py_UNICODE *p; |
| 5383 | register long x; |
| 5384 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5385 | if (self->hash != -1) |
| 5386 | return self->hash; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 5387 | len = PyUnicode_GET_SIZE(self); |
| 5388 | p = PyUnicode_AS_UNICODE(self); |
| 5389 | x = *p << 7; |
| 5390 | while (--len >= 0) |
| 5391 | x = (1000003*x) ^ *p++; |
| 5392 | x ^= PyUnicode_GET_SIZE(self); |
| 5393 | if (x == -1) |
| 5394 | x = -2; |
| 5395 | self->hash = x; |
| 5396 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5397 | } |
| 5398 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5399 | PyDoc_STRVAR(index__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5400 | "S.index(sub [,start [,end]]) -> int\n\ |
| 5401 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5402 | 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] | 5403 | |
| 5404 | static PyObject * |
| 5405 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 5406 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5407 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5408 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5409 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5410 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5411 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 5412 | if (!PyArg_ParseTuple(args, "O|O&O&:index", &substring, |
| 5413 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5414 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5415 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5416 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 5417 | (PyObject *)substring); |
| 5418 | if (substring == NULL) |
| 5419 | return NULL; |
| 5420 | |
| 5421 | result = findstring(self, substring, start, end, 1); |
| 5422 | |
| 5423 | Py_DECREF(substring); |
| 5424 | if (result < 0) { |
| 5425 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 5426 | return NULL; |
| 5427 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5428 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5429 | } |
| 5430 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5431 | PyDoc_STRVAR(islower__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5432 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5433 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5434 | 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] | 5435 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5436 | |
| 5437 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5438 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5439 | { |
| 5440 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5441 | register const Py_UNICODE *e; |
| 5442 | int cased; |
| 5443 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5444 | /* Shortcut for single character strings */ |
| 5445 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5446 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5447 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5448 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5449 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5450 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5451 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5452 | e = p + PyUnicode_GET_SIZE(self); |
| 5453 | cased = 0; |
| 5454 | for (; p < e; p++) { |
| 5455 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5456 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5457 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5458 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5459 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 5460 | cased = 1; |
| 5461 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5462 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5463 | } |
| 5464 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5465 | PyDoc_STRVAR(isupper__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5466 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5467 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5468 | 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] | 5469 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5470 | |
| 5471 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5472 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5473 | { |
| 5474 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5475 | register const Py_UNICODE *e; |
| 5476 | int cased; |
| 5477 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5478 | /* Shortcut for single character strings */ |
| 5479 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5480 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5481 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5482 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5483 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5484 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5485 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5486 | e = p + PyUnicode_GET_SIZE(self); |
| 5487 | cased = 0; |
| 5488 | for (; p < e; p++) { |
| 5489 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5490 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5491 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5492 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5493 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 5494 | cased = 1; |
| 5495 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5496 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5497 | } |
| 5498 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5499 | PyDoc_STRVAR(istitle__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5500 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5501 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5502 | Return True if S is a titlecased string and there is at least one\n\ |
| 5503 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 5504 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 5505 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5506 | |
| 5507 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5508 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5509 | { |
| 5510 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5511 | register const Py_UNICODE *e; |
| 5512 | int cased, previous_is_cased; |
| 5513 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5514 | /* Shortcut for single character strings */ |
| 5515 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5516 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 5517 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5518 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5519 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5520 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5521 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5522 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5523 | e = p + PyUnicode_GET_SIZE(self); |
| 5524 | cased = 0; |
| 5525 | previous_is_cased = 0; |
| 5526 | for (; p < e; p++) { |
| 5527 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5528 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5529 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 5530 | if (previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5531 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5532 | previous_is_cased = 1; |
| 5533 | cased = 1; |
| 5534 | } |
| 5535 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 5536 | if (!previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5537 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5538 | previous_is_cased = 1; |
| 5539 | cased = 1; |
| 5540 | } |
| 5541 | else |
| 5542 | previous_is_cased = 0; |
| 5543 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5544 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5545 | } |
| 5546 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5547 | PyDoc_STRVAR(isspace__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5548 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5549 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5550 | Return True if all characters in S are whitespace\n\ |
| 5551 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5552 | |
| 5553 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5554 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5555 | { |
| 5556 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5557 | register const Py_UNICODE *e; |
| 5558 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5559 | /* Shortcut for single character strings */ |
| 5560 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5561 | Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5562 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5563 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5564 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5565 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5566 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5567 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5568 | e = p + PyUnicode_GET_SIZE(self); |
| 5569 | for (; p < e; p++) { |
| 5570 | if (!Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5571 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5572 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5573 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5574 | } |
| 5575 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5576 | PyDoc_STRVAR(isalpha__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5577 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5578 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5579 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5580 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5581 | |
| 5582 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5583 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5584 | { |
| 5585 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5586 | register const Py_UNICODE *e; |
| 5587 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5588 | /* Shortcut for single character strings */ |
| 5589 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5590 | Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5591 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5592 | |
| 5593 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5594 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5595 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5596 | |
| 5597 | e = p + PyUnicode_GET_SIZE(self); |
| 5598 | for (; p < e; p++) { |
| 5599 | if (!Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5600 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5601 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5602 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5603 | } |
| 5604 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5605 | PyDoc_STRVAR(isalnum__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5606 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5607 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5608 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5609 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5610 | |
| 5611 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5612 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5613 | { |
| 5614 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5615 | register const Py_UNICODE *e; |
| 5616 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5617 | /* Shortcut for single character strings */ |
| 5618 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5619 | Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5620 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5621 | |
| 5622 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5623 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5624 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5625 | |
| 5626 | e = p + PyUnicode_GET_SIZE(self); |
| 5627 | for (; p < e; p++) { |
| 5628 | if (!Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5629 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5630 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5631 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 5632 | } |
| 5633 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5634 | PyDoc_STRVAR(isdecimal__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5635 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5636 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5637 | 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] | 5638 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5639 | |
| 5640 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5641 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5642 | { |
| 5643 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5644 | register const Py_UNICODE *e; |
| 5645 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5646 | /* Shortcut for single character strings */ |
| 5647 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5648 | Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5649 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5650 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5651 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5652 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5653 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5654 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5655 | e = p + PyUnicode_GET_SIZE(self); |
| 5656 | for (; p < e; p++) { |
| 5657 | if (!Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5658 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5659 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5660 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5661 | } |
| 5662 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5663 | PyDoc_STRVAR(isdigit__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5664 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5665 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5666 | Return True if all characters in S are digits\n\ |
| 5667 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5668 | |
| 5669 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5670 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5671 | { |
| 5672 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5673 | register const Py_UNICODE *e; |
| 5674 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5675 | /* Shortcut for single character strings */ |
| 5676 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5677 | Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5678 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5679 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5680 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5681 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5682 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5683 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5684 | e = p + PyUnicode_GET_SIZE(self); |
| 5685 | for (; p < e; p++) { |
| 5686 | if (!Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5687 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5688 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5689 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5690 | } |
| 5691 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5692 | PyDoc_STRVAR(isnumeric__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5693 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5694 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5695 | 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] | 5696 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5697 | |
| 5698 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5699 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5700 | { |
| 5701 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5702 | register const Py_UNICODE *e; |
| 5703 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5704 | /* Shortcut for single character strings */ |
| 5705 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 5706 | Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5707 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5708 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5709 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5710 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5711 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5712 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5713 | e = p + PyUnicode_GET_SIZE(self); |
| 5714 | for (; p < e; p++) { |
| 5715 | if (!Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5716 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5717 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5718 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5719 | } |
| 5720 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5721 | PyDoc_STRVAR(join__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5722 | "S.join(sequence) -> unicode\n\ |
| 5723 | \n\ |
| 5724 | 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] | 5725 | sequence. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5726 | |
| 5727 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5728 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5729 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5730 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5731 | } |
| 5732 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5733 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5734 | unicode_length(PyUnicodeObject *self) |
| 5735 | { |
| 5736 | return self->length; |
| 5737 | } |
| 5738 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5739 | PyDoc_STRVAR(ljust__doc__, |
Hye-Shik Chang | 974ed7c | 2004-06-02 16:49:17 +0000 | [diff] [blame] | 5740 | "S.ljust(width[, fillchar]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5741 | \n\ |
| 5742 | 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] | 5743 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5744 | |
| 5745 | static PyObject * |
| 5746 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 5747 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5748 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5749 | Py_UNICODE fillchar = ' '; |
| 5750 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5751 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5752 | return NULL; |
| 5753 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5754 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5755 | Py_INCREF(self); |
| 5756 | return (PyObject*) self; |
| 5757 | } |
| 5758 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5759 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5760 | } |
| 5761 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5762 | PyDoc_STRVAR(lower__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5763 | "S.lower() -> unicode\n\ |
| 5764 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5765 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5766 | |
| 5767 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5768 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5769 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5770 | return fixup(self, fixlower); |
| 5771 | } |
| 5772 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5773 | #define LEFTSTRIP 0 |
| 5774 | #define RIGHTSTRIP 1 |
| 5775 | #define BOTHSTRIP 2 |
| 5776 | |
| 5777 | /* Arrays indexed by above */ |
| 5778 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 5779 | |
| 5780 | #define STRIPNAME(i) (stripformat[i]+3) |
| 5781 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5782 | /* externally visible for str.strip(unicode) */ |
| 5783 | PyObject * |
| 5784 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 5785 | { |
| 5786 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5787 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5788 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5789 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 5790 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5791 | |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5792 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
| 5793 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5794 | i = 0; |
| 5795 | if (striptype != RIGHTSTRIP) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5796 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 5797 | i++; |
| 5798 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5799 | } |
| 5800 | |
| 5801 | j = len; |
| 5802 | if (striptype != LEFTSTRIP) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5803 | do { |
| 5804 | j--; |
| 5805 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 5806 | j++; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5807 | } |
| 5808 | |
| 5809 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5810 | Py_INCREF(self); |
| 5811 | return (PyObject*)self; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5812 | } |
| 5813 | else |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5814 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5815 | } |
| 5816 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5817 | |
| 5818 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5819 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5820 | { |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5821 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5822 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5823 | |
| 5824 | i = 0; |
| 5825 | if (striptype != RIGHTSTRIP) { |
| 5826 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 5827 | i++; |
| 5828 | } |
| 5829 | } |
| 5830 | |
| 5831 | j = len; |
| 5832 | if (striptype != LEFTSTRIP) { |
| 5833 | do { |
| 5834 | j--; |
| 5835 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 5836 | j++; |
| 5837 | } |
| 5838 | |
| 5839 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 5840 | Py_INCREF(self); |
| 5841 | return (PyObject*)self; |
| 5842 | } |
| 5843 | else |
| 5844 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5845 | } |
| 5846 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5847 | |
| 5848 | static PyObject * |
| 5849 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 5850 | { |
| 5851 | PyObject *sep = NULL; |
| 5852 | |
| 5853 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 5854 | return NULL; |
| 5855 | |
| 5856 | if (sep != NULL && sep != Py_None) { |
| 5857 | if (PyUnicode_Check(sep)) |
| 5858 | return _PyUnicode_XStrip(self, striptype, sep); |
| 5859 | else if (PyString_Check(sep)) { |
| 5860 | PyObject *res; |
| 5861 | sep = PyUnicode_FromObject(sep); |
| 5862 | if (sep==NULL) |
| 5863 | return NULL; |
| 5864 | res = _PyUnicode_XStrip(self, striptype, sep); |
| 5865 | Py_DECREF(sep); |
| 5866 | return res; |
| 5867 | } |
| 5868 | else { |
| 5869 | PyErr_Format(PyExc_TypeError, |
| 5870 | "%s arg must be None, unicode or str", |
| 5871 | STRIPNAME(striptype)); |
| 5872 | return NULL; |
| 5873 | } |
| 5874 | } |
| 5875 | |
| 5876 | return do_strip(self, striptype); |
| 5877 | } |
| 5878 | |
| 5879 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5880 | PyDoc_STRVAR(strip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5881 | "S.strip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5882 | \n\ |
| 5883 | Return a copy of the string S with leading and trailing\n\ |
| 5884 | whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5885 | If chars is given and not None, remove characters in chars instead.\n\ |
| 5886 | 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] | 5887 | |
| 5888 | static PyObject * |
| 5889 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 5890 | { |
| 5891 | if (PyTuple_GET_SIZE(args) == 0) |
| 5892 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 5893 | else |
| 5894 | return do_argstrip(self, BOTHSTRIP, args); |
| 5895 | } |
| 5896 | |
| 5897 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5898 | PyDoc_STRVAR(lstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5899 | "S.lstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5900 | \n\ |
| 5901 | Return a copy of the string S with leading whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5902 | If chars is given and not None, remove characters in chars instead.\n\ |
| 5903 | 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] | 5904 | |
| 5905 | static PyObject * |
| 5906 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 5907 | { |
| 5908 | if (PyTuple_GET_SIZE(args) == 0) |
| 5909 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 5910 | else |
| 5911 | return do_argstrip(self, LEFTSTRIP, args); |
| 5912 | } |
| 5913 | |
| 5914 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5915 | PyDoc_STRVAR(rstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5916 | "S.rstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 5917 | \n\ |
| 5918 | Return a copy of the string S with trailing whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 5919 | If chars is given and not None, remove characters in chars instead.\n\ |
| 5920 | 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] | 5921 | |
| 5922 | static PyObject * |
| 5923 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 5924 | { |
| 5925 | if (PyTuple_GET_SIZE(args) == 0) |
| 5926 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 5927 | else |
| 5928 | return do_argstrip(self, RIGHTSTRIP, args); |
| 5929 | } |
| 5930 | |
| 5931 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5932 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5933 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5934 | { |
| 5935 | PyUnicodeObject *u; |
| 5936 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5937 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 5938 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5939 | |
| 5940 | if (len < 0) |
| 5941 | len = 0; |
| 5942 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5943 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5944 | /* no repeat, return original string */ |
| 5945 | Py_INCREF(str); |
| 5946 | return (PyObject*) str; |
| 5947 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 5948 | |
| 5949 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 5950 | * needed doesn't overflow size_t |
| 5951 | */ |
| 5952 | nchars = len * str->length; |
| 5953 | if (len && nchars / len != str->length) { |
| 5954 | PyErr_SetString(PyExc_OverflowError, |
| 5955 | "repeated string is too long"); |
| 5956 | return NULL; |
| 5957 | } |
| 5958 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 5959 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 5960 | PyErr_SetString(PyExc_OverflowError, |
| 5961 | "repeated string is too long"); |
| 5962 | return NULL; |
| 5963 | } |
| 5964 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5965 | if (!u) |
| 5966 | return NULL; |
| 5967 | |
| 5968 | p = u->str; |
| 5969 | |
Fredrik Lundh | f1d60a5 | 2006-05-22 16:29:30 +0000 | [diff] [blame] | 5970 | if (str->length == 1 && len > 0) { |
| 5971 | Py_UNICODE_FILL(p, str->str[0], len); |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 5972 | } else { |
Tim Peters | 1bacc64 | 2006-05-23 05:47:16 +0000 | [diff] [blame] | 5973 | Py_ssize_t done = 0; /* number of characters copied this far */ |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 5974 | if (done < nchars) { |
Fredrik Lundh | f1d60a5 | 2006-05-22 16:29:30 +0000 | [diff] [blame] | 5975 | Py_UNICODE_COPY(p, str->str, str->length); |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 5976 | done = str->length; |
| 5977 | } |
| 5978 | while (done < nchars) { |
| 5979 | int n = (done <= nchars-done) ? done : nchars-done; |
| 5980 | Py_UNICODE_COPY(p+done, p, n); |
| 5981 | done += n; |
| 5982 | } |
| 5983 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5984 | |
| 5985 | return (PyObject*) u; |
| 5986 | } |
| 5987 | |
| 5988 | PyObject *PyUnicode_Replace(PyObject *obj, |
| 5989 | PyObject *subobj, |
| 5990 | PyObject *replobj, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5991 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5992 | { |
| 5993 | PyObject *self; |
| 5994 | PyObject *str1; |
| 5995 | PyObject *str2; |
| 5996 | PyObject *result; |
| 5997 | |
| 5998 | self = PyUnicode_FromObject(obj); |
| 5999 | if (self == NULL) |
| 6000 | return NULL; |
| 6001 | str1 = PyUnicode_FromObject(subobj); |
| 6002 | if (str1 == NULL) { |
| 6003 | Py_DECREF(self); |
| 6004 | return NULL; |
| 6005 | } |
| 6006 | str2 = PyUnicode_FromObject(replobj); |
| 6007 | if (str2 == NULL) { |
| 6008 | Py_DECREF(self); |
| 6009 | Py_DECREF(str1); |
| 6010 | return NULL; |
| 6011 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6012 | result = replace((PyUnicodeObject *)self, |
| 6013 | (PyUnicodeObject *)str1, |
| 6014 | (PyUnicodeObject *)str2, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6015 | maxcount); |
| 6016 | Py_DECREF(self); |
| 6017 | Py_DECREF(str1); |
| 6018 | Py_DECREF(str2); |
| 6019 | return result; |
| 6020 | } |
| 6021 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6022 | PyDoc_STRVAR(replace__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6023 | "S.replace (old, new[, maxsplit]) -> unicode\n\ |
| 6024 | \n\ |
| 6025 | Return a copy of S with all occurrences of substring\n\ |
| 6026 | 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] | 6027 | given, only the first maxsplit occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6028 | |
| 6029 | static PyObject* |
| 6030 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 6031 | { |
| 6032 | PyUnicodeObject *str1; |
| 6033 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6034 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6035 | PyObject *result; |
| 6036 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6037 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6038 | return NULL; |
| 6039 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 6040 | if (str1 == NULL) |
| 6041 | return NULL; |
| 6042 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 6043 | if (str2 == NULL) { |
| 6044 | Py_DECREF(str1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6045 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 6046 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6047 | |
| 6048 | result = replace(self, str1, str2, maxcount); |
| 6049 | |
| 6050 | Py_DECREF(str1); |
| 6051 | Py_DECREF(str2); |
| 6052 | return result; |
| 6053 | } |
| 6054 | |
| 6055 | static |
| 6056 | PyObject *unicode_repr(PyObject *unicode) |
| 6057 | { |
| 6058 | return unicodeescape_string(PyUnicode_AS_UNICODE(unicode), |
| 6059 | PyUnicode_GET_SIZE(unicode), |
| 6060 | 1); |
| 6061 | } |
| 6062 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6063 | PyDoc_STRVAR(rfind__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6064 | "S.rfind(sub [,start [,end]]) -> int\n\ |
| 6065 | \n\ |
| 6066 | Return the highest index in S where substring sub is found,\n\ |
| 6067 | such that sub is contained within s[start,end]. Optional\n\ |
| 6068 | arguments start and end are interpreted as in slice notation.\n\ |
| 6069 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6070 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6071 | |
| 6072 | static PyObject * |
| 6073 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 6074 | { |
| 6075 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6076 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6077 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6078 | PyObject *result; |
| 6079 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6080 | if (!PyArg_ParseTuple(args, "O|O&O&:rfind", &substring, |
| 6081 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6082 | return NULL; |
| 6083 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 6084 | (PyObject *)substring); |
| 6085 | if (substring == NULL) |
| 6086 | return NULL; |
| 6087 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6088 | result = PyInt_FromSsize_t(findstring(self, substring, start, end, -1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6089 | |
| 6090 | Py_DECREF(substring); |
| 6091 | return result; |
| 6092 | } |
| 6093 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6094 | PyDoc_STRVAR(rindex__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6095 | "S.rindex(sub [,start [,end]]) -> int\n\ |
| 6096 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6097 | 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] | 6098 | |
| 6099 | static PyObject * |
| 6100 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 6101 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6102 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6103 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6104 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6105 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6106 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6107 | if (!PyArg_ParseTuple(args, "O|O&O&:rindex", &substring, |
| 6108 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6109 | return NULL; |
| 6110 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 6111 | (PyObject *)substring); |
| 6112 | if (substring == NULL) |
| 6113 | return NULL; |
| 6114 | |
| 6115 | result = findstring(self, substring, start, end, -1); |
| 6116 | |
| 6117 | Py_DECREF(substring); |
| 6118 | if (result < 0) { |
| 6119 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 6120 | return NULL; |
| 6121 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6122 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6123 | } |
| 6124 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6125 | PyDoc_STRVAR(rjust__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6126 | "S.rjust(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6127 | \n\ |
| 6128 | 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] | 6129 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6130 | |
| 6131 | static PyObject * |
| 6132 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 6133 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6134 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6135 | Py_UNICODE fillchar = ' '; |
| 6136 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6137 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6138 | return NULL; |
| 6139 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6140 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6141 | Py_INCREF(self); |
| 6142 | return (PyObject*) self; |
| 6143 | } |
| 6144 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6145 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6146 | } |
| 6147 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6148 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6149 | 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] | 6150 | { |
| 6151 | /* standard clamping */ |
| 6152 | if (start < 0) |
| 6153 | start = 0; |
| 6154 | if (end < 0) |
| 6155 | end = 0; |
| 6156 | if (end > self->length) |
| 6157 | end = self->length; |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6158 | if (start == 0 && end == self->length && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6159 | /* full slice, return original string */ |
| 6160 | Py_INCREF(self); |
| 6161 | return (PyObject*) self; |
| 6162 | } |
| 6163 | if (start > end) |
| 6164 | start = end; |
| 6165 | /* copy slice */ |
| 6166 | return (PyObject*) PyUnicode_FromUnicode(self->str + start, |
| 6167 | end - start); |
| 6168 | } |
| 6169 | |
| 6170 | PyObject *PyUnicode_Split(PyObject *s, |
| 6171 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6172 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6173 | { |
| 6174 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6175 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6176 | s = PyUnicode_FromObject(s); |
| 6177 | if (s == NULL) |
| 6178 | return NULL; |
| 6179 | if (sep != NULL) { |
| 6180 | sep = PyUnicode_FromObject(sep); |
| 6181 | if (sep == NULL) { |
| 6182 | Py_DECREF(s); |
| 6183 | return NULL; |
| 6184 | } |
| 6185 | } |
| 6186 | |
| 6187 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 6188 | |
| 6189 | Py_DECREF(s); |
| 6190 | Py_XDECREF(sep); |
| 6191 | return result; |
| 6192 | } |
| 6193 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6194 | PyDoc_STRVAR(split__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6195 | "S.split([sep [,maxsplit]]) -> list of strings\n\ |
| 6196 | \n\ |
| 6197 | Return a list of the words in S, using sep as the\n\ |
| 6198 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Thomas Heller | ca0d2cb | 2004-09-15 11:41:32 +0000 | [diff] [blame] | 6199 | 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] | 6200 | any whitespace string is a separator."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6201 | |
| 6202 | static PyObject* |
| 6203 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 6204 | { |
| 6205 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6206 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6207 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6208 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6209 | return NULL; |
| 6210 | |
| 6211 | if (substring == Py_None) |
| 6212 | return split(self, NULL, maxcount); |
| 6213 | else if (PyUnicode_Check(substring)) |
| 6214 | return split(self, (PyUnicodeObject *)substring, maxcount); |
| 6215 | else |
| 6216 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
| 6217 | } |
| 6218 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6219 | PyObject * |
| 6220 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 6221 | { |
| 6222 | PyObject* str_obj; |
| 6223 | PyObject* sep_obj; |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6224 | PyObject* out; |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 6225 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6226 | str_obj = PyUnicode_FromObject(str_in); |
| 6227 | if (!str_obj) |
| 6228 | return NULL; |
| 6229 | sep_obj = PyUnicode_FromObject(sep_in); |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 6230 | if (!sep_obj) { |
| 6231 | Py_DECREF(str_obj); |
| 6232 | return NULL; |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6233 | } |
| 6234 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 6235 | out = stringlib_partition( |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 6236 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 6237 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 6238 | ); |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6239 | |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 6240 | Py_DECREF(sep_obj); |
| 6241 | Py_DECREF(str_obj); |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6242 | |
| 6243 | return out; |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6244 | } |
| 6245 | |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 6246 | |
| 6247 | PyObject * |
| 6248 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 6249 | { |
| 6250 | PyObject* str_obj; |
| 6251 | PyObject* sep_obj; |
| 6252 | PyObject* out; |
| 6253 | |
| 6254 | str_obj = PyUnicode_FromObject(str_in); |
| 6255 | if (!str_obj) |
| 6256 | return NULL; |
| 6257 | sep_obj = PyUnicode_FromObject(sep_in); |
| 6258 | if (!sep_obj) { |
| 6259 | Py_DECREF(str_obj); |
| 6260 | return NULL; |
| 6261 | } |
| 6262 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 6263 | out = stringlib_rpartition( |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 6264 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 6265 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 6266 | ); |
| 6267 | |
| 6268 | Py_DECREF(sep_obj); |
| 6269 | Py_DECREF(str_obj); |
| 6270 | |
| 6271 | return out; |
| 6272 | } |
| 6273 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6274 | PyDoc_STRVAR(partition__doc__, |
| 6275 | "S.partition(sep) -> (head, sep, tail)\n\ |
| 6276 | \n\ |
| 6277 | Searches for the separator sep in S, and returns the part before it,\n\ |
| 6278 | the separator itself, and the part after it. If the separator is not\n\ |
| 6279 | found, returns S and two empty strings."); |
| 6280 | |
| 6281 | static PyObject* |
Fredrik Lundh | 450277f | 2006-05-26 09:46:59 +0000 | [diff] [blame] | 6282 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6283 | { |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6284 | return PyUnicode_Partition((PyObject *)self, separator); |
| 6285 | } |
| 6286 | |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 6287 | PyDoc_STRVAR(rpartition__doc__, |
| 6288 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
| 6289 | \n\ |
| 6290 | Searches for the separator sep in S, starting at the end of S, and returns\n\ |
| 6291 | the part before it, the separator itself, and the part after it. If the\n\ |
| 6292 | separator is not found, returns S and two empty strings."); |
| 6293 | |
| 6294 | static PyObject* |
| 6295 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 6296 | { |
| 6297 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 6298 | } |
| 6299 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6300 | PyObject *PyUnicode_RSplit(PyObject *s, |
| 6301 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6302 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6303 | { |
| 6304 | PyObject *result; |
| 6305 | |
| 6306 | s = PyUnicode_FromObject(s); |
| 6307 | if (s == NULL) |
| 6308 | return NULL; |
| 6309 | if (sep != NULL) { |
| 6310 | sep = PyUnicode_FromObject(sep); |
| 6311 | if (sep == NULL) { |
| 6312 | Py_DECREF(s); |
| 6313 | return NULL; |
| 6314 | } |
| 6315 | } |
| 6316 | |
| 6317 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 6318 | |
| 6319 | Py_DECREF(s); |
| 6320 | Py_XDECREF(sep); |
| 6321 | return result; |
| 6322 | } |
| 6323 | |
| 6324 | PyDoc_STRVAR(rsplit__doc__, |
| 6325 | "S.rsplit([sep [,maxsplit]]) -> list of strings\n\ |
| 6326 | \n\ |
| 6327 | Return a list of the words in S, using sep as the\n\ |
| 6328 | delimiter string, starting at the end of the string and\n\ |
| 6329 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 6330 | splits are done. If sep is not specified, any whitespace string\n\ |
| 6331 | is a separator."); |
| 6332 | |
| 6333 | static PyObject* |
| 6334 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 6335 | { |
| 6336 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6337 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6338 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6339 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6340 | return NULL; |
| 6341 | |
| 6342 | if (substring == Py_None) |
| 6343 | return rsplit(self, NULL, maxcount); |
| 6344 | else if (PyUnicode_Check(substring)) |
| 6345 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
| 6346 | else |
| 6347 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
| 6348 | } |
| 6349 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6350 | PyDoc_STRVAR(splitlines__doc__, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6351 | "S.splitlines([keepends]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6352 | \n\ |
| 6353 | 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] | 6354 | 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] | 6355 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6356 | |
| 6357 | static PyObject* |
| 6358 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 6359 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6360 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6361 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6362 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6363 | return NULL; |
| 6364 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6365 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6366 | } |
| 6367 | |
| 6368 | static |
| 6369 | PyObject *unicode_str(PyUnicodeObject *self) |
| 6370 | { |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6371 | return PyUnicode_AsEncodedString((PyObject *)self, NULL, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6372 | } |
| 6373 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6374 | PyDoc_STRVAR(swapcase__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6375 | "S.swapcase() -> unicode\n\ |
| 6376 | \n\ |
| 6377 | 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] | 6378 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6379 | |
| 6380 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6381 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6382 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6383 | return fixup(self, fixswapcase); |
| 6384 | } |
| 6385 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6386 | PyDoc_STRVAR(translate__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6387 | "S.translate(table) -> unicode\n\ |
| 6388 | \n\ |
| 6389 | Return a copy of the string S, where all characters have been mapped\n\ |
| 6390 | 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] | 6391 | Unicode ordinals to Unicode ordinals, Unicode strings or None.\n\ |
| 6392 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 6393 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6394 | |
| 6395 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6396 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6397 | { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6398 | return PyUnicode_TranslateCharmap(self->str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6399 | self->length, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6400 | table, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6401 | "ignore"); |
| 6402 | } |
| 6403 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6404 | PyDoc_STRVAR(upper__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6405 | "S.upper() -> unicode\n\ |
| 6406 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6407 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6408 | |
| 6409 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6410 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6411 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6412 | return fixup(self, fixupper); |
| 6413 | } |
| 6414 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6415 | PyDoc_STRVAR(zfill__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6416 | "S.zfill(width) -> unicode\n\ |
| 6417 | \n\ |
| 6418 | 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] | 6419 | of the specified width. The string x is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6420 | |
| 6421 | static PyObject * |
| 6422 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 6423 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6424 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6425 | PyUnicodeObject *u; |
| 6426 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6427 | Py_ssize_t width; |
| 6428 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6429 | return NULL; |
| 6430 | |
| 6431 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 6432 | if (PyUnicode_CheckExact(self)) { |
| 6433 | Py_INCREF(self); |
| 6434 | return (PyObject*) self; |
| 6435 | } |
| 6436 | else |
| 6437 | return PyUnicode_FromUnicode( |
| 6438 | PyUnicode_AS_UNICODE(self), |
| 6439 | PyUnicode_GET_SIZE(self) |
| 6440 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6441 | } |
| 6442 | |
| 6443 | fill = width - self->length; |
| 6444 | |
| 6445 | u = pad(self, fill, 0, '0'); |
| 6446 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 6447 | if (u == NULL) |
| 6448 | return NULL; |
| 6449 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6450 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 6451 | /* move sign to beginning of string */ |
| 6452 | u->str[0] = u->str[fill]; |
| 6453 | u->str[fill] = '0'; |
| 6454 | } |
| 6455 | |
| 6456 | return (PyObject*) u; |
| 6457 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6458 | |
| 6459 | #if 0 |
| 6460 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6461 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6462 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6463 | return PyInt_FromLong(unicode_freelist_size); |
| 6464 | } |
| 6465 | #endif |
| 6466 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6467 | PyDoc_STRVAR(startswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6468 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6469 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 6470 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 6471 | With optional start, test S beginning at that position.\n\ |
| 6472 | With optional end, stop comparing S at that position."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6473 | |
| 6474 | static PyObject * |
| 6475 | unicode_startswith(PyUnicodeObject *self, |
| 6476 | PyObject *args) |
| 6477 | { |
| 6478 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6479 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6480 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6481 | PyObject *result; |
| 6482 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6483 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &substring, |
| 6484 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6485 | return NULL; |
| 6486 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 6487 | (PyObject *)substring); |
| 6488 | if (substring == NULL) |
| 6489 | return NULL; |
| 6490 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6491 | result = PyBool_FromLong(tailmatch(self, substring, start, end, -1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6492 | |
| 6493 | Py_DECREF(substring); |
| 6494 | return result; |
| 6495 | } |
| 6496 | |
| 6497 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6498 | PyDoc_STRVAR(endswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6499 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6500 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 6501 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 6502 | With optional start, test S beginning at that position.\n\ |
| 6503 | With optional end, stop comparing S at that position."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6504 | |
| 6505 | static PyObject * |
| 6506 | unicode_endswith(PyUnicodeObject *self, |
| 6507 | PyObject *args) |
| 6508 | { |
| 6509 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6510 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6511 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6512 | PyObject *result; |
| 6513 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6514 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &substring, |
| 6515 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6516 | return NULL; |
| 6517 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 6518 | (PyObject *)substring); |
| 6519 | if (substring == NULL) |
| 6520 | return NULL; |
| 6521 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6522 | result = PyBool_FromLong(tailmatch(self, substring, start, end, +1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6523 | |
| 6524 | Py_DECREF(substring); |
| 6525 | return result; |
| 6526 | } |
| 6527 | |
| 6528 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 6529 | |
| 6530 | static PyObject * |
| 6531 | unicode_getnewargs(PyUnicodeObject *v) |
| 6532 | { |
| 6533 | return Py_BuildValue("(u#)", v->str, v->length); |
| 6534 | } |
| 6535 | |
| 6536 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6537 | static PyMethodDef unicode_methods[] = { |
| 6538 | |
| 6539 | /* Order is according to common usage: often used methods should |
| 6540 | appear first, since lookup is done sequentially. */ |
| 6541 | |
Georg Brandl | ecdc0a9 | 2006-03-30 12:19:07 +0000 | [diff] [blame] | 6542 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6543 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 6544 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6545 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6546 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 6547 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 6548 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 6549 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 6550 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 6551 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 6552 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Fredrik Lundh | 450277f | 2006-05-26 09:46:59 +0000 | [diff] [blame] | 6553 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6554 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 6555 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 6556 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6557 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6558 | {"decode", (PyCFunction) unicode_decode, METH_VARARGS, decode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6559 | /* {"maketrans", (PyCFunction) unicode_maketrans, METH_VARARGS, maketrans__doc__}, */ |
| 6560 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 6561 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 6562 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6563 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 6564 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6565 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6566 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6567 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 6568 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 6569 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 6570 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 6571 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 6572 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 6573 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 6574 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 6575 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 6576 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 6577 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 6578 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 6579 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 6580 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6581 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 6582 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6583 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6584 | #endif |
| 6585 | |
| 6586 | #if 0 |
| 6587 | /* This one is just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6588 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6589 | #endif |
| 6590 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 6591 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6592 | {NULL, NULL} |
| 6593 | }; |
| 6594 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 6595 | static PyObject * |
| 6596 | unicode_mod(PyObject *v, PyObject *w) |
| 6597 | { |
| 6598 | if (!PyUnicode_Check(v)) { |
| 6599 | Py_INCREF(Py_NotImplemented); |
| 6600 | return Py_NotImplemented; |
| 6601 | } |
| 6602 | return PyUnicode_Format(v, w); |
| 6603 | } |
| 6604 | |
| 6605 | static PyNumberMethods unicode_as_number = { |
| 6606 | 0, /*nb_add*/ |
| 6607 | 0, /*nb_subtract*/ |
| 6608 | 0, /*nb_multiply*/ |
| 6609 | 0, /*nb_divide*/ |
| 6610 | unicode_mod, /*nb_remainder*/ |
| 6611 | }; |
| 6612 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6613 | static PySequenceMethods unicode_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6614 | (lenfunc) unicode_length, /* sq_length */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 6615 | PyUnicode_Concat, /* sq_concat */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6616 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 6617 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 6618 | (ssizessizeargfunc) unicode_slice, /* sq_slice */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6619 | 0, /* sq_ass_item */ |
| 6620 | 0, /* sq_ass_slice */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 6621 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6622 | }; |
| 6623 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 6624 | #define HASINDEX(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_HAVE_INDEX) |
| 6625 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6626 | static PyObject* |
| 6627 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 6628 | { |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 6629 | PyNumberMethods *nb = item->ob_type->tp_as_number; |
| 6630 | if (nb != NULL && HASINDEX(item) && nb->nb_index != NULL) { |
| 6631 | Py_ssize_t i = nb->nb_index(item); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6632 | if (i == -1 && PyErr_Occurred()) |
| 6633 | return NULL; |
| 6634 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6635 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6636 | return unicode_getitem(self, i); |
| 6637 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6638 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6639 | Py_UNICODE* source_buf; |
| 6640 | Py_UNICODE* result_buf; |
| 6641 | PyObject* result; |
| 6642 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6643 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6644 | &start, &stop, &step, &slicelength) < 0) { |
| 6645 | return NULL; |
| 6646 | } |
| 6647 | |
| 6648 | if (slicelength <= 0) { |
| 6649 | return PyUnicode_FromUnicode(NULL, 0); |
| 6650 | } else { |
| 6651 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 6652 | result_buf = (Py_UNICODE *)PyMem_MALLOC(slicelength* |
| 6653 | sizeof(Py_UNICODE)); |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6654 | |
| 6655 | if (result_buf == NULL) |
| 6656 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6657 | |
| 6658 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 6659 | result_buf[i] = source_buf[cur]; |
| 6660 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6661 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6662 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
| 6663 | PyMem_FREE(result_buf); |
| 6664 | return result; |
| 6665 | } |
| 6666 | } else { |
| 6667 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 6668 | return NULL; |
| 6669 | } |
| 6670 | } |
| 6671 | |
| 6672 | static PyMappingMethods unicode_as_mapping = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6673 | (lenfunc)unicode_length, /* mp_length */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 6674 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 6675 | (objobjargproc)0, /* mp_ass_subscript */ |
| 6676 | }; |
| 6677 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6678 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6679 | unicode_buffer_getreadbuf(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6680 | Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6681 | const void **ptr) |
| 6682 | { |
| 6683 | if (index != 0) { |
| 6684 | PyErr_SetString(PyExc_SystemError, |
| 6685 | "accessing non-existent unicode segment"); |
| 6686 | return -1; |
| 6687 | } |
| 6688 | *ptr = (void *) self->str; |
| 6689 | return PyUnicode_GET_DATA_SIZE(self); |
| 6690 | } |
| 6691 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6692 | static Py_ssize_t |
| 6693 | unicode_buffer_getwritebuf(PyUnicodeObject *self, Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6694 | const void **ptr) |
| 6695 | { |
| 6696 | PyErr_SetString(PyExc_TypeError, |
Neal Norwitz | 20e7213 | 2002-06-13 21:25:17 +0000 | [diff] [blame] | 6697 | "cannot use unicode as modifiable buffer"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6698 | return -1; |
| 6699 | } |
| 6700 | |
| 6701 | static int |
| 6702 | unicode_buffer_getsegcount(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6703 | Py_ssize_t *lenp) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6704 | { |
| 6705 | if (lenp) |
| 6706 | *lenp = PyUnicode_GET_DATA_SIZE(self); |
| 6707 | return 1; |
| 6708 | } |
| 6709 | |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 6710 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6711 | unicode_buffer_getcharbuf(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6712 | Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6713 | const void **ptr) |
| 6714 | { |
| 6715 | PyObject *str; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6716 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6717 | if (index != 0) { |
| 6718 | PyErr_SetString(PyExc_SystemError, |
| 6719 | "accessing non-existent unicode segment"); |
| 6720 | return -1; |
| 6721 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 6722 | str = _PyUnicode_AsDefaultEncodedString((PyObject *)self, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6723 | if (str == NULL) |
| 6724 | return -1; |
| 6725 | *ptr = (void *) PyString_AS_STRING(str); |
| 6726 | return PyString_GET_SIZE(str); |
| 6727 | } |
| 6728 | |
| 6729 | /* Helpers for PyUnicode_Format() */ |
| 6730 | |
| 6731 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6732 | 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] | 6733 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6734 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6735 | if (argidx < arglen) { |
| 6736 | (*p_argidx)++; |
| 6737 | if (arglen < 0) |
| 6738 | return args; |
| 6739 | else |
| 6740 | return PyTuple_GetItem(args, argidx); |
| 6741 | } |
| 6742 | PyErr_SetString(PyExc_TypeError, |
| 6743 | "not enough arguments for format string"); |
| 6744 | return NULL; |
| 6745 | } |
| 6746 | |
| 6747 | #define F_LJUST (1<<0) |
| 6748 | #define F_SIGN (1<<1) |
| 6749 | #define F_BLANK (1<<2) |
| 6750 | #define F_ALT (1<<3) |
| 6751 | #define F_ZERO (1<<4) |
| 6752 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6753 | static Py_ssize_t |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6754 | strtounicode(Py_UNICODE *buffer, const char *charbuffer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6755 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6756 | register Py_ssize_t i; |
| 6757 | Py_ssize_t len = strlen(charbuffer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6758 | for (i = len - 1; i >= 0; i--) |
| 6759 | buffer[i] = (Py_UNICODE) charbuffer[i]; |
| 6760 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6761 | return len; |
| 6762 | } |
| 6763 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6764 | static int |
| 6765 | doubletounicode(Py_UNICODE *buffer, size_t len, const char *format, double x) |
| 6766 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 6767 | Py_ssize_t result; |
| 6768 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6769 | PyOS_ascii_formatd((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 6770 | result = strtounicode(buffer, (char *)buffer); |
| 6771 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6772 | } |
| 6773 | |
| 6774 | static int |
| 6775 | longtounicode(Py_UNICODE *buffer, size_t len, const char *format, long x) |
| 6776 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 6777 | Py_ssize_t result; |
| 6778 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6779 | PyOS_snprintf((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 6780 | result = strtounicode(buffer, (char *)buffer); |
| 6781 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6782 | } |
| 6783 | |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 6784 | /* XXX To save some code duplication, formatfloat/long/int could have been |
| 6785 | shared with stringobject.c, converting from 8-bit to Unicode after the |
| 6786 | formatting is done. */ |
| 6787 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6788 | static int |
| 6789 | formatfloat(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6790 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6791 | int flags, |
| 6792 | int prec, |
| 6793 | int type, |
| 6794 | PyObject *v) |
| 6795 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6796 | /* fmt = '%#.' + `prec` + `type` |
| 6797 | 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] | 6798 | char fmt[20]; |
| 6799 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6800 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6801 | x = PyFloat_AsDouble(v); |
| 6802 | if (x == -1.0 && PyErr_Occurred()) |
| 6803 | return -1; |
| 6804 | if (prec < 0) |
| 6805 | prec = 6; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6806 | if (type == 'f' && (fabs(x) / 1e25) >= 1e25) |
| 6807 | type = 'g'; |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 6808 | /* Worst case length calc to ensure no buffer overrun: |
| 6809 | |
| 6810 | 'g' formats: |
| 6811 | fmt = %#.<prec>g |
| 6812 | buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
| 6813 | for any double rep.) |
| 6814 | len = 1 + prec + 1 + 2 + 5 = 9 + prec |
| 6815 | |
| 6816 | 'f' formats: |
| 6817 | buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
| 6818 | len = 1 + 50 + 1 + prec = 52 + prec |
| 6819 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6820 | If prec=0 the effective precision is 1 (the leading digit is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6821 | always given), therefore increase the length by one. |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 6822 | |
| 6823 | */ |
| 6824 | if ((type == 'g' && buflen <= (size_t)10 + (size_t)prec) || |
| 6825 | (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6826 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 6827 | "formatted float is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6828 | return -1; |
| 6829 | } |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 6830 | PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
| 6831 | (flags&F_ALT) ? "#" : "", |
| 6832 | prec, type); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6833 | return doubletounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6834 | } |
| 6835 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6836 | static PyObject* |
| 6837 | formatlong(PyObject *val, int flags, int prec, int type) |
| 6838 | { |
| 6839 | char *buf; |
| 6840 | int i, len; |
| 6841 | PyObject *str; /* temporary string object. */ |
| 6842 | PyUnicodeObject *result; |
| 6843 | |
| 6844 | str = _PyString_FormatLong(val, flags, prec, type, &buf, &len); |
| 6845 | if (!str) |
| 6846 | return NULL; |
| 6847 | result = _PyUnicode_New(len); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 6848 | if (!result) { |
| 6849 | Py_DECREF(str); |
| 6850 | return NULL; |
| 6851 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6852 | for (i = 0; i < len; i++) |
| 6853 | result->str[i] = buf[i]; |
| 6854 | result->str[len] = 0; |
| 6855 | Py_DECREF(str); |
| 6856 | return (PyObject*)result; |
| 6857 | } |
| 6858 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6859 | static int |
| 6860 | formatint(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6861 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6862 | int flags, |
| 6863 | int prec, |
| 6864 | int type, |
| 6865 | PyObject *v) |
| 6866 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6867 | /* fmt = '%#.' + `prec` + 'l' + `type` |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6868 | * worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
| 6869 | * + 1 + 1 |
| 6870 | * = 24 |
| 6871 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 6872 | char fmt[64]; /* plenty big enough! */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6873 | char *sign; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6874 | long x; |
| 6875 | |
| 6876 | x = PyInt_AsLong(v); |
| 6877 | if (x == -1 && PyErr_Occurred()) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6878 | return -1; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6879 | if (x < 0 && type == 'u') { |
| 6880 | type = 'd'; |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 6881 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6882 | if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) |
| 6883 | sign = "-"; |
| 6884 | else |
| 6885 | sign = ""; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6886 | if (prec < 0) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6887 | prec = 1; |
| 6888 | |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6889 | /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) |
| 6890 | * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6891 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6892 | if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6893 | PyErr_SetString(PyExc_OverflowError, |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6894 | "formatted integer is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6895 | return -1; |
| 6896 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6897 | |
| 6898 | if ((flags & F_ALT) && |
| 6899 | (type == 'x' || type == 'X')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6900 | /* When converting under %#x or %#X, there are a number |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6901 | * of issues that cause pain: |
| 6902 | * - when 0 is being converted, the C standard leaves off |
| 6903 | * the '0x' or '0X', which is inconsistent with other |
| 6904 | * %#x/%#X conversions and inconsistent with Python's |
| 6905 | * hex() function |
| 6906 | * - there are platforms that violate the standard and |
| 6907 | * convert 0 with the '0x' or '0X' |
| 6908 | * (Metrowerks, Compaq Tru64) |
| 6909 | * - there are platforms that give '0x' when converting |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6910 | * under %#X, but convert 0 in accordance with the |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6911 | * standard (OS/2 EMX) |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6912 | * |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6913 | * We can achieve the desired consistency by inserting our |
| 6914 | * own '0x' or '0X' prefix, and substituting %x/%X in place |
| 6915 | * of %#x/%#X. |
| 6916 | * |
| 6917 | * Note that this is the same approach as used in |
| 6918 | * formatint() in stringobject.c |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 6919 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6920 | PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", |
| 6921 | sign, type, prec, type); |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 6922 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6923 | else { |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6924 | PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", |
| 6925 | sign, (flags&F_ALT) ? "#" : "", |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 6926 | prec, type); |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 6927 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6928 | if (sign[0]) |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6929 | return longtounicode(buf, buflen, fmt, -x); |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 6930 | else |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 6931 | return longtounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6932 | } |
| 6933 | |
| 6934 | static int |
| 6935 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6936 | size_t buflen, |
| 6937 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6938 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6939 | /* presume that the buffer is at least 2 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6940 | if (PyUnicode_Check(v)) { |
| 6941 | if (PyUnicode_GET_SIZE(v) != 1) |
| 6942 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6943 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6944 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6945 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6946 | else if (PyString_Check(v)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6947 | if (PyString_GET_SIZE(v) != 1) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6948 | goto onError; |
| 6949 | buf[0] = (Py_UNICODE)PyString_AS_STRING(v)[0]; |
| 6950 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6951 | |
| 6952 | else { |
| 6953 | /* Integer input truncated to a character */ |
| 6954 | long x; |
| 6955 | x = PyInt_AsLong(v); |
| 6956 | if (x == -1 && PyErr_Occurred()) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6957 | goto onError; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 6958 | #ifdef Py_UNICODE_WIDE |
| 6959 | if (x < 0 || x > 0x10ffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 6960 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 6961 | "%c arg not in range(0x110000) " |
| 6962 | "(wide Python build)"); |
| 6963 | return -1; |
| 6964 | } |
| 6965 | #else |
| 6966 | if (x < 0 || x > 0xffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 6967 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 6968 | "%c arg not in range(0x10000) " |
| 6969 | "(narrow Python build)"); |
| 6970 | return -1; |
| 6971 | } |
| 6972 | #endif |
| 6973 | buf[0] = (Py_UNICODE) x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6974 | } |
| 6975 | buf[1] = '\0'; |
| 6976 | return 1; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 6977 | |
| 6978 | onError: |
| 6979 | PyErr_SetString(PyExc_TypeError, |
| 6980 | "%c requires int or char"); |
| 6981 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6982 | } |
| 6983 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 6984 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 6985 | |
| 6986 | FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
| 6987 | chars are formatted. XXX This is a magic number. Each formatting |
| 6988 | routine does bounds checking to ensure no overflow, but a better |
| 6989 | solution may be to malloc a buffer of appropriate size for each |
| 6990 | format. For now, the current solution is sufficient. |
| 6991 | */ |
| 6992 | #define FORMATBUFLEN (size_t)120 |
| 6993 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6994 | PyObject *PyUnicode_Format(PyObject *format, |
| 6995 | PyObject *args) |
| 6996 | { |
| 6997 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6998 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6999 | int args_owned = 0; |
| 7000 | PyUnicodeObject *result = NULL; |
| 7001 | PyObject *dict = NULL; |
| 7002 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7003 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7004 | if (format == NULL || args == NULL) { |
| 7005 | PyErr_BadInternalCall(); |
| 7006 | return NULL; |
| 7007 | } |
| 7008 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7009 | if (uformat == NULL) |
| 7010 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7011 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 7012 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 7013 | |
| 7014 | reslen = rescnt = fmtcnt + 100; |
| 7015 | result = _PyUnicode_New(reslen); |
| 7016 | if (result == NULL) |
| 7017 | goto onError; |
| 7018 | res = PyUnicode_AS_UNICODE(result); |
| 7019 | |
| 7020 | if (PyTuple_Check(args)) { |
| 7021 | arglen = PyTuple_Size(args); |
| 7022 | argidx = 0; |
| 7023 | } |
| 7024 | else { |
| 7025 | arglen = -1; |
| 7026 | argidx = -2; |
| 7027 | } |
Neal Norwitz | 80a1bf4 | 2002-11-12 23:01:12 +0000 | [diff] [blame] | 7028 | if (args->ob_type->tp_as_mapping && !PyTuple_Check(args) && |
| 7029 | !PyObject_TypeCheck(args, &PyBaseString_Type)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7030 | dict = args; |
| 7031 | |
| 7032 | while (--fmtcnt >= 0) { |
| 7033 | if (*fmt != '%') { |
| 7034 | if (--rescnt < 0) { |
| 7035 | rescnt = fmtcnt + 100; |
| 7036 | reslen += rescnt; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7037 | if (_PyUnicode_Resize(&result, reslen) < 0) |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 7038 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7039 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 7040 | --rescnt; |
| 7041 | } |
| 7042 | *res++ = *fmt++; |
| 7043 | } |
| 7044 | else { |
| 7045 | /* Got a format specifier */ |
| 7046 | int flags = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7047 | Py_ssize_t width = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7048 | int prec = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7049 | Py_UNICODE c = '\0'; |
| 7050 | Py_UNICODE fill; |
| 7051 | PyObject *v = NULL; |
| 7052 | PyObject *temp = NULL; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7053 | Py_UNICODE *pbuf; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7054 | Py_UNICODE sign; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7055 | Py_ssize_t len; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7056 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7057 | |
| 7058 | fmt++; |
| 7059 | if (*fmt == '(') { |
| 7060 | Py_UNICODE *keystart; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7061 | Py_ssize_t keylen; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7062 | PyObject *key; |
| 7063 | int pcount = 1; |
| 7064 | |
| 7065 | if (dict == NULL) { |
| 7066 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7067 | "format requires a mapping"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7068 | goto onError; |
| 7069 | } |
| 7070 | ++fmt; |
| 7071 | --fmtcnt; |
| 7072 | keystart = fmt; |
| 7073 | /* Skip over balanced parentheses */ |
| 7074 | while (pcount > 0 && --fmtcnt >= 0) { |
| 7075 | if (*fmt == ')') |
| 7076 | --pcount; |
| 7077 | else if (*fmt == '(') |
| 7078 | ++pcount; |
| 7079 | fmt++; |
| 7080 | } |
| 7081 | keylen = fmt - keystart - 1; |
| 7082 | if (fmtcnt < 0 || pcount > 0) { |
| 7083 | PyErr_SetString(PyExc_ValueError, |
| 7084 | "incomplete format key"); |
| 7085 | goto onError; |
| 7086 | } |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 7087 | #if 0 |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7088 | /* keys are converted to strings using UTF-8 and |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7089 | then looked up since Python uses strings to hold |
| 7090 | variables names etc. in its namespaces and we |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7091 | wouldn't want to break common idioms. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7092 | key = PyUnicode_EncodeUTF8(keystart, |
| 7093 | keylen, |
| 7094 | NULL); |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 7095 | #else |
| 7096 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 7097 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7098 | if (key == NULL) |
| 7099 | goto onError; |
| 7100 | if (args_owned) { |
| 7101 | Py_DECREF(args); |
| 7102 | args_owned = 0; |
| 7103 | } |
| 7104 | args = PyObject_GetItem(dict, key); |
| 7105 | Py_DECREF(key); |
| 7106 | if (args == NULL) { |
| 7107 | goto onError; |
| 7108 | } |
| 7109 | args_owned = 1; |
| 7110 | arglen = -1; |
| 7111 | argidx = -2; |
| 7112 | } |
| 7113 | while (--fmtcnt >= 0) { |
| 7114 | switch (c = *fmt++) { |
| 7115 | case '-': flags |= F_LJUST; continue; |
| 7116 | case '+': flags |= F_SIGN; continue; |
| 7117 | case ' ': flags |= F_BLANK; continue; |
| 7118 | case '#': flags |= F_ALT; continue; |
| 7119 | case '0': flags |= F_ZERO; continue; |
| 7120 | } |
| 7121 | break; |
| 7122 | } |
| 7123 | if (c == '*') { |
| 7124 | v = getnextarg(args, arglen, &argidx); |
| 7125 | if (v == NULL) |
| 7126 | goto onError; |
| 7127 | if (!PyInt_Check(v)) { |
| 7128 | PyErr_SetString(PyExc_TypeError, |
| 7129 | "* wants int"); |
| 7130 | goto onError; |
| 7131 | } |
| 7132 | width = PyInt_AsLong(v); |
| 7133 | if (width < 0) { |
| 7134 | flags |= F_LJUST; |
| 7135 | width = -width; |
| 7136 | } |
| 7137 | if (--fmtcnt >= 0) |
| 7138 | c = *fmt++; |
| 7139 | } |
| 7140 | else if (c >= '0' && c <= '9') { |
| 7141 | width = c - '0'; |
| 7142 | while (--fmtcnt >= 0) { |
| 7143 | c = *fmt++; |
| 7144 | if (c < '0' || c > '9') |
| 7145 | break; |
| 7146 | if ((width*10) / 10 != width) { |
| 7147 | PyErr_SetString(PyExc_ValueError, |
| 7148 | "width too big"); |
| 7149 | goto onError; |
| 7150 | } |
| 7151 | width = width*10 + (c - '0'); |
| 7152 | } |
| 7153 | } |
| 7154 | if (c == '.') { |
| 7155 | prec = 0; |
| 7156 | if (--fmtcnt >= 0) |
| 7157 | c = *fmt++; |
| 7158 | if (c == '*') { |
| 7159 | v = getnextarg(args, arglen, &argidx); |
| 7160 | if (v == NULL) |
| 7161 | goto onError; |
| 7162 | if (!PyInt_Check(v)) { |
| 7163 | PyErr_SetString(PyExc_TypeError, |
| 7164 | "* wants int"); |
| 7165 | goto onError; |
| 7166 | } |
| 7167 | prec = PyInt_AsLong(v); |
| 7168 | if (prec < 0) |
| 7169 | prec = 0; |
| 7170 | if (--fmtcnt >= 0) |
| 7171 | c = *fmt++; |
| 7172 | } |
| 7173 | else if (c >= '0' && c <= '9') { |
| 7174 | prec = c - '0'; |
| 7175 | while (--fmtcnt >= 0) { |
| 7176 | c = Py_CHARMASK(*fmt++); |
| 7177 | if (c < '0' || c > '9') |
| 7178 | break; |
| 7179 | if ((prec*10) / 10 != prec) { |
| 7180 | PyErr_SetString(PyExc_ValueError, |
| 7181 | "prec too big"); |
| 7182 | goto onError; |
| 7183 | } |
| 7184 | prec = prec*10 + (c - '0'); |
| 7185 | } |
| 7186 | } |
| 7187 | } /* prec */ |
| 7188 | if (fmtcnt >= 0) { |
| 7189 | if (c == 'h' || c == 'l' || c == 'L') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7190 | if (--fmtcnt >= 0) |
| 7191 | c = *fmt++; |
| 7192 | } |
| 7193 | } |
| 7194 | if (fmtcnt < 0) { |
| 7195 | PyErr_SetString(PyExc_ValueError, |
| 7196 | "incomplete format"); |
| 7197 | goto onError; |
| 7198 | } |
| 7199 | if (c != '%') { |
| 7200 | v = getnextarg(args, arglen, &argidx); |
| 7201 | if (v == NULL) |
| 7202 | goto onError; |
| 7203 | } |
| 7204 | sign = 0; |
| 7205 | fill = ' '; |
| 7206 | switch (c) { |
| 7207 | |
| 7208 | case '%': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7209 | pbuf = formatbuf; |
| 7210 | /* presume that buffer length is at least 1 */ |
| 7211 | pbuf[0] = '%'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7212 | len = 1; |
| 7213 | break; |
| 7214 | |
| 7215 | case 's': |
| 7216 | case 'r': |
| 7217 | if (PyUnicode_Check(v) && c == 's') { |
| 7218 | temp = v; |
| 7219 | Py_INCREF(temp); |
| 7220 | } |
| 7221 | else { |
| 7222 | PyObject *unicode; |
| 7223 | if (c == 's') |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 7224 | temp = PyObject_Unicode(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7225 | else |
| 7226 | temp = PyObject_Repr(v); |
| 7227 | if (temp == NULL) |
| 7228 | goto onError; |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 7229 | if (PyUnicode_Check(temp)) |
| 7230 | /* nothing to do */; |
| 7231 | else if (PyString_Check(temp)) { |
| 7232 | /* convert to string to Unicode */ |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7233 | unicode = PyUnicode_Decode(PyString_AS_STRING(temp), |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7234 | PyString_GET_SIZE(temp), |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7235 | NULL, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7236 | "strict"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7237 | Py_DECREF(temp); |
| 7238 | temp = unicode; |
| 7239 | if (temp == NULL) |
| 7240 | goto onError; |
| 7241 | } |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 7242 | else { |
| 7243 | Py_DECREF(temp); |
| 7244 | PyErr_SetString(PyExc_TypeError, |
| 7245 | "%s argument has non-string str()"); |
| 7246 | goto onError; |
| 7247 | } |
| 7248 | } |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7249 | pbuf = PyUnicode_AS_UNICODE(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7250 | len = PyUnicode_GET_SIZE(temp); |
| 7251 | if (prec >= 0 && len > prec) |
| 7252 | len = prec; |
| 7253 | break; |
| 7254 | |
| 7255 | case 'i': |
| 7256 | case 'd': |
| 7257 | case 'u': |
| 7258 | case 'o': |
| 7259 | case 'x': |
| 7260 | case 'X': |
| 7261 | if (c == 'i') |
| 7262 | c = 'd'; |
Tim Peters | a3a3a03 | 2000-11-30 05:22:44 +0000 | [diff] [blame] | 7263 | if (PyLong_Check(v)) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7264 | temp = formatlong(v, flags, prec, c); |
| 7265 | if (!temp) |
| 7266 | goto onError; |
| 7267 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 7268 | len = PyUnicode_GET_SIZE(temp); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7269 | sign = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7270 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7271 | else { |
| 7272 | pbuf = formatbuf; |
| 7273 | len = formatint(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 7274 | flags, prec, c, v); |
| 7275 | if (len < 0) |
| 7276 | goto onError; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7277 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7278 | } |
| 7279 | if (flags & F_ZERO) |
| 7280 | fill = '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7281 | break; |
| 7282 | |
| 7283 | case 'e': |
| 7284 | case 'E': |
| 7285 | case 'f': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 7286 | case 'F': |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7287 | case 'g': |
| 7288 | case 'G': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 7289 | if (c == 'F') |
| 7290 | c = 'f'; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7291 | pbuf = formatbuf; |
| 7292 | len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 7293 | flags, prec, c, v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7294 | if (len < 0) |
| 7295 | goto onError; |
| 7296 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7297 | if (flags & F_ZERO) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7298 | fill = '0'; |
| 7299 | break; |
| 7300 | |
| 7301 | case 'c': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7302 | pbuf = formatbuf; |
| 7303 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7304 | if (len < 0) |
| 7305 | goto onError; |
| 7306 | break; |
| 7307 | |
| 7308 | default: |
| 7309 | PyErr_Format(PyExc_ValueError, |
Andrew M. Kuchling | 6ca8917 | 2000-12-15 13:07:46 +0000 | [diff] [blame] | 7310 | "unsupported format character '%c' (0x%x) " |
| 7311 | "at index %i", |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7312 | (31<=c && c<=126) ? (char)c : '?', |
Marc-André Lemburg | 24e53b6 | 2002-09-24 09:32:14 +0000 | [diff] [blame] | 7313 | (int)c, |
Guido van Rossum | efc1188 | 2002-09-12 14:43:41 +0000 | [diff] [blame] | 7314 | (int)(fmt -1 - PyUnicode_AS_UNICODE(uformat))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7315 | goto onError; |
| 7316 | } |
| 7317 | if (sign) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7318 | if (*pbuf == '-' || *pbuf == '+') { |
| 7319 | sign = *pbuf++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7320 | len--; |
| 7321 | } |
| 7322 | else if (flags & F_SIGN) |
| 7323 | sign = '+'; |
| 7324 | else if (flags & F_BLANK) |
| 7325 | sign = ' '; |
| 7326 | else |
| 7327 | sign = 0; |
| 7328 | } |
| 7329 | if (width < len) |
| 7330 | width = len; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 7331 | if (rescnt - (sign != 0) < width) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7332 | reslen -= rescnt; |
| 7333 | rescnt = width + fmtcnt + 100; |
| 7334 | reslen += rescnt; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 7335 | if (reslen < 0) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 7336 | Py_XDECREF(temp); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7337 | PyErr_NoMemory(); |
| 7338 | goto onError; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 7339 | } |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7340 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 7341 | Py_XDECREF(temp); |
| 7342 | goto onError; |
| 7343 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7344 | res = PyUnicode_AS_UNICODE(result) |
| 7345 | + reslen - rescnt; |
| 7346 | } |
| 7347 | if (sign) { |
| 7348 | if (fill != ' ') |
| 7349 | *res++ = sign; |
| 7350 | rescnt--; |
| 7351 | if (width > len) |
| 7352 | width--; |
| 7353 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7354 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
| 7355 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 7356 | assert(pbuf[1] == c); |
| 7357 | if (fill != ' ') { |
| 7358 | *res++ = *pbuf++; |
| 7359 | *res++ = *pbuf++; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7360 | } |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 7361 | rescnt -= 2; |
| 7362 | width -= 2; |
| 7363 | if (width < 0) |
| 7364 | width = 0; |
| 7365 | len -= 2; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7366 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7367 | if (width > len && !(flags & F_LJUST)) { |
| 7368 | do { |
| 7369 | --rescnt; |
| 7370 | *res++ = fill; |
| 7371 | } while (--width > len); |
| 7372 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7373 | if (fill == ' ') { |
| 7374 | if (sign) |
| 7375 | *res++ = sign; |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 7376 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7377 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 7378 | assert(pbuf[1] == c); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7379 | *res++ = *pbuf++; |
| 7380 | *res++ = *pbuf++; |
| 7381 | } |
| 7382 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7383 | Py_UNICODE_COPY(res, pbuf, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7384 | res += len; |
| 7385 | rescnt -= len; |
| 7386 | while (--width >= len) { |
| 7387 | --rescnt; |
| 7388 | *res++ = ' '; |
| 7389 | } |
| 7390 | if (dict && (argidx < arglen) && c != '%') { |
| 7391 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 7392 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7393 | Py_XDECREF(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7394 | goto onError; |
| 7395 | } |
| 7396 | Py_XDECREF(temp); |
| 7397 | } /* '%' */ |
| 7398 | } /* until end */ |
| 7399 | if (argidx < arglen && !dict) { |
| 7400 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 7401 | "not all arguments converted during string formatting"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7402 | goto onError; |
| 7403 | } |
| 7404 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7405 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
| 7406 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7407 | if (args_owned) { |
| 7408 | Py_DECREF(args); |
| 7409 | } |
| 7410 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7411 | return (PyObject *)result; |
| 7412 | |
| 7413 | onError: |
| 7414 | Py_XDECREF(result); |
| 7415 | Py_DECREF(uformat); |
| 7416 | if (args_owned) { |
| 7417 | Py_DECREF(args); |
| 7418 | } |
| 7419 | return NULL; |
| 7420 | } |
| 7421 | |
| 7422 | static PyBufferProcs unicode_as_buffer = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7423 | (readbufferproc) unicode_buffer_getreadbuf, |
| 7424 | (writebufferproc) unicode_buffer_getwritebuf, |
| 7425 | (segcountproc) unicode_buffer_getsegcount, |
| 7426 | (charbufferproc) unicode_buffer_getcharbuf, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7427 | }; |
| 7428 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 7429 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7430 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 7431 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7432 | static PyObject * |
| 7433 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 7434 | { |
| 7435 | PyObject *x = NULL; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 7436 | static char *kwlist[] = {"string", "encoding", "errors", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7437 | char *encoding = NULL; |
| 7438 | char *errors = NULL; |
| 7439 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7440 | if (type != &PyUnicode_Type) |
| 7441 | return unicode_subtype_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7442 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:unicode", |
| 7443 | kwlist, &x, &encoding, &errors)) |
| 7444 | return NULL; |
| 7445 | if (x == NULL) |
| 7446 | return (PyObject *)_PyUnicode_New(0); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 7447 | if (encoding == NULL && errors == NULL) |
| 7448 | return PyObject_Unicode(x); |
| 7449 | else |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7450 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
| 7451 | } |
| 7452 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7453 | static PyObject * |
| 7454 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 7455 | { |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7456 | PyUnicodeObject *tmp, *pnew; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7457 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7458 | |
| 7459 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 7460 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 7461 | if (tmp == NULL) |
| 7462 | return NULL; |
| 7463 | assert(PyUnicode_Check(tmp)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7464 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 7465 | if (pnew == NULL) { |
| 7466 | Py_DECREF(tmp); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7467 | return NULL; |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 7468 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7469 | pnew->str = PyMem_NEW(Py_UNICODE, n+1); |
| 7470 | if (pnew->str == NULL) { |
| 7471 | _Py_ForgetReference((PyObject *)pnew); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 7472 | PyObject_Del(pnew); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 7473 | Py_DECREF(tmp); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 7474 | return PyErr_NoMemory(); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7475 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7476 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 7477 | pnew->length = n; |
| 7478 | pnew->hash = tmp->hash; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7479 | Py_DECREF(tmp); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7480 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7481 | } |
| 7482 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7483 | PyDoc_STRVAR(unicode_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7484 | "unicode(string [, encoding[, errors]]) -> object\n\ |
| 7485 | \n\ |
| 7486 | Create a new Unicode object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 7487 | encoding defaults to the current default string encoding.\n\ |
| 7488 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7489 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7490 | PyTypeObject PyUnicode_Type = { |
| 7491 | PyObject_HEAD_INIT(&PyType_Type) |
| 7492 | 0, /* ob_size */ |
| 7493 | "unicode", /* tp_name */ |
| 7494 | sizeof(PyUnicodeObject), /* tp_size */ |
| 7495 | 0, /* tp_itemsize */ |
| 7496 | /* Slots */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 7497 | (destructor)unicode_dealloc, /* tp_dealloc */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7498 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7499 | 0, /* tp_getattr */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7500 | 0, /* tp_setattr */ |
| 7501 | (cmpfunc) unicode_compare, /* tp_compare */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 7502 | unicode_repr, /* tp_repr */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 7503 | &unicode_as_number, /* tp_as_number */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7504 | &unicode_as_sequence, /* tp_as_sequence */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7505 | &unicode_as_mapping, /* tp_as_mapping */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7506 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 7507 | 0, /* tp_call*/ |
| 7508 | (reprfunc) unicode_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7509 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 7510 | 0, /* tp_setattro */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7511 | &unicode_as_buffer, /* tp_as_buffer */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 7512 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
| 7513 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7514 | unicode_doc, /* tp_doc */ |
| 7515 | 0, /* tp_traverse */ |
| 7516 | 0, /* tp_clear */ |
| 7517 | 0, /* tp_richcompare */ |
| 7518 | 0, /* tp_weaklistoffset */ |
| 7519 | 0, /* tp_iter */ |
| 7520 | 0, /* tp_iternext */ |
| 7521 | unicode_methods, /* tp_methods */ |
| 7522 | 0, /* tp_members */ |
| 7523 | 0, /* tp_getset */ |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 7524 | &PyBaseString_Type, /* tp_base */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7525 | 0, /* tp_dict */ |
| 7526 | 0, /* tp_descr_get */ |
| 7527 | 0, /* tp_descr_set */ |
| 7528 | 0, /* tp_dictoffset */ |
| 7529 | 0, /* tp_init */ |
| 7530 | 0, /* tp_alloc */ |
| 7531 | unicode_new, /* tp_new */ |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 7532 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7533 | }; |
| 7534 | |
| 7535 | /* Initialize the Unicode implementation */ |
| 7536 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 7537 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7538 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7539 | int i; |
| 7540 | |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 7541 | /* XXX - move this array to unicodectype.c ? */ |
| 7542 | Py_UNICODE linebreak[] = { |
| 7543 | 0x000A, /* LINE FEED */ |
| 7544 | 0x000D, /* CARRIAGE RETURN */ |
| 7545 | 0x001C, /* FILE SEPARATOR */ |
| 7546 | 0x001D, /* GROUP SEPARATOR */ |
| 7547 | 0x001E, /* RECORD SEPARATOR */ |
| 7548 | 0x0085, /* NEXT LINE */ |
| 7549 | 0x2028, /* LINE SEPARATOR */ |
| 7550 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 7551 | }; |
| 7552 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7553 | /* Init the implementation */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7554 | unicode_freelist = NULL; |
| 7555 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7556 | unicode_empty = _PyUnicode_New(0); |
Marc-André Lemburg | 90e8147 | 2000-06-07 09:13:21 +0000 | [diff] [blame] | 7557 | strcpy(unicode_default_encoding, "ascii"); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7558 | for (i = 0; i < 256; i++) |
| 7559 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 7560 | if (PyType_Ready(&PyUnicode_Type) < 0) |
| 7561 | Py_FatalError("Can't initialize 'unicode'"); |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 7562 | |
| 7563 | /* initialize the linebreak bloom filter */ |
| 7564 | bloom_linebreak = make_bloom_mask( |
| 7565 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 7566 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7567 | } |
| 7568 | |
| 7569 | /* Finalize the Unicode implementation */ |
| 7570 | |
| 7571 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 7572 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7573 | { |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 7574 | PyUnicodeObject *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7575 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7576 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 7577 | Py_XDECREF(unicode_empty); |
| 7578 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 7579 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7580 | for (i = 0; i < 256; i++) { |
| 7581 | if (unicode_latin1[i]) { |
| 7582 | Py_DECREF(unicode_latin1[i]); |
| 7583 | unicode_latin1[i] = NULL; |
| 7584 | } |
| 7585 | } |
| 7586 | |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 7587 | for (u = unicode_freelist; u != NULL;) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7588 | PyUnicodeObject *v = u; |
| 7589 | u = *(PyUnicodeObject **)u; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 7590 | if (v->str) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 7591 | PyMem_DEL(v->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 7592 | Py_XDECREF(v->defenc); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 7593 | PyObject_Del(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7594 | } |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7595 | unicode_freelist = NULL; |
| 7596 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7597 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 7598 | |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 7599 | #ifdef __cplusplus |
| 7600 | } |
| 7601 | #endif |
| 7602 | |
| 7603 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 7604 | /* |
| 7605 | Local variables: |
| 7606 | c-basic-offset: 4 |
| 7607 | indent-tabs-mode: nil |
| 7608 | End: |
| 7609 | */ |