Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1 | /* |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2 | |
| 3 | Unicode implementation based on original code by Fredrik Lundh, |
Fred Drake | 785d14f | 2000-05-09 19:54:43 +0000 | [diff] [blame] | 4 | modified by Marc-Andre Lemburg <mal@lemburg.com> according to the |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5 | Unicode Integration Proposal (see file Misc/unicode.txt). |
| 6 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7 | Major speed upgrades to the method implementations at the Reykjavik |
| 8 | NeedForSpeed sprint, by Fredrik Lundh and Andrew Dalke. |
| 9 | |
Guido van Rossum | 16b1ad9 | 2000-08-03 16:24:25 +0000 | [diff] [blame] | 10 | Copyright (c) Corporation for National Research Initiatives. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 12 | -------------------------------------------------------------------- |
| 13 | The original string type implementation is: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 15 | Copyright (c) 1999 by Secret Labs AB |
| 16 | Copyright (c) 1999 by Fredrik Lundh |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 17 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 18 | By obtaining, using, and/or copying this software and/or its |
| 19 | associated documentation, you agree that you have read, understood, |
| 20 | and will comply with the following terms and conditions: |
| 21 | |
| 22 | Permission to use, copy, modify, and distribute this software and its |
| 23 | associated documentation for any purpose and without fee is hereby |
| 24 | granted, provided that the above copyright notice appears in all |
| 25 | copies, and that both that copyright notice and this permission notice |
| 26 | appear in supporting documentation, and that the name of Secret Labs |
| 27 | AB or the author not be used in advertising or publicity pertaining to |
| 28 | distribution of the software without specific, written prior |
| 29 | permission. |
| 30 | |
| 31 | SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 32 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 33 | FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR |
| 34 | ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 35 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 36 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 37 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 38 | -------------------------------------------------------------------- |
| 39 | |
| 40 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 41 | |
Martin v. Löwis | 5cb6936 | 2006-04-14 09:08:42 +0000 | [diff] [blame] | 42 | #define PY_SSIZE_T_CLEAN |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 43 | #include "Python.h" |
| 44 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 45 | #include "unicodeobject.h" |
Marc-André Lemburg | d49e5b4 | 2000-06-30 14:58:20 +0000 | [diff] [blame] | 46 | #include "ucnhash.h" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 47 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 48 | #ifdef MS_WINDOWS |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 49 | #include <windows.h> |
| 50 | #endif |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 51 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 52 | /* Limit for the Unicode object free list */ |
| 53 | |
| 54 | #define MAX_UNICODE_FREELIST_SIZE 1024 |
| 55 | |
| 56 | /* Limit for the Unicode object free list stay alive optimization. |
| 57 | |
| 58 | The implementation will keep allocated Unicode memory intact for |
| 59 | all objects on the free list having a size less than this |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 60 | limit. This reduces malloc() overhead for small Unicode objects. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 61 | |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 62 | At worst this will result in MAX_UNICODE_FREELIST_SIZE * |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 63 | (sizeof(PyUnicodeObject) + KEEPALIVE_SIZE_LIMIT + |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 64 | malloc()-overhead) bytes of unused garbage. |
| 65 | |
| 66 | Setting the limit to 0 effectively turns the feature off. |
| 67 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 68 | Note: This is an experimental feature ! If you get core dumps when |
| 69 | using Unicode objects, turn this feature off. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 70 | |
| 71 | */ |
| 72 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 73 | #define KEEPALIVE_SIZE_LIMIT 9 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 74 | |
| 75 | /* Endianness switches; defaults to little endian */ |
| 76 | |
| 77 | #ifdef WORDS_BIGENDIAN |
| 78 | # define BYTEORDER_IS_BIG_ENDIAN |
| 79 | #else |
| 80 | # define BYTEORDER_IS_LITTLE_ENDIAN |
| 81 | #endif |
| 82 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 83 | /* --- Globals ------------------------------------------------------------ |
| 84 | |
| 85 | The globals are initialized by the _PyUnicode_Init() API and should |
| 86 | not be used before calling that API. |
| 87 | |
| 88 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 89 | |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 90 | |
| 91 | #ifdef __cplusplus |
| 92 | extern "C" { |
| 93 | #endif |
| 94 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 95 | /* Free list for Unicode objects */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 96 | static PyUnicodeObject *unicode_freelist; |
| 97 | static int unicode_freelist_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 98 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 99 | /* The empty Unicode object is shared to improve performance. */ |
| 100 | static PyUnicodeObject *unicode_empty; |
| 101 | |
| 102 | /* Single character Unicode strings in the Latin-1 range are being |
| 103 | shared as well. */ |
| 104 | static PyUnicodeObject *unicode_latin1[256]; |
| 105 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 106 | /* Default encoding to use and assume when NULL is passed as encoding |
| 107 | parameter; it is initialized by _PyUnicode_Init(). |
| 108 | |
| 109 | Always use the PyUnicode_SetDefaultEncoding() and |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 110 | PyUnicode_GetDefaultEncoding() APIs to access this global. |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 111 | |
| 112 | */ |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 113 | static char unicode_default_encoding[100]; |
| 114 | |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 115 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 116 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 117 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 118 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 119 | return 0x10FFFF; |
| 120 | #else |
| 121 | /* This is actually an illegal character, so it should |
| 122 | not be passed to unichr. */ |
| 123 | return 0xFFFF; |
| 124 | #endif |
| 125 | } |
| 126 | |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 127 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 128 | |
| 129 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 130 | to keep things simple, we use a single bitmask, using the least 5 |
| 131 | bits from each unicode characters as the bit index. */ |
| 132 | |
| 133 | /* the linebreak mask is set up by Unicode_Init below */ |
| 134 | |
| 135 | #define BLOOM_MASK unsigned long |
| 136 | |
| 137 | static BLOOM_MASK bloom_linebreak; |
| 138 | |
| 139 | #define BLOOM(mask, ch) ((mask & (1 << ((ch) & 0x1F)))) |
| 140 | |
| 141 | #define BLOOM_LINEBREAK(ch)\ |
| 142 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK((ch))) |
| 143 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 144 | Py_LOCAL_INLINE(BLOOM_MASK) make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len) |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 145 | { |
| 146 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 147 | |
| 148 | long mask; |
| 149 | Py_ssize_t i; |
| 150 | |
| 151 | mask = 0; |
| 152 | for (i = 0; i < len; i++) |
| 153 | mask |= (1 << (ptr[i] & 0x1F)); |
| 154 | |
| 155 | return mask; |
| 156 | } |
| 157 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 158 | Py_LOCAL_INLINE(int) unicode_member(Py_UNICODE chr, Py_UNICODE* set, Py_ssize_t setlen) |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 159 | { |
| 160 | Py_ssize_t i; |
| 161 | |
| 162 | for (i = 0; i < setlen; i++) |
| 163 | if (set[i] == chr) |
| 164 | return 1; |
| 165 | |
Fredrik Lundh | 7763351 | 2006-05-23 19:47:35 +0000 | [diff] [blame] | 166 | return 0; |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | #define BLOOM_MEMBER(mask, chr, set, setlen)\ |
| 170 | BLOOM(mask, chr) && unicode_member(chr, set, setlen) |
| 171 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 172 | /* --- Unicode Object ----------------------------------------------------- */ |
| 173 | |
| 174 | static |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 175 | int unicode_resize(register PyUnicodeObject *unicode, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 176 | Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 177 | { |
| 178 | void *oldstr; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 179 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 180 | /* Shortcut if there's nothing much to do. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 181 | if (unicode->length == length) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 182 | goto reset; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 183 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 184 | /* Resizing shared object (unicode_empty or single character |
| 185 | objects) in-place is not allowed. Use PyUnicode_Resize() |
| 186 | instead ! */ |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 187 | |
Martin v. Löwis | 80d2e59 | 2006-04-13 06:06:08 +0000 | [diff] [blame] | 188 | if (unicode == unicode_empty || |
| 189 | (unicode->length == 1 && |
| 190 | unicode->str[0] < 256U && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 191 | unicode_latin1[unicode->str[0]] == unicode)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 192 | PyErr_SetString(PyExc_SystemError, |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 193 | "can't resize shared unicode objects"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 194 | return -1; |
| 195 | } |
| 196 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 197 | /* We allocate one more byte to make sure the string is Ux0000 terminated. |
| 198 | The overallocation is also used by fastsearch, which assumes that it's |
Andrew M. Kuchling | 07bbfc6 | 2006-05-26 19:51:10 +0000 | [diff] [blame] | 199 | safe to look at str[length] (without making any assumptions about what |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 200 | it contains). */ |
| 201 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 202 | oldstr = unicode->str; |
Neal Norwitz | b3635f9 | 2008-03-18 04:17:36 +0000 | [diff] [blame] | 203 | unicode->str = PyObject_REALLOC(unicode->str, |
| 204 | sizeof(Py_UNICODE) * (length + 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 205 | if (!unicode->str) { |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 206 | unicode->str = (Py_UNICODE *)oldstr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 207 | PyErr_NoMemory(); |
| 208 | return -1; |
| 209 | } |
| 210 | unicode->str[length] = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 211 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 212 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 213 | reset: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 214 | /* Reset the object caches */ |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 215 | if (unicode->defenc) { |
| 216 | Py_DECREF(unicode->defenc); |
| 217 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 218 | } |
| 219 | unicode->hash = -1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 220 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | /* We allocate one more byte to make sure the string is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 225 | Ux0000 terminated -- XXX is this needed ? |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 226 | |
| 227 | XXX This allocator could further be enhanced by assuring that the |
| 228 | free list never reduces its size below 1. |
| 229 | |
| 230 | */ |
| 231 | |
| 232 | static |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 233 | PyUnicodeObject *_PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 234 | { |
| 235 | register PyUnicodeObject *unicode; |
| 236 | |
Andrew Dalke | e0df762 | 2006-05-27 11:04:36 +0000 | [diff] [blame] | 237 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 238 | if (length == 0 && unicode_empty != NULL) { |
| 239 | Py_INCREF(unicode_empty); |
| 240 | return unicode_empty; |
| 241 | } |
| 242 | |
| 243 | /* Unicode freelist & memory allocation */ |
| 244 | if (unicode_freelist) { |
| 245 | unicode = unicode_freelist; |
Marc-André Lemburg | bea47e7 | 2000-06-17 20:31:17 +0000 | [diff] [blame] | 246 | unicode_freelist = *(PyUnicodeObject **)unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 247 | unicode_freelist_size--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 248 | if (unicode->str) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 249 | /* Keep-Alive optimization: we only upsize the buffer, |
| 250 | never downsize it. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 251 | if ((unicode->length < length) && |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 252 | unicode_resize(unicode, length) < 0) { |
Neal Norwitz | b3635f9 | 2008-03-18 04:17:36 +0000 | [diff] [blame] | 253 | PyObject_DEL(unicode->str); |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 254 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 255 | } |
| 256 | } |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 257 | else { |
Neal Norwitz | b3635f9 | 2008-03-18 04:17:36 +0000 | [diff] [blame] | 258 | size_t new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 259 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 260 | } |
| 261 | PyObject_INIT(unicode, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 262 | } |
| 263 | else { |
Neal Norwitz | b3635f9 | 2008-03-18 04:17:36 +0000 | [diff] [blame] | 264 | size_t new_size; |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 265 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 266 | if (unicode == NULL) |
| 267 | return NULL; |
Neal Norwitz | b3635f9 | 2008-03-18 04:17:36 +0000 | [diff] [blame] | 268 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 269 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 272 | if (!unicode->str) { |
| 273 | PyErr_NoMemory(); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 274 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 275 | } |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 276 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 277 | * the caller fails before initializing str -- unicode_resize() |
| 278 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 279 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 280 | * We don't want unicode_resize to read uninitialized memory in |
| 281 | * that case. |
| 282 | */ |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 283 | unicode->str[0] = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 284 | unicode->str[length] = 0; |
Martin v. Löwis | f15da69 | 2006-04-13 07:24:50 +0000 | [diff] [blame] | 285 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 286 | unicode->hash = -1; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 287 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 288 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 289 | |
| 290 | onError: |
| 291 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 292 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 293 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | static |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 297 | void unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 298 | { |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 299 | if (PyUnicode_CheckExact(unicode) && |
| 300 | unicode_freelist_size < MAX_UNICODE_FREELIST_SIZE) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 301 | /* Keep-Alive optimization */ |
| 302 | if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { |
Neal Norwitz | b3635f9 | 2008-03-18 04:17:36 +0000 | [diff] [blame] | 303 | PyObject_DEL(unicode->str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 304 | unicode->str = NULL; |
| 305 | unicode->length = 0; |
| 306 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 307 | if (unicode->defenc) { |
| 308 | Py_DECREF(unicode->defenc); |
| 309 | unicode->defenc = NULL; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 310 | } |
| 311 | /* Add to free list */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 312 | *(PyUnicodeObject **)unicode = unicode_freelist; |
| 313 | unicode_freelist = unicode; |
| 314 | unicode_freelist_size++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 315 | } |
| 316 | else { |
Neal Norwitz | b3635f9 | 2008-03-18 04:17:36 +0000 | [diff] [blame] | 317 | PyObject_DEL(unicode->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 318 | Py_XDECREF(unicode->defenc); |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 319 | unicode->ob_type->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 323 | int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 324 | { |
| 325 | register PyUnicodeObject *v; |
| 326 | |
| 327 | /* Argument checks */ |
| 328 | if (unicode == NULL) { |
| 329 | PyErr_BadInternalCall(); |
| 330 | return -1; |
| 331 | } |
| 332 | v = (PyUnicodeObject *)*unicode; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 333 | 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] | 334 | PyErr_BadInternalCall(); |
| 335 | return -1; |
| 336 | } |
| 337 | |
| 338 | /* Resizing unicode_empty and single character objects is not |
| 339 | possible since these are being shared. We simply return a fresh |
| 340 | copy with the same Unicode content. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 341 | if (v->length != length && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 342 | (v == unicode_empty || v->length == 1)) { |
| 343 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 344 | if (w == NULL) |
| 345 | return -1; |
| 346 | Py_UNICODE_COPY(w->str, v->str, |
| 347 | length < v->length ? length : v->length); |
Raymond Hettinger | c8df578 | 2003-03-09 07:30:43 +0000 | [diff] [blame] | 348 | Py_DECREF(*unicode); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 349 | *unicode = (PyObject *)w; |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 354 | objects, since we can modify them in-place. */ |
| 355 | return unicode_resize(v, length); |
| 356 | } |
| 357 | |
| 358 | /* Internal API for use in unicodeobject.c only ! */ |
| 359 | #define _PyUnicode_Resize(unicodevar, length) \ |
| 360 | PyUnicode_Resize(((PyObject **)(unicodevar)), length) |
| 361 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 362 | PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 363 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 364 | { |
| 365 | PyUnicodeObject *unicode; |
| 366 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 367 | /* If the Unicode data is known at construction time, we can apply |
| 368 | some optimizations which share commonly used objects. */ |
| 369 | if (u != NULL) { |
| 370 | |
| 371 | /* Optimization for empty strings */ |
| 372 | if (size == 0 && unicode_empty != NULL) { |
| 373 | Py_INCREF(unicode_empty); |
| 374 | return (PyObject *)unicode_empty; |
| 375 | } |
| 376 | |
| 377 | /* Single character Unicode objects in the Latin-1 range are |
| 378 | shared when using this constructor */ |
| 379 | if (size == 1 && *u < 256) { |
| 380 | unicode = unicode_latin1[*u]; |
| 381 | if (!unicode) { |
| 382 | unicode = _PyUnicode_New(1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 383 | if (!unicode) |
| 384 | return NULL; |
Marc-André Lemburg | 8879a33 | 2001-06-07 12:26:56 +0000 | [diff] [blame] | 385 | unicode->str[0] = *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 386 | unicode_latin1[*u] = unicode; |
| 387 | } |
| 388 | Py_INCREF(unicode); |
| 389 | return (PyObject *)unicode; |
| 390 | } |
| 391 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 392 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 393 | unicode = _PyUnicode_New(size); |
| 394 | if (!unicode) |
| 395 | return NULL; |
| 396 | |
| 397 | /* Copy the Unicode data into the new object */ |
| 398 | if (u != NULL) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 399 | Py_UNICODE_COPY(unicode->str, u, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 400 | |
| 401 | return (PyObject *)unicode; |
| 402 | } |
| 403 | |
| 404 | #ifdef HAVE_WCHAR_H |
| 405 | |
| 406 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 407 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 408 | { |
| 409 | PyUnicodeObject *unicode; |
| 410 | |
| 411 | if (w == NULL) { |
| 412 | PyErr_BadInternalCall(); |
| 413 | return NULL; |
| 414 | } |
| 415 | |
| 416 | unicode = _PyUnicode_New(size); |
| 417 | if (!unicode) |
| 418 | return NULL; |
| 419 | |
| 420 | /* Copy the wchar_t data into the new object */ |
| 421 | #ifdef HAVE_USABLE_WCHAR_T |
| 422 | memcpy(unicode->str, w, size * sizeof(wchar_t)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 423 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 424 | { |
| 425 | register Py_UNICODE *u; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 426 | register Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 427 | u = PyUnicode_AS_UNICODE(unicode); |
Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 428 | for (i = size; i > 0; i--) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 429 | *u++ = *w++; |
| 430 | } |
| 431 | #endif |
| 432 | |
| 433 | return (PyObject *)unicode; |
| 434 | } |
| 435 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 436 | Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
| 437 | wchar_t *w, |
| 438 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 439 | { |
| 440 | if (unicode == NULL) { |
| 441 | PyErr_BadInternalCall(); |
| 442 | return -1; |
| 443 | } |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 444 | |
| 445 | /* If possible, try to copy the 0-termination as well */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 446 | if (size > PyUnicode_GET_SIZE(unicode)) |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 447 | size = PyUnicode_GET_SIZE(unicode) + 1; |
| 448 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 449 | #ifdef HAVE_USABLE_WCHAR_T |
| 450 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 451 | #else |
| 452 | { |
| 453 | register Py_UNICODE *u; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 454 | register Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 455 | u = PyUnicode_AS_UNICODE(unicode); |
Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 456 | for (i = size; i > 0; i--) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 457 | *w++ = *u++; |
| 458 | } |
| 459 | #endif |
| 460 | |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 461 | if (size > PyUnicode_GET_SIZE(unicode)) |
| 462 | return PyUnicode_GET_SIZE(unicode); |
| 463 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 464 | return size; |
| 465 | } |
| 466 | |
| 467 | #endif |
| 468 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 469 | PyObject *PyUnicode_FromOrdinal(int ordinal) |
| 470 | { |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 471 | Py_UNICODE s[1]; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 472 | |
| 473 | #ifdef Py_UNICODE_WIDE |
| 474 | if (ordinal < 0 || ordinal > 0x10ffff) { |
| 475 | PyErr_SetString(PyExc_ValueError, |
| 476 | "unichr() arg not in range(0x110000) " |
| 477 | "(wide Python build)"); |
| 478 | return NULL; |
| 479 | } |
| 480 | #else |
| 481 | if (ordinal < 0 || ordinal > 0xffff) { |
| 482 | PyErr_SetString(PyExc_ValueError, |
| 483 | "unichr() arg not in range(0x10000) " |
| 484 | "(narrow Python build)"); |
| 485 | return NULL; |
| 486 | } |
| 487 | #endif |
| 488 | |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 489 | s[0] = (Py_UNICODE)ordinal; |
| 490 | return PyUnicode_FromUnicode(s, 1); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 493 | PyObject *PyUnicode_FromObject(register PyObject *obj) |
| 494 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 495 | /* XXX Perhaps we should make this API an alias of |
| 496 | PyObject_Unicode() instead ?! */ |
| 497 | if (PyUnicode_CheckExact(obj)) { |
| 498 | Py_INCREF(obj); |
| 499 | return obj; |
| 500 | } |
| 501 | if (PyUnicode_Check(obj)) { |
| 502 | /* For a Unicode subtype that's not a Unicode object, |
| 503 | return a true Unicode object with the same data. */ |
| 504 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 505 | PyUnicode_GET_SIZE(obj)); |
| 506 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 507 | return PyUnicode_FromEncodedObject(obj, NULL, "strict"); |
| 508 | } |
| 509 | |
| 510 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
| 511 | const char *encoding, |
| 512 | const char *errors) |
| 513 | { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 514 | const char *s = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 515 | Py_ssize_t len; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 516 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 517 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 518 | if (obj == NULL) { |
| 519 | PyErr_BadInternalCall(); |
| 520 | return NULL; |
| 521 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 522 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 523 | #if 0 |
| 524 | /* For b/w compatibility we also accept Unicode objects provided |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 525 | that no encodings is given and then redirect to |
| 526 | PyObject_Unicode() which then applies the additional logic for |
| 527 | Unicode subclasses. |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 528 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 529 | NOTE: This API should really only be used for object which |
| 530 | represent *encoded* Unicode ! |
| 531 | |
| 532 | */ |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 533 | if (PyUnicode_Check(obj)) { |
| 534 | if (encoding) { |
| 535 | PyErr_SetString(PyExc_TypeError, |
| 536 | "decoding Unicode is not supported"); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 537 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 538 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 539 | return PyObject_Unicode(obj); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 540 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 541 | #else |
| 542 | if (PyUnicode_Check(obj)) { |
| 543 | PyErr_SetString(PyExc_TypeError, |
| 544 | "decoding Unicode is not supported"); |
| 545 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 546 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 547 | #endif |
| 548 | |
| 549 | /* Coerce object */ |
| 550 | if (PyString_Check(obj)) { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 551 | s = PyString_AS_STRING(obj); |
| 552 | len = PyString_GET_SIZE(obj); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 553 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 554 | else if (PyObject_AsCharBuffer(obj, &s, &len)) { |
| 555 | /* Overwrite the error message with something more useful in |
| 556 | case of a TypeError. */ |
| 557 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 558 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 559 | "coercing to Unicode: need string or buffer, " |
| 560 | "%.80s found", |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 561 | obj->ob_type->tp_name); |
| 562 | goto onError; |
| 563 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 564 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 565 | /* Convert to Unicode */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 566 | if (len == 0) { |
| 567 | Py_INCREF(unicode_empty); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 568 | v = (PyObject *)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 569 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 570 | else |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 571 | v = PyUnicode_Decode(s, len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 572 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 573 | return v; |
| 574 | |
| 575 | onError: |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 576 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | PyObject *PyUnicode_Decode(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 580 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 581 | const char *encoding, |
| 582 | const char *errors) |
| 583 | { |
| 584 | PyObject *buffer = NULL, *unicode; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 585 | |
| 586 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 587 | encoding = PyUnicode_GetDefaultEncoding(); |
| 588 | |
| 589 | /* Shortcuts for common default encodings */ |
| 590 | if (strcmp(encoding, "utf-8") == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 591 | return PyUnicode_DecodeUTF8(s, size, errors); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 592 | else if (strcmp(encoding, "latin-1") == 0) |
| 593 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 594 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 595 | else if (strcmp(encoding, "mbcs") == 0) |
| 596 | return PyUnicode_DecodeMBCS(s, size, errors); |
| 597 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 598 | else if (strcmp(encoding, "ascii") == 0) |
| 599 | return PyUnicode_DecodeASCII(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 600 | |
| 601 | /* Decode via the codec registry */ |
| 602 | buffer = PyBuffer_FromMemory((void *)s, size); |
| 603 | if (buffer == NULL) |
| 604 | goto onError; |
| 605 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 606 | if (unicode == NULL) |
| 607 | goto onError; |
| 608 | if (!PyUnicode_Check(unicode)) { |
| 609 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 610 | "decoder did not return an unicode object (type=%.400s)", |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 611 | unicode->ob_type->tp_name); |
| 612 | Py_DECREF(unicode); |
| 613 | goto onError; |
| 614 | } |
| 615 | Py_DECREF(buffer); |
| 616 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 617 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 618 | onError: |
| 619 | Py_XDECREF(buffer); |
| 620 | return NULL; |
| 621 | } |
| 622 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 623 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 624 | const char *encoding, |
| 625 | const char *errors) |
| 626 | { |
| 627 | PyObject *v; |
| 628 | |
| 629 | if (!PyUnicode_Check(unicode)) { |
| 630 | PyErr_BadArgument(); |
| 631 | goto onError; |
| 632 | } |
| 633 | |
| 634 | if (encoding == NULL) |
| 635 | encoding = PyUnicode_GetDefaultEncoding(); |
| 636 | |
| 637 | /* Decode via the codec registry */ |
| 638 | v = PyCodec_Decode(unicode, encoding, errors); |
| 639 | if (v == NULL) |
| 640 | goto onError; |
| 641 | return v; |
| 642 | |
| 643 | onError: |
| 644 | return NULL; |
| 645 | } |
| 646 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 647 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 648 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 649 | const char *encoding, |
| 650 | const char *errors) |
| 651 | { |
| 652 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 653 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 654 | unicode = PyUnicode_FromUnicode(s, size); |
| 655 | if (unicode == NULL) |
| 656 | return NULL; |
| 657 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 658 | Py_DECREF(unicode); |
| 659 | return v; |
| 660 | } |
| 661 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 662 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 663 | const char *encoding, |
| 664 | const char *errors) |
| 665 | { |
| 666 | PyObject *v; |
| 667 | |
| 668 | if (!PyUnicode_Check(unicode)) { |
| 669 | PyErr_BadArgument(); |
| 670 | goto onError; |
| 671 | } |
| 672 | |
| 673 | if (encoding == NULL) |
| 674 | encoding = PyUnicode_GetDefaultEncoding(); |
| 675 | |
| 676 | /* Encode via the codec registry */ |
| 677 | v = PyCodec_Encode(unicode, encoding, errors); |
| 678 | if (v == NULL) |
| 679 | goto onError; |
| 680 | return v; |
| 681 | |
| 682 | onError: |
| 683 | return NULL; |
| 684 | } |
| 685 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 686 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 687 | const char *encoding, |
| 688 | const char *errors) |
| 689 | { |
| 690 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 691 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 692 | if (!PyUnicode_Check(unicode)) { |
| 693 | PyErr_BadArgument(); |
| 694 | goto onError; |
| 695 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 696 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 697 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 698 | encoding = PyUnicode_GetDefaultEncoding(); |
| 699 | |
| 700 | /* Shortcuts for common default encodings */ |
| 701 | if (errors == NULL) { |
| 702 | if (strcmp(encoding, "utf-8") == 0) |
Jeremy Hylton | 9cea41c | 2001-05-29 17:13:15 +0000 | [diff] [blame] | 703 | return PyUnicode_AsUTF8String(unicode); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 704 | else if (strcmp(encoding, "latin-1") == 0) |
| 705 | return PyUnicode_AsLatin1String(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 706 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 707 | else if (strcmp(encoding, "mbcs") == 0) |
| 708 | return PyUnicode_AsMBCSString(unicode); |
| 709 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 710 | else if (strcmp(encoding, "ascii") == 0) |
| 711 | return PyUnicode_AsASCIIString(unicode); |
| 712 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 713 | |
| 714 | /* Encode via the codec registry */ |
| 715 | v = PyCodec_Encode(unicode, encoding, errors); |
| 716 | if (v == NULL) |
| 717 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 718 | if (!PyString_Check(v)) { |
| 719 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 720 | "encoder did not return a string object (type=%.400s)", |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 721 | v->ob_type->tp_name); |
| 722 | Py_DECREF(v); |
| 723 | goto onError; |
| 724 | } |
| 725 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 726 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 727 | onError: |
| 728 | return NULL; |
| 729 | } |
| 730 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 731 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
| 732 | const char *errors) |
| 733 | { |
| 734 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
| 735 | |
| 736 | if (v) |
| 737 | return v; |
| 738 | v = PyUnicode_AsEncodedString(unicode, NULL, errors); |
| 739 | if (v && errors == NULL) |
| 740 | ((PyUnicodeObject *)unicode)->defenc = v; |
| 741 | return v; |
| 742 | } |
| 743 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 744 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 745 | { |
| 746 | if (!PyUnicode_Check(unicode)) { |
| 747 | PyErr_BadArgument(); |
| 748 | goto onError; |
| 749 | } |
| 750 | return PyUnicode_AS_UNICODE(unicode); |
| 751 | |
| 752 | onError: |
| 753 | return NULL; |
| 754 | } |
| 755 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 756 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 757 | { |
| 758 | if (!PyUnicode_Check(unicode)) { |
| 759 | PyErr_BadArgument(); |
| 760 | goto onError; |
| 761 | } |
| 762 | return PyUnicode_GET_SIZE(unicode); |
| 763 | |
| 764 | onError: |
| 765 | return -1; |
| 766 | } |
| 767 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 768 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 769 | { |
| 770 | return unicode_default_encoding; |
| 771 | } |
| 772 | |
| 773 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 774 | { |
| 775 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 776 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 777 | /* Make sure the encoding is valid. As side effect, this also |
| 778 | loads the encoding into the codec registry cache. */ |
| 779 | v = _PyCodec_Lookup(encoding); |
| 780 | if (v == NULL) |
| 781 | goto onError; |
| 782 | Py_DECREF(v); |
| 783 | strncpy(unicode_default_encoding, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 784 | encoding, |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 785 | sizeof(unicode_default_encoding)); |
| 786 | return 0; |
| 787 | |
| 788 | onError: |
| 789 | return -1; |
| 790 | } |
| 791 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 792 | /* error handling callback helper: |
| 793 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 794 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 795 | and adjust various state variables. |
| 796 | return 0 on success, -1 on error |
| 797 | */ |
| 798 | |
| 799 | static |
| 800 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
| 801 | const char *encoding, const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 802 | const char *input, Py_ssize_t insize, Py_ssize_t *startinpos, Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 803 | PyObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 804 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 805 | 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] | 806 | |
| 807 | PyObject *restuple = NULL; |
| 808 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 809 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
| 810 | Py_ssize_t requiredsize; |
| 811 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 812 | Py_UNICODE *repptr; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 813 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 814 | int res = -1; |
| 815 | |
| 816 | if (*errorHandler == NULL) { |
| 817 | *errorHandler = PyCodec_LookupError(errors); |
| 818 | if (*errorHandler == NULL) |
| 819 | goto onError; |
| 820 | } |
| 821 | |
| 822 | if (*exceptionObject == NULL) { |
| 823 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 824 | encoding, input, insize, *startinpos, *endinpos, reason); |
| 825 | if (*exceptionObject == NULL) |
| 826 | goto onError; |
| 827 | } |
| 828 | else { |
| 829 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 830 | goto onError; |
| 831 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
| 832 | goto onError; |
| 833 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 834 | goto onError; |
| 835 | } |
| 836 | |
| 837 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 838 | if (restuple == NULL) |
| 839 | goto onError; |
| 840 | if (!PyTuple_Check(restuple)) { |
| 841 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 842 | goto onError; |
| 843 | } |
| 844 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
| 845 | goto onError; |
| 846 | if (newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 847 | newpos = insize+newpos; |
| 848 | if (newpos<0 || newpos>insize) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 849 | 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] | 850 | goto onError; |
| 851 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 852 | |
| 853 | /* need more space? (at least enough for what we |
| 854 | have+the replacement+the rest of the string (starting |
| 855 | at the new input position), so we won't have to check space |
| 856 | when there are no errors in the rest of the string) */ |
| 857 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 858 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 859 | requiredsize = *outpos + repsize + insize-newpos; |
| 860 | if (requiredsize > outsize) { |
| 861 | if (requiredsize<2*outsize) |
| 862 | requiredsize = 2*outsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 863 | if (PyUnicode_Resize(output, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 864 | goto onError; |
| 865 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
| 866 | } |
| 867 | *endinpos = newpos; |
| 868 | *inptr = input + newpos; |
| 869 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 870 | *outptr += repsize; |
| 871 | *outpos += repsize; |
| 872 | /* we made it! */ |
| 873 | res = 0; |
| 874 | |
| 875 | onError: |
| 876 | Py_XDECREF(restuple); |
| 877 | return res; |
| 878 | } |
| 879 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 880 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 881 | |
| 882 | /* see RFC2152 for details */ |
| 883 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 884 | static |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 885 | char utf7_special[128] = { |
| 886 | /* indicate whether a UTF-7 character is special i.e. cannot be directly |
| 887 | encoded: |
| 888 | 0 - not special |
| 889 | 1 - special |
| 890 | 2 - whitespace (optional) |
| 891 | 3 - RFC2152 Set O (optional) */ |
| 892 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, |
| 893 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 894 | 2, 3, 3, 3, 3, 3, 3, 0, 0, 0, 3, 1, 0, 0, 0, 1, |
| 895 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, |
| 896 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 897 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, |
| 898 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 899 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 1, 1, |
| 900 | |
| 901 | }; |
| 902 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 903 | /* Note: The comparison (c) <= 0 is a trick to work-around gcc |
| 904 | warnings about the comparison always being false; since |
| 905 | utf7_special[0] is 1, we can safely make that one comparison |
| 906 | true */ |
| 907 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 908 | #define SPECIAL(c, encodeO, encodeWS) \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 909 | ((c) > 127 || (c) <= 0 || utf7_special[(c)] == 1 || \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 910 | (encodeWS && (utf7_special[(c)] == 2)) || \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 911 | (encodeO && (utf7_special[(c)] == 3))) |
| 912 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 913 | #define B64(n) \ |
| 914 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 915 | #define B64CHAR(c) \ |
| 916 | (isalnum(c) || (c) == '+' || (c) == '/') |
| 917 | #define UB64(c) \ |
| 918 | ((c) == '+' ? 62 : (c) == '/' ? 63 : (c) >= 'a' ? \ |
| 919 | (c) - 71 : (c) >= 'A' ? (c) - 65 : (c) + 4 ) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 920 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 921 | #define ENCODE(out, ch, bits) \ |
| 922 | while (bits >= 6) { \ |
| 923 | *out++ = B64(ch >> (bits-6)); \ |
| 924 | bits -= 6; \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 925 | } |
| 926 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 927 | #define DECODE(out, ch, bits, surrogate) \ |
| 928 | while (bits >= 16) { \ |
| 929 | Py_UNICODE outCh = (Py_UNICODE) ((ch >> (bits-16)) & 0xffff); \ |
| 930 | bits -= 16; \ |
| 931 | if (surrogate) { \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 932 | /* We have already generated an error for the high surrogate \ |
| 933 | 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] | 934 | surrogate = 0; \ |
| 935 | } else if (0xDC00 <= outCh && outCh <= 0xDFFF) { \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 936 | /* This is a surrogate pair. Unfortunately we can't represent \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 937 | it in a 16-bit character */ \ |
| 938 | surrogate = 1; \ |
| 939 | errmsg = "code pairs are not supported"; \ |
| 940 | goto utf7Error; \ |
| 941 | } else { \ |
| 942 | *out++ = outCh; \ |
| 943 | } \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 944 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 945 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 946 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 947 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 948 | const char *errors) |
| 949 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 950 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 951 | Py_ssize_t startinpos; |
| 952 | Py_ssize_t endinpos; |
| 953 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 954 | const char *e; |
| 955 | PyUnicodeObject *unicode; |
| 956 | Py_UNICODE *p; |
| 957 | const char *errmsg = ""; |
| 958 | int inShift = 0; |
| 959 | unsigned int bitsleft = 0; |
| 960 | unsigned long charsleft = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 961 | int surrogate = 0; |
| 962 | PyObject *errorHandler = NULL; |
| 963 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 964 | |
| 965 | unicode = _PyUnicode_New(size); |
| 966 | if (!unicode) |
| 967 | return NULL; |
| 968 | if (size == 0) |
| 969 | return (PyObject *)unicode; |
| 970 | |
| 971 | p = unicode->str; |
| 972 | e = s + size; |
| 973 | |
| 974 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 975 | Py_UNICODE ch; |
| 976 | restart: |
| 977 | ch = *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 978 | |
| 979 | if (inShift) { |
| 980 | if ((ch == '-') || !B64CHAR(ch)) { |
| 981 | inShift = 0; |
| 982 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 983 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 984 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 985 | if (bitsleft >= 6) { |
| 986 | /* The shift sequence has a partial character in it. If |
| 987 | bitsleft < 6 then we could just classify it as padding |
| 988 | but that is not the case here */ |
| 989 | |
| 990 | errmsg = "partial character in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 991 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 992 | } |
| 993 | /* According to RFC2152 the remaining bits should be zero. We |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 994 | choose to signal an error/insert a replacement character |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 995 | here so indicate the potential of a misencoded character. */ |
| 996 | |
| 997 | /* On x86, a << b == a << (b%32) so make sure that bitsleft != 0 */ |
| 998 | if (bitsleft && charsleft << (sizeof(charsleft) * 8 - bitsleft)) { |
| 999 | errmsg = "non-zero padding bits in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1000 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | if (ch == '-') { |
| 1004 | if ((s < e) && (*(s) == '-')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1005 | *p++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1006 | inShift = 1; |
| 1007 | } |
| 1008 | } else if (SPECIAL(ch,0,0)) { |
| 1009 | errmsg = "unexpected special character"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1010 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1011 | } else { |
| 1012 | *p++ = ch; |
| 1013 | } |
| 1014 | } else { |
| 1015 | charsleft = (charsleft << 6) | UB64(ch); |
| 1016 | bitsleft += 6; |
| 1017 | s++; |
| 1018 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1019 | } |
| 1020 | } |
| 1021 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1022 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1023 | s++; |
| 1024 | if (s < e && *s == '-') { |
| 1025 | s++; |
| 1026 | *p++ = '+'; |
| 1027 | } else |
| 1028 | { |
| 1029 | inShift = 1; |
| 1030 | bitsleft = 0; |
| 1031 | } |
| 1032 | } |
| 1033 | else if (SPECIAL(ch,0,0)) { |
| 1034 | errmsg = "unexpected special character"; |
| 1035 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1036 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1037 | } |
| 1038 | else { |
| 1039 | *p++ = ch; |
| 1040 | s++; |
| 1041 | } |
| 1042 | continue; |
| 1043 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1044 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1045 | endinpos = s-starts; |
| 1046 | if (unicode_decode_call_errorhandler( |
| 1047 | errors, &errorHandler, |
| 1048 | "utf7", errmsg, |
| 1049 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1050 | (PyObject **)&unicode, &outpos, &p)) |
| 1051 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | if (inShift) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1055 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1056 | endinpos = size; |
| 1057 | if (unicode_decode_call_errorhandler( |
| 1058 | errors, &errorHandler, |
| 1059 | "utf7", "unterminated shift sequence", |
| 1060 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1061 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1062 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1063 | if (s < e) |
| 1064 | goto restart; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1067 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1068 | goto onError; |
| 1069 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1070 | Py_XDECREF(errorHandler); |
| 1071 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1072 | return (PyObject *)unicode; |
| 1073 | |
| 1074 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1075 | Py_XDECREF(errorHandler); |
| 1076 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1077 | Py_DECREF(unicode); |
| 1078 | return NULL; |
| 1079 | } |
| 1080 | |
| 1081 | |
| 1082 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1083 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1084 | int encodeSetO, |
| 1085 | int encodeWhiteSpace, |
| 1086 | const char *errors) |
| 1087 | { |
| 1088 | PyObject *v; |
| 1089 | /* It might be possible to tighten this worst case */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1090 | Py_ssize_t cbAllocated = 5 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1091 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1092 | Py_ssize_t i = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1093 | unsigned int bitsleft = 0; |
| 1094 | unsigned long charsleft = 0; |
| 1095 | char * out; |
| 1096 | char * start; |
| 1097 | |
| 1098 | if (size == 0) |
| 1099 | return PyString_FromStringAndSize(NULL, 0); |
| 1100 | |
| 1101 | v = PyString_FromStringAndSize(NULL, cbAllocated); |
| 1102 | if (v == NULL) |
| 1103 | return NULL; |
| 1104 | |
| 1105 | start = out = PyString_AS_STRING(v); |
| 1106 | for (;i < size; ++i) { |
| 1107 | Py_UNICODE ch = s[i]; |
| 1108 | |
| 1109 | if (!inShift) { |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1110 | if (ch == '+') { |
| 1111 | *out++ = '+'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1112 | *out++ = '-'; |
| 1113 | } else if (SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1114 | charsleft = ch; |
| 1115 | bitsleft = 16; |
| 1116 | *out++ = '+'; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1117 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1118 | inShift = bitsleft > 0; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1119 | } else { |
| 1120 | *out++ = (char) ch; |
| 1121 | } |
| 1122 | } else { |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1123 | if (!SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1124 | *out++ = B64(charsleft << (6-bitsleft)); |
| 1125 | charsleft = 0; |
| 1126 | bitsleft = 0; |
| 1127 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 1128 | so no '-' is required, except if the character is itself a '-' */ |
| 1129 | if (B64CHAR(ch) || ch == '-') { |
| 1130 | *out++ = '-'; |
| 1131 | } |
| 1132 | inShift = 0; |
| 1133 | *out++ = (char) ch; |
| 1134 | } else { |
| 1135 | bitsleft += 16; |
| 1136 | charsleft = (charsleft << 16) | ch; |
| 1137 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
| 1138 | |
| 1139 | /* If the next character is special then we dont' need to terminate |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1140 | 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] | 1141 | or '-' then the shift sequence will be terminated implicitly and we |
| 1142 | don't have to insert a '-'. */ |
| 1143 | |
| 1144 | if (bitsleft == 0) { |
| 1145 | if (i + 1 < size) { |
| 1146 | Py_UNICODE ch2 = s[i+1]; |
| 1147 | |
| 1148 | if (SPECIAL(ch2, encodeSetO, encodeWhiteSpace)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1149 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1150 | } else if (B64CHAR(ch2) || ch2 == '-') { |
| 1151 | *out++ = '-'; |
| 1152 | inShift = 0; |
| 1153 | } else { |
| 1154 | inShift = 0; |
| 1155 | } |
| 1156 | |
| 1157 | } |
| 1158 | else { |
| 1159 | *out++ = '-'; |
| 1160 | inShift = 0; |
| 1161 | } |
| 1162 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1163 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1164 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1165 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1166 | if (bitsleft) { |
| 1167 | *out++= B64(charsleft << (6-bitsleft) ); |
| 1168 | *out++ = '-'; |
| 1169 | } |
| 1170 | |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 1171 | _PyString_Resize(&v, out - start); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1172 | return v; |
| 1173 | } |
| 1174 | |
| 1175 | #undef SPECIAL |
| 1176 | #undef B64 |
| 1177 | #undef B64CHAR |
| 1178 | #undef UB64 |
| 1179 | #undef ENCODE |
| 1180 | #undef DECODE |
| 1181 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1182 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 1183 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1184 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1185 | char utf8_code_length[256] = { |
| 1186 | /* Map UTF-8 encoded prefix byte to sequence length. zero means |
| 1187 | illegal prefix. see RFC 2279 for details */ |
| 1188 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1189 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1190 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1191 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1192 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1193 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1194 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1195 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1196 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1197 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1198 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1199 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1200 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1201 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1202 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 1203 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 |
| 1204 | }; |
| 1205 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1206 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1207 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1208 | const char *errors) |
| 1209 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1210 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 1211 | } |
| 1212 | |
| 1213 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1214 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1215 | const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1216 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1217 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1218 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1219 | int n; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1220 | Py_ssize_t startinpos; |
| 1221 | Py_ssize_t endinpos; |
| 1222 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1223 | const char *e; |
| 1224 | PyUnicodeObject *unicode; |
| 1225 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1226 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1227 | PyObject *errorHandler = NULL; |
| 1228 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1229 | |
| 1230 | /* Note: size will always be longer than the resulting Unicode |
| 1231 | character count */ |
| 1232 | unicode = _PyUnicode_New(size); |
| 1233 | if (!unicode) |
| 1234 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1235 | if (size == 0) { |
| 1236 | if (consumed) |
| 1237 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1238 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1239 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1240 | |
| 1241 | /* Unpack UTF-8 encoded data */ |
| 1242 | p = unicode->str; |
| 1243 | e = s + size; |
| 1244 | |
| 1245 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1246 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1247 | |
| 1248 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1249 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1250 | s++; |
| 1251 | continue; |
| 1252 | } |
| 1253 | |
| 1254 | n = utf8_code_length[ch]; |
| 1255 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1256 | if (s + n > e) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1257 | if (consumed) |
| 1258 | break; |
| 1259 | else { |
| 1260 | errmsg = "unexpected end of data"; |
| 1261 | startinpos = s-starts; |
| 1262 | endinpos = size; |
| 1263 | goto utf8Error; |
| 1264 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1265 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1266 | |
| 1267 | switch (n) { |
| 1268 | |
| 1269 | case 0: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1270 | errmsg = "unexpected code byte"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1271 | startinpos = s-starts; |
| 1272 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1273 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1274 | |
| 1275 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1276 | errmsg = "internal error"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1277 | startinpos = s-starts; |
| 1278 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1279 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1280 | |
| 1281 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1282 | if ((s[1] & 0xc0) != 0x80) { |
| 1283 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1284 | startinpos = s-starts; |
| 1285 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1286 | goto utf8Error; |
| 1287 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1288 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1289 | if (ch < 0x80) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1290 | startinpos = s-starts; |
| 1291 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1292 | errmsg = "illegal encoding"; |
| 1293 | goto utf8Error; |
| 1294 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1295 | else |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1296 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1297 | break; |
| 1298 | |
| 1299 | case 3: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1300 | if ((s[1] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1301 | (s[2] & 0xc0) != 0x80) { |
| 1302 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1303 | startinpos = s-starts; |
| 1304 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1305 | goto utf8Error; |
| 1306 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1307 | 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] | 1308 | if (ch < 0x0800) { |
| 1309 | /* Note: UTF-8 encodings of surrogates are considered |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1310 | legal UTF-8 sequences; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1311 | |
| 1312 | XXX For wide builds (UCS-4) we should probably try |
| 1313 | to recombine the surrogates into a single code |
| 1314 | unit. |
| 1315 | */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1316 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1317 | startinpos = s-starts; |
| 1318 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1319 | goto utf8Error; |
| 1320 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1321 | else |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1322 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1323 | break; |
| 1324 | |
| 1325 | case 4: |
| 1326 | if ((s[1] & 0xc0) != 0x80 || |
| 1327 | (s[2] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1328 | (s[3] & 0xc0) != 0x80) { |
| 1329 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1330 | startinpos = s-starts; |
| 1331 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1332 | goto utf8Error; |
| 1333 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1334 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 1335 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 1336 | /* validate and convert to UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1337 | if ((ch < 0x10000) /* minimum value allowed for 4 |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1338 | byte encoding */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1339 | || (ch > 0x10ffff)) /* maximum value allowed for |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1340 | UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1341 | { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1342 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1343 | startinpos = s-starts; |
| 1344 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1345 | goto utf8Error; |
| 1346 | } |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1347 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1348 | *p++ = (Py_UNICODE)ch; |
| 1349 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1350 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1351 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1352 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 1353 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1354 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1355 | /* high surrogate = top 10 bits added to D800 */ |
| 1356 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1357 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1358 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 1359 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1360 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1361 | break; |
| 1362 | |
| 1363 | default: |
| 1364 | /* Other sizes are only needed for UCS-4 */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1365 | errmsg = "unsupported Unicode code range"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1366 | startinpos = s-starts; |
| 1367 | endinpos = startinpos+n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1368 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1369 | } |
| 1370 | s += n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1371 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1372 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1373 | utf8Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1374 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1375 | if (unicode_decode_call_errorhandler( |
| 1376 | errors, &errorHandler, |
| 1377 | "utf8", errmsg, |
| 1378 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1379 | (PyObject **)&unicode, &outpos, &p)) |
| 1380 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1381 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1382 | if (consumed) |
| 1383 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1384 | |
| 1385 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1386 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1387 | goto onError; |
| 1388 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1389 | Py_XDECREF(errorHandler); |
| 1390 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1391 | return (PyObject *)unicode; |
| 1392 | |
| 1393 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1394 | Py_XDECREF(errorHandler); |
| 1395 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1396 | Py_DECREF(unicode); |
| 1397 | return NULL; |
| 1398 | } |
| 1399 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1400 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 1401 | and allocate exactly as much space needed at the end. Else allocate the |
| 1402 | maximum possible needed (4 result bytes per Unicode character), and return |
| 1403 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1404 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1405 | PyObject * |
| 1406 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1407 | Py_ssize_t size, |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1408 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1409 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1410 | #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] | 1411 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1412 | Py_ssize_t i; /* index into s of next input byte */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1413 | PyObject *v; /* result string object */ |
| 1414 | char *p; /* next free byte in output buffer */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1415 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 1416 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1417 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1418 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1419 | assert(s != NULL); |
| 1420 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1421 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1422 | if (size <= MAX_SHORT_UNICHARS) { |
| 1423 | /* Write into the stack buffer; nallocated can't overflow. |
| 1424 | * At the end, we'll allocate exactly as much heap space as it |
| 1425 | * turns out we need. |
| 1426 | */ |
| 1427 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
| 1428 | v = NULL; /* will allocate after we're done */ |
| 1429 | p = stackbuf; |
| 1430 | } |
| 1431 | else { |
| 1432 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 1433 | nallocated = size * 4; |
| 1434 | if (nallocated / 4 != size) /* overflow! */ |
| 1435 | return PyErr_NoMemory(); |
| 1436 | v = PyString_FromStringAndSize(NULL, nallocated); |
| 1437 | if (v == NULL) |
| 1438 | return NULL; |
| 1439 | p = PyString_AS_STRING(v); |
| 1440 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1441 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1442 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1443 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1444 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1445 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1446 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1447 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1448 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1449 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1450 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 1451 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 1452 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1453 | } |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 1454 | else { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1455 | /* Encode UCS2 Unicode ordinals */ |
| 1456 | if (ch < 0x10000) { |
| 1457 | /* Special case: check for high surrogate */ |
| 1458 | if (0xD800 <= ch && ch <= 0xDBFF && i != size) { |
| 1459 | Py_UCS4 ch2 = s[i]; |
| 1460 | /* Check for low surrogate and combine the two to |
| 1461 | form a UCS4 value */ |
| 1462 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1463 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1464 | i++; |
| 1465 | goto encodeUCS4; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1466 | } |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1467 | /* Fall through: handles isolated high surrogates */ |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1468 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1469 | *p++ = (char)(0xe0 | (ch >> 12)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1470 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 1471 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 1472 | continue; |
| 1473 | } |
| 1474 | encodeUCS4: |
| 1475 | /* Encode UCS4 Unicode ordinals */ |
| 1476 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 1477 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 1478 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 1479 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 1480 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1481 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 1482 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1483 | if (v == NULL) { |
| 1484 | /* This was stack allocated. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 1485 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1486 | assert(nneeded <= nallocated); |
| 1487 | v = PyString_FromStringAndSize(stackbuf, nneeded); |
| 1488 | } |
| 1489 | else { |
| 1490 | /* Cut back to size actually needed. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 1491 | nneeded = p - PyString_AS_STRING(v); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1492 | assert(nneeded <= nallocated); |
| 1493 | _PyString_Resize(&v, nneeded); |
| 1494 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1495 | return v; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1496 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1497 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1500 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 1501 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1502 | if (!PyUnicode_Check(unicode)) { |
| 1503 | PyErr_BadArgument(); |
| 1504 | return NULL; |
| 1505 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 1506 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 1507 | PyUnicode_GET_SIZE(unicode), |
| 1508 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 1512 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1513 | PyObject * |
| 1514 | PyUnicode_DecodeUTF16(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1515 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1516 | const char *errors, |
| 1517 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1518 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1519 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 1520 | } |
| 1521 | |
| 1522 | PyObject * |
| 1523 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1524 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1525 | const char *errors, |
| 1526 | int *byteorder, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1527 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1528 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1529 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1530 | Py_ssize_t startinpos; |
| 1531 | Py_ssize_t endinpos; |
| 1532 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1533 | PyUnicodeObject *unicode; |
| 1534 | Py_UNICODE *p; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1535 | const unsigned char *q, *e; |
| 1536 | int bo = 0; /* assume native ordering by default */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1537 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1538 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 1539 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 1540 | int ihi = 1, ilo = 0; |
| 1541 | #else |
| 1542 | int ihi = 0, ilo = 1; |
| 1543 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1544 | PyObject *errorHandler = NULL; |
| 1545 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1546 | |
| 1547 | /* Note: size will always be longer than the resulting Unicode |
| 1548 | character count */ |
| 1549 | unicode = _PyUnicode_New(size); |
| 1550 | if (!unicode) |
| 1551 | return NULL; |
| 1552 | if (size == 0) |
| 1553 | return (PyObject *)unicode; |
| 1554 | |
| 1555 | /* Unpack UTF-16 encoded data */ |
| 1556 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1557 | q = (unsigned char *)s; |
| 1558 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1559 | |
| 1560 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1561 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1562 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1563 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 1564 | byte order setting accordingly. In native mode, the leading BOM |
| 1565 | mark is skipped, in all other modes, it is copied to the output |
| 1566 | stream as-is (giving a ZWNBSP character). */ |
| 1567 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1568 | if (size >= 2) { |
| 1569 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1570 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1571 | if (bom == 0xFEFF) { |
| 1572 | q += 2; |
| 1573 | bo = -1; |
| 1574 | } |
| 1575 | else if (bom == 0xFFFE) { |
| 1576 | q += 2; |
| 1577 | bo = 1; |
| 1578 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1579 | #else |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1580 | if (bom == 0xFEFF) { |
| 1581 | q += 2; |
| 1582 | bo = 1; |
| 1583 | } |
| 1584 | else if (bom == 0xFFFE) { |
| 1585 | q += 2; |
| 1586 | bo = -1; |
| 1587 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1588 | #endif |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1589 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 1590 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1591 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1592 | if (bo == -1) { |
| 1593 | /* force LE */ |
| 1594 | ihi = 1; |
| 1595 | ilo = 0; |
| 1596 | } |
| 1597 | else if (bo == 1) { |
| 1598 | /* force BE */ |
| 1599 | ihi = 0; |
| 1600 | ilo = 1; |
| 1601 | } |
| 1602 | |
| 1603 | while (q < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1604 | Py_UNICODE ch; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1605 | /* remaining bytes at the end? (size should be even) */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1606 | if (e-q<2) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1607 | if (consumed) |
| 1608 | break; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1609 | errmsg = "truncated data"; |
| 1610 | startinpos = ((const char *)q)-starts; |
| 1611 | endinpos = ((const char *)e)-starts; |
| 1612 | goto utf16Error; |
| 1613 | /* The remaining input chars are ignored if the callback |
| 1614 | chooses to skip the input */ |
| 1615 | } |
| 1616 | ch = (q[ihi] << 8) | q[ilo]; |
| 1617 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1618 | q += 2; |
| 1619 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1620 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 1621 | *p++ = ch; |
| 1622 | continue; |
| 1623 | } |
| 1624 | |
| 1625 | /* UTF-16 code pair: */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1626 | if (q >= e) { |
| 1627 | errmsg = "unexpected end of data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1628 | startinpos = (((const char *)q)-2)-starts; |
| 1629 | endinpos = ((const char *)e)-starts; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1630 | goto utf16Error; |
| 1631 | } |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 1632 | if (0xD800 <= ch && ch <= 0xDBFF) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1633 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 1634 | q += 2; |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 1635 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1636 | #ifndef Py_UNICODE_WIDE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1637 | *p++ = ch; |
| 1638 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1639 | #else |
| 1640 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1641 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1642 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1643 | } |
| 1644 | else { |
| 1645 | errmsg = "illegal UTF-16 surrogate"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1646 | startinpos = (((const char *)q)-4)-starts; |
| 1647 | endinpos = startinpos+2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1648 | goto utf16Error; |
| 1649 | } |
| 1650 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1651 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1652 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1653 | startinpos = (((const char *)q)-2)-starts; |
| 1654 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1655 | /* Fall through to report the error */ |
| 1656 | |
| 1657 | utf16Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1658 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1659 | if (unicode_decode_call_errorhandler( |
| 1660 | errors, &errorHandler, |
| 1661 | "utf16", errmsg, |
| 1662 | starts, size, &startinpos, &endinpos, &exc, (const char **)&q, |
| 1663 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1664 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1665 | } |
| 1666 | |
| 1667 | if (byteorder) |
| 1668 | *byteorder = bo; |
| 1669 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1670 | if (consumed) |
| 1671 | *consumed = (const char *)q-starts; |
| 1672 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1673 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1674 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1675 | goto onError; |
| 1676 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1677 | Py_XDECREF(errorHandler); |
| 1678 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1679 | return (PyObject *)unicode; |
| 1680 | |
| 1681 | onError: |
| 1682 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1683 | Py_XDECREF(errorHandler); |
| 1684 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1685 | return NULL; |
| 1686 | } |
| 1687 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1688 | PyObject * |
| 1689 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1690 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1691 | const char *errors, |
| 1692 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1693 | { |
| 1694 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1695 | unsigned char *p; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1696 | #ifdef Py_UNICODE_WIDE |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1697 | int i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1698 | #else |
| 1699 | const int pairs = 0; |
| 1700 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1701 | /* Offsets from p for storing byte pairs in the right order. */ |
| 1702 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 1703 | int ihi = 1, ilo = 0; |
| 1704 | #else |
| 1705 | int ihi = 0, ilo = 1; |
| 1706 | #endif |
| 1707 | |
| 1708 | #define STORECHAR(CH) \ |
| 1709 | do { \ |
| 1710 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 1711 | p[ilo] = (CH) & 0xff; \ |
| 1712 | p += 2; \ |
| 1713 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1714 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1715 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1716 | for (i = pairs = 0; i < size; i++) |
| 1717 | if (s[i] >= 0x10000) |
| 1718 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1719 | #endif |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1720 | v = PyString_FromStringAndSize(NULL, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1721 | 2 * (size + pairs + (byteorder == 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1722 | if (v == NULL) |
| 1723 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1724 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1725 | p = (unsigned char *)PyString_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1726 | if (byteorder == 0) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1727 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 1728 | if (size == 0) |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 1729 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1730 | |
| 1731 | if (byteorder == -1) { |
| 1732 | /* force LE */ |
| 1733 | ihi = 1; |
| 1734 | ilo = 0; |
| 1735 | } |
| 1736 | else if (byteorder == 1) { |
| 1737 | /* force BE */ |
| 1738 | ihi = 0; |
| 1739 | ilo = 1; |
| 1740 | } |
| 1741 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1742 | while (size-- > 0) { |
| 1743 | Py_UNICODE ch = *s++; |
| 1744 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1745 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1746 | if (ch >= 0x10000) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1747 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 1748 | ch = 0xD800 | ((ch-0x10000) >> 10); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1749 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 1750 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1751 | STORECHAR(ch); |
| 1752 | if (ch2) |
| 1753 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1754 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1755 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 1756 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
| 1759 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 1760 | { |
| 1761 | if (!PyUnicode_Check(unicode)) { |
| 1762 | PyErr_BadArgument(); |
| 1763 | return NULL; |
| 1764 | } |
| 1765 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
| 1766 | PyUnicode_GET_SIZE(unicode), |
| 1767 | NULL, |
| 1768 | 0); |
| 1769 | } |
| 1770 | |
| 1771 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 1772 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 1773 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 1774 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1775 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1776 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1777 | const char *errors) |
| 1778 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1779 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1780 | Py_ssize_t startinpos; |
| 1781 | Py_ssize_t endinpos; |
| 1782 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1783 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1784 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1785 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1786 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1787 | char* message; |
| 1788 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1789 | PyObject *errorHandler = NULL; |
| 1790 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1791 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1792 | /* Escaped strings will always be longer than the resulting |
| 1793 | 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] | 1794 | length after conversion to the true value. |
| 1795 | (but if the error callback returns a long replacement string |
| 1796 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1797 | v = _PyUnicode_New(size); |
| 1798 | if (v == NULL) |
| 1799 | goto onError; |
| 1800 | if (size == 0) |
| 1801 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1802 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1803 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1804 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1805 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1806 | while (s < end) { |
| 1807 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 1808 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1809 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1810 | |
| 1811 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 1812 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1813 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1814 | continue; |
| 1815 | } |
| 1816 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1817 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1818 | /* \ - Escapes */ |
| 1819 | s++; |
Georg Brandl | 1dcb9c9 | 2007-11-02 22:46:38 +0000 | [diff] [blame] | 1820 | c = *s++; |
| 1821 | if (s > end) |
| 1822 | c = '\0'; /* Invalid after \ */ |
| 1823 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1824 | |
| 1825 | /* \x escapes */ |
| 1826 | case '\n': break; |
| 1827 | case '\\': *p++ = '\\'; break; |
| 1828 | case '\'': *p++ = '\''; break; |
| 1829 | case '\"': *p++ = '\"'; break; |
| 1830 | case 'b': *p++ = '\b'; break; |
| 1831 | case 'f': *p++ = '\014'; break; /* FF */ |
| 1832 | case 't': *p++ = '\t'; break; |
| 1833 | case 'n': *p++ = '\n'; break; |
| 1834 | case 'r': *p++ = '\r'; break; |
| 1835 | case 'v': *p++ = '\013'; break; /* VT */ |
| 1836 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 1837 | |
| 1838 | /* \OOO (octal) escapes */ |
| 1839 | case '0': case '1': case '2': case '3': |
| 1840 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1841 | x = s[-1] - '0'; |
Georg Brandl | 1dcb9c9 | 2007-11-02 22:46:38 +0000 | [diff] [blame] | 1842 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1843 | x = (x<<3) + *s++ - '0'; |
Georg Brandl | 1dcb9c9 | 2007-11-02 22:46:38 +0000 | [diff] [blame] | 1844 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1845 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1846 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 1847 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1848 | break; |
| 1849 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1850 | /* hex escapes */ |
| 1851 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1852 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1853 | digits = 2; |
| 1854 | message = "truncated \\xXX escape"; |
| 1855 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1856 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1857 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1858 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1859 | digits = 4; |
| 1860 | message = "truncated \\uXXXX escape"; |
| 1861 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1862 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1863 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1864 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1865 | digits = 8; |
| 1866 | message = "truncated \\UXXXXXXXX escape"; |
| 1867 | hexescape: |
| 1868 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1869 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1870 | if (s+digits>end) { |
| 1871 | endinpos = size; |
| 1872 | if (unicode_decode_call_errorhandler( |
| 1873 | errors, &errorHandler, |
| 1874 | "unicodeescape", "end of string in escape sequence", |
| 1875 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1876 | (PyObject **)&v, &outpos, &p)) |
| 1877 | goto onError; |
| 1878 | goto nextByte; |
| 1879 | } |
| 1880 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1881 | c = (unsigned char) s[i]; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1882 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1883 | endinpos = (s+i+1)-starts; |
| 1884 | if (unicode_decode_call_errorhandler( |
| 1885 | errors, &errorHandler, |
| 1886 | "unicodeescape", message, |
| 1887 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1888 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1889 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1890 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1891 | } |
| 1892 | chr = (chr<<4) & ~0xF; |
| 1893 | if (c >= '0' && c <= '9') |
| 1894 | chr += c - '0'; |
| 1895 | else if (c >= 'a' && c <= 'f') |
| 1896 | chr += 10 + c - 'a'; |
| 1897 | else |
| 1898 | chr += 10 + c - 'A'; |
| 1899 | } |
| 1900 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 1901 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1902 | /* _decoding_error will have already written into the |
| 1903 | target buffer. */ |
| 1904 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1905 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1906 | /* when we get here, chr is a 32-bit unicode character */ |
| 1907 | if (chr <= 0xffff) |
| 1908 | /* UCS-2 character */ |
| 1909 | *p++ = (Py_UNICODE) chr; |
| 1910 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 1911 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1912 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1913 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1914 | *p++ = chr; |
| 1915 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1916 | chr -= 0x10000L; |
| 1917 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 1918 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1919 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1920 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1921 | endinpos = s-starts; |
| 1922 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1923 | if (unicode_decode_call_errorhandler( |
| 1924 | errors, &errorHandler, |
| 1925 | "unicodeescape", "illegal Unicode character", |
| 1926 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1927 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 1928 | goto onError; |
| 1929 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1930 | break; |
| 1931 | |
| 1932 | /* \N{name} */ |
| 1933 | case 'N': |
| 1934 | message = "malformed \\N character escape"; |
| 1935 | if (ucnhash_CAPI == NULL) { |
| 1936 | /* load the unicode data module */ |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 1937 | PyObject *m, *api; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1938 | m = PyImport_ImportModule("unicodedata"); |
| 1939 | if (m == NULL) |
| 1940 | goto ucnhashError; |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 1941 | api = PyObject_GetAttrString(m, "ucnhash_CAPI"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1942 | Py_DECREF(m); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 1943 | if (api == NULL) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1944 | goto ucnhashError; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 1945 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCObject_AsVoidPtr(api); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 1946 | Py_DECREF(api); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1947 | if (ucnhash_CAPI == NULL) |
| 1948 | goto ucnhashError; |
| 1949 | } |
| 1950 | if (*s == '{') { |
| 1951 | const char *start = s+1; |
| 1952 | /* look for the closing brace */ |
| 1953 | while (*s != '}' && s < end) |
| 1954 | s++; |
| 1955 | if (s > start && s < end && *s == '}') { |
| 1956 | /* found a name. look it up in the unicode database */ |
| 1957 | message = "unknown Unicode character name"; |
| 1958 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 1959 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1960 | goto store; |
| 1961 | } |
| 1962 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1963 | endinpos = s-starts; |
| 1964 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1965 | if (unicode_decode_call_errorhandler( |
| 1966 | errors, &errorHandler, |
| 1967 | "unicodeescape", message, |
| 1968 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1969 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1970 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1971 | break; |
| 1972 | |
| 1973 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1974 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1975 | message = "\\ at end of string"; |
| 1976 | s--; |
| 1977 | endinpos = s-starts; |
| 1978 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 1979 | if (unicode_decode_call_errorhandler( |
| 1980 | errors, &errorHandler, |
| 1981 | "unicodeescape", message, |
| 1982 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1983 | (PyObject **)&v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 1984 | goto onError; |
| 1985 | } |
| 1986 | else { |
| 1987 | *p++ = '\\'; |
| 1988 | *p++ = (unsigned char)s[-1]; |
| 1989 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 1990 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1991 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1992 | nextByte: |
| 1993 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1994 | } |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 1995 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1996 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 1997 | Py_XDECREF(errorHandler); |
| 1998 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1999 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2000 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2001 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2002 | PyErr_SetString( |
| 2003 | PyExc_UnicodeError, |
| 2004 | "\\N escapes not supported (can't load unicodedata module)" |
| 2005 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2006 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2007 | Py_XDECREF(errorHandler); |
| 2008 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 2009 | return NULL; |
| 2010 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2011 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2012 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2013 | Py_XDECREF(errorHandler); |
| 2014 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2015 | return NULL; |
| 2016 | } |
| 2017 | |
| 2018 | /* Return a Unicode-Escape string version of the Unicode object. |
| 2019 | |
| 2020 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 2021 | appropriate. |
| 2022 | |
| 2023 | */ |
| 2024 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 2025 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
Fredrik Lundh | 95e2a91 | 2006-05-26 11:38:15 +0000 | [diff] [blame] | 2026 | Py_ssize_t size, |
| 2027 | Py_UNICODE ch) |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 2028 | { |
| 2029 | /* like wcschr, but doesn't stop at NULL characters */ |
| 2030 | |
| 2031 | while (size-- > 0) { |
| 2032 | if (*s == ch) |
| 2033 | return s; |
| 2034 | s++; |
| 2035 | } |
| 2036 | |
| 2037 | return NULL; |
| 2038 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 2039 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2040 | static |
| 2041 | PyObject *unicodeescape_string(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2042 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2043 | int quotes) |
| 2044 | { |
| 2045 | PyObject *repr; |
| 2046 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2047 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2048 | static const char *hexdigit = "0123456789abcdef"; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2049 | |
Neal Norwitz | 19c35bb | 2006-08-21 22:13:11 +0000 | [diff] [blame] | 2050 | /* Initial allocation is based on the longest-possible unichr |
| 2051 | escape. |
| 2052 | |
| 2053 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 2054 | unichr, so in this case it's the longest unichr escape. In |
| 2055 | narrow (UTF-16) builds this is five chars per source unichr |
| 2056 | since there are two unichrs in the surrogate pair, so in narrow |
| 2057 | (UTF-16) builds it's not the longest unichr escape. |
| 2058 | |
| 2059 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 2060 | so in the narrow (UTF-16) build case it's the longest unichr |
| 2061 | escape. |
| 2062 | */ |
| 2063 | |
| 2064 | repr = PyString_FromStringAndSize(NULL, |
| 2065 | 2 |
| 2066 | #ifdef Py_UNICODE_WIDE |
| 2067 | + 10*size |
| 2068 | #else |
| 2069 | + 6*size |
| 2070 | #endif |
| 2071 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2072 | if (repr == NULL) |
| 2073 | return NULL; |
| 2074 | |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2075 | p = PyString_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2076 | |
| 2077 | if (quotes) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2078 | *p++ = 'u'; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2079 | *p++ = (findchar(s, size, '\'') && |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2080 | !findchar(s, size, '"')) ? '"' : '\''; |
| 2081 | } |
| 2082 | while (size-- > 0) { |
| 2083 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2084 | |
Hye-Shik Chang | 835b243 | 2005-12-17 04:38:31 +0000 | [diff] [blame] | 2085 | /* Escape quotes and backslashes */ |
| 2086 | if ((quotes && |
| 2087 | ch == (Py_UNICODE) PyString_AS_STRING(repr)[1]) || ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2088 | *p++ = '\\'; |
| 2089 | *p++ = (char) ch; |
Guido van Rossum | ad9744a | 2001-09-21 15:38:17 +0000 | [diff] [blame] | 2090 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2091 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2092 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 2093 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2094 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 2095 | else if (ch >= 0x10000) { |
| 2096 | *p++ = '\\'; |
| 2097 | *p++ = 'U'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2098 | *p++ = hexdigit[(ch >> 28) & 0x0000000F]; |
| 2099 | *p++ = hexdigit[(ch >> 24) & 0x0000000F]; |
| 2100 | *p++ = hexdigit[(ch >> 20) & 0x0000000F]; |
| 2101 | *p++ = hexdigit[(ch >> 16) & 0x0000000F]; |
| 2102 | *p++ = hexdigit[(ch >> 12) & 0x0000000F]; |
| 2103 | *p++ = hexdigit[(ch >> 8) & 0x0000000F]; |
| 2104 | *p++ = hexdigit[(ch >> 4) & 0x0000000F]; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2105 | *p++ = hexdigit[ch & 0x0000000F]; |
| 2106 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2107 | } |
Neal Norwitz | 19c35bb | 2006-08-21 22:13:11 +0000 | [diff] [blame] | 2108 | #else |
| 2109 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2110 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 2111 | Py_UNICODE ch2; |
| 2112 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2113 | |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2114 | ch2 = *s++; |
| 2115 | size--; |
| 2116 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 2117 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 2118 | *p++ = '\\'; |
| 2119 | *p++ = 'U'; |
| 2120 | *p++ = hexdigit[(ucs >> 28) & 0x0000000F]; |
| 2121 | *p++ = hexdigit[(ucs >> 24) & 0x0000000F]; |
| 2122 | *p++ = hexdigit[(ucs >> 20) & 0x0000000F]; |
| 2123 | *p++ = hexdigit[(ucs >> 16) & 0x0000000F]; |
| 2124 | *p++ = hexdigit[(ucs >> 12) & 0x0000000F]; |
| 2125 | *p++ = hexdigit[(ucs >> 8) & 0x0000000F]; |
| 2126 | *p++ = hexdigit[(ucs >> 4) & 0x0000000F]; |
| 2127 | *p++ = hexdigit[ucs & 0x0000000F]; |
| 2128 | continue; |
| 2129 | } |
| 2130 | /* Fall through: isolated surrogates are copied as-is */ |
| 2131 | s--; |
| 2132 | size++; |
| 2133 | } |
Neal Norwitz | 19c35bb | 2006-08-21 22:13:11 +0000 | [diff] [blame] | 2134 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2135 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2136 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2137 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2138 | *p++ = '\\'; |
| 2139 | *p++ = 'u'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2140 | *p++ = hexdigit[(ch >> 12) & 0x000F]; |
| 2141 | *p++ = hexdigit[(ch >> 8) & 0x000F]; |
| 2142 | *p++ = hexdigit[(ch >> 4) & 0x000F]; |
| 2143 | *p++ = hexdigit[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2144 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2145 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2146 | /* Map special whitespace to '\t', \n', '\r' */ |
| 2147 | else if (ch == '\t') { |
| 2148 | *p++ = '\\'; |
| 2149 | *p++ = 't'; |
| 2150 | } |
| 2151 | else if (ch == '\n') { |
| 2152 | *p++ = '\\'; |
| 2153 | *p++ = 'n'; |
| 2154 | } |
| 2155 | else if (ch == '\r') { |
| 2156 | *p++ = '\\'; |
| 2157 | *p++ = 'r'; |
| 2158 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2159 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2160 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 2161 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2162 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2163 | *p++ = 'x'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2164 | *p++ = hexdigit[(ch >> 4) & 0x000F]; |
| 2165 | *p++ = hexdigit[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2166 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2167 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2168 | /* Copy everything else as-is */ |
| 2169 | else |
| 2170 | *p++ = (char) ch; |
| 2171 | } |
| 2172 | if (quotes) |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2173 | *p++ = PyString_AS_STRING(repr)[1]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2174 | |
| 2175 | *p = '\0'; |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 2176 | _PyString_Resize(&repr, p - PyString_AS_STRING(repr)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2177 | return repr; |
| 2178 | } |
| 2179 | |
| 2180 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2181 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2182 | { |
| 2183 | return unicodeescape_string(s, size, 0); |
| 2184 | } |
| 2185 | |
| 2186 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
| 2187 | { |
| 2188 | if (!PyUnicode_Check(unicode)) { |
| 2189 | PyErr_BadArgument(); |
| 2190 | return NULL; |
| 2191 | } |
| 2192 | return PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 2193 | PyUnicode_GET_SIZE(unicode)); |
| 2194 | } |
| 2195 | |
| 2196 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 2197 | |
| 2198 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2199 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2200 | const char *errors) |
| 2201 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2202 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2203 | Py_ssize_t startinpos; |
| 2204 | Py_ssize_t endinpos; |
| 2205 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2206 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2207 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2208 | const char *end; |
| 2209 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2210 | PyObject *errorHandler = NULL; |
| 2211 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2212 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2213 | /* Escaped strings will always be longer than the resulting |
| 2214 | 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] | 2215 | length after conversion to the true value. (But decoding error |
| 2216 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2217 | v = _PyUnicode_New(size); |
| 2218 | if (v == NULL) |
| 2219 | goto onError; |
| 2220 | if (size == 0) |
| 2221 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2222 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2223 | end = s + size; |
| 2224 | while (s < end) { |
| 2225 | unsigned char c; |
Martin v. Löwis | 047c05e | 2002-03-21 08:55:28 +0000 | [diff] [blame] | 2226 | Py_UCS4 x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2227 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2228 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2229 | |
| 2230 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 2231 | if (*s != '\\') { |
| 2232 | *p++ = (unsigned char)*s++; |
| 2233 | continue; |
| 2234 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2235 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2236 | |
| 2237 | /* \u-escapes are only interpreted iff the number of leading |
| 2238 | backslashes if odd */ |
| 2239 | bs = s; |
| 2240 | for (;s < end;) { |
| 2241 | if (*s != '\\') |
| 2242 | break; |
| 2243 | *p++ = (unsigned char)*s++; |
| 2244 | } |
| 2245 | if (((s - bs) & 1) == 0 || |
| 2246 | s >= end || |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2247 | (*s != 'u' && *s != 'U')) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2248 | continue; |
| 2249 | } |
| 2250 | p--; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2251 | count = *s=='u' ? 4 : 8; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2252 | s++; |
| 2253 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2254 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2255 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2256 | for (x = 0, i = 0; i < count; ++i, ++s) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2257 | c = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2258 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2259 | endinpos = s-starts; |
| 2260 | if (unicode_decode_call_errorhandler( |
| 2261 | errors, &errorHandler, |
| 2262 | "rawunicodeescape", "truncated \\uXXXX", |
| 2263 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2264 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2265 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2266 | goto nextByte; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2267 | } |
| 2268 | x = (x<<4) & ~0xF; |
| 2269 | if (c >= '0' && c <= '9') |
| 2270 | x += c - '0'; |
| 2271 | else if (c >= 'a' && c <= 'f') |
| 2272 | x += 10 + c - 'a'; |
| 2273 | else |
| 2274 | x += 10 + c - 'A'; |
| 2275 | } |
Amaury Forgeot d'Arc | a79e050 | 2008-03-24 21:16:28 +0000 | [diff] [blame] | 2276 | if (x <= 0xffff) |
| 2277 | /* UCS-2 character */ |
| 2278 | *p++ = (Py_UNICODE) x; |
| 2279 | else if (x <= 0x10ffff) { |
| 2280 | /* UCS-4 character. Either store directly, or as |
| 2281 | surrogate pair. */ |
| 2282 | #ifdef Py_UNICODE_WIDE |
| 2283 | *p++ = (Py_UNICODE) x; |
| 2284 | #else |
| 2285 | x -= 0x10000L; |
| 2286 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 2287 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
| 2288 | #endif |
| 2289 | } else { |
| 2290 | endinpos = s-starts; |
| 2291 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2292 | if (unicode_decode_call_errorhandler( |
| 2293 | errors, &errorHandler, |
| 2294 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
| 2295 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2296 | (PyObject **)&v, &outpos, &p)) |
| 2297 | goto onError; |
| 2298 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2299 | nextByte: |
| 2300 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2301 | } |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2302 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 2303 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2304 | Py_XDECREF(errorHandler); |
| 2305 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2306 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2307 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2308 | onError: |
| 2309 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2310 | Py_XDECREF(errorHandler); |
| 2311 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2312 | return NULL; |
| 2313 | } |
| 2314 | |
| 2315 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2316 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2317 | { |
| 2318 | PyObject *repr; |
| 2319 | char *p; |
| 2320 | char *q; |
| 2321 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2322 | static const char *hexdigit = "0123456789abcdef"; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2323 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2324 | #ifdef Py_UNICODE_WIDE |
| 2325 | repr = PyString_FromStringAndSize(NULL, 10 * size); |
| 2326 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2327 | repr = PyString_FromStringAndSize(NULL, 6 * size); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2328 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2329 | if (repr == NULL) |
| 2330 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 2331 | if (size == 0) |
| 2332 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2333 | |
| 2334 | p = q = PyString_AS_STRING(repr); |
| 2335 | while (size-- > 0) { |
| 2336 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2337 | #ifdef Py_UNICODE_WIDE |
| 2338 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 2339 | if (ch >= 0x10000) { |
| 2340 | *p++ = '\\'; |
| 2341 | *p++ = 'U'; |
| 2342 | *p++ = hexdigit[(ch >> 28) & 0xf]; |
| 2343 | *p++ = hexdigit[(ch >> 24) & 0xf]; |
| 2344 | *p++ = hexdigit[(ch >> 20) & 0xf]; |
| 2345 | *p++ = hexdigit[(ch >> 16) & 0xf]; |
| 2346 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 2347 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 2348 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 2349 | *p++ = hexdigit[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2350 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2351 | else |
Amaury Forgeot d'Arc | a79e050 | 2008-03-24 21:16:28 +0000 | [diff] [blame] | 2352 | #else |
| 2353 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 2354 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 2355 | Py_UNICODE ch2; |
| 2356 | Py_UCS4 ucs; |
| 2357 | |
| 2358 | ch2 = *s++; |
| 2359 | size--; |
| 2360 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 2361 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 2362 | *p++ = '\\'; |
| 2363 | *p++ = 'U'; |
| 2364 | *p++ = hexdigit[(ucs >> 28) & 0xf]; |
| 2365 | *p++ = hexdigit[(ucs >> 24) & 0xf]; |
| 2366 | *p++ = hexdigit[(ucs >> 20) & 0xf]; |
| 2367 | *p++ = hexdigit[(ucs >> 16) & 0xf]; |
| 2368 | *p++ = hexdigit[(ucs >> 12) & 0xf]; |
| 2369 | *p++ = hexdigit[(ucs >> 8) & 0xf]; |
| 2370 | *p++ = hexdigit[(ucs >> 4) & 0xf]; |
| 2371 | *p++ = hexdigit[ucs & 0xf]; |
| 2372 | continue; |
| 2373 | } |
| 2374 | /* Fall through: isolated surrogates are copied as-is */ |
| 2375 | s--; |
| 2376 | size++; |
| 2377 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 2378 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2379 | /* Map 16-bit characters to '\uxxxx' */ |
| 2380 | if (ch >= 256) { |
| 2381 | *p++ = '\\'; |
| 2382 | *p++ = 'u'; |
| 2383 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 2384 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 2385 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 2386 | *p++ = hexdigit[ch & 15]; |
| 2387 | } |
| 2388 | /* Copy everything else as-is */ |
| 2389 | else |
| 2390 | *p++ = (char) ch; |
| 2391 | } |
| 2392 | *p = '\0'; |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 2393 | _PyString_Resize(&repr, p - q); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2394 | return repr; |
| 2395 | } |
| 2396 | |
| 2397 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 2398 | { |
| 2399 | if (!PyUnicode_Check(unicode)) { |
| 2400 | PyErr_BadArgument(); |
| 2401 | return NULL; |
| 2402 | } |
| 2403 | return PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 2404 | PyUnicode_GET_SIZE(unicode)); |
| 2405 | } |
| 2406 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2407 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 2408 | |
| 2409 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2410 | Py_ssize_t size, |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2411 | const char *errors) |
| 2412 | { |
| 2413 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2414 | Py_ssize_t startinpos; |
| 2415 | Py_ssize_t endinpos; |
| 2416 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2417 | PyUnicodeObject *v; |
| 2418 | Py_UNICODE *p; |
| 2419 | const char *end; |
| 2420 | const char *reason; |
| 2421 | PyObject *errorHandler = NULL; |
| 2422 | PyObject *exc = NULL; |
| 2423 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 2424 | #ifdef Py_UNICODE_WIDE |
| 2425 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 2426 | #endif |
| 2427 | |
Armin Rigo | 4b63c21 | 2006-10-04 11:44:06 +0000 | [diff] [blame] | 2428 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2429 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 2430 | if (v == NULL) |
| 2431 | goto onError; |
| 2432 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
| 2433 | return (PyObject *)v; |
| 2434 | p = PyUnicode_AS_UNICODE(v); |
| 2435 | end = s + size; |
| 2436 | |
| 2437 | while (s < end) { |
Neal Norwitz | 1004a53 | 2006-05-15 07:17:23 +0000 | [diff] [blame] | 2438 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2439 | /* We have to sanity check the raw data, otherwise doom looms for |
| 2440 | some malformed UCS-4 data. */ |
| 2441 | if ( |
| 2442 | #ifdef Py_UNICODE_WIDE |
| 2443 | *p > unimax || *p < 0 || |
| 2444 | #endif |
| 2445 | end-s < Py_UNICODE_SIZE |
| 2446 | ) |
| 2447 | { |
| 2448 | startinpos = s - starts; |
| 2449 | if (end-s < Py_UNICODE_SIZE) { |
| 2450 | endinpos = end-starts; |
| 2451 | reason = "truncated input"; |
| 2452 | } |
| 2453 | else { |
| 2454 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 2455 | reason = "illegal code point (> 0x10FFFF)"; |
| 2456 | } |
| 2457 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 2458 | if (unicode_decode_call_errorhandler( |
| 2459 | errors, &errorHandler, |
| 2460 | "unicode_internal", reason, |
| 2461 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2462 | (PyObject **)&v, &outpos, &p)) { |
| 2463 | goto onError; |
| 2464 | } |
| 2465 | } |
| 2466 | else { |
| 2467 | p++; |
| 2468 | s += Py_UNICODE_SIZE; |
| 2469 | } |
| 2470 | } |
| 2471 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2472 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 2473 | goto onError; |
| 2474 | Py_XDECREF(errorHandler); |
| 2475 | Py_XDECREF(exc); |
| 2476 | return (PyObject *)v; |
| 2477 | |
| 2478 | onError: |
| 2479 | Py_XDECREF(v); |
| 2480 | Py_XDECREF(errorHandler); |
| 2481 | Py_XDECREF(exc); |
| 2482 | return NULL; |
| 2483 | } |
| 2484 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2485 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 2486 | |
| 2487 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2488 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2489 | const char *errors) |
| 2490 | { |
| 2491 | PyUnicodeObject *v; |
| 2492 | Py_UNICODE *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2493 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2494 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2495 | if (size == 1) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 2496 | Py_UNICODE r = *(unsigned char*)s; |
| 2497 | return PyUnicode_FromUnicode(&r, 1); |
| 2498 | } |
| 2499 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2500 | v = _PyUnicode_New(size); |
| 2501 | if (v == NULL) |
| 2502 | goto onError; |
| 2503 | if (size == 0) |
| 2504 | return (PyObject *)v; |
| 2505 | p = PyUnicode_AS_UNICODE(v); |
| 2506 | while (size-- > 0) |
| 2507 | *p++ = (unsigned char)*s++; |
| 2508 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2509 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2510 | onError: |
| 2511 | Py_XDECREF(v); |
| 2512 | return NULL; |
| 2513 | } |
| 2514 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2515 | /* create or adjust a UnicodeEncodeError */ |
| 2516 | static void make_encode_exception(PyObject **exceptionObject, |
| 2517 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2518 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 2519 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2520 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2521 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2522 | if (*exceptionObject == NULL) { |
| 2523 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 2524 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2525 | } |
| 2526 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2527 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 2528 | goto onError; |
| 2529 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 2530 | goto onError; |
| 2531 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 2532 | goto onError; |
| 2533 | return; |
| 2534 | onError: |
| 2535 | Py_DECREF(*exceptionObject); |
| 2536 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2537 | } |
| 2538 | } |
| 2539 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2540 | /* raises a UnicodeEncodeError */ |
| 2541 | static void raise_encode_exception(PyObject **exceptionObject, |
| 2542 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2543 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 2544 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2545 | const char *reason) |
| 2546 | { |
| 2547 | make_encode_exception(exceptionObject, |
| 2548 | encoding, unicode, size, startpos, endpos, reason); |
| 2549 | if (*exceptionObject != NULL) |
| 2550 | PyCodec_StrictErrors(*exceptionObject); |
| 2551 | } |
| 2552 | |
| 2553 | /* error handling callback helper: |
| 2554 | build arguments, call the callback and check the arguments, |
| 2555 | put the result into newpos and return the replacement string, which |
| 2556 | has to be freed by the caller */ |
| 2557 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
| 2558 | PyObject **errorHandler, |
| 2559 | const char *encoding, const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2560 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 2561 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 2562 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2563 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2564 | 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] | 2565 | |
| 2566 | PyObject *restuple; |
| 2567 | PyObject *resunicode; |
| 2568 | |
| 2569 | if (*errorHandler == NULL) { |
| 2570 | *errorHandler = PyCodec_LookupError(errors); |
| 2571 | if (*errorHandler == NULL) |
| 2572 | return NULL; |
| 2573 | } |
| 2574 | |
| 2575 | make_encode_exception(exceptionObject, |
| 2576 | encoding, unicode, size, startpos, endpos, reason); |
| 2577 | if (*exceptionObject == NULL) |
| 2578 | return NULL; |
| 2579 | |
| 2580 | restuple = PyObject_CallFunctionObjArgs( |
| 2581 | *errorHandler, *exceptionObject, NULL); |
| 2582 | if (restuple == NULL) |
| 2583 | return NULL; |
| 2584 | if (!PyTuple_Check(restuple)) { |
| 2585 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 2586 | Py_DECREF(restuple); |
| 2587 | return NULL; |
| 2588 | } |
| 2589 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
| 2590 | &resunicode, newpos)) { |
| 2591 | Py_DECREF(restuple); |
| 2592 | return NULL; |
| 2593 | } |
| 2594 | if (*newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 2595 | *newpos = size+*newpos; |
| 2596 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 2597 | 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] | 2598 | Py_DECREF(restuple); |
| 2599 | return NULL; |
| 2600 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2601 | Py_INCREF(resunicode); |
| 2602 | Py_DECREF(restuple); |
| 2603 | return resunicode; |
| 2604 | } |
| 2605 | |
| 2606 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2607 | Py_ssize_t size, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2608 | const char *errors, |
| 2609 | int limit) |
| 2610 | { |
| 2611 | /* output object */ |
| 2612 | PyObject *res; |
| 2613 | /* pointers to the beginning and end+1 of input */ |
| 2614 | const Py_UNICODE *startp = p; |
| 2615 | const Py_UNICODE *endp = p + size; |
| 2616 | /* pointer to the beginning of the unencodable characters */ |
| 2617 | /* const Py_UNICODE *badp = NULL; */ |
| 2618 | /* pointer into the output */ |
| 2619 | char *str; |
| 2620 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2621 | Py_ssize_t respos = 0; |
| 2622 | Py_ssize_t ressize; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2623 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 2624 | 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] | 2625 | PyObject *errorHandler = NULL; |
| 2626 | PyObject *exc = NULL; |
| 2627 | /* the following variable is used for caching string comparisons |
| 2628 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 2629 | int known_errorHandler = -1; |
| 2630 | |
| 2631 | /* allocate enough for a simple encoding without |
| 2632 | replacements, if we need more, we'll resize */ |
| 2633 | res = PyString_FromStringAndSize(NULL, size); |
| 2634 | if (res == NULL) |
| 2635 | goto onError; |
| 2636 | if (size == 0) |
| 2637 | return res; |
| 2638 | str = PyString_AS_STRING(res); |
| 2639 | ressize = size; |
| 2640 | |
| 2641 | while (p<endp) { |
| 2642 | Py_UNICODE c = *p; |
| 2643 | |
| 2644 | /* can we encode this? */ |
| 2645 | if (c<limit) { |
| 2646 | /* no overflow check, because we know that the space is enough */ |
| 2647 | *str++ = (char)c; |
| 2648 | ++p; |
| 2649 | } |
| 2650 | else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2651 | Py_ssize_t unicodepos = p-startp; |
| 2652 | Py_ssize_t requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2653 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2654 | Py_ssize_t repsize; |
| 2655 | Py_ssize_t newpos; |
| 2656 | Py_ssize_t respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2657 | Py_UNICODE *uni2; |
| 2658 | /* startpos for collecting unencodable chars */ |
| 2659 | const Py_UNICODE *collstart = p; |
| 2660 | const Py_UNICODE *collend = p; |
| 2661 | /* find all unecodable characters */ |
| 2662 | while ((collend < endp) && ((*collend)>=limit)) |
| 2663 | ++collend; |
| 2664 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 2665 | if (known_errorHandler==-1) { |
| 2666 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 2667 | known_errorHandler = 1; |
| 2668 | else if (!strcmp(errors, "replace")) |
| 2669 | known_errorHandler = 2; |
| 2670 | else if (!strcmp(errors, "ignore")) |
| 2671 | known_errorHandler = 3; |
| 2672 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 2673 | known_errorHandler = 4; |
| 2674 | else |
| 2675 | known_errorHandler = 0; |
| 2676 | } |
| 2677 | switch (known_errorHandler) { |
| 2678 | case 1: /* strict */ |
| 2679 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 2680 | goto onError; |
| 2681 | case 2: /* replace */ |
| 2682 | while (collstart++<collend) |
| 2683 | *str++ = '?'; /* fall through */ |
| 2684 | case 3: /* ignore */ |
| 2685 | p = collend; |
| 2686 | break; |
| 2687 | case 4: /* xmlcharrefreplace */ |
| 2688 | respos = str-PyString_AS_STRING(res); |
| 2689 | /* determine replacement size (temporarily (mis)uses p) */ |
| 2690 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 2691 | if (*p<10) |
| 2692 | repsize += 2+1+1; |
| 2693 | else if (*p<100) |
| 2694 | repsize += 2+2+1; |
| 2695 | else if (*p<1000) |
| 2696 | repsize += 2+3+1; |
| 2697 | else if (*p<10000) |
| 2698 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 2699 | #ifndef Py_UNICODE_WIDE |
| 2700 | else |
| 2701 | repsize += 2+5+1; |
| 2702 | #else |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2703 | else if (*p<100000) |
| 2704 | repsize += 2+5+1; |
| 2705 | else if (*p<1000000) |
| 2706 | repsize += 2+6+1; |
| 2707 | else |
| 2708 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2709 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2710 | } |
| 2711 | requiredsize = respos+repsize+(endp-collend); |
| 2712 | if (requiredsize > ressize) { |
| 2713 | if (requiredsize<2*ressize) |
| 2714 | requiredsize = 2*ressize; |
| 2715 | if (_PyString_Resize(&res, requiredsize)) |
| 2716 | goto onError; |
| 2717 | str = PyString_AS_STRING(res) + respos; |
| 2718 | ressize = requiredsize; |
| 2719 | } |
| 2720 | /* generate replacement (temporarily (mis)uses p) */ |
| 2721 | for (p = collstart; p < collend; ++p) { |
| 2722 | str += sprintf(str, "&#%d;", (int)*p); |
| 2723 | } |
| 2724 | p = collend; |
| 2725 | break; |
| 2726 | default: |
| 2727 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 2728 | encoding, reason, startp, size, &exc, |
| 2729 | collstart-startp, collend-startp, &newpos); |
| 2730 | if (repunicode == NULL) |
| 2731 | goto onError; |
| 2732 | /* need more space? (at least enough for what we |
| 2733 | have+the replacement+the rest of the string, so |
| 2734 | we won't have to check space for encodable characters) */ |
| 2735 | respos = str-PyString_AS_STRING(res); |
| 2736 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 2737 | requiredsize = respos+repsize+(endp-collend); |
| 2738 | if (requiredsize > ressize) { |
| 2739 | if (requiredsize<2*ressize) |
| 2740 | requiredsize = 2*ressize; |
| 2741 | if (_PyString_Resize(&res, requiredsize)) { |
| 2742 | Py_DECREF(repunicode); |
| 2743 | goto onError; |
| 2744 | } |
| 2745 | str = PyString_AS_STRING(res) + respos; |
| 2746 | ressize = requiredsize; |
| 2747 | } |
| 2748 | /* check if there is anything unencodable in the replacement |
| 2749 | and copy it to the output */ |
| 2750 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 2751 | c = *uni2; |
| 2752 | if (c >= limit) { |
| 2753 | raise_encode_exception(&exc, encoding, startp, size, |
| 2754 | unicodepos, unicodepos+1, reason); |
| 2755 | Py_DECREF(repunicode); |
| 2756 | goto onError; |
| 2757 | } |
| 2758 | *str = (char)c; |
| 2759 | } |
| 2760 | p = startp + newpos; |
| 2761 | Py_DECREF(repunicode); |
| 2762 | } |
| 2763 | } |
| 2764 | } |
| 2765 | /* Resize if we allocated to much */ |
| 2766 | respos = str-PyString_AS_STRING(res); |
| 2767 | if (respos<ressize) |
| 2768 | /* If this falls res will be NULL */ |
| 2769 | _PyString_Resize(&res, respos); |
| 2770 | Py_XDECREF(errorHandler); |
| 2771 | Py_XDECREF(exc); |
| 2772 | return res; |
| 2773 | |
| 2774 | onError: |
| 2775 | Py_XDECREF(res); |
| 2776 | Py_XDECREF(errorHandler); |
| 2777 | Py_XDECREF(exc); |
| 2778 | return NULL; |
| 2779 | } |
| 2780 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2781 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2782 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2783 | const char *errors) |
| 2784 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2785 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2786 | } |
| 2787 | |
| 2788 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 2789 | { |
| 2790 | if (!PyUnicode_Check(unicode)) { |
| 2791 | PyErr_BadArgument(); |
| 2792 | return NULL; |
| 2793 | } |
| 2794 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 2795 | PyUnicode_GET_SIZE(unicode), |
| 2796 | NULL); |
| 2797 | } |
| 2798 | |
| 2799 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 2800 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2801 | PyObject *PyUnicode_DecodeASCII(const char *s, |
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 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2806 | PyUnicodeObject *v; |
| 2807 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2808 | Py_ssize_t startinpos; |
| 2809 | Py_ssize_t endinpos; |
| 2810 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2811 | const char *e; |
| 2812 | PyObject *errorHandler = NULL; |
| 2813 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2814 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2815 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 2816 | if (size == 1 && *(unsigned char*)s < 128) { |
| 2817 | Py_UNICODE r = *(unsigned char*)s; |
| 2818 | return PyUnicode_FromUnicode(&r, 1); |
| 2819 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2820 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2821 | v = _PyUnicode_New(size); |
| 2822 | if (v == NULL) |
| 2823 | goto onError; |
| 2824 | if (size == 0) |
| 2825 | return (PyObject *)v; |
| 2826 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2827 | e = s + size; |
| 2828 | while (s < e) { |
| 2829 | register unsigned char c = (unsigned char)*s; |
| 2830 | if (c < 128) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2831 | *p++ = c; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2832 | ++s; |
| 2833 | } |
| 2834 | else { |
| 2835 | startinpos = s-starts; |
| 2836 | endinpos = startinpos + 1; |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 2837 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2838 | if (unicode_decode_call_errorhandler( |
| 2839 | errors, &errorHandler, |
| 2840 | "ascii", "ordinal not in range(128)", |
| 2841 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2842 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2843 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2844 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2845 | } |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 2846 | if (p - PyUnicode_AS_UNICODE(v) < PyString_GET_SIZE(v)) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2847 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 2848 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2849 | Py_XDECREF(errorHandler); |
| 2850 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2851 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2852 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2853 | onError: |
| 2854 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2855 | Py_XDECREF(errorHandler); |
| 2856 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2857 | return NULL; |
| 2858 | } |
| 2859 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2860 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2861 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2862 | const char *errors) |
| 2863 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2864 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2865 | } |
| 2866 | |
| 2867 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 2868 | { |
| 2869 | if (!PyUnicode_Check(unicode)) { |
| 2870 | PyErr_BadArgument(); |
| 2871 | return NULL; |
| 2872 | } |
| 2873 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 2874 | PyUnicode_GET_SIZE(unicode), |
| 2875 | NULL); |
| 2876 | } |
| 2877 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 2878 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 2879 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2880 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 2881 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 2882 | #if SIZEOF_INT < SIZEOF_SSIZE_T |
| 2883 | #define NEED_RETRY |
| 2884 | #endif |
| 2885 | |
| 2886 | /* XXX This code is limited to "true" double-byte encodings, as |
| 2887 | a) it assumes an incomplete character consists of a single byte, and |
| 2888 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
| 2889 | encodings, see IsDBCSLeadByteEx documentation. */ |
| 2890 | |
| 2891 | static int is_dbcs_lead_byte(const char *s, int offset) |
| 2892 | { |
| 2893 | const char *curr = s + offset; |
| 2894 | |
| 2895 | if (IsDBCSLeadByte(*curr)) { |
| 2896 | const char *prev = CharPrev(s, curr); |
| 2897 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
| 2898 | } |
| 2899 | return 0; |
| 2900 | } |
| 2901 | |
| 2902 | /* |
| 2903 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 2904 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 2905 | */ |
| 2906 | static int decode_mbcs(PyUnicodeObject **v, |
| 2907 | const char *s, /* MBCS string */ |
| 2908 | int size, /* sizeof MBCS string */ |
| 2909 | int final) |
| 2910 | { |
| 2911 | Py_UNICODE *p; |
| 2912 | Py_ssize_t n = 0; |
| 2913 | int usize = 0; |
| 2914 | |
| 2915 | assert(size >= 0); |
| 2916 | |
| 2917 | /* Skip trailing lead-byte unless 'final' is set */ |
| 2918 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
| 2919 | --size; |
| 2920 | |
| 2921 | /* First get the size of the result */ |
| 2922 | if (size > 0) { |
| 2923 | usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); |
| 2924 | if (usize == 0) { |
| 2925 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2926 | return -1; |
| 2927 | } |
| 2928 | } |
| 2929 | |
| 2930 | if (*v == NULL) { |
| 2931 | /* Create unicode object */ |
| 2932 | *v = _PyUnicode_New(usize); |
| 2933 | if (*v == NULL) |
| 2934 | return -1; |
| 2935 | } |
| 2936 | else { |
| 2937 | /* Extend unicode object */ |
| 2938 | n = PyUnicode_GET_SIZE(*v); |
| 2939 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 2940 | return -1; |
| 2941 | } |
| 2942 | |
| 2943 | /* Do the conversion */ |
| 2944 | if (size > 0) { |
| 2945 | p = PyUnicode_AS_UNICODE(*v) + n; |
| 2946 | if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { |
| 2947 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 2948 | return -1; |
| 2949 | } |
| 2950 | } |
| 2951 | |
| 2952 | return size; |
| 2953 | } |
| 2954 | |
| 2955 | PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
| 2956 | Py_ssize_t size, |
| 2957 | const char *errors, |
| 2958 | Py_ssize_t *consumed) |
| 2959 | { |
| 2960 | PyUnicodeObject *v = NULL; |
| 2961 | int done; |
| 2962 | |
| 2963 | if (consumed) |
| 2964 | *consumed = 0; |
| 2965 | |
| 2966 | #ifdef NEED_RETRY |
| 2967 | retry: |
| 2968 | if (size > INT_MAX) |
| 2969 | done = decode_mbcs(&v, s, INT_MAX, 0); |
| 2970 | else |
| 2971 | #endif |
| 2972 | done = decode_mbcs(&v, s, (int)size, !consumed); |
| 2973 | |
| 2974 | if (done < 0) { |
| 2975 | Py_XDECREF(v); |
| 2976 | return NULL; |
| 2977 | } |
| 2978 | |
| 2979 | if (consumed) |
| 2980 | *consumed += done; |
| 2981 | |
| 2982 | #ifdef NEED_RETRY |
| 2983 | if (size > INT_MAX) { |
| 2984 | s += done; |
| 2985 | size -= done; |
| 2986 | goto retry; |
| 2987 | } |
| 2988 | #endif |
| 2989 | |
| 2990 | return (PyObject *)v; |
| 2991 | } |
| 2992 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2993 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2994 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 2995 | const char *errors) |
| 2996 | { |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 2997 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 2998 | } |
| 2999 | |
| 3000 | /* |
| 3001 | * Convert unicode into string object (MBCS). |
| 3002 | * Returns 0 if succeed, -1 otherwise. |
| 3003 | */ |
| 3004 | static int encode_mbcs(PyObject **repr, |
| 3005 | const Py_UNICODE *p, /* unicode */ |
| 3006 | int size) /* size of unicode */ |
| 3007 | { |
| 3008 | int mbcssize = 0; |
| 3009 | Py_ssize_t n = 0; |
| 3010 | |
| 3011 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3012 | |
| 3013 | /* First get the size of the result */ |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3014 | if (size > 0) { |
| 3015 | mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); |
| 3016 | if (mbcssize == 0) { |
| 3017 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3018 | return -1; |
| 3019 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3020 | } |
| 3021 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3022 | if (*repr == NULL) { |
| 3023 | /* Create string object */ |
| 3024 | *repr = PyString_FromStringAndSize(NULL, mbcssize); |
| 3025 | if (*repr == NULL) |
| 3026 | return -1; |
| 3027 | } |
| 3028 | else { |
| 3029 | /* Extend string object */ |
| 3030 | n = PyString_Size(*repr); |
| 3031 | if (_PyString_Resize(repr, n + mbcssize) < 0) |
| 3032 | return -1; |
| 3033 | } |
| 3034 | |
| 3035 | /* Do the conversion */ |
| 3036 | if (size > 0) { |
| 3037 | char *s = PyString_AS_STRING(*repr) + n; |
| 3038 | if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { |
| 3039 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3040 | return -1; |
| 3041 | } |
| 3042 | } |
| 3043 | |
| 3044 | return 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3045 | } |
| 3046 | |
| 3047 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3048 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3049 | const char *errors) |
| 3050 | { |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3051 | PyObject *repr = NULL; |
| 3052 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 3053 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3054 | #ifdef NEED_RETRY |
| 3055 | retry: |
| 3056 | if (size > INT_MAX) |
| 3057 | ret = encode_mbcs(&repr, p, INT_MAX); |
| 3058 | else |
| 3059 | #endif |
| 3060 | ret = encode_mbcs(&repr, p, (int)size); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3061 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3062 | if (ret < 0) { |
| 3063 | Py_XDECREF(repr); |
| 3064 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3065 | } |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3066 | |
| 3067 | #ifdef NEED_RETRY |
| 3068 | if (size > INT_MAX) { |
| 3069 | p += INT_MAX; |
| 3070 | size -= INT_MAX; |
| 3071 | goto retry; |
| 3072 | } |
| 3073 | #endif |
| 3074 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3075 | return repr; |
| 3076 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3077 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3078 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 3079 | { |
| 3080 | if (!PyUnicode_Check(unicode)) { |
| 3081 | PyErr_BadArgument(); |
| 3082 | return NULL; |
| 3083 | } |
| 3084 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 3085 | PyUnicode_GET_SIZE(unicode), |
| 3086 | NULL); |
| 3087 | } |
| 3088 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3089 | #undef NEED_RETRY |
| 3090 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3091 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3092 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3093 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 3094 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3095 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3096 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3097 | PyObject *mapping, |
| 3098 | const char *errors) |
| 3099 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3100 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3101 | Py_ssize_t startinpos; |
| 3102 | Py_ssize_t endinpos; |
| 3103 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3104 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3105 | PyUnicodeObject *v; |
| 3106 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3107 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3108 | PyObject *errorHandler = NULL; |
| 3109 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3110 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3111 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3112 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3113 | /* Default to Latin-1 */ |
| 3114 | if (mapping == NULL) |
| 3115 | return PyUnicode_DecodeLatin1(s, size, errors); |
| 3116 | |
| 3117 | v = _PyUnicode_New(size); |
| 3118 | if (v == NULL) |
| 3119 | goto onError; |
| 3120 | if (size == 0) |
| 3121 | return (PyObject *)v; |
| 3122 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3123 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3124 | if (PyUnicode_CheckExact(mapping)) { |
| 3125 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 3126 | maplen = PyUnicode_GET_SIZE(mapping); |
| 3127 | while (s < e) { |
| 3128 | unsigned char ch = *s; |
| 3129 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3130 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3131 | if (ch < maplen) |
| 3132 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3133 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3134 | if (x == 0xfffe) { |
| 3135 | /* undefined mapping */ |
| 3136 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3137 | startinpos = s-starts; |
| 3138 | endinpos = startinpos+1; |
| 3139 | if (unicode_decode_call_errorhandler( |
| 3140 | errors, &errorHandler, |
| 3141 | "charmap", "character maps to <undefined>", |
| 3142 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 3143 | (PyObject **)&v, &outpos, &p)) { |
| 3144 | goto onError; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 3145 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3146 | continue; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 3147 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3148 | *p++ = x; |
| 3149 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3150 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3151 | } |
| 3152 | else { |
| 3153 | while (s < e) { |
| 3154 | unsigned char ch = *s; |
| 3155 | PyObject *w, *x; |
| 3156 | |
| 3157 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 3158 | w = PyInt_FromLong((long)ch); |
| 3159 | if (w == NULL) |
| 3160 | goto onError; |
| 3161 | x = PyObject_GetItem(mapping, w); |
| 3162 | Py_DECREF(w); |
| 3163 | if (x == NULL) { |
| 3164 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 3165 | /* No mapping found means: mapping is undefined. */ |
| 3166 | PyErr_Clear(); |
| 3167 | x = Py_None; |
| 3168 | Py_INCREF(x); |
| 3169 | } else |
| 3170 | goto onError; |
| 3171 | } |
| 3172 | |
| 3173 | /* Apply mapping */ |
| 3174 | if (PyInt_Check(x)) { |
| 3175 | long value = PyInt_AS_LONG(x); |
| 3176 | if (value < 0 || value > 65535) { |
| 3177 | PyErr_SetString(PyExc_TypeError, |
| 3178 | "character mapping must be in range(65536)"); |
| 3179 | Py_DECREF(x); |
| 3180 | goto onError; |
| 3181 | } |
| 3182 | *p++ = (Py_UNICODE)value; |
| 3183 | } |
| 3184 | else if (x == Py_None) { |
| 3185 | /* undefined mapping */ |
| 3186 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3187 | startinpos = s-starts; |
| 3188 | endinpos = startinpos+1; |
| 3189 | if (unicode_decode_call_errorhandler( |
| 3190 | errors, &errorHandler, |
| 3191 | "charmap", "character maps to <undefined>", |
| 3192 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 3193 | (PyObject **)&v, &outpos, &p)) { |
| 3194 | Py_DECREF(x); |
| 3195 | goto onError; |
| 3196 | } |
Walter Dörwald | d4fff17 | 2005-11-28 22:15:56 +0000 | [diff] [blame] | 3197 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3198 | continue; |
| 3199 | } |
| 3200 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3201 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3202 | |
| 3203 | if (targetsize == 1) |
| 3204 | /* 1-1 mapping */ |
| 3205 | *p++ = *PyUnicode_AS_UNICODE(x); |
| 3206 | |
| 3207 | else if (targetsize > 1) { |
| 3208 | /* 1-n mapping */ |
| 3209 | if (targetsize > extrachars) { |
| 3210 | /* resize first */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3211 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 3212 | Py_ssize_t needed = (targetsize - extrachars) + \ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3213 | (targetsize << 2); |
| 3214 | extrachars += needed; |
Armin Rigo | 4b63c21 | 2006-10-04 11:44:06 +0000 | [diff] [blame] | 3215 | /* XXX overflow detection missing */ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3216 | if (_PyUnicode_Resize(&v, |
| 3217 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 3218 | Py_DECREF(x); |
| 3219 | goto onError; |
| 3220 | } |
| 3221 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 3222 | } |
| 3223 | Py_UNICODE_COPY(p, |
| 3224 | PyUnicode_AS_UNICODE(x), |
| 3225 | targetsize); |
| 3226 | p += targetsize; |
| 3227 | extrachars -= targetsize; |
| 3228 | } |
| 3229 | /* 1-0 mapping: skip the character */ |
| 3230 | } |
| 3231 | else { |
| 3232 | /* wrong return value */ |
| 3233 | PyErr_SetString(PyExc_TypeError, |
| 3234 | "character mapping must return integer, None or unicode"); |
| 3235 | Py_DECREF(x); |
| 3236 | goto onError; |
| 3237 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3238 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3239 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3240 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3241 | } |
| 3242 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 3243 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3244 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3245 | Py_XDECREF(errorHandler); |
| 3246 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3247 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3248 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3249 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3250 | Py_XDECREF(errorHandler); |
| 3251 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3252 | Py_XDECREF(v); |
| 3253 | return NULL; |
| 3254 | } |
| 3255 | |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3256 | /* Charmap encoding: the lookup table */ |
| 3257 | |
| 3258 | struct encoding_map{ |
| 3259 | PyObject_HEAD |
| 3260 | unsigned char level1[32]; |
| 3261 | int count2, count3; |
| 3262 | unsigned char level23[1]; |
| 3263 | }; |
| 3264 | |
| 3265 | static PyObject* |
| 3266 | encoding_map_size(PyObject *obj, PyObject* args) |
| 3267 | { |
| 3268 | struct encoding_map *map = (struct encoding_map*)obj; |
| 3269 | return PyInt_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
| 3270 | 128*map->count3); |
| 3271 | } |
| 3272 | |
| 3273 | static PyMethodDef encoding_map_methods[] = { |
| 3274 | {"size", encoding_map_size, METH_NOARGS, |
| 3275 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 3276 | { 0 } |
| 3277 | }; |
| 3278 | |
| 3279 | static void |
| 3280 | encoding_map_dealloc(PyObject* o) |
| 3281 | { |
| 3282 | PyObject_FREE(o); |
| 3283 | } |
| 3284 | |
| 3285 | static PyTypeObject EncodingMapType = { |
| 3286 | PyObject_HEAD_INIT(NULL) |
| 3287 | 0, /*ob_size*/ |
| 3288 | "EncodingMap", /*tp_name*/ |
| 3289 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 3290 | 0, /*tp_itemsize*/ |
| 3291 | /* methods */ |
| 3292 | encoding_map_dealloc, /*tp_dealloc*/ |
| 3293 | 0, /*tp_print*/ |
| 3294 | 0, /*tp_getattr*/ |
| 3295 | 0, /*tp_setattr*/ |
| 3296 | 0, /*tp_compare*/ |
| 3297 | 0, /*tp_repr*/ |
| 3298 | 0, /*tp_as_number*/ |
| 3299 | 0, /*tp_as_sequence*/ |
| 3300 | 0, /*tp_as_mapping*/ |
| 3301 | 0, /*tp_hash*/ |
| 3302 | 0, /*tp_call*/ |
| 3303 | 0, /*tp_str*/ |
| 3304 | 0, /*tp_getattro*/ |
| 3305 | 0, /*tp_setattro*/ |
| 3306 | 0, /*tp_as_buffer*/ |
| 3307 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 3308 | 0, /*tp_doc*/ |
| 3309 | 0, /*tp_traverse*/ |
| 3310 | 0, /*tp_clear*/ |
| 3311 | 0, /*tp_richcompare*/ |
| 3312 | 0, /*tp_weaklistoffset*/ |
| 3313 | 0, /*tp_iter*/ |
| 3314 | 0, /*tp_iternext*/ |
| 3315 | encoding_map_methods, /*tp_methods*/ |
| 3316 | 0, /*tp_members*/ |
| 3317 | 0, /*tp_getset*/ |
| 3318 | 0, /*tp_base*/ |
| 3319 | 0, /*tp_dict*/ |
| 3320 | 0, /*tp_descr_get*/ |
| 3321 | 0, /*tp_descr_set*/ |
| 3322 | 0, /*tp_dictoffset*/ |
| 3323 | 0, /*tp_init*/ |
| 3324 | 0, /*tp_alloc*/ |
| 3325 | 0, /*tp_new*/ |
| 3326 | 0, /*tp_free*/ |
| 3327 | 0, /*tp_is_gc*/ |
| 3328 | }; |
| 3329 | |
| 3330 | PyObject* |
| 3331 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 3332 | { |
| 3333 | Py_UNICODE *decode; |
| 3334 | PyObject *result; |
| 3335 | struct encoding_map *mresult; |
| 3336 | int i; |
| 3337 | int need_dict = 0; |
| 3338 | unsigned char level1[32]; |
| 3339 | unsigned char level2[512]; |
| 3340 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 3341 | int count2 = 0, count3 = 0; |
| 3342 | |
| 3343 | if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { |
| 3344 | PyErr_BadArgument(); |
| 3345 | return NULL; |
| 3346 | } |
| 3347 | decode = PyUnicode_AS_UNICODE(string); |
| 3348 | memset(level1, 0xFF, sizeof level1); |
| 3349 | memset(level2, 0xFF, sizeof level2); |
| 3350 | |
| 3351 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 3352 | or if there are non-BMP characters, we need to use |
| 3353 | a mapping dictionary. */ |
| 3354 | if (decode[0] != 0) |
| 3355 | need_dict = 1; |
| 3356 | for (i = 1; i < 256; i++) { |
| 3357 | int l1, l2; |
| 3358 | if (decode[i] == 0 |
| 3359 | #ifdef Py_UNICODE_WIDE |
| 3360 | || decode[i] > 0xFFFF |
| 3361 | #endif |
| 3362 | ) { |
| 3363 | need_dict = 1; |
| 3364 | break; |
| 3365 | } |
| 3366 | if (decode[i] == 0xFFFE) |
| 3367 | /* unmapped character */ |
| 3368 | continue; |
| 3369 | l1 = decode[i] >> 11; |
| 3370 | l2 = decode[i] >> 7; |
| 3371 | if (level1[l1] == 0xFF) |
| 3372 | level1[l1] = count2++; |
| 3373 | if (level2[l2] == 0xFF) |
| 3374 | level2[l2] = count3++; |
| 3375 | } |
| 3376 | |
| 3377 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 3378 | need_dict = 1; |
| 3379 | |
| 3380 | if (need_dict) { |
| 3381 | PyObject *result = PyDict_New(); |
| 3382 | PyObject *key, *value; |
| 3383 | if (!result) |
| 3384 | return NULL; |
| 3385 | for (i = 0; i < 256; i++) { |
| 3386 | key = value = NULL; |
| 3387 | key = PyInt_FromLong(decode[i]); |
| 3388 | value = PyInt_FromLong(i); |
| 3389 | if (!key || !value) |
| 3390 | goto failed1; |
| 3391 | if (PyDict_SetItem(result, key, value) == -1) |
| 3392 | goto failed1; |
Georg Brandl | 9f16760 | 2006-06-04 21:46:16 +0000 | [diff] [blame] | 3393 | Py_DECREF(key); |
| 3394 | Py_DECREF(value); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3395 | } |
| 3396 | return result; |
| 3397 | failed1: |
| 3398 | Py_XDECREF(key); |
| 3399 | Py_XDECREF(value); |
| 3400 | Py_DECREF(result); |
| 3401 | return NULL; |
| 3402 | } |
| 3403 | |
| 3404 | /* Create a three-level trie */ |
| 3405 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 3406 | 16*count2 + 128*count3 - 1); |
| 3407 | if (!result) |
| 3408 | return PyErr_NoMemory(); |
| 3409 | PyObject_Init(result, &EncodingMapType); |
| 3410 | mresult = (struct encoding_map*)result; |
| 3411 | mresult->count2 = count2; |
| 3412 | mresult->count3 = count3; |
| 3413 | mlevel1 = mresult->level1; |
| 3414 | mlevel2 = mresult->level23; |
| 3415 | mlevel3 = mresult->level23 + 16*count2; |
| 3416 | memcpy(mlevel1, level1, 32); |
| 3417 | memset(mlevel2, 0xFF, 16*count2); |
| 3418 | memset(mlevel3, 0, 128*count3); |
| 3419 | count3 = 0; |
| 3420 | for (i = 1; i < 256; i++) { |
| 3421 | int o1, o2, o3, i2, i3; |
| 3422 | if (decode[i] == 0xFFFE) |
| 3423 | /* unmapped character */ |
| 3424 | continue; |
| 3425 | o1 = decode[i]>>11; |
| 3426 | o2 = (decode[i]>>7) & 0xF; |
| 3427 | i2 = 16*mlevel1[o1] + o2; |
| 3428 | if (mlevel2[i2] == 0xFF) |
| 3429 | mlevel2[i2] = count3++; |
| 3430 | o3 = decode[i] & 0x7F; |
| 3431 | i3 = 128*mlevel2[i2] + o3; |
| 3432 | mlevel3[i3] = i; |
| 3433 | } |
| 3434 | return result; |
| 3435 | } |
| 3436 | |
| 3437 | static int |
| 3438 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 3439 | { |
| 3440 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 3441 | int l1 = c>>11; |
| 3442 | int l2 = (c>>7) & 0xF; |
| 3443 | int l3 = c & 0x7F; |
| 3444 | int i; |
| 3445 | |
| 3446 | #ifdef Py_UNICODE_WIDE |
| 3447 | if (c > 0xFFFF) { |
| 3448 | return -1; |
| 3449 | } |
| 3450 | #endif |
| 3451 | if (c == 0) |
| 3452 | return 0; |
| 3453 | /* level 1*/ |
| 3454 | i = map->level1[l1]; |
| 3455 | if (i == 0xFF) { |
| 3456 | return -1; |
| 3457 | } |
| 3458 | /* level 2*/ |
| 3459 | i = map->level23[16*i+l2]; |
| 3460 | if (i == 0xFF) { |
| 3461 | return -1; |
| 3462 | } |
| 3463 | /* level 3 */ |
| 3464 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 3465 | if (i == 0) { |
| 3466 | return -1; |
| 3467 | } |
| 3468 | return i; |
| 3469 | } |
| 3470 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3471 | /* Lookup the character ch in the mapping. If the character |
| 3472 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 3473 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3474 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3475 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3476 | PyObject *w = PyInt_FromLong((long)c); |
| 3477 | PyObject *x; |
| 3478 | |
| 3479 | if (w == NULL) |
| 3480 | return NULL; |
| 3481 | x = PyObject_GetItem(mapping, w); |
| 3482 | Py_DECREF(w); |
| 3483 | if (x == NULL) { |
| 3484 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 3485 | /* No mapping found means: mapping is undefined. */ |
| 3486 | PyErr_Clear(); |
| 3487 | x = Py_None; |
| 3488 | Py_INCREF(x); |
| 3489 | return x; |
| 3490 | } else |
| 3491 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3492 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 3493 | else if (x == Py_None) |
| 3494 | return x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3495 | else if (PyInt_Check(x)) { |
| 3496 | long value = PyInt_AS_LONG(x); |
| 3497 | if (value < 0 || value > 255) { |
| 3498 | PyErr_SetString(PyExc_TypeError, |
| 3499 | "character mapping must be in range(256)"); |
| 3500 | Py_DECREF(x); |
| 3501 | return NULL; |
| 3502 | } |
| 3503 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3504 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3505 | else if (PyString_Check(x)) |
| 3506 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3507 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3508 | /* wrong return value */ |
| 3509 | PyErr_SetString(PyExc_TypeError, |
| 3510 | "character mapping must return integer, None or str"); |
| 3511 | Py_DECREF(x); |
| 3512 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3513 | } |
| 3514 | } |
| 3515 | |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3516 | static int |
| 3517 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
| 3518 | { |
| 3519 | Py_ssize_t outsize = PyString_GET_SIZE(*outobj); |
| 3520 | /* exponentially overallocate to minimize reallocations */ |
| 3521 | if (requiredsize < 2*outsize) |
| 3522 | requiredsize = 2*outsize; |
| 3523 | if (_PyString_Resize(outobj, requiredsize)) { |
| 3524 | return 0; |
| 3525 | } |
| 3526 | return 1; |
| 3527 | } |
| 3528 | |
| 3529 | typedef enum charmapencode_result { |
| 3530 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
| 3531 | }charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3532 | /* lookup the character, put the result in the output string and adjust |
| 3533 | various state variables. Reallocate the output string if not enough |
| 3534 | space is available. Return a new reference to the object that |
| 3535 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 3536 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 3537 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3538 | static |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3539 | charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3540 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3541 | { |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3542 | PyObject *rep; |
| 3543 | char *outstart; |
| 3544 | Py_ssize_t outsize = PyString_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3545 | |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3546 | if (mapping->ob_type == &EncodingMapType) { |
| 3547 | int res = encoding_map_lookup(c, mapping); |
| 3548 | Py_ssize_t requiredsize = *outpos+1; |
| 3549 | if (res == -1) |
| 3550 | return enc_FAILED; |
| 3551 | if (outsize<requiredsize) |
| 3552 | if (!charmapencode_resize(outobj, outpos, requiredsize)) |
| 3553 | return enc_EXCEPTION; |
| 3554 | outstart = PyString_AS_STRING(*outobj); |
| 3555 | outstart[(*outpos)++] = (char)res; |
| 3556 | return enc_SUCCESS; |
| 3557 | } |
| 3558 | |
| 3559 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3560 | if (rep==NULL) |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3561 | return enc_EXCEPTION; |
| 3562 | else if (rep==Py_None) { |
| 3563 | Py_DECREF(rep); |
| 3564 | return enc_FAILED; |
| 3565 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3566 | if (PyInt_Check(rep)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3567 | Py_ssize_t requiredsize = *outpos+1; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3568 | if (outsize<requiredsize) |
| 3569 | if (!charmapencode_resize(outobj, outpos, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3570 | Py_DECREF(rep); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3571 | return enc_EXCEPTION; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3572 | } |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3573 | outstart = PyString_AS_STRING(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3574 | outstart[(*outpos)++] = (char)PyInt_AS_LONG(rep); |
| 3575 | } |
| 3576 | else { |
| 3577 | const char *repchars = PyString_AS_STRING(rep); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3578 | Py_ssize_t repsize = PyString_GET_SIZE(rep); |
| 3579 | Py_ssize_t requiredsize = *outpos+repsize; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3580 | if (outsize<requiredsize) |
| 3581 | if (!charmapencode_resize(outobj, outpos, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3582 | Py_DECREF(rep); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3583 | return enc_EXCEPTION; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3584 | } |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3585 | outstart = PyString_AS_STRING(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3586 | memcpy(outstart + *outpos, repchars, repsize); |
| 3587 | *outpos += repsize; |
| 3588 | } |
| 3589 | } |
Georg Brandl | 9f16760 | 2006-06-04 21:46:16 +0000 | [diff] [blame] | 3590 | Py_DECREF(rep); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3591 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3592 | } |
| 3593 | |
| 3594 | /* handle an error in PyUnicode_EncodeCharmap |
| 3595 | Return 0 on success, -1 on error */ |
| 3596 | static |
| 3597 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3598 | 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] | 3599 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 3600 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3601 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3602 | { |
| 3603 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3604 | Py_ssize_t repsize; |
| 3605 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3606 | Py_UNICODE *uni2; |
| 3607 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3608 | Py_ssize_t collstartpos = *inpos; |
| 3609 | Py_ssize_t collendpos = *inpos+1; |
| 3610 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3611 | char *encoding = "charmap"; |
| 3612 | char *reason = "character maps to <undefined>"; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3613 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3614 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3615 | /* find all unencodable characters */ |
| 3616 | while (collendpos < size) { |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3617 | PyObject *rep; |
| 3618 | if (mapping->ob_type == &EncodingMapType) { |
| 3619 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 3620 | if (res != -1) |
| 3621 | break; |
| 3622 | ++collendpos; |
| 3623 | continue; |
| 3624 | } |
| 3625 | |
| 3626 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 3627 | if (rep==NULL) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3628 | return -1; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3629 | else if (rep!=Py_None) { |
| 3630 | Py_DECREF(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3631 | break; |
| 3632 | } |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3633 | Py_DECREF(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3634 | ++collendpos; |
| 3635 | } |
| 3636 | /* cache callback name lookup |
| 3637 | * (if not done yet, i.e. it's the first error) */ |
| 3638 | if (*known_errorHandler==-1) { |
| 3639 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3640 | *known_errorHandler = 1; |
| 3641 | else if (!strcmp(errors, "replace")) |
| 3642 | *known_errorHandler = 2; |
| 3643 | else if (!strcmp(errors, "ignore")) |
| 3644 | *known_errorHandler = 3; |
| 3645 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3646 | *known_errorHandler = 4; |
| 3647 | else |
| 3648 | *known_errorHandler = 0; |
| 3649 | } |
| 3650 | switch (*known_errorHandler) { |
| 3651 | case 1: /* strict */ |
| 3652 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3653 | return -1; |
| 3654 | case 2: /* replace */ |
| 3655 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
| 3656 | x = charmapencode_output('?', mapping, res, respos); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3657 | if (x==enc_EXCEPTION) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3658 | return -1; |
| 3659 | } |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3660 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3661 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3662 | return -1; |
| 3663 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3664 | } |
| 3665 | /* fall through */ |
| 3666 | case 3: /* ignore */ |
| 3667 | *inpos = collendpos; |
| 3668 | break; |
| 3669 | case 4: /* xmlcharrefreplace */ |
| 3670 | /* generate replacement (temporarily (mis)uses p) */ |
| 3671 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
| 3672 | char buffer[2+29+1+1]; |
| 3673 | char *cp; |
| 3674 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 3675 | for (cp = buffer; *cp; ++cp) { |
| 3676 | x = charmapencode_output(*cp, mapping, res, respos); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3677 | if (x==enc_EXCEPTION) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3678 | return -1; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3679 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3680 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3681 | return -1; |
| 3682 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3683 | } |
| 3684 | } |
| 3685 | *inpos = collendpos; |
| 3686 | break; |
| 3687 | default: |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 3688 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3689 | encoding, reason, p, size, exceptionObject, |
| 3690 | collstartpos, collendpos, &newpos); |
| 3691 | if (repunicode == NULL) |
| 3692 | return -1; |
| 3693 | /* generate replacement */ |
| 3694 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3695 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 3696 | x = charmapencode_output(*uni2, mapping, res, respos); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3697 | if (x==enc_EXCEPTION) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3698 | return -1; |
| 3699 | } |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3700 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3701 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3702 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 3703 | return -1; |
| 3704 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3705 | } |
| 3706 | *inpos = newpos; |
| 3707 | Py_DECREF(repunicode); |
| 3708 | } |
| 3709 | return 0; |
| 3710 | } |
| 3711 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3712 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3713 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3714 | PyObject *mapping, |
| 3715 | const char *errors) |
| 3716 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3717 | /* output object */ |
| 3718 | PyObject *res = NULL; |
| 3719 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3720 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3721 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3722 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3723 | PyObject *errorHandler = NULL; |
| 3724 | PyObject *exc = NULL; |
| 3725 | /* the following variable is used for caching string comparisons |
| 3726 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 3727 | * 3=ignore, 4=xmlcharrefreplace */ |
| 3728 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3729 | |
| 3730 | /* Default to Latin-1 */ |
| 3731 | if (mapping == NULL) |
| 3732 | return PyUnicode_EncodeLatin1(p, size, errors); |
| 3733 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3734 | /* allocate enough for a simple encoding without |
| 3735 | replacements, if we need more, we'll resize */ |
| 3736 | res = PyString_FromStringAndSize(NULL, size); |
| 3737 | if (res == NULL) |
| 3738 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3739 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3740 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3741 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3742 | while (inpos<size) { |
| 3743 | /* try to encode it */ |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3744 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 3745 | if (x==enc_EXCEPTION) /* error */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3746 | goto onError; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 3747 | if (x==enc_FAILED) { /* unencodable character */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3748 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 3749 | &exc, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 3750 | &known_errorHandler, &errorHandler, errors, |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 3751 | &res, &respos)) { |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 3752 | goto onError; |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 3753 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3754 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3755 | else |
| 3756 | /* done with this character => adjust input position */ |
| 3757 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3758 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3759 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3760 | /* Resize if we allocated to much */ |
| 3761 | if (respos<PyString_GET_SIZE(res)) { |
| 3762 | if (_PyString_Resize(&res, respos)) |
| 3763 | goto onError; |
| 3764 | } |
| 3765 | Py_XDECREF(exc); |
| 3766 | Py_XDECREF(errorHandler); |
| 3767 | return res; |
| 3768 | |
| 3769 | onError: |
| 3770 | Py_XDECREF(res); |
| 3771 | Py_XDECREF(exc); |
| 3772 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3773 | return NULL; |
| 3774 | } |
| 3775 | |
| 3776 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
| 3777 | PyObject *mapping) |
| 3778 | { |
| 3779 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
| 3780 | PyErr_BadArgument(); |
| 3781 | return NULL; |
| 3782 | } |
| 3783 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
| 3784 | PyUnicode_GET_SIZE(unicode), |
| 3785 | mapping, |
| 3786 | NULL); |
| 3787 | } |
| 3788 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3789 | /* create or adjust a UnicodeTranslateError */ |
| 3790 | static void make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3791 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3792 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3793 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3794 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3795 | if (*exceptionObject == NULL) { |
| 3796 | *exceptionObject = PyUnicodeTranslateError_Create( |
| 3797 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3798 | } |
| 3799 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3800 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 3801 | goto onError; |
| 3802 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 3803 | goto onError; |
| 3804 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 3805 | goto onError; |
| 3806 | return; |
| 3807 | onError: |
| 3808 | Py_DECREF(*exceptionObject); |
| 3809 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3810 | } |
| 3811 | } |
| 3812 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3813 | /* raises a UnicodeTranslateError */ |
| 3814 | static void raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3815 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3816 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3817 | const char *reason) |
| 3818 | { |
| 3819 | make_translate_exception(exceptionObject, |
| 3820 | unicode, size, startpos, endpos, reason); |
| 3821 | if (*exceptionObject != NULL) |
| 3822 | PyCodec_StrictErrors(*exceptionObject); |
| 3823 | } |
| 3824 | |
| 3825 | /* error handling callback helper: |
| 3826 | build arguments, call the callback and check the arguments, |
| 3827 | put the result into newpos and return the replacement string, which |
| 3828 | has to be freed by the caller */ |
| 3829 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
| 3830 | PyObject **errorHandler, |
| 3831 | const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3832 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 3833 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3834 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3835 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 3836 | 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] | 3837 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 3838 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3839 | PyObject *restuple; |
| 3840 | PyObject *resunicode; |
| 3841 | |
| 3842 | if (*errorHandler == NULL) { |
| 3843 | *errorHandler = PyCodec_LookupError(errors); |
| 3844 | if (*errorHandler == NULL) |
| 3845 | return NULL; |
| 3846 | } |
| 3847 | |
| 3848 | make_translate_exception(exceptionObject, |
| 3849 | unicode, size, startpos, endpos, reason); |
| 3850 | if (*exceptionObject == NULL) |
| 3851 | return NULL; |
| 3852 | |
| 3853 | restuple = PyObject_CallFunctionObjArgs( |
| 3854 | *errorHandler, *exceptionObject, NULL); |
| 3855 | if (restuple == NULL) |
| 3856 | return NULL; |
| 3857 | if (!PyTuple_Check(restuple)) { |
| 3858 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 3859 | Py_DECREF(restuple); |
| 3860 | return NULL; |
| 3861 | } |
| 3862 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3863 | &resunicode, &i_newpos)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3864 | Py_DECREF(restuple); |
| 3865 | return NULL; |
| 3866 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3867 | if (i_newpos<0) |
| 3868 | *newpos = size+i_newpos; |
| 3869 | else |
| 3870 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3871 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 3872 | 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] | 3873 | Py_DECREF(restuple); |
| 3874 | return NULL; |
| 3875 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3876 | Py_INCREF(resunicode); |
| 3877 | Py_DECREF(restuple); |
| 3878 | return resunicode; |
| 3879 | } |
| 3880 | |
| 3881 | /* Lookup the character ch in the mapping and put the result in result, |
| 3882 | which must be decrefed by the caller. |
| 3883 | Return 0 on success, -1 on error */ |
| 3884 | static |
| 3885 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 3886 | { |
| 3887 | PyObject *w = PyInt_FromLong((long)c); |
| 3888 | PyObject *x; |
| 3889 | |
| 3890 | if (w == NULL) |
| 3891 | return -1; |
| 3892 | x = PyObject_GetItem(mapping, w); |
| 3893 | Py_DECREF(w); |
| 3894 | if (x == NULL) { |
| 3895 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 3896 | /* No mapping found means: use 1:1 mapping. */ |
| 3897 | PyErr_Clear(); |
| 3898 | *result = NULL; |
| 3899 | return 0; |
| 3900 | } else |
| 3901 | return -1; |
| 3902 | } |
| 3903 | else if (x == Py_None) { |
| 3904 | *result = x; |
| 3905 | return 0; |
| 3906 | } |
| 3907 | else if (PyInt_Check(x)) { |
| 3908 | long value = PyInt_AS_LONG(x); |
| 3909 | long max = PyUnicode_GetMax(); |
| 3910 | if (value < 0 || value > max) { |
| 3911 | PyErr_Format(PyExc_TypeError, |
| 3912 | "character mapping must be in range(0x%lx)", max+1); |
| 3913 | Py_DECREF(x); |
| 3914 | return -1; |
| 3915 | } |
| 3916 | *result = x; |
| 3917 | return 0; |
| 3918 | } |
| 3919 | else if (PyUnicode_Check(x)) { |
| 3920 | *result = x; |
| 3921 | return 0; |
| 3922 | } |
| 3923 | else { |
| 3924 | /* wrong return value */ |
| 3925 | PyErr_SetString(PyExc_TypeError, |
| 3926 | "character mapping must return integer, None or unicode"); |
Walter Dörwald | 150523e | 2003-08-15 16:52:19 +0000 | [diff] [blame] | 3927 | Py_DECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3928 | return -1; |
| 3929 | } |
| 3930 | } |
| 3931 | /* ensure that *outobj is at least requiredsize characters long, |
| 3932 | if not reallocate and adjust various state variables. |
| 3933 | Return 0 on success, -1 on error */ |
| 3934 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3935 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3936 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3937 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3938 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3939 | if (requiredsize > oldsize) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3940 | /* remember old output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3941 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3942 | /* exponentially overallocate to minimize reallocations */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3943 | if (requiredsize < 2 * oldsize) |
| 3944 | requiredsize = 2 * oldsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3945 | if (_PyUnicode_Resize(outobj, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3946 | return -1; |
| 3947 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3948 | } |
| 3949 | return 0; |
| 3950 | } |
| 3951 | /* lookup the character, put the result in the output string and adjust |
| 3952 | various state variables. Return a new reference to the object that |
| 3953 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 3954 | undefined (in which case no character was written). |
| 3955 | The called must decref result. |
| 3956 | Return 0 on success, -1 on error. */ |
| 3957 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3958 | 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] | 3959 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3960 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3961 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3962 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3963 | return -1; |
| 3964 | if (*res==NULL) { |
| 3965 | /* not found => default to 1:1 mapping */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3966 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3967 | } |
| 3968 | else if (*res==Py_None) |
| 3969 | ; |
| 3970 | else if (PyInt_Check(*res)) { |
| 3971 | /* no overflow check, because we know that the space is enough */ |
| 3972 | *(*outp)++ = (Py_UNICODE)PyInt_AS_LONG(*res); |
| 3973 | } |
| 3974 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3975 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3976 | if (repsize==1) { |
| 3977 | /* no overflow check, because we know that the space is enough */ |
| 3978 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 3979 | } |
| 3980 | else if (repsize!=0) { |
| 3981 | /* more than one character */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3982 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
Walter Dörwald | cd736e7 | 2004-02-05 17:36:00 +0000 | [diff] [blame] | 3983 | (insize - (curinp-startinp)) + |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 3984 | repsize - 1; |
| 3985 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3986 | return -1; |
| 3987 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 3988 | *outp += repsize; |
| 3989 | } |
| 3990 | } |
| 3991 | else |
| 3992 | return -1; |
| 3993 | return 0; |
| 3994 | } |
| 3995 | |
| 3996 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3997 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3998 | PyObject *mapping, |
| 3999 | const char *errors) |
| 4000 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4001 | /* output object */ |
| 4002 | PyObject *res = NULL; |
| 4003 | /* pointers to the beginning and end+1 of input */ |
| 4004 | const Py_UNICODE *startp = p; |
| 4005 | const Py_UNICODE *endp = p + size; |
| 4006 | /* pointer into the output */ |
| 4007 | Py_UNICODE *str; |
| 4008 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4009 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4010 | char *reason = "character maps to <undefined>"; |
| 4011 | PyObject *errorHandler = NULL; |
| 4012 | PyObject *exc = NULL; |
| 4013 | /* the following variable is used for caching string comparisons |
| 4014 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 4015 | * 3=ignore, 4=xmlcharrefreplace */ |
| 4016 | int known_errorHandler = -1; |
| 4017 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4018 | if (mapping == NULL) { |
| 4019 | PyErr_BadArgument(); |
| 4020 | return NULL; |
| 4021 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4022 | |
| 4023 | /* allocate enough for a simple 1:1 translation without |
| 4024 | replacements, if we need more, we'll resize */ |
| 4025 | res = PyUnicode_FromUnicode(NULL, size); |
| 4026 | if (res == NULL) |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4027 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4028 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4029 | return res; |
| 4030 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4031 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4032 | while (p<endp) { |
| 4033 | /* try to encode it */ |
| 4034 | PyObject *x = NULL; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4035 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4036 | Py_XDECREF(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4037 | goto onError; |
| 4038 | } |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 4039 | Py_XDECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4040 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 4041 | ++p; |
| 4042 | else { /* untranslatable character */ |
| 4043 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4044 | Py_ssize_t repsize; |
| 4045 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4046 | Py_UNICODE *uni2; |
| 4047 | /* startpos for collecting untranslatable chars */ |
| 4048 | const Py_UNICODE *collstart = p; |
| 4049 | const Py_UNICODE *collend = p+1; |
| 4050 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4051 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4052 | /* find all untranslatable characters */ |
| 4053 | while (collend < endp) { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4054 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4055 | goto onError; |
| 4056 | Py_XDECREF(x); |
| 4057 | if (x!=Py_None) |
| 4058 | break; |
| 4059 | ++collend; |
| 4060 | } |
| 4061 | /* cache callback name lookup |
| 4062 | * (if not done yet, i.e. it's the first error) */ |
| 4063 | if (known_errorHandler==-1) { |
| 4064 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4065 | known_errorHandler = 1; |
| 4066 | else if (!strcmp(errors, "replace")) |
| 4067 | known_errorHandler = 2; |
| 4068 | else if (!strcmp(errors, "ignore")) |
| 4069 | known_errorHandler = 3; |
| 4070 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4071 | known_errorHandler = 4; |
| 4072 | else |
| 4073 | known_errorHandler = 0; |
| 4074 | } |
| 4075 | switch (known_errorHandler) { |
| 4076 | case 1: /* strict */ |
| 4077 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
| 4078 | goto onError; |
| 4079 | case 2: /* replace */ |
| 4080 | /* No need to check for space, this is a 1:1 replacement */ |
| 4081 | for (coll = collstart; coll<collend; ++coll) |
| 4082 | *str++ = '?'; |
| 4083 | /* fall through */ |
| 4084 | case 3: /* ignore */ |
| 4085 | p = collend; |
| 4086 | break; |
| 4087 | case 4: /* xmlcharrefreplace */ |
| 4088 | /* generate replacement (temporarily (mis)uses p) */ |
| 4089 | for (p = collstart; p < collend; ++p) { |
| 4090 | char buffer[2+29+1+1]; |
| 4091 | char *cp; |
| 4092 | sprintf(buffer, "&#%d;", (int)*p); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4093 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4094 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 4095 | goto onError; |
| 4096 | for (cp = buffer; *cp; ++cp) |
| 4097 | *str++ = *cp; |
| 4098 | } |
| 4099 | p = collend; |
| 4100 | break; |
| 4101 | default: |
| 4102 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 4103 | reason, startp, size, &exc, |
| 4104 | collstart-startp, collend-startp, &newpos); |
| 4105 | if (repunicode == NULL) |
| 4106 | goto onError; |
| 4107 | /* generate replacement */ |
| 4108 | repsize = PyUnicode_GET_SIZE(repunicode); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4109 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4110 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 4111 | Py_DECREF(repunicode); |
| 4112 | goto onError; |
| 4113 | } |
| 4114 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 4115 | *str++ = *uni2; |
| 4116 | p = startp + newpos; |
| 4117 | Py_DECREF(repunicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4118 | } |
| 4119 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4120 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4121 | /* Resize if we allocated to much */ |
| 4122 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4123 | if (respos<PyUnicode_GET_SIZE(res)) { |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 4124 | if (_PyUnicode_Resize(&res, respos) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 4125 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4126 | } |
| 4127 | Py_XDECREF(exc); |
| 4128 | Py_XDECREF(errorHandler); |
| 4129 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4130 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4131 | onError: |
| 4132 | Py_XDECREF(res); |
| 4133 | Py_XDECREF(exc); |
| 4134 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4135 | return NULL; |
| 4136 | } |
| 4137 | |
| 4138 | PyObject *PyUnicode_Translate(PyObject *str, |
| 4139 | PyObject *mapping, |
| 4140 | const char *errors) |
| 4141 | { |
| 4142 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4143 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4144 | str = PyUnicode_FromObject(str); |
| 4145 | if (str == NULL) |
| 4146 | goto onError; |
| 4147 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
| 4148 | PyUnicode_GET_SIZE(str), |
| 4149 | mapping, |
| 4150 | errors); |
| 4151 | Py_DECREF(str); |
| 4152 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4153 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4154 | onError: |
| 4155 | Py_XDECREF(str); |
| 4156 | return NULL; |
| 4157 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4158 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4159 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 4160 | |
| 4161 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4162 | Py_ssize_t length, |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4163 | char *output, |
| 4164 | const char *errors) |
| 4165 | { |
| 4166 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4167 | PyObject *errorHandler = NULL; |
| 4168 | PyObject *exc = NULL; |
| 4169 | const char *encoding = "decimal"; |
| 4170 | const char *reason = "invalid decimal Unicode string"; |
| 4171 | /* the following variable is used for caching string comparisons |
| 4172 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 4173 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4174 | |
| 4175 | if (output == NULL) { |
| 4176 | PyErr_BadArgument(); |
| 4177 | return -1; |
| 4178 | } |
| 4179 | |
| 4180 | p = s; |
| 4181 | end = s + length; |
| 4182 | while (p < end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4183 | register Py_UNICODE ch = *p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4184 | int decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4185 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4186 | Py_ssize_t repsize; |
| 4187 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4188 | Py_UNICODE *uni2; |
| 4189 | Py_UNICODE *collstart; |
| 4190 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4191 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4192 | if (Py_UNICODE_ISSPACE(ch)) { |
| 4193 | *output++ = ' '; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4194 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4195 | continue; |
| 4196 | } |
| 4197 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 4198 | if (decimal >= 0) { |
| 4199 | *output++ = '0' + decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4200 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4201 | continue; |
| 4202 | } |
Guido van Rossum | ba47704 | 2000-04-06 18:18:10 +0000 | [diff] [blame] | 4203 | if (0 < ch && ch < 256) { |
Guido van Rossum | 42c29aa | 2000-05-03 23:58:29 +0000 | [diff] [blame] | 4204 | *output++ = (char)ch; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4205 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4206 | continue; |
| 4207 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4208 | /* All other characters are considered unencodable */ |
| 4209 | collstart = p; |
| 4210 | collend = p+1; |
| 4211 | while (collend < end) { |
| 4212 | if ((0 < *collend && *collend < 256) || |
| 4213 | !Py_UNICODE_ISSPACE(*collend) || |
| 4214 | Py_UNICODE_TODECIMAL(*collend)) |
| 4215 | break; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4216 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4217 | /* cache callback name lookup |
| 4218 | * (if not done yet, i.e. it's the first error) */ |
| 4219 | if (known_errorHandler==-1) { |
| 4220 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4221 | known_errorHandler = 1; |
| 4222 | else if (!strcmp(errors, "replace")) |
| 4223 | known_errorHandler = 2; |
| 4224 | else if (!strcmp(errors, "ignore")) |
| 4225 | known_errorHandler = 3; |
| 4226 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4227 | known_errorHandler = 4; |
| 4228 | else |
| 4229 | known_errorHandler = 0; |
| 4230 | } |
| 4231 | switch (known_errorHandler) { |
| 4232 | case 1: /* strict */ |
| 4233 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 4234 | goto onError; |
| 4235 | case 2: /* replace */ |
| 4236 | for (p = collstart; p < collend; ++p) |
| 4237 | *output++ = '?'; |
| 4238 | /* fall through */ |
| 4239 | case 3: /* ignore */ |
| 4240 | p = collend; |
| 4241 | break; |
| 4242 | case 4: /* xmlcharrefreplace */ |
| 4243 | /* generate replacement (temporarily (mis)uses p) */ |
| 4244 | for (p = collstart; p < collend; ++p) |
| 4245 | output += sprintf(output, "&#%d;", (int)*p); |
| 4246 | p = collend; |
| 4247 | break; |
| 4248 | default: |
| 4249 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 4250 | encoding, reason, s, length, &exc, |
| 4251 | collstart-s, collend-s, &newpos); |
| 4252 | if (repunicode == NULL) |
| 4253 | goto onError; |
| 4254 | /* generate replacement */ |
| 4255 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4256 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 4257 | Py_UNICODE ch = *uni2; |
| 4258 | if (Py_UNICODE_ISSPACE(ch)) |
| 4259 | *output++ = ' '; |
| 4260 | else { |
| 4261 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 4262 | if (decimal >= 0) |
| 4263 | *output++ = '0' + decimal; |
| 4264 | else if (0 < ch && ch < 256) |
| 4265 | *output++ = (char)ch; |
| 4266 | else { |
| 4267 | Py_DECREF(repunicode); |
| 4268 | raise_encode_exception(&exc, encoding, |
| 4269 | s, length, collstart-s, collend-s, reason); |
| 4270 | goto onError; |
| 4271 | } |
| 4272 | } |
| 4273 | } |
| 4274 | p = s + newpos; |
| 4275 | Py_DECREF(repunicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4276 | } |
| 4277 | } |
| 4278 | /* 0-terminate the output string */ |
| 4279 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4280 | Py_XDECREF(exc); |
| 4281 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4282 | return 0; |
| 4283 | |
| 4284 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4285 | Py_XDECREF(exc); |
| 4286 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4287 | return -1; |
| 4288 | } |
| 4289 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4290 | /* --- Helpers ------------------------------------------------------------ */ |
| 4291 | |
Fredrik Lundh | a50d201 | 2006-05-26 17:04:58 +0000 | [diff] [blame] | 4292 | #define STRINGLIB_CHAR Py_UNICODE |
Fredrik Lundh | 6471ee4 | 2006-05-24 14:28:11 +0000 | [diff] [blame] | 4293 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 4294 | #define STRINGLIB_LEN PyUnicode_GET_SIZE |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 4295 | #define STRINGLIB_NEW PyUnicode_FromUnicode |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 4296 | #define STRINGLIB_STR PyUnicode_AS_UNICODE |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 4297 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 4298 | Py_LOCAL_INLINE(int) |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 4299 | STRINGLIB_CMP(const Py_UNICODE* str, const Py_UNICODE* other, Py_ssize_t len) |
| 4300 | { |
Fredrik Lundh | 9c0e9c0 | 2006-05-26 18:24:15 +0000 | [diff] [blame] | 4301 | if (str[0] != other[0]) |
| 4302 | return 1; |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 4303 | return memcmp((void*) str, (void*) other, len * sizeof(Py_UNICODE)); |
| 4304 | } |
| 4305 | |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 4306 | #define STRINGLIB_EMPTY unicode_empty |
| 4307 | |
Fredrik Lundh | a50d201 | 2006-05-26 17:04:58 +0000 | [diff] [blame] | 4308 | #include "stringlib/fastsearch.h" |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 4309 | |
| 4310 | #include "stringlib/count.h" |
| 4311 | #include "stringlib/find.h" |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 4312 | #include "stringlib/partition.h" |
| 4313 | |
Fredrik Lundh | c816281 | 2006-05-26 19:33:03 +0000 | [diff] [blame] | 4314 | /* helper macro to fixup start/end slice values */ |
| 4315 | #define FIX_START_END(obj) \ |
| 4316 | if (start < 0) \ |
| 4317 | start += (obj)->length; \ |
| 4318 | if (start < 0) \ |
| 4319 | start = 0; \ |
| 4320 | if (end > (obj)->length) \ |
| 4321 | end = (obj)->length; \ |
| 4322 | if (end < 0) \ |
| 4323 | end += (obj)->length; \ |
| 4324 | if (end < 0) \ |
| 4325 | end = 0; |
| 4326 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4327 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 4328 | PyObject *substr, |
| 4329 | Py_ssize_t start, |
| 4330 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4331 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4332 | Py_ssize_t result; |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 4333 | PyUnicodeObject* str_obj; |
| 4334 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4335 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 4336 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 4337 | if (!str_obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4338 | return -1; |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 4339 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 4340 | if (!sub_obj) { |
| 4341 | Py_DECREF(str_obj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4342 | return -1; |
| 4343 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4344 | |
Fredrik Lundh | c816281 | 2006-05-26 19:33:03 +0000 | [diff] [blame] | 4345 | FIX_START_END(str_obj); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4346 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 4347 | result = stringlib_count( |
| 4348 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length |
| 4349 | ); |
| 4350 | |
| 4351 | Py_DECREF(sub_obj); |
| 4352 | Py_DECREF(str_obj); |
| 4353 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4354 | return result; |
| 4355 | } |
| 4356 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4357 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 4358 | PyObject *sub, |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 4359 | Py_ssize_t start, |
| 4360 | Py_ssize_t end, |
| 4361 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4362 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4363 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4364 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 4365 | str = PyUnicode_FromObject(str); |
| 4366 | if (!str) |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 4367 | return -2; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 4368 | sub = PyUnicode_FromObject(sub); |
| 4369 | if (!sub) { |
| 4370 | Py_DECREF(str); |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 4371 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4372 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4373 | |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 4374 | if (direction > 0) |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 4375 | result = stringlib_find_slice( |
| 4376 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 4377 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 4378 | start, end |
| 4379 | ); |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 4380 | else |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 4381 | result = stringlib_rfind_slice( |
| 4382 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 4383 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 4384 | start, end |
| 4385 | ); |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 4386 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 4387 | Py_DECREF(str); |
| 4388 | Py_DECREF(sub); |
| 4389 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4390 | return result; |
| 4391 | } |
| 4392 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4393 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4394 | int tailmatch(PyUnicodeObject *self, |
| 4395 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4396 | Py_ssize_t start, |
| 4397 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4398 | int direction) |
| 4399 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4400 | if (substring->length == 0) |
| 4401 | return 1; |
| 4402 | |
Fredrik Lundh | c816281 | 2006-05-26 19:33:03 +0000 | [diff] [blame] | 4403 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4404 | |
| 4405 | end -= substring->length; |
| 4406 | if (end < start) |
| 4407 | return 0; |
| 4408 | |
| 4409 | if (direction > 0) { |
| 4410 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 4411 | return 1; |
| 4412 | } else { |
| 4413 | if (Py_UNICODE_MATCH(self, start, substring)) |
| 4414 | return 1; |
| 4415 | } |
| 4416 | |
| 4417 | return 0; |
| 4418 | } |
| 4419 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4420 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4421 | PyObject *substr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4422 | Py_ssize_t start, |
| 4423 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4424 | int direction) |
| 4425 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4426 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4427 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4428 | str = PyUnicode_FromObject(str); |
| 4429 | if (str == NULL) |
| 4430 | return -1; |
| 4431 | substr = PyUnicode_FromObject(substr); |
| 4432 | if (substr == NULL) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 4433 | Py_DECREF(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4434 | return -1; |
| 4435 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4436 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4437 | result = tailmatch((PyUnicodeObject *)str, |
| 4438 | (PyUnicodeObject *)substr, |
| 4439 | start, end, direction); |
| 4440 | Py_DECREF(str); |
| 4441 | Py_DECREF(substr); |
| 4442 | return result; |
| 4443 | } |
| 4444 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4445 | /* Apply fixfct filter to the Unicode object self and return a |
| 4446 | reference to the modified object */ |
| 4447 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4448 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4449 | PyObject *fixup(PyUnicodeObject *self, |
| 4450 | int (*fixfct)(PyUnicodeObject *s)) |
| 4451 | { |
| 4452 | |
| 4453 | PyUnicodeObject *u; |
| 4454 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4455 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4456 | if (u == NULL) |
| 4457 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4458 | |
| 4459 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 4460 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4461 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4462 | /* fixfct should return TRUE if it modified the buffer. If |
| 4463 | FALSE, return a reference to the original buffer instead |
| 4464 | (to save space, not time) */ |
| 4465 | Py_INCREF(self); |
| 4466 | Py_DECREF(u); |
| 4467 | return (PyObject*) self; |
| 4468 | } |
| 4469 | return (PyObject*) u; |
| 4470 | } |
| 4471 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4472 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4473 | int fixupper(PyUnicodeObject *self) |
| 4474 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4475 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4476 | Py_UNICODE *s = self->str; |
| 4477 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4478 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4479 | while (len-- > 0) { |
| 4480 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4481 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4482 | ch = Py_UNICODE_TOUPPER(*s); |
| 4483 | if (ch != *s) { |
| 4484 | status = 1; |
| 4485 | *s = ch; |
| 4486 | } |
| 4487 | s++; |
| 4488 | } |
| 4489 | |
| 4490 | return status; |
| 4491 | } |
| 4492 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4493 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4494 | int fixlower(PyUnicodeObject *self) |
| 4495 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4496 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4497 | Py_UNICODE *s = self->str; |
| 4498 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4499 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4500 | while (len-- > 0) { |
| 4501 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4502 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4503 | ch = Py_UNICODE_TOLOWER(*s); |
| 4504 | if (ch != *s) { |
| 4505 | status = 1; |
| 4506 | *s = ch; |
| 4507 | } |
| 4508 | s++; |
| 4509 | } |
| 4510 | |
| 4511 | return status; |
| 4512 | } |
| 4513 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4514 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4515 | int fixswapcase(PyUnicodeObject *self) |
| 4516 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4517 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4518 | Py_UNICODE *s = self->str; |
| 4519 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4520 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4521 | while (len-- > 0) { |
| 4522 | if (Py_UNICODE_ISUPPER(*s)) { |
| 4523 | *s = Py_UNICODE_TOLOWER(*s); |
| 4524 | status = 1; |
| 4525 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 4526 | *s = Py_UNICODE_TOUPPER(*s); |
| 4527 | status = 1; |
| 4528 | } |
| 4529 | s++; |
| 4530 | } |
| 4531 | |
| 4532 | return status; |
| 4533 | } |
| 4534 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4535 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4536 | int fixcapitalize(PyUnicodeObject *self) |
| 4537 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4538 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 4539 | Py_UNICODE *s = self->str; |
| 4540 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4541 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 4542 | if (len == 0) |
| 4543 | return 0; |
| 4544 | if (Py_UNICODE_ISLOWER(*s)) { |
| 4545 | *s = Py_UNICODE_TOUPPER(*s); |
| 4546 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4547 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 4548 | s++; |
| 4549 | while (--len > 0) { |
| 4550 | if (Py_UNICODE_ISUPPER(*s)) { |
| 4551 | *s = Py_UNICODE_TOLOWER(*s); |
| 4552 | status = 1; |
| 4553 | } |
| 4554 | s++; |
| 4555 | } |
| 4556 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4557 | } |
| 4558 | |
| 4559 | static |
| 4560 | int fixtitle(PyUnicodeObject *self) |
| 4561 | { |
| 4562 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 4563 | register Py_UNICODE *e; |
| 4564 | int previous_is_cased; |
| 4565 | |
| 4566 | /* Shortcut for single character strings */ |
| 4567 | if (PyUnicode_GET_SIZE(self) == 1) { |
| 4568 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 4569 | if (*p != ch) { |
| 4570 | *p = ch; |
| 4571 | return 1; |
| 4572 | } |
| 4573 | else |
| 4574 | return 0; |
| 4575 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4576 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4577 | e = p + PyUnicode_GET_SIZE(self); |
| 4578 | previous_is_cased = 0; |
| 4579 | for (; p < e; p++) { |
| 4580 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4581 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4582 | if (previous_is_cased) |
| 4583 | *p = Py_UNICODE_TOLOWER(ch); |
| 4584 | else |
| 4585 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4586 | |
| 4587 | if (Py_UNICODE_ISLOWER(ch) || |
| 4588 | Py_UNICODE_ISUPPER(ch) || |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4589 | Py_UNICODE_ISTITLE(ch)) |
| 4590 | previous_is_cased = 1; |
| 4591 | else |
| 4592 | previous_is_cased = 0; |
| 4593 | } |
| 4594 | return 1; |
| 4595 | } |
| 4596 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4597 | PyObject * |
| 4598 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4599 | { |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4600 | PyObject *internal_separator = NULL; |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 4601 | const Py_UNICODE blank = ' '; |
| 4602 | const Py_UNICODE *sep = ␣ |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 4603 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4604 | PyUnicodeObject *res = NULL; /* the result */ |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 4605 | Py_ssize_t res_alloc = 100; /* # allocated bytes for string in res */ |
| 4606 | Py_ssize_t res_used; /* # used bytes */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4607 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 4608 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4609 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4610 | PyObject *item; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4611 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4612 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4613 | fseq = PySequence_Fast(seq, ""); |
| 4614 | if (fseq == NULL) { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4615 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4616 | } |
| 4617 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 4618 | /* Grrrr. A codec may be invoked to convert str objects to |
| 4619 | * Unicode, and so it's possible to call back into Python code |
| 4620 | * during PyUnicode_FromObject(), and so it's possible for a sick |
| 4621 | * codec to change the size of fseq (if seq is a list). Therefore |
| 4622 | * we have to keep refetching the size -- can't assume seqlen |
| 4623 | * is invariant. |
| 4624 | */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4625 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 4626 | /* If empty sequence, return u"". */ |
| 4627 | if (seqlen == 0) { |
| 4628 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 4629 | goto Done; |
| 4630 | } |
| 4631 | /* If singleton sequence with an exact Unicode, return that. */ |
| 4632 | if (seqlen == 1) { |
| 4633 | item = PySequence_Fast_GET_ITEM(fseq, 0); |
| 4634 | if (PyUnicode_CheckExact(item)) { |
| 4635 | Py_INCREF(item); |
| 4636 | res = (PyUnicodeObject *)item; |
| 4637 | goto Done; |
| 4638 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4639 | } |
| 4640 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4641 | /* At least two items to join, or one that isn't exact Unicode. */ |
| 4642 | if (seqlen > 1) { |
| 4643 | /* Set up sep and seplen -- they're needed. */ |
| 4644 | if (separator == NULL) { |
| 4645 | sep = ␣ |
| 4646 | seplen = 1; |
| 4647 | } |
| 4648 | else { |
| 4649 | internal_separator = PyUnicode_FromObject(separator); |
| 4650 | if (internal_separator == NULL) |
| 4651 | goto onError; |
| 4652 | sep = PyUnicode_AS_UNICODE(internal_separator); |
| 4653 | seplen = PyUnicode_GET_SIZE(internal_separator); |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 4654 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 4655 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4656 | } |
| 4657 | } |
| 4658 | |
| 4659 | /* Get space. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4660 | res = _PyUnicode_New(res_alloc); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4661 | if (res == NULL) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4662 | goto onError; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4663 | res_p = PyUnicode_AS_UNICODE(res); |
| 4664 | res_used = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4665 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4666 | for (i = 0; i < seqlen; ++i) { |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 4667 | Py_ssize_t itemlen; |
| 4668 | Py_ssize_t new_res_used; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4669 | |
| 4670 | item = PySequence_Fast_GET_ITEM(fseq, i); |
| 4671 | /* Convert item to Unicode. */ |
| 4672 | if (! PyUnicode_Check(item) && ! PyString_Check(item)) { |
| 4673 | PyErr_Format(PyExc_TypeError, |
Thomas Wouters | 715a4cd | 2006-04-16 22:04:49 +0000 | [diff] [blame] | 4674 | "sequence item %zd: expected string or Unicode," |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4675 | " %.80s found", |
| 4676 | i, item->ob_type->tp_name); |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 4677 | goto onError; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4678 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4679 | item = PyUnicode_FromObject(item); |
| 4680 | if (item == NULL) |
| 4681 | goto onError; |
| 4682 | /* We own a reference to item from here on. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4683 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 4684 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 4685 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 4686 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4687 | /* 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] | 4688 | itemlen = PyUnicode_GET_SIZE(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4689 | new_res_used = res_used + itemlen; |
Georg Brandl | 90e27d3 | 2006-06-10 06:40:50 +0000 | [diff] [blame] | 4690 | if (new_res_used < 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4691 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4692 | if (i < seqlen - 1) { |
| 4693 | new_res_used += seplen; |
Georg Brandl | 90e27d3 | 2006-06-10 06:40:50 +0000 | [diff] [blame] | 4694 | if (new_res_used < 0) |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4695 | goto Overflow; |
| 4696 | } |
| 4697 | if (new_res_used > res_alloc) { |
| 4698 | /* double allocated size until it's big enough */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4699 | do { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4700 | res_alloc += res_alloc; |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 4701 | if (res_alloc <= 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4702 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4703 | } while (new_res_used > res_alloc); |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4704 | if (_PyUnicode_Resize(&res, res_alloc) < 0) { |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 4705 | Py_DECREF(item); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4706 | goto onError; |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 4707 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4708 | res_p = PyUnicode_AS_UNICODE(res) + res_used; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4709 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4710 | |
| 4711 | /* Copy item, and maybe the separator. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4712 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4713 | res_p += itemlen; |
| 4714 | if (i < seqlen - 1) { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4715 | Py_UNICODE_COPY(res_p, sep, seplen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4716 | res_p += seplen; |
| 4717 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4718 | Py_DECREF(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4719 | res_used = new_res_used; |
| 4720 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4721 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4722 | /* Shrink res to match the used area; this probably can't fail, |
| 4723 | * but it's cheap to check. |
| 4724 | */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4725 | if (_PyUnicode_Resize(&res, res_used) < 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4726 | goto onError; |
| 4727 | |
| 4728 | Done: |
| 4729 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4730 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4731 | return (PyObject *)res; |
| 4732 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4733 | Overflow: |
| 4734 | PyErr_SetString(PyExc_OverflowError, |
Georg Brandl | 90e27d3 | 2006-06-10 06:40:50 +0000 | [diff] [blame] | 4735 | "join() result is too long for a Python string"); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4736 | Py_DECREF(item); |
| 4737 | /* fall through */ |
| 4738 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4739 | onError: |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4740 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 4741 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 4742 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4743 | return NULL; |
| 4744 | } |
| 4745 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4746 | static |
| 4747 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4748 | Py_ssize_t left, |
| 4749 | Py_ssize_t right, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4750 | Py_UNICODE fill) |
| 4751 | { |
| 4752 | PyUnicodeObject *u; |
| 4753 | |
| 4754 | if (left < 0) |
| 4755 | left = 0; |
| 4756 | if (right < 0) |
| 4757 | right = 0; |
| 4758 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 4759 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4760 | Py_INCREF(self); |
| 4761 | return self; |
| 4762 | } |
| 4763 | |
| 4764 | u = _PyUnicode_New(left + self->length + right); |
| 4765 | if (u) { |
| 4766 | if (left) |
| 4767 | Py_UNICODE_FILL(u->str, fill, left); |
| 4768 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 4769 | if (right) |
| 4770 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 4771 | } |
| 4772 | |
| 4773 | return u; |
| 4774 | } |
| 4775 | |
| 4776 | #define SPLIT_APPEND(data, left, right) \ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4777 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4778 | if (!str) \ |
| 4779 | goto onError; \ |
| 4780 | if (PyList_Append(list, str)) { \ |
| 4781 | Py_DECREF(str); \ |
| 4782 | goto onError; \ |
| 4783 | } \ |
| 4784 | else \ |
| 4785 | Py_DECREF(str); |
| 4786 | |
| 4787 | static |
| 4788 | PyObject *split_whitespace(PyUnicodeObject *self, |
| 4789 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4790 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4791 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4792 | register Py_ssize_t i; |
| 4793 | register Py_ssize_t j; |
| 4794 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4795 | PyObject *str; |
| 4796 | |
| 4797 | for (i = j = 0; i < len; ) { |
| 4798 | /* find a token */ |
| 4799 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 4800 | i++; |
| 4801 | j = i; |
| 4802 | while (i < len && !Py_UNICODE_ISSPACE(self->str[i])) |
| 4803 | i++; |
| 4804 | if (j < i) { |
| 4805 | if (maxcount-- <= 0) |
| 4806 | break; |
| 4807 | SPLIT_APPEND(self->str, j, i); |
| 4808 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 4809 | i++; |
| 4810 | j = i; |
| 4811 | } |
| 4812 | } |
| 4813 | if (j < len) { |
| 4814 | SPLIT_APPEND(self->str, j, len); |
| 4815 | } |
| 4816 | return list; |
| 4817 | |
| 4818 | onError: |
| 4819 | Py_DECREF(list); |
| 4820 | return NULL; |
| 4821 | } |
| 4822 | |
| 4823 | PyObject *PyUnicode_Splitlines(PyObject *string, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4824 | int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4825 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4826 | register Py_ssize_t i; |
| 4827 | register Py_ssize_t j; |
| 4828 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4829 | PyObject *list; |
| 4830 | PyObject *str; |
| 4831 | Py_UNICODE *data; |
| 4832 | |
| 4833 | string = PyUnicode_FromObject(string); |
| 4834 | if (string == NULL) |
| 4835 | return NULL; |
| 4836 | data = PyUnicode_AS_UNICODE(string); |
| 4837 | len = PyUnicode_GET_SIZE(string); |
| 4838 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4839 | list = PyList_New(0); |
| 4840 | if (!list) |
| 4841 | goto onError; |
| 4842 | |
| 4843 | for (i = j = 0; i < len; ) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4844 | Py_ssize_t eol; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4845 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4846 | /* Find a line and append it */ |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 4847 | while (i < len && !BLOOM_LINEBREAK(data[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4848 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4849 | |
| 4850 | /* Skip the line break reading CRLF as one line break */ |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4851 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4852 | if (i < len) { |
| 4853 | if (data[i] == '\r' && i + 1 < len && |
| 4854 | data[i+1] == '\n') |
| 4855 | i += 2; |
| 4856 | else |
| 4857 | i++; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4858 | if (keepends) |
| 4859 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4860 | } |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 4861 | SPLIT_APPEND(data, j, eol); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4862 | j = i; |
| 4863 | } |
| 4864 | if (j < len) { |
| 4865 | SPLIT_APPEND(data, j, len); |
| 4866 | } |
| 4867 | |
| 4868 | Py_DECREF(string); |
| 4869 | return list; |
| 4870 | |
| 4871 | onError: |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 4872 | Py_XDECREF(list); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4873 | Py_DECREF(string); |
| 4874 | return NULL; |
| 4875 | } |
| 4876 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4877 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4878 | PyObject *split_char(PyUnicodeObject *self, |
| 4879 | PyObject *list, |
| 4880 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4881 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4882 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4883 | register Py_ssize_t i; |
| 4884 | register Py_ssize_t j; |
| 4885 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4886 | PyObject *str; |
| 4887 | |
| 4888 | for (i = j = 0; i < len; ) { |
| 4889 | if (self->str[i] == ch) { |
| 4890 | if (maxcount-- <= 0) |
| 4891 | break; |
| 4892 | SPLIT_APPEND(self->str, j, i); |
| 4893 | i = j = i + 1; |
| 4894 | } else |
| 4895 | i++; |
| 4896 | } |
| 4897 | if (j <= len) { |
| 4898 | SPLIT_APPEND(self->str, j, len); |
| 4899 | } |
| 4900 | return list; |
| 4901 | |
| 4902 | onError: |
| 4903 | Py_DECREF(list); |
| 4904 | return NULL; |
| 4905 | } |
| 4906 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4907 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4908 | PyObject *split_substring(PyUnicodeObject *self, |
| 4909 | PyObject *list, |
| 4910 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4911 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4912 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4913 | register Py_ssize_t i; |
| 4914 | register Py_ssize_t j; |
| 4915 | Py_ssize_t len = self->length; |
| 4916 | Py_ssize_t sublen = substring->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4917 | PyObject *str; |
| 4918 | |
Guido van Rossum | cda4f9a | 2000-12-19 02:23:19 +0000 | [diff] [blame] | 4919 | for (i = j = 0; i <= len - sublen; ) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4920 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 4921 | if (maxcount-- <= 0) |
| 4922 | break; |
| 4923 | SPLIT_APPEND(self->str, j, i); |
| 4924 | i = j = i + sublen; |
| 4925 | } else |
| 4926 | i++; |
| 4927 | } |
| 4928 | if (j <= len) { |
| 4929 | SPLIT_APPEND(self->str, j, len); |
| 4930 | } |
| 4931 | return list; |
| 4932 | |
| 4933 | onError: |
| 4934 | Py_DECREF(list); |
| 4935 | return NULL; |
| 4936 | } |
| 4937 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4938 | static |
| 4939 | PyObject *rsplit_whitespace(PyUnicodeObject *self, |
| 4940 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4941 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4942 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4943 | register Py_ssize_t i; |
| 4944 | register Py_ssize_t j; |
| 4945 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4946 | PyObject *str; |
| 4947 | |
| 4948 | for (i = j = len - 1; i >= 0; ) { |
| 4949 | /* find a token */ |
| 4950 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 4951 | i--; |
| 4952 | j = i; |
| 4953 | while (i >= 0 && !Py_UNICODE_ISSPACE(self->str[i])) |
| 4954 | i--; |
| 4955 | if (j > i) { |
| 4956 | if (maxcount-- <= 0) |
| 4957 | break; |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 4958 | SPLIT_APPEND(self->str, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4959 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 4960 | i--; |
| 4961 | j = i; |
| 4962 | } |
| 4963 | } |
| 4964 | if (j >= 0) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 4965 | SPLIT_APPEND(self->str, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4966 | } |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 4967 | if (PyList_Reverse(list) < 0) |
| 4968 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4969 | return list; |
| 4970 | |
| 4971 | onError: |
| 4972 | Py_DECREF(list); |
| 4973 | return NULL; |
| 4974 | } |
| 4975 | |
| 4976 | static |
| 4977 | PyObject *rsplit_char(PyUnicodeObject *self, |
| 4978 | PyObject *list, |
| 4979 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4980 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4981 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4982 | register Py_ssize_t i; |
| 4983 | register Py_ssize_t j; |
| 4984 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4985 | PyObject *str; |
| 4986 | |
| 4987 | for (i = j = len - 1; i >= 0; ) { |
| 4988 | if (self->str[i] == ch) { |
| 4989 | if (maxcount-- <= 0) |
| 4990 | break; |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 4991 | SPLIT_APPEND(self->str, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4992 | j = i = i - 1; |
| 4993 | } else |
| 4994 | i--; |
| 4995 | } |
Hye-Shik Chang | 7fc4cf5 | 2003-12-23 09:10:16 +0000 | [diff] [blame] | 4996 | if (j >= -1) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 4997 | SPLIT_APPEND(self->str, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 4998 | } |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 4999 | if (PyList_Reverse(list) < 0) |
| 5000 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5001 | return list; |
| 5002 | |
| 5003 | onError: |
| 5004 | Py_DECREF(list); |
| 5005 | return NULL; |
| 5006 | } |
| 5007 | |
| 5008 | static |
| 5009 | PyObject *rsplit_substring(PyUnicodeObject *self, |
| 5010 | PyObject *list, |
| 5011 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5012 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5013 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5014 | register Py_ssize_t i; |
| 5015 | register Py_ssize_t j; |
| 5016 | Py_ssize_t len = self->length; |
| 5017 | Py_ssize_t sublen = substring->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5018 | PyObject *str; |
| 5019 | |
| 5020 | for (i = len - sublen, j = len; i >= 0; ) { |
| 5021 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 5022 | if (maxcount-- <= 0) |
| 5023 | break; |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5024 | SPLIT_APPEND(self->str, i + sublen, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5025 | j = i; |
| 5026 | i -= sublen; |
| 5027 | } else |
| 5028 | i--; |
| 5029 | } |
| 5030 | if (j >= 0) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5031 | SPLIT_APPEND(self->str, 0, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5032 | } |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5033 | if (PyList_Reverse(list) < 0) |
| 5034 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5035 | return list; |
| 5036 | |
| 5037 | onError: |
| 5038 | Py_DECREF(list); |
| 5039 | return NULL; |
| 5040 | } |
| 5041 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5042 | #undef SPLIT_APPEND |
| 5043 | |
| 5044 | static |
| 5045 | PyObject *split(PyUnicodeObject *self, |
| 5046 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5047 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5048 | { |
| 5049 | PyObject *list; |
| 5050 | |
| 5051 | if (maxcount < 0) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5052 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5053 | |
| 5054 | list = PyList_New(0); |
| 5055 | if (!list) |
| 5056 | return NULL; |
| 5057 | |
| 5058 | if (substring == NULL) |
| 5059 | return split_whitespace(self,list,maxcount); |
| 5060 | |
| 5061 | else if (substring->length == 1) |
| 5062 | return split_char(self,list,substring->str[0],maxcount); |
| 5063 | |
| 5064 | else if (substring->length == 0) { |
| 5065 | Py_DECREF(list); |
| 5066 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 5067 | return NULL; |
| 5068 | } |
| 5069 | else |
| 5070 | return split_substring(self,list,substring,maxcount); |
| 5071 | } |
| 5072 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5073 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5074 | PyObject *rsplit(PyUnicodeObject *self, |
| 5075 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5076 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5077 | { |
| 5078 | PyObject *list; |
| 5079 | |
| 5080 | if (maxcount < 0) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5081 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5082 | |
| 5083 | list = PyList_New(0); |
| 5084 | if (!list) |
| 5085 | return NULL; |
| 5086 | |
| 5087 | if (substring == NULL) |
| 5088 | return rsplit_whitespace(self,list,maxcount); |
| 5089 | |
| 5090 | else if (substring->length == 1) |
| 5091 | return rsplit_char(self,list,substring->str[0],maxcount); |
| 5092 | |
| 5093 | else if (substring->length == 0) { |
| 5094 | Py_DECREF(list); |
| 5095 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 5096 | return NULL; |
| 5097 | } |
| 5098 | else |
| 5099 | return rsplit_substring(self,list,substring,maxcount); |
| 5100 | } |
| 5101 | |
| 5102 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5103 | PyObject *replace(PyUnicodeObject *self, |
| 5104 | PyUnicodeObject *str1, |
| 5105 | PyUnicodeObject *str2, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5106 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5107 | { |
| 5108 | PyUnicodeObject *u; |
| 5109 | |
| 5110 | if (maxcount < 0) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5111 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5112 | |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5113 | if (str1->length == str2->length) { |
| 5114 | /* same length */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5115 | Py_ssize_t i; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5116 | if (str1->length == 1) { |
| 5117 | /* replace characters */ |
| 5118 | Py_UNICODE u1, u2; |
| 5119 | if (!findchar(self->str, self->length, str1->str[0])) |
| 5120 | goto nothing; |
| 5121 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 5122 | if (!u) |
| 5123 | return NULL; |
| 5124 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5125 | u1 = str1->str[0]; |
| 5126 | u2 = str2->str[0]; |
| 5127 | for (i = 0; i < u->length; i++) |
| 5128 | if (u->str[i] == u1) { |
| 5129 | if (--maxcount < 0) |
| 5130 | break; |
| 5131 | u->str[i] = u2; |
| 5132 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5133 | } else { |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5134 | i = fastsearch( |
| 5135 | self->str, self->length, str1->str, str1->length, FAST_SEARCH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5136 | ); |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5137 | if (i < 0) |
| 5138 | goto nothing; |
| 5139 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 5140 | if (!u) |
| 5141 | return NULL; |
| 5142 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5143 | while (i <= self->length - str1->length) |
| 5144 | if (Py_UNICODE_MATCH(self, i, str1)) { |
| 5145 | if (--maxcount < 0) |
| 5146 | break; |
| 5147 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 5148 | i += str1->length; |
| 5149 | } else |
| 5150 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5151 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5152 | } else { |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5153 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 5154 | Py_ssize_t n, i, j, e; |
Fredrik Lundh | 0c71f88 | 2006-05-25 16:46:54 +0000 | [diff] [blame] | 5155 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5156 | Py_UNICODE *p; |
| 5157 | |
| 5158 | /* replace strings */ |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5159 | n = stringlib_count(self->str, self->length, str1->str, str1->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5160 | if (n > maxcount) |
| 5161 | n = maxcount; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5162 | if (n == 0) |
| 5163 | goto nothing; |
Fredrik Lundh | 0c71f88 | 2006-05-25 16:46:54 +0000 | [diff] [blame] | 5164 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 5165 | delta = (str2->length - str1->length); |
| 5166 | if (delta == 0) { |
| 5167 | new_size = self->length; |
| 5168 | } else { |
| 5169 | product = n * (str2->length - str1->length); |
| 5170 | if ((product / (str2->length - str1->length)) != n) { |
| 5171 | PyErr_SetString(PyExc_OverflowError, |
| 5172 | "replace string is too long"); |
| 5173 | return NULL; |
| 5174 | } |
| 5175 | new_size = self->length + product; |
| 5176 | if (new_size < 0) { |
| 5177 | PyErr_SetString(PyExc_OverflowError, |
| 5178 | "replace string is too long"); |
| 5179 | return NULL; |
| 5180 | } |
| 5181 | } |
| 5182 | u = _PyUnicode_New(new_size); |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5183 | if (!u) |
| 5184 | return NULL; |
| 5185 | i = 0; |
| 5186 | p = u->str; |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 5187 | e = self->length - str1->length; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5188 | if (str1->length > 0) { |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 5189 | while (n-- > 0) { |
| 5190 | /* look for next match */ |
| 5191 | j = i; |
| 5192 | while (j <= e) { |
| 5193 | if (Py_UNICODE_MATCH(self, j, str1)) |
| 5194 | break; |
| 5195 | j++; |
| 5196 | } |
| 5197 | if (j > i) { |
| 5198 | if (j > e) |
| 5199 | break; |
| 5200 | /* copy unchanged part [i:j] */ |
| 5201 | Py_UNICODE_COPY(p, self->str+i, j-i); |
| 5202 | p += j - i; |
| 5203 | } |
| 5204 | /* copy substitution string */ |
| 5205 | if (str2->length > 0) { |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5206 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 5207 | p += str2->length; |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 5208 | } |
| 5209 | i = j + str1->length; |
| 5210 | } |
| 5211 | if (i < self->length) |
| 5212 | /* copy tail [i:] */ |
| 5213 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5214 | } else { |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 5215 | /* interleave */ |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5216 | while (n > 0) { |
| 5217 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 5218 | p += str2->length; |
| 5219 | if (--n <= 0) |
| 5220 | break; |
| 5221 | *p++ = self->str[i++]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5222 | } |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5223 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5224 | } |
| 5225 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5226 | return (PyObject *) u; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5227 | |
| 5228 | nothing: |
| 5229 | /* nothing to replace; return original string (when possible) */ |
| 5230 | if (PyUnicode_CheckExact(self)) { |
| 5231 | Py_INCREF(self); |
| 5232 | return (PyObject *) self; |
| 5233 | } |
| 5234 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5235 | } |
| 5236 | |
| 5237 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 5238 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5239 | PyDoc_STRVAR(title__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5240 | "S.title() -> unicode\n\ |
| 5241 | \n\ |
| 5242 | 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] | 5243 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5244 | |
| 5245 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5246 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5247 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5248 | return fixup(self, fixtitle); |
| 5249 | } |
| 5250 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5251 | PyDoc_STRVAR(capitalize__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5252 | "S.capitalize() -> unicode\n\ |
| 5253 | \n\ |
| 5254 | 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] | 5255 | have upper case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5256 | |
| 5257 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5258 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5259 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5260 | return fixup(self, fixcapitalize); |
| 5261 | } |
| 5262 | |
| 5263 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5264 | PyDoc_STRVAR(capwords__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5265 | "S.capwords() -> unicode\n\ |
| 5266 | \n\ |
| 5267 | 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] | 5268 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5269 | |
| 5270 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5271 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5272 | { |
| 5273 | PyObject *list; |
| 5274 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5275 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5276 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5277 | /* Split into words */ |
| 5278 | list = split(self, NULL, -1); |
| 5279 | if (!list) |
| 5280 | return NULL; |
| 5281 | |
| 5282 | /* Capitalize each word */ |
| 5283 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 5284 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
| 5285 | fixcapitalize); |
| 5286 | if (item == NULL) |
| 5287 | goto onError; |
| 5288 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 5289 | PyList_SET_ITEM(list, i, item); |
| 5290 | } |
| 5291 | |
| 5292 | /* Join the words to form a new string */ |
| 5293 | item = PyUnicode_Join(NULL, list); |
| 5294 | |
| 5295 | onError: |
| 5296 | Py_DECREF(list); |
| 5297 | return (PyObject *)item; |
| 5298 | } |
| 5299 | #endif |
| 5300 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5301 | /* Argument converter. Coerces to a single unicode character */ |
| 5302 | |
| 5303 | static int |
| 5304 | convert_uc(PyObject *obj, void *addr) |
| 5305 | { |
| 5306 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 5307 | PyObject *uniobj; |
| 5308 | Py_UNICODE *unistr; |
| 5309 | |
| 5310 | uniobj = PyUnicode_FromObject(obj); |
| 5311 | if (uniobj == NULL) { |
| 5312 | PyErr_SetString(PyExc_TypeError, |
| 5313 | "The fill character cannot be converted to Unicode"); |
| 5314 | return 0; |
| 5315 | } |
| 5316 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 5317 | PyErr_SetString(PyExc_TypeError, |
| 5318 | "The fill character must be exactly one character long"); |
| 5319 | Py_DECREF(uniobj); |
| 5320 | return 0; |
| 5321 | } |
| 5322 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 5323 | *fillcharloc = unistr[0]; |
| 5324 | Py_DECREF(uniobj); |
| 5325 | return 1; |
| 5326 | } |
| 5327 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5328 | PyDoc_STRVAR(center__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5329 | "S.center(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5330 | \n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5331 | Return S centered in a Unicode string of length width. Padding is\n\ |
| 5332 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5333 | |
| 5334 | static PyObject * |
| 5335 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 5336 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5337 | Py_ssize_t marg, left; |
| 5338 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5339 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5340 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 5341 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5342 | return NULL; |
| 5343 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5344 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5345 | Py_INCREF(self); |
| 5346 | return (PyObject*) self; |
| 5347 | } |
| 5348 | |
| 5349 | marg = width - self->length; |
| 5350 | left = marg / 2 + (marg & width & 1); |
| 5351 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 5352 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5353 | } |
| 5354 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5355 | #if 0 |
| 5356 | |
| 5357 | /* This code should go into some future Unicode collation support |
| 5358 | module. The basic comparison should compare ordinals on a naive |
Trent Mick | 20abf57 | 2000-08-12 22:14:34 +0000 | [diff] [blame] | 5359 | basis (this is what Java does and thus JPython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5360 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5361 | /* speedy UTF-16 code point order comparison */ |
| 5362 | /* gleaned from: */ |
| 5363 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 5364 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 5365 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5366 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5367 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5368 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 5369 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 5370 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5371 | }; |
| 5372 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5373 | static int |
| 5374 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 5375 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5376 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5377 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5378 | Py_UNICODE *s1 = str1->str; |
| 5379 | Py_UNICODE *s2 = str2->str; |
| 5380 | |
| 5381 | len1 = str1->length; |
| 5382 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5383 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5384 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5385 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5386 | |
| 5387 | c1 = *s1++; |
| 5388 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 5389 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5390 | if (c1 > (1<<11) * 26) |
| 5391 | c1 += utf16Fixup[c1>>11]; |
| 5392 | if (c2 > (1<<11) * 26) |
| 5393 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5394 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 5395 | |
| 5396 | if (c1 != c2) |
| 5397 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5398 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 5399 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5400 | } |
| 5401 | |
| 5402 | return (len1 < len2) ? -1 : (len1 != len2); |
| 5403 | } |
| 5404 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5405 | #else |
| 5406 | |
| 5407 | static int |
| 5408 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 5409 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5410 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5411 | |
| 5412 | Py_UNICODE *s1 = str1->str; |
| 5413 | Py_UNICODE *s2 = str2->str; |
| 5414 | |
| 5415 | len1 = str1->length; |
| 5416 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5417 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5418 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5419 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5420 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 5421 | c1 = *s1++; |
| 5422 | c2 = *s2++; |
| 5423 | |
| 5424 | if (c1 != c2) |
| 5425 | return (c1 < c2) ? -1 : 1; |
| 5426 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 5427 | len1--; len2--; |
| 5428 | } |
| 5429 | |
| 5430 | return (len1 < len2) ? -1 : (len1 != len2); |
| 5431 | } |
| 5432 | |
| 5433 | #endif |
| 5434 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5435 | int PyUnicode_Compare(PyObject *left, |
| 5436 | PyObject *right) |
| 5437 | { |
| 5438 | PyUnicodeObject *u = NULL, *v = NULL; |
| 5439 | int result; |
| 5440 | |
| 5441 | /* Coerce the two arguments */ |
| 5442 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 5443 | if (u == NULL) |
| 5444 | goto onError; |
| 5445 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 5446 | if (v == NULL) |
| 5447 | goto onError; |
| 5448 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5449 | /* Shortcut for empty or interned objects */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5450 | if (v == u) { |
| 5451 | Py_DECREF(u); |
| 5452 | Py_DECREF(v); |
| 5453 | return 0; |
| 5454 | } |
| 5455 | |
| 5456 | result = unicode_compare(u, v); |
| 5457 | |
| 5458 | Py_DECREF(u); |
| 5459 | Py_DECREF(v); |
| 5460 | return result; |
| 5461 | |
| 5462 | onError: |
| 5463 | Py_XDECREF(u); |
| 5464 | Py_XDECREF(v); |
| 5465 | return -1; |
| 5466 | } |
| 5467 | |
Marc-André Lemburg | 040f76b | 2006-08-14 10:55:19 +0000 | [diff] [blame] | 5468 | PyObject *PyUnicode_RichCompare(PyObject *left, |
| 5469 | PyObject *right, |
| 5470 | int op) |
| 5471 | { |
| 5472 | int result; |
| 5473 | |
| 5474 | result = PyUnicode_Compare(left, right); |
| 5475 | if (result == -1 && PyErr_Occurred()) |
| 5476 | goto onError; |
| 5477 | |
| 5478 | /* Convert the return value to a Boolean */ |
| 5479 | switch (op) { |
| 5480 | case Py_EQ: |
| 5481 | result = (result == 0); |
| 5482 | break; |
| 5483 | case Py_NE: |
| 5484 | result = (result != 0); |
| 5485 | break; |
| 5486 | case Py_LE: |
| 5487 | result = (result <= 0); |
| 5488 | break; |
| 5489 | case Py_GE: |
| 5490 | result = (result >= 0); |
| 5491 | break; |
| 5492 | case Py_LT: |
| 5493 | result = (result == -1); |
| 5494 | break; |
| 5495 | case Py_GT: |
| 5496 | result = (result == 1); |
| 5497 | break; |
| 5498 | } |
| 5499 | return PyBool_FromLong(result); |
| 5500 | |
| 5501 | onError: |
| 5502 | |
| 5503 | /* Standard case |
| 5504 | |
| 5505 | Type errors mean that PyUnicode_FromObject() could not convert |
| 5506 | one of the arguments (usually the right hand side) to Unicode, |
| 5507 | ie. we can't handle the comparison request. However, it is |
| 5508 | possible that the other object knows a comparison method, which |
| 5509 | is why we return Py_NotImplemented to give the other object a |
| 5510 | chance. |
| 5511 | |
| 5512 | */ |
| 5513 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 5514 | PyErr_Clear(); |
| 5515 | Py_INCREF(Py_NotImplemented); |
| 5516 | return Py_NotImplemented; |
| 5517 | } |
| 5518 | if (op != Py_EQ && op != Py_NE) |
| 5519 | return NULL; |
| 5520 | |
| 5521 | /* Equality comparison. |
| 5522 | |
| 5523 | This is a special case: we silence any PyExc_UnicodeDecodeError |
| 5524 | and instead turn it into a PyErr_UnicodeWarning. |
| 5525 | |
| 5526 | */ |
| 5527 | if (!PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) |
| 5528 | return NULL; |
| 5529 | PyErr_Clear(); |
| 5530 | if (PyErr_Warn(PyExc_UnicodeWarning, |
| 5531 | (op == Py_EQ) ? |
| 5532 | "Unicode equal comparison " |
| 5533 | "failed to convert both arguments to Unicode - " |
| 5534 | "interpreting them as being unequal" : |
| 5535 | "Unicode unequal comparison " |
| 5536 | "failed to convert both arguments to Unicode - " |
| 5537 | "interpreting them as being unequal" |
| 5538 | ) < 0) |
| 5539 | return NULL; |
| 5540 | result = (op == Py_NE); |
| 5541 | return PyBool_FromLong(result); |
| 5542 | } |
| 5543 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5544 | int PyUnicode_Contains(PyObject *container, |
| 5545 | PyObject *element) |
| 5546 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5547 | PyObject *str, *sub; |
| 5548 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5549 | |
| 5550 | /* Coerce the two arguments */ |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5551 | sub = PyUnicode_FromObject(element); |
| 5552 | if (!sub) { |
Marc-André Lemburg | 7c01468 | 2000-06-28 08:11:47 +0000 | [diff] [blame] | 5553 | PyErr_SetString(PyExc_TypeError, |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 5554 | "'in <string>' requires string as left operand"); |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 5555 | return -1; |
Marc-André Lemburg | 7c01468 | 2000-06-28 08:11:47 +0000 | [diff] [blame] | 5556 | } |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 5557 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5558 | str = PyUnicode_FromObject(container); |
| 5559 | if (!str) { |
| 5560 | Py_DECREF(sub); |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 5561 | return -1; |
| 5562 | } |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5563 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5564 | result = stringlib_contains_obj(str, sub); |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 5565 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5566 | Py_DECREF(str); |
| 5567 | Py_DECREF(sub); |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5568 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5569 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 5570 | } |
| 5571 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5572 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 5573 | |
| 5574 | PyObject *PyUnicode_Concat(PyObject *left, |
| 5575 | PyObject *right) |
| 5576 | { |
| 5577 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 5578 | |
| 5579 | /* Coerce the two arguments */ |
| 5580 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 5581 | if (u == NULL) |
| 5582 | goto onError; |
| 5583 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 5584 | if (v == NULL) |
| 5585 | goto onError; |
| 5586 | |
| 5587 | /* Shortcuts */ |
| 5588 | if (v == unicode_empty) { |
| 5589 | Py_DECREF(v); |
| 5590 | return (PyObject *)u; |
| 5591 | } |
| 5592 | if (u == unicode_empty) { |
| 5593 | Py_DECREF(u); |
| 5594 | return (PyObject *)v; |
| 5595 | } |
| 5596 | |
| 5597 | /* Concat the two Unicode strings */ |
| 5598 | w = _PyUnicode_New(u->length + v->length); |
| 5599 | if (w == NULL) |
| 5600 | goto onError; |
| 5601 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 5602 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 5603 | |
| 5604 | Py_DECREF(u); |
| 5605 | Py_DECREF(v); |
| 5606 | return (PyObject *)w; |
| 5607 | |
| 5608 | onError: |
| 5609 | Py_XDECREF(u); |
| 5610 | Py_XDECREF(v); |
| 5611 | return NULL; |
| 5612 | } |
| 5613 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5614 | PyDoc_STRVAR(count__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5615 | "S.count(sub[, start[, end]]) -> int\n\ |
| 5616 | \n\ |
Fredrik Lundh | 763b50f | 2006-05-22 15:35:12 +0000 | [diff] [blame] | 5617 | Return the number of non-overlapping occurrences of substring sub in\n\ |
| 5618 | 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] | 5619 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5620 | |
| 5621 | static PyObject * |
| 5622 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 5623 | { |
| 5624 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5625 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5626 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5627 | PyObject *result; |
| 5628 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 5629 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
| 5630 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5631 | return NULL; |
| 5632 | |
| 5633 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5634 | (PyObject *)substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5635 | if (substring == NULL) |
| 5636 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5637 | |
Fredrik Lundh | c816281 | 2006-05-26 19:33:03 +0000 | [diff] [blame] | 5638 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5639 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5640 | result = PyInt_FromSsize_t( |
| 5641 | stringlib_count(self->str + start, end - start, |
| 5642 | substring->str, substring->length) |
| 5643 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5644 | |
| 5645 | Py_DECREF(substring); |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5646 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5647 | return result; |
| 5648 | } |
| 5649 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5650 | PyDoc_STRVAR(encode__doc__, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5651 | "S.encode([encoding[,errors]]) -> string or unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5652 | \n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5653 | Encodes S using the codec registered for encoding. encoding defaults\n\ |
| 5654 | 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] | 5655 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5656 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 5657 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 5658 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5659 | |
| 5660 | static PyObject * |
| 5661 | unicode_encode(PyUnicodeObject *self, PyObject *args) |
| 5662 | { |
| 5663 | char *encoding = NULL; |
| 5664 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5665 | PyObject *v; |
| 5666 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5667 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 5668 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5669 | v = PyUnicode_AsEncodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5670 | if (v == NULL) |
| 5671 | goto onError; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5672 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
| 5673 | PyErr_Format(PyExc_TypeError, |
| 5674 | "encoder did not return a string/unicode object " |
| 5675 | "(type=%.400s)", |
| 5676 | v->ob_type->tp_name); |
| 5677 | Py_DECREF(v); |
| 5678 | return NULL; |
| 5679 | } |
| 5680 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5681 | |
| 5682 | onError: |
| 5683 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5684 | } |
| 5685 | |
| 5686 | PyDoc_STRVAR(decode__doc__, |
| 5687 | "S.decode([encoding[,errors]]) -> string or unicode\n\ |
| 5688 | \n\ |
| 5689 | Decodes S using the codec registered for encoding. encoding defaults\n\ |
| 5690 | to the default encoding. errors may be given to set a different error\n\ |
| 5691 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
| 5692 | a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ |
| 5693 | as well as any other name registerd with codecs.register_error that is\n\ |
| 5694 | able to handle UnicodeDecodeErrors."); |
| 5695 | |
| 5696 | static PyObject * |
Marc-André Lemburg | 126b44c | 2004-07-10 12:04:20 +0000 | [diff] [blame] | 5697 | unicode_decode(PyUnicodeObject *self, PyObject *args) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5698 | { |
| 5699 | char *encoding = NULL; |
| 5700 | char *errors = NULL; |
| 5701 | PyObject *v; |
| 5702 | |
| 5703 | if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors)) |
| 5704 | return NULL; |
| 5705 | v = PyUnicode_AsDecodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5706 | if (v == NULL) |
| 5707 | goto onError; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 5708 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
| 5709 | PyErr_Format(PyExc_TypeError, |
| 5710 | "decoder did not return a string/unicode object " |
| 5711 | "(type=%.400s)", |
| 5712 | v->ob_type->tp_name); |
| 5713 | Py_DECREF(v); |
| 5714 | return NULL; |
| 5715 | } |
| 5716 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 5717 | |
| 5718 | onError: |
| 5719 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5720 | } |
| 5721 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5722 | PyDoc_STRVAR(expandtabs__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5723 | "S.expandtabs([tabsize]) -> unicode\n\ |
| 5724 | \n\ |
| 5725 | 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] | 5726 | 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] | 5727 | |
| 5728 | static PyObject* |
| 5729 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 5730 | { |
| 5731 | Py_UNICODE *e; |
| 5732 | Py_UNICODE *p; |
| 5733 | Py_UNICODE *q; |
Guido van Rossum | 44a93e5 | 2008-03-11 21:14:54 +0000 | [diff] [blame] | 5734 | Py_UNICODE *qe; |
| 5735 | Py_ssize_t i, j, incr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5736 | PyUnicodeObject *u; |
| 5737 | int tabsize = 8; |
| 5738 | |
| 5739 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
| 5740 | return NULL; |
| 5741 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 5742 | /* First pass: determine size of output string */ |
Guido van Rossum | 44a93e5 | 2008-03-11 21:14:54 +0000 | [diff] [blame] | 5743 | i = 0; /* chars up to and including most recent \n or \r */ |
| 5744 | j = 0; /* chars since most recent \n or \r (use in tab calculations) */ |
| 5745 | e = self->str + self->length; /* end of input */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5746 | for (p = self->str; p < e; p++) |
| 5747 | if (*p == '\t') { |
Neal Norwitz | 66e64e2 | 2007-06-09 04:06:30 +0000 | [diff] [blame] | 5748 | if (tabsize > 0) { |
Guido van Rossum | 44a93e5 | 2008-03-11 21:14:54 +0000 | [diff] [blame] | 5749 | incr = tabsize - (j % tabsize); /* cannot overflow */ |
| 5750 | if (j > PY_SSIZE_T_MAX - incr) |
| 5751 | goto overflow1; |
| 5752 | j += incr; |
| 5753 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5754 | } |
| 5755 | else { |
Guido van Rossum | 44a93e5 | 2008-03-11 21:14:54 +0000 | [diff] [blame] | 5756 | if (j > PY_SSIZE_T_MAX - 1) |
| 5757 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5758 | j++; |
| 5759 | if (*p == '\n' || *p == '\r') { |
Guido van Rossum | 44a93e5 | 2008-03-11 21:14:54 +0000 | [diff] [blame] | 5760 | if (i > PY_SSIZE_T_MAX - j) |
| 5761 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5762 | i += j; |
Guido van Rossum | 44a93e5 | 2008-03-11 21:14:54 +0000 | [diff] [blame] | 5763 | j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5764 | } |
| 5765 | } |
| 5766 | |
Guido van Rossum | 44a93e5 | 2008-03-11 21:14:54 +0000 | [diff] [blame] | 5767 | if (i > PY_SSIZE_T_MAX - j) |
| 5768 | goto overflow1; |
Neal Norwitz | 66e64e2 | 2007-06-09 04:06:30 +0000 | [diff] [blame] | 5769 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5770 | /* Second pass: create output string and fill it */ |
| 5771 | u = _PyUnicode_New(i + j); |
| 5772 | if (!u) |
| 5773 | return NULL; |
| 5774 | |
Guido van Rossum | 44a93e5 | 2008-03-11 21:14:54 +0000 | [diff] [blame] | 5775 | j = 0; /* same as in first pass */ |
| 5776 | q = u->str; /* next output char */ |
| 5777 | qe = u->str + u->length; /* end of output */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5778 | |
| 5779 | for (p = self->str; p < e; p++) |
| 5780 | if (*p == '\t') { |
| 5781 | if (tabsize > 0) { |
| 5782 | i = tabsize - (j % tabsize); |
| 5783 | j += i; |
Guido van Rossum | 44a93e5 | 2008-03-11 21:14:54 +0000 | [diff] [blame] | 5784 | while (i--) { |
| 5785 | if (q >= qe) |
| 5786 | goto overflow2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5787 | *q++ = ' '; |
Guido van Rossum | 44a93e5 | 2008-03-11 21:14:54 +0000 | [diff] [blame] | 5788 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5789 | } |
| 5790 | } |
| 5791 | else { |
Guido van Rossum | 44a93e5 | 2008-03-11 21:14:54 +0000 | [diff] [blame] | 5792 | if (q >= qe) |
| 5793 | goto overflow2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5794 | *q++ = *p; |
Guido van Rossum | 44a93e5 | 2008-03-11 21:14:54 +0000 | [diff] [blame] | 5795 | j++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5796 | if (*p == '\n' || *p == '\r') |
| 5797 | j = 0; |
| 5798 | } |
| 5799 | |
| 5800 | return (PyObject*) u; |
Guido van Rossum | 44a93e5 | 2008-03-11 21:14:54 +0000 | [diff] [blame] | 5801 | |
| 5802 | overflow2: |
| 5803 | Py_DECREF(u); |
| 5804 | overflow1: |
| 5805 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 5806 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5807 | } |
| 5808 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5809 | PyDoc_STRVAR(find__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5810 | "S.find(sub [,start [,end]]) -> int\n\ |
| 5811 | \n\ |
| 5812 | Return the lowest index in S where substring sub is found,\n\ |
Georg Brandl | b4d100c | 2007-07-29 17:37:22 +0000 | [diff] [blame] | 5813 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5814 | arguments start and end are interpreted as in slice notation.\n\ |
| 5815 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5816 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5817 | |
| 5818 | static PyObject * |
| 5819 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 5820 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5821 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5822 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5823 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5824 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5825 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 5826 | if (!PyArg_ParseTuple(args, "O|O&O&:find", &substring, |
| 5827 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5828 | return NULL; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5829 | substring = PyUnicode_FromObject(substring); |
| 5830 | if (!substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5831 | return NULL; |
| 5832 | |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 5833 | result = stringlib_find_slice( |
| 5834 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 5835 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 5836 | start, end |
| 5837 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5838 | |
| 5839 | Py_DECREF(substring); |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5840 | |
| 5841 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5842 | } |
| 5843 | |
| 5844 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5845 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5846 | { |
| 5847 | if (index < 0 || index >= self->length) { |
| 5848 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 5849 | return NULL; |
| 5850 | } |
| 5851 | |
| 5852 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 5853 | } |
| 5854 | |
| 5855 | static long |
| 5856 | unicode_hash(PyUnicodeObject *self) |
| 5857 | { |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 5858 | /* Since Unicode objects compare equal to their ASCII string |
| 5859 | counterparts, they should use the individual character values |
| 5860 | as basis for their hash value. This is needed to assure that |
| 5861 | strings and Unicode objects behave in the same way as |
| 5862 | dictionary keys. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5863 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5864 | register Py_ssize_t len; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 5865 | register Py_UNICODE *p; |
| 5866 | register long x; |
| 5867 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5868 | if (self->hash != -1) |
| 5869 | return self->hash; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 5870 | len = PyUnicode_GET_SIZE(self); |
| 5871 | p = PyUnicode_AS_UNICODE(self); |
| 5872 | x = *p << 7; |
| 5873 | while (--len >= 0) |
| 5874 | x = (1000003*x) ^ *p++; |
| 5875 | x ^= PyUnicode_GET_SIZE(self); |
| 5876 | if (x == -1) |
| 5877 | x = -2; |
| 5878 | self->hash = x; |
| 5879 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5880 | } |
| 5881 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5882 | PyDoc_STRVAR(index__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5883 | "S.index(sub [,start [,end]]) -> int\n\ |
| 5884 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5885 | 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] | 5886 | |
| 5887 | static PyObject * |
| 5888 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 5889 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5890 | Py_ssize_t result; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5891 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5892 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5893 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5894 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 5895 | if (!PyArg_ParseTuple(args, "O|O&O&:index", &substring, |
| 5896 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5897 | return NULL; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5898 | substring = PyUnicode_FromObject(substring); |
| 5899 | if (!substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5900 | return NULL; |
| 5901 | |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 5902 | result = stringlib_find_slice( |
| 5903 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 5904 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 5905 | start, end |
| 5906 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5907 | |
| 5908 | Py_DECREF(substring); |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5909 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5910 | if (result < 0) { |
| 5911 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 5912 | return NULL; |
| 5913 | } |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5914 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5915 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5916 | } |
| 5917 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5918 | PyDoc_STRVAR(islower__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5919 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5920 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5921 | 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] | 5922 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5923 | |
| 5924 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5925 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5926 | { |
| 5927 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5928 | register const Py_UNICODE *e; |
| 5929 | int cased; |
| 5930 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5931 | /* Shortcut for single character strings */ |
| 5932 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5933 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5934 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5935 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5936 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5937 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5938 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5939 | e = p + PyUnicode_GET_SIZE(self); |
| 5940 | cased = 0; |
| 5941 | for (; p < e; p++) { |
| 5942 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5943 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5944 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5945 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5946 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 5947 | cased = 1; |
| 5948 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5949 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5950 | } |
| 5951 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5952 | PyDoc_STRVAR(isupper__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5953 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5954 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5955 | 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] | 5956 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5957 | |
| 5958 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5959 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5960 | { |
| 5961 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5962 | register const Py_UNICODE *e; |
| 5963 | int cased; |
| 5964 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5965 | /* Shortcut for single character strings */ |
| 5966 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5967 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5968 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5969 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 5970 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5971 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 5972 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5973 | e = p + PyUnicode_GET_SIZE(self); |
| 5974 | cased = 0; |
| 5975 | for (; p < e; p++) { |
| 5976 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5977 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5978 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5979 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5980 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 5981 | cased = 1; |
| 5982 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5983 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5984 | } |
| 5985 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5986 | PyDoc_STRVAR(istitle__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 5987 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5988 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 5989 | Return True if S is a titlecased string and there is at least one\n\ |
| 5990 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 5991 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 5992 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5993 | |
| 5994 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 5995 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5996 | { |
| 5997 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5998 | register const Py_UNICODE *e; |
| 5999 | int cased, previous_is_cased; |
| 6000 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6001 | /* Shortcut for single character strings */ |
| 6002 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6003 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 6004 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6005 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6006 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6007 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6008 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6009 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6010 | e = p + PyUnicode_GET_SIZE(self); |
| 6011 | cased = 0; |
| 6012 | previous_is_cased = 0; |
| 6013 | for (; p < e; p++) { |
| 6014 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6015 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6016 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 6017 | if (previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6018 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6019 | previous_is_cased = 1; |
| 6020 | cased = 1; |
| 6021 | } |
| 6022 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 6023 | if (!previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6024 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6025 | previous_is_cased = 1; |
| 6026 | cased = 1; |
| 6027 | } |
| 6028 | else |
| 6029 | previous_is_cased = 0; |
| 6030 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6031 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6032 | } |
| 6033 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6034 | PyDoc_STRVAR(isspace__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6035 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6036 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6037 | Return True if all characters in S are whitespace\n\ |
| 6038 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6039 | |
| 6040 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6041 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6042 | { |
| 6043 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6044 | register const Py_UNICODE *e; |
| 6045 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6046 | /* Shortcut for single character strings */ |
| 6047 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6048 | Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6049 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6050 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6051 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6052 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6053 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6054 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6055 | e = p + PyUnicode_GET_SIZE(self); |
| 6056 | for (; p < e; p++) { |
| 6057 | if (!Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6058 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6059 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6060 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6061 | } |
| 6062 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6063 | PyDoc_STRVAR(isalpha__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6064 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6065 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6066 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6067 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6068 | |
| 6069 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6070 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6071 | { |
| 6072 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6073 | register const Py_UNICODE *e; |
| 6074 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6075 | /* Shortcut for single character strings */ |
| 6076 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6077 | Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6078 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6079 | |
| 6080 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6081 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6082 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6083 | |
| 6084 | e = p + PyUnicode_GET_SIZE(self); |
| 6085 | for (; p < e; p++) { |
| 6086 | if (!Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6087 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6088 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6089 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6090 | } |
| 6091 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6092 | PyDoc_STRVAR(isalnum__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6093 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6094 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6095 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6096 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6097 | |
| 6098 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6099 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6100 | { |
| 6101 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6102 | register const Py_UNICODE *e; |
| 6103 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6104 | /* Shortcut for single character strings */ |
| 6105 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6106 | Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6107 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6108 | |
| 6109 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6110 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6111 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6112 | |
| 6113 | e = p + PyUnicode_GET_SIZE(self); |
| 6114 | for (; p < e; p++) { |
| 6115 | if (!Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6116 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6117 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6118 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6119 | } |
| 6120 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6121 | PyDoc_STRVAR(isdecimal__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6122 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6123 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6124 | 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] | 6125 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6126 | |
| 6127 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6128 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6129 | { |
| 6130 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6131 | register const Py_UNICODE *e; |
| 6132 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6133 | /* Shortcut for single character strings */ |
| 6134 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6135 | Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6136 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6137 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6138 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6139 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6140 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6141 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6142 | e = p + PyUnicode_GET_SIZE(self); |
| 6143 | for (; p < e; p++) { |
| 6144 | if (!Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6145 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6146 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6147 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6148 | } |
| 6149 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6150 | PyDoc_STRVAR(isdigit__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6151 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6152 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6153 | Return True if all characters in S are digits\n\ |
| 6154 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6155 | |
| 6156 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6157 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6158 | { |
| 6159 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6160 | register const Py_UNICODE *e; |
| 6161 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6162 | /* Shortcut for single character strings */ |
| 6163 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6164 | Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6165 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6166 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6167 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6168 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6169 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6170 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6171 | e = p + PyUnicode_GET_SIZE(self); |
| 6172 | for (; p < e; p++) { |
| 6173 | if (!Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6174 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6175 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6176 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6177 | } |
| 6178 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6179 | PyDoc_STRVAR(isnumeric__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6180 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6181 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6182 | 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] | 6183 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6184 | |
| 6185 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6186 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6187 | { |
| 6188 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6189 | register const Py_UNICODE *e; |
| 6190 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6191 | /* Shortcut for single character strings */ |
| 6192 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6193 | Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6194 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6195 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6196 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6197 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6198 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6199 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6200 | e = p + PyUnicode_GET_SIZE(self); |
| 6201 | for (; p < e; p++) { |
| 6202 | if (!Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6203 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6204 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6205 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6206 | } |
| 6207 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6208 | PyDoc_STRVAR(join__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6209 | "S.join(sequence) -> unicode\n\ |
| 6210 | \n\ |
| 6211 | 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] | 6212 | sequence. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6213 | |
| 6214 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6215 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6216 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6217 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6218 | } |
| 6219 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6220 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6221 | unicode_length(PyUnicodeObject *self) |
| 6222 | { |
| 6223 | return self->length; |
| 6224 | } |
| 6225 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6226 | PyDoc_STRVAR(ljust__doc__, |
Hye-Shik Chang | 974ed7c | 2004-06-02 16:49:17 +0000 | [diff] [blame] | 6227 | "S.ljust(width[, fillchar]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6228 | \n\ |
| 6229 | 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] | 6230 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6231 | |
| 6232 | static PyObject * |
| 6233 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 6234 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6235 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6236 | Py_UNICODE fillchar = ' '; |
| 6237 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6238 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6239 | return NULL; |
| 6240 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6241 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6242 | Py_INCREF(self); |
| 6243 | return (PyObject*) self; |
| 6244 | } |
| 6245 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6246 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6247 | } |
| 6248 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6249 | PyDoc_STRVAR(lower__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6250 | "S.lower() -> unicode\n\ |
| 6251 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6252 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6253 | |
| 6254 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6255 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6256 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6257 | return fixup(self, fixlower); |
| 6258 | } |
| 6259 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6260 | #define LEFTSTRIP 0 |
| 6261 | #define RIGHTSTRIP 1 |
| 6262 | #define BOTHSTRIP 2 |
| 6263 | |
| 6264 | /* Arrays indexed by above */ |
| 6265 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 6266 | |
| 6267 | #define STRIPNAME(i) (stripformat[i]+3) |
| 6268 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6269 | /* externally visible for str.strip(unicode) */ |
| 6270 | PyObject * |
| 6271 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 6272 | { |
| 6273 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6274 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6275 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6276 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 6277 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6278 | |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 6279 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
| 6280 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6281 | i = 0; |
| 6282 | if (striptype != RIGHTSTRIP) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 6283 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 6284 | i++; |
| 6285 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6286 | } |
| 6287 | |
| 6288 | j = len; |
| 6289 | if (striptype != LEFTSTRIP) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 6290 | do { |
| 6291 | j--; |
| 6292 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 6293 | j++; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6294 | } |
| 6295 | |
| 6296 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 6297 | Py_INCREF(self); |
| 6298 | return (PyObject*)self; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6299 | } |
| 6300 | else |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 6301 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6302 | } |
| 6303 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6304 | |
| 6305 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6306 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6307 | { |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6308 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6309 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6310 | |
| 6311 | i = 0; |
| 6312 | if (striptype != RIGHTSTRIP) { |
| 6313 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 6314 | i++; |
| 6315 | } |
| 6316 | } |
| 6317 | |
| 6318 | j = len; |
| 6319 | if (striptype != LEFTSTRIP) { |
| 6320 | do { |
| 6321 | j--; |
| 6322 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 6323 | j++; |
| 6324 | } |
| 6325 | |
| 6326 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 6327 | Py_INCREF(self); |
| 6328 | return (PyObject*)self; |
| 6329 | } |
| 6330 | else |
| 6331 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6332 | } |
| 6333 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6334 | |
| 6335 | static PyObject * |
| 6336 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 6337 | { |
| 6338 | PyObject *sep = NULL; |
| 6339 | |
| 6340 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 6341 | return NULL; |
| 6342 | |
| 6343 | if (sep != NULL && sep != Py_None) { |
| 6344 | if (PyUnicode_Check(sep)) |
| 6345 | return _PyUnicode_XStrip(self, striptype, sep); |
| 6346 | else if (PyString_Check(sep)) { |
| 6347 | PyObject *res; |
| 6348 | sep = PyUnicode_FromObject(sep); |
| 6349 | if (sep==NULL) |
| 6350 | return NULL; |
| 6351 | res = _PyUnicode_XStrip(self, striptype, sep); |
| 6352 | Py_DECREF(sep); |
| 6353 | return res; |
| 6354 | } |
| 6355 | else { |
| 6356 | PyErr_Format(PyExc_TypeError, |
| 6357 | "%s arg must be None, unicode or str", |
| 6358 | STRIPNAME(striptype)); |
| 6359 | return NULL; |
| 6360 | } |
| 6361 | } |
| 6362 | |
| 6363 | return do_strip(self, striptype); |
| 6364 | } |
| 6365 | |
| 6366 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6367 | PyDoc_STRVAR(strip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6368 | "S.strip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6369 | \n\ |
| 6370 | Return a copy of the string S with leading and trailing\n\ |
| 6371 | whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6372 | If chars is given and not None, remove characters in chars instead.\n\ |
| 6373 | 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] | 6374 | |
| 6375 | static PyObject * |
| 6376 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 6377 | { |
| 6378 | if (PyTuple_GET_SIZE(args) == 0) |
| 6379 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 6380 | else |
| 6381 | return do_argstrip(self, BOTHSTRIP, args); |
| 6382 | } |
| 6383 | |
| 6384 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6385 | PyDoc_STRVAR(lstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6386 | "S.lstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6387 | \n\ |
| 6388 | Return a copy of the string S with leading whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6389 | If chars is given and not None, remove characters in chars instead.\n\ |
| 6390 | 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] | 6391 | |
| 6392 | static PyObject * |
| 6393 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 6394 | { |
| 6395 | if (PyTuple_GET_SIZE(args) == 0) |
| 6396 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 6397 | else |
| 6398 | return do_argstrip(self, LEFTSTRIP, args); |
| 6399 | } |
| 6400 | |
| 6401 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6402 | PyDoc_STRVAR(rstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6403 | "S.rstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 6404 | \n\ |
| 6405 | Return a copy of the string S with trailing whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 6406 | If chars is given and not None, remove characters in chars instead.\n\ |
| 6407 | 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] | 6408 | |
| 6409 | static PyObject * |
| 6410 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 6411 | { |
| 6412 | if (PyTuple_GET_SIZE(args) == 0) |
| 6413 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 6414 | else |
| 6415 | return do_argstrip(self, RIGHTSTRIP, args); |
| 6416 | } |
| 6417 | |
| 6418 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6419 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6420 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6421 | { |
| 6422 | PyUnicodeObject *u; |
| 6423 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6424 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 6425 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6426 | |
| 6427 | if (len < 0) |
| 6428 | len = 0; |
| 6429 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6430 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6431 | /* no repeat, return original string */ |
| 6432 | Py_INCREF(str); |
| 6433 | return (PyObject*) str; |
| 6434 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 6435 | |
| 6436 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 6437 | * needed doesn't overflow size_t |
| 6438 | */ |
| 6439 | nchars = len * str->length; |
| 6440 | if (len && nchars / len != str->length) { |
| 6441 | PyErr_SetString(PyExc_OverflowError, |
| 6442 | "repeated string is too long"); |
| 6443 | return NULL; |
| 6444 | } |
| 6445 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 6446 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 6447 | PyErr_SetString(PyExc_OverflowError, |
| 6448 | "repeated string is too long"); |
| 6449 | return NULL; |
| 6450 | } |
| 6451 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6452 | if (!u) |
| 6453 | return NULL; |
| 6454 | |
| 6455 | p = u->str; |
| 6456 | |
Fredrik Lundh | f1d60a5 | 2006-05-22 16:29:30 +0000 | [diff] [blame] | 6457 | if (str->length == 1 && len > 0) { |
| 6458 | Py_UNICODE_FILL(p, str->str[0], len); |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 6459 | } else { |
Tim Peters | 1bacc64 | 2006-05-23 05:47:16 +0000 | [diff] [blame] | 6460 | Py_ssize_t done = 0; /* number of characters copied this far */ |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 6461 | if (done < nchars) { |
Fredrik Lundh | f1d60a5 | 2006-05-22 16:29:30 +0000 | [diff] [blame] | 6462 | Py_UNICODE_COPY(p, str->str, str->length); |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 6463 | done = str->length; |
| 6464 | } |
| 6465 | while (done < nchars) { |
| 6466 | int n = (done <= nchars-done) ? done : nchars-done; |
| 6467 | Py_UNICODE_COPY(p+done, p, n); |
| 6468 | done += n; |
| 6469 | } |
| 6470 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6471 | |
| 6472 | return (PyObject*) u; |
| 6473 | } |
| 6474 | |
| 6475 | PyObject *PyUnicode_Replace(PyObject *obj, |
| 6476 | PyObject *subobj, |
| 6477 | PyObject *replobj, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6478 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6479 | { |
| 6480 | PyObject *self; |
| 6481 | PyObject *str1; |
| 6482 | PyObject *str2; |
| 6483 | PyObject *result; |
| 6484 | |
| 6485 | self = PyUnicode_FromObject(obj); |
| 6486 | if (self == NULL) |
| 6487 | return NULL; |
| 6488 | str1 = PyUnicode_FromObject(subobj); |
| 6489 | if (str1 == NULL) { |
| 6490 | Py_DECREF(self); |
| 6491 | return NULL; |
| 6492 | } |
| 6493 | str2 = PyUnicode_FromObject(replobj); |
| 6494 | if (str2 == NULL) { |
| 6495 | Py_DECREF(self); |
| 6496 | Py_DECREF(str1); |
| 6497 | return NULL; |
| 6498 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6499 | result = replace((PyUnicodeObject *)self, |
| 6500 | (PyUnicodeObject *)str1, |
| 6501 | (PyUnicodeObject *)str2, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6502 | maxcount); |
| 6503 | Py_DECREF(self); |
| 6504 | Py_DECREF(str1); |
| 6505 | Py_DECREF(str2); |
| 6506 | return result; |
| 6507 | } |
| 6508 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6509 | PyDoc_STRVAR(replace__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6510 | "S.replace (old, new[, maxsplit]) -> unicode\n\ |
| 6511 | \n\ |
| 6512 | Return a copy of S with all occurrences of substring\n\ |
| 6513 | 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] | 6514 | given, only the first maxsplit occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6515 | |
| 6516 | static PyObject* |
| 6517 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 6518 | { |
| 6519 | PyUnicodeObject *str1; |
| 6520 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6521 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6522 | PyObject *result; |
| 6523 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6524 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6525 | return NULL; |
| 6526 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 6527 | if (str1 == NULL) |
| 6528 | return NULL; |
| 6529 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 6530 | if (str2 == NULL) { |
| 6531 | Py_DECREF(str1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6532 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 6533 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6534 | |
| 6535 | result = replace(self, str1, str2, maxcount); |
| 6536 | |
| 6537 | Py_DECREF(str1); |
| 6538 | Py_DECREF(str2); |
| 6539 | return result; |
| 6540 | } |
| 6541 | |
| 6542 | static |
| 6543 | PyObject *unicode_repr(PyObject *unicode) |
| 6544 | { |
| 6545 | return unicodeescape_string(PyUnicode_AS_UNICODE(unicode), |
| 6546 | PyUnicode_GET_SIZE(unicode), |
| 6547 | 1); |
| 6548 | } |
| 6549 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6550 | PyDoc_STRVAR(rfind__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6551 | "S.rfind(sub [,start [,end]]) -> int\n\ |
| 6552 | \n\ |
| 6553 | Return the highest index in S where substring sub is found,\n\ |
Georg Brandl | b4d100c | 2007-07-29 17:37:22 +0000 | [diff] [blame] | 6554 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6555 | arguments start and end are interpreted as in slice notation.\n\ |
| 6556 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6557 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6558 | |
| 6559 | static PyObject * |
| 6560 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 6561 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6562 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6563 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6564 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6565 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6566 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6567 | if (!PyArg_ParseTuple(args, "O|O&O&:rfind", &substring, |
| 6568 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6569 | return NULL; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6570 | substring = PyUnicode_FromObject(substring); |
| 6571 | if (!substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6572 | return NULL; |
| 6573 | |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 6574 | result = stringlib_rfind_slice( |
| 6575 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6576 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6577 | start, end |
| 6578 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6579 | |
| 6580 | Py_DECREF(substring); |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6581 | |
| 6582 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6583 | } |
| 6584 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6585 | PyDoc_STRVAR(rindex__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6586 | "S.rindex(sub [,start [,end]]) -> int\n\ |
| 6587 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6588 | 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] | 6589 | |
| 6590 | static PyObject * |
| 6591 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 6592 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6593 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6594 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6595 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6596 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6597 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6598 | if (!PyArg_ParseTuple(args, "O|O&O&:rindex", &substring, |
| 6599 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6600 | return NULL; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6601 | substring = PyUnicode_FromObject(substring); |
| 6602 | if (!substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6603 | return NULL; |
| 6604 | |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 6605 | result = stringlib_rfind_slice( |
| 6606 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6607 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6608 | start, end |
| 6609 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6610 | |
| 6611 | Py_DECREF(substring); |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6612 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6613 | if (result < 0) { |
| 6614 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 6615 | return NULL; |
| 6616 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6617 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6618 | } |
| 6619 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6620 | PyDoc_STRVAR(rjust__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6621 | "S.rjust(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6622 | \n\ |
| 6623 | 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] | 6624 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6625 | |
| 6626 | static PyObject * |
| 6627 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 6628 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6629 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6630 | Py_UNICODE fillchar = ' '; |
| 6631 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6632 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6633 | return NULL; |
| 6634 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6635 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6636 | Py_INCREF(self); |
| 6637 | return (PyObject*) self; |
| 6638 | } |
| 6639 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6640 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6641 | } |
| 6642 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6643 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6644 | 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] | 6645 | { |
| 6646 | /* standard clamping */ |
| 6647 | if (start < 0) |
| 6648 | start = 0; |
| 6649 | if (end < 0) |
| 6650 | end = 0; |
| 6651 | if (end > self->length) |
| 6652 | end = self->length; |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6653 | if (start == 0 && end == self->length && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6654 | /* full slice, return original string */ |
| 6655 | Py_INCREF(self); |
| 6656 | return (PyObject*) self; |
| 6657 | } |
| 6658 | if (start > end) |
| 6659 | start = end; |
| 6660 | /* copy slice */ |
| 6661 | return (PyObject*) PyUnicode_FromUnicode(self->str + start, |
| 6662 | end - start); |
| 6663 | } |
| 6664 | |
| 6665 | PyObject *PyUnicode_Split(PyObject *s, |
| 6666 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6667 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6668 | { |
| 6669 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6670 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6671 | s = PyUnicode_FromObject(s); |
| 6672 | if (s == NULL) |
| 6673 | return NULL; |
| 6674 | if (sep != NULL) { |
| 6675 | sep = PyUnicode_FromObject(sep); |
| 6676 | if (sep == NULL) { |
| 6677 | Py_DECREF(s); |
| 6678 | return NULL; |
| 6679 | } |
| 6680 | } |
| 6681 | |
| 6682 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 6683 | |
| 6684 | Py_DECREF(s); |
| 6685 | Py_XDECREF(sep); |
| 6686 | return result; |
| 6687 | } |
| 6688 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6689 | PyDoc_STRVAR(split__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6690 | "S.split([sep [,maxsplit]]) -> list of strings\n\ |
| 6691 | \n\ |
| 6692 | Return a list of the words in S, using sep as the\n\ |
| 6693 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Thomas Heller | ca0d2cb | 2004-09-15 11:41:32 +0000 | [diff] [blame] | 6694 | 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] | 6695 | any whitespace string is a separator."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6696 | |
| 6697 | static PyObject* |
| 6698 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 6699 | { |
| 6700 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6701 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6702 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6703 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6704 | return NULL; |
| 6705 | |
| 6706 | if (substring == Py_None) |
| 6707 | return split(self, NULL, maxcount); |
| 6708 | else if (PyUnicode_Check(substring)) |
| 6709 | return split(self, (PyUnicodeObject *)substring, maxcount); |
| 6710 | else |
| 6711 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
| 6712 | } |
| 6713 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6714 | PyObject * |
| 6715 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 6716 | { |
| 6717 | PyObject* str_obj; |
| 6718 | PyObject* sep_obj; |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6719 | PyObject* out; |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 6720 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6721 | str_obj = PyUnicode_FromObject(str_in); |
| 6722 | if (!str_obj) |
| 6723 | return NULL; |
| 6724 | sep_obj = PyUnicode_FromObject(sep_in); |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 6725 | if (!sep_obj) { |
| 6726 | Py_DECREF(str_obj); |
| 6727 | return NULL; |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6728 | } |
| 6729 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 6730 | out = stringlib_partition( |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 6731 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 6732 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 6733 | ); |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6734 | |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 6735 | Py_DECREF(sep_obj); |
| 6736 | Py_DECREF(str_obj); |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6737 | |
| 6738 | return out; |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6739 | } |
| 6740 | |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 6741 | |
| 6742 | PyObject * |
| 6743 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 6744 | { |
| 6745 | PyObject* str_obj; |
| 6746 | PyObject* sep_obj; |
| 6747 | PyObject* out; |
| 6748 | |
| 6749 | str_obj = PyUnicode_FromObject(str_in); |
| 6750 | if (!str_obj) |
| 6751 | return NULL; |
| 6752 | sep_obj = PyUnicode_FromObject(sep_in); |
| 6753 | if (!sep_obj) { |
| 6754 | Py_DECREF(str_obj); |
| 6755 | return NULL; |
| 6756 | } |
| 6757 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 6758 | out = stringlib_rpartition( |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 6759 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 6760 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 6761 | ); |
| 6762 | |
| 6763 | Py_DECREF(sep_obj); |
| 6764 | Py_DECREF(str_obj); |
| 6765 | |
| 6766 | return out; |
| 6767 | } |
| 6768 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6769 | PyDoc_STRVAR(partition__doc__, |
| 6770 | "S.partition(sep) -> (head, sep, tail)\n\ |
| 6771 | \n\ |
| 6772 | Searches for the separator sep in S, and returns the part before it,\n\ |
| 6773 | the separator itself, and the part after it. If the separator is not\n\ |
| 6774 | found, returns S and two empty strings."); |
| 6775 | |
| 6776 | static PyObject* |
Fredrik Lundh | 450277f | 2006-05-26 09:46:59 +0000 | [diff] [blame] | 6777 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6778 | { |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 6779 | return PyUnicode_Partition((PyObject *)self, separator); |
| 6780 | } |
| 6781 | |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 6782 | PyDoc_STRVAR(rpartition__doc__, |
Neal Norwitz | 29a5fdb | 2006-09-05 02:21:38 +0000 | [diff] [blame] | 6783 | "S.rpartition(sep) -> (tail, sep, head)\n\ |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 6784 | \n\ |
| 6785 | Searches for the separator sep in S, starting at the end of S, and returns\n\ |
| 6786 | the part before it, the separator itself, and the part after it. If the\n\ |
Neal Norwitz | 29a5fdb | 2006-09-05 02:21:38 +0000 | [diff] [blame] | 6787 | separator is not found, returns two empty strings and S."); |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 6788 | |
| 6789 | static PyObject* |
| 6790 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 6791 | { |
| 6792 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 6793 | } |
| 6794 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6795 | PyObject *PyUnicode_RSplit(PyObject *s, |
| 6796 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6797 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6798 | { |
| 6799 | PyObject *result; |
| 6800 | |
| 6801 | s = PyUnicode_FromObject(s); |
| 6802 | if (s == NULL) |
| 6803 | return NULL; |
| 6804 | if (sep != NULL) { |
| 6805 | sep = PyUnicode_FromObject(sep); |
| 6806 | if (sep == NULL) { |
| 6807 | Py_DECREF(s); |
| 6808 | return NULL; |
| 6809 | } |
| 6810 | } |
| 6811 | |
| 6812 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 6813 | |
| 6814 | Py_DECREF(s); |
| 6815 | Py_XDECREF(sep); |
| 6816 | return result; |
| 6817 | } |
| 6818 | |
| 6819 | PyDoc_STRVAR(rsplit__doc__, |
| 6820 | "S.rsplit([sep [,maxsplit]]) -> list of strings\n\ |
| 6821 | \n\ |
| 6822 | Return a list of the words in S, using sep as the\n\ |
| 6823 | delimiter string, starting at the end of the string and\n\ |
| 6824 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 6825 | splits are done. If sep is not specified, any whitespace string\n\ |
| 6826 | is a separator."); |
| 6827 | |
| 6828 | static PyObject* |
| 6829 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 6830 | { |
| 6831 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6832 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6833 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6834 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6835 | return NULL; |
| 6836 | |
| 6837 | if (substring == Py_None) |
| 6838 | return rsplit(self, NULL, maxcount); |
| 6839 | else if (PyUnicode_Check(substring)) |
| 6840 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
| 6841 | else |
| 6842 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
| 6843 | } |
| 6844 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6845 | PyDoc_STRVAR(splitlines__doc__, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6846 | "S.splitlines([keepends]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6847 | \n\ |
| 6848 | 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] | 6849 | 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] | 6850 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6851 | |
| 6852 | static PyObject* |
| 6853 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 6854 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6855 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6856 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6857 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6858 | return NULL; |
| 6859 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 6860 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6861 | } |
| 6862 | |
| 6863 | static |
| 6864 | PyObject *unicode_str(PyUnicodeObject *self) |
| 6865 | { |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 6866 | return PyUnicode_AsEncodedString((PyObject *)self, NULL, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6867 | } |
| 6868 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6869 | PyDoc_STRVAR(swapcase__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6870 | "S.swapcase() -> unicode\n\ |
| 6871 | \n\ |
| 6872 | 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] | 6873 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6874 | |
| 6875 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6876 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6877 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6878 | return fixup(self, fixswapcase); |
| 6879 | } |
| 6880 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6881 | PyDoc_STRVAR(translate__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6882 | "S.translate(table) -> unicode\n\ |
| 6883 | \n\ |
| 6884 | Return a copy of the string S, where all characters have been mapped\n\ |
| 6885 | 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] | 6886 | Unicode ordinals to Unicode ordinals, Unicode strings or None.\n\ |
| 6887 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 6888 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6889 | |
| 6890 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6891 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6892 | { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6893 | return PyUnicode_TranslateCharmap(self->str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6894 | self->length, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6895 | table, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6896 | "ignore"); |
| 6897 | } |
| 6898 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6899 | PyDoc_STRVAR(upper__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6900 | "S.upper() -> unicode\n\ |
| 6901 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6902 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6903 | |
| 6904 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6905 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6906 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6907 | return fixup(self, fixupper); |
| 6908 | } |
| 6909 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6910 | PyDoc_STRVAR(zfill__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6911 | "S.zfill(width) -> unicode\n\ |
| 6912 | \n\ |
| 6913 | 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] | 6914 | of the specified width. The string x is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6915 | |
| 6916 | static PyObject * |
| 6917 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 6918 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6919 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6920 | PyUnicodeObject *u; |
| 6921 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6922 | Py_ssize_t width; |
| 6923 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6924 | return NULL; |
| 6925 | |
| 6926 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 6927 | if (PyUnicode_CheckExact(self)) { |
| 6928 | Py_INCREF(self); |
| 6929 | return (PyObject*) self; |
| 6930 | } |
| 6931 | else |
| 6932 | return PyUnicode_FromUnicode( |
| 6933 | PyUnicode_AS_UNICODE(self), |
| 6934 | PyUnicode_GET_SIZE(self) |
| 6935 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6936 | } |
| 6937 | |
| 6938 | fill = width - self->length; |
| 6939 | |
| 6940 | u = pad(self, fill, 0, '0'); |
| 6941 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 6942 | if (u == NULL) |
| 6943 | return NULL; |
| 6944 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6945 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 6946 | /* move sign to beginning of string */ |
| 6947 | u->str[0] = u->str[fill]; |
| 6948 | u->str[fill] = '0'; |
| 6949 | } |
| 6950 | |
| 6951 | return (PyObject*) u; |
| 6952 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6953 | |
| 6954 | #if 0 |
| 6955 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6956 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6957 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6958 | return PyInt_FromLong(unicode_freelist_size); |
| 6959 | } |
| 6960 | #endif |
| 6961 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6962 | PyDoc_STRVAR(startswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6963 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6964 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 6965 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 6966 | With optional start, test S beginning at that position.\n\ |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 6967 | With optional end, stop comparing S at that position.\n\ |
| 6968 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6969 | |
| 6970 | static PyObject * |
| 6971 | unicode_startswith(PyUnicodeObject *self, |
| 6972 | PyObject *args) |
| 6973 | { |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 6974 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6975 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6976 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6977 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 6978 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6979 | |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 6980 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6981 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6982 | return NULL; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 6983 | if (PyTuple_Check(subobj)) { |
| 6984 | Py_ssize_t i; |
| 6985 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 6986 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 6987 | PyTuple_GET_ITEM(subobj, i)); |
| 6988 | if (substring == NULL) |
| 6989 | return NULL; |
| 6990 | result = tailmatch(self, substring, start, end, -1); |
| 6991 | Py_DECREF(substring); |
| 6992 | if (result) { |
| 6993 | Py_RETURN_TRUE; |
| 6994 | } |
| 6995 | } |
| 6996 | /* nothing matched */ |
| 6997 | Py_RETURN_FALSE; |
| 6998 | } |
| 6999 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7000 | if (substring == NULL) |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7001 | return NULL; |
| 7002 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7003 | Py_DECREF(substring); |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7004 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7005 | } |
| 7006 | |
| 7007 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7008 | PyDoc_STRVAR(endswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7009 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7010 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 7011 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 7012 | With optional start, test S beginning at that position.\n\ |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7013 | With optional end, stop comparing S at that position.\n\ |
| 7014 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7015 | |
| 7016 | static PyObject * |
| 7017 | unicode_endswith(PyUnicodeObject *self, |
| 7018 | PyObject *args) |
| 7019 | { |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7020 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7021 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7022 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7023 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7024 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7025 | |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7026 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
| 7027 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7028 | return NULL; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7029 | if (PyTuple_Check(subobj)) { |
| 7030 | Py_ssize_t i; |
| 7031 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 7032 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 7033 | PyTuple_GET_ITEM(subobj, i)); |
| 7034 | if (substring == NULL) |
| 7035 | return NULL; |
| 7036 | result = tailmatch(self, substring, start, end, +1); |
| 7037 | Py_DECREF(substring); |
| 7038 | if (result) { |
| 7039 | Py_RETURN_TRUE; |
| 7040 | } |
| 7041 | } |
| 7042 | Py_RETURN_FALSE; |
| 7043 | } |
| 7044 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7045 | if (substring == NULL) |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7046 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7047 | |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7048 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7049 | Py_DECREF(substring); |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7050 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7051 | } |
| 7052 | |
| 7053 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 7054 | |
| 7055 | static PyObject * |
| 7056 | unicode_getnewargs(PyUnicodeObject *v) |
| 7057 | { |
| 7058 | return Py_BuildValue("(u#)", v->str, v->length); |
| 7059 | } |
| 7060 | |
| 7061 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7062 | static PyMethodDef unicode_methods[] = { |
| 7063 | |
| 7064 | /* Order is according to common usage: often used methods should |
| 7065 | appear first, since lookup is done sequentially. */ |
| 7066 | |
Georg Brandl | ecdc0a9 | 2006-03-30 12:19:07 +0000 | [diff] [blame] | 7067 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7068 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 7069 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7070 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7071 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 7072 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 7073 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 7074 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 7075 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 7076 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 7077 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Fredrik Lundh | 450277f | 2006-05-26 09:46:59 +0000 | [diff] [blame] | 7078 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7079 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 7080 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 7081 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7082 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7083 | {"decode", (PyCFunction) unicode_decode, METH_VARARGS, decode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7084 | /* {"maketrans", (PyCFunction) unicode_maketrans, METH_VARARGS, maketrans__doc__}, */ |
| 7085 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 7086 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 7087 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7088 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7089 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7090 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7091 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7092 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 7093 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 7094 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 7095 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 7096 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 7097 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 7098 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 7099 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 7100 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 7101 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 7102 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 7103 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 7104 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 7105 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7106 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 7107 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7108 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7109 | #endif |
| 7110 | |
| 7111 | #if 0 |
| 7112 | /* This one is just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7113 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7114 | #endif |
| 7115 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 7116 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7117 | {NULL, NULL} |
| 7118 | }; |
| 7119 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 7120 | static PyObject * |
| 7121 | unicode_mod(PyObject *v, PyObject *w) |
| 7122 | { |
| 7123 | if (!PyUnicode_Check(v)) { |
| 7124 | Py_INCREF(Py_NotImplemented); |
| 7125 | return Py_NotImplemented; |
| 7126 | } |
| 7127 | return PyUnicode_Format(v, w); |
| 7128 | } |
| 7129 | |
| 7130 | static PyNumberMethods unicode_as_number = { |
| 7131 | 0, /*nb_add*/ |
| 7132 | 0, /*nb_subtract*/ |
| 7133 | 0, /*nb_multiply*/ |
| 7134 | 0, /*nb_divide*/ |
| 7135 | unicode_mod, /*nb_remainder*/ |
| 7136 | }; |
| 7137 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7138 | static PySequenceMethods unicode_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7139 | (lenfunc) unicode_length, /* sq_length */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 7140 | PyUnicode_Concat, /* sq_concat */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7141 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 7142 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 7143 | (ssizessizeargfunc) unicode_slice, /* sq_slice */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7144 | 0, /* sq_ass_item */ |
| 7145 | 0, /* sq_ass_slice */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 7146 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7147 | }; |
| 7148 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7149 | static PyObject* |
| 7150 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 7151 | { |
Marc-André Lemburg | 3a45779 | 2006-08-14 12:57:27 +0000 | [diff] [blame] | 7152 | if (PyIndex_Check(item)) { |
| 7153 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7154 | if (i == -1 && PyErr_Occurred()) |
| 7155 | return NULL; |
| 7156 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7157 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7158 | return unicode_getitem(self, i); |
| 7159 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7160 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7161 | Py_UNICODE* source_buf; |
| 7162 | Py_UNICODE* result_buf; |
| 7163 | PyObject* result; |
| 7164 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7165 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7166 | &start, &stop, &step, &slicelength) < 0) { |
| 7167 | return NULL; |
| 7168 | } |
| 7169 | |
| 7170 | if (slicelength <= 0) { |
| 7171 | return PyUnicode_FromUnicode(NULL, 0); |
| 7172 | } else { |
| 7173 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Neal Norwitz | b3635f9 | 2008-03-18 04:17:36 +0000 | [diff] [blame] | 7174 | result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* |
| 7175 | sizeof(Py_UNICODE)); |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7176 | |
| 7177 | if (result_buf == NULL) |
| 7178 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7179 | |
| 7180 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 7181 | result_buf[i] = source_buf[cur]; |
| 7182 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7183 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7184 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
Neal Norwitz | b3635f9 | 2008-03-18 04:17:36 +0000 | [diff] [blame] | 7185 | PyObject_FREE(result_buf); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7186 | return result; |
| 7187 | } |
| 7188 | } else { |
| 7189 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 7190 | return NULL; |
| 7191 | } |
| 7192 | } |
| 7193 | |
| 7194 | static PyMappingMethods unicode_as_mapping = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7195 | (lenfunc)unicode_length, /* mp_length */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7196 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 7197 | (objobjargproc)0, /* mp_ass_subscript */ |
| 7198 | }; |
| 7199 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7200 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7201 | unicode_buffer_getreadbuf(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7202 | Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7203 | const void **ptr) |
| 7204 | { |
| 7205 | if (index != 0) { |
| 7206 | PyErr_SetString(PyExc_SystemError, |
| 7207 | "accessing non-existent unicode segment"); |
| 7208 | return -1; |
| 7209 | } |
| 7210 | *ptr = (void *) self->str; |
| 7211 | return PyUnicode_GET_DATA_SIZE(self); |
| 7212 | } |
| 7213 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7214 | static Py_ssize_t |
| 7215 | unicode_buffer_getwritebuf(PyUnicodeObject *self, Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7216 | const void **ptr) |
| 7217 | { |
| 7218 | PyErr_SetString(PyExc_TypeError, |
Neal Norwitz | 20e7213 | 2002-06-13 21:25:17 +0000 | [diff] [blame] | 7219 | "cannot use unicode as modifiable buffer"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7220 | return -1; |
| 7221 | } |
| 7222 | |
| 7223 | static int |
| 7224 | unicode_buffer_getsegcount(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7225 | Py_ssize_t *lenp) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7226 | { |
| 7227 | if (lenp) |
| 7228 | *lenp = PyUnicode_GET_DATA_SIZE(self); |
| 7229 | return 1; |
| 7230 | } |
| 7231 | |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 7232 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7233 | unicode_buffer_getcharbuf(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7234 | Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7235 | const void **ptr) |
| 7236 | { |
| 7237 | PyObject *str; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7238 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7239 | if (index != 0) { |
| 7240 | PyErr_SetString(PyExc_SystemError, |
| 7241 | "accessing non-existent unicode segment"); |
| 7242 | return -1; |
| 7243 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 7244 | str = _PyUnicode_AsDefaultEncodedString((PyObject *)self, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7245 | if (str == NULL) |
| 7246 | return -1; |
| 7247 | *ptr = (void *) PyString_AS_STRING(str); |
| 7248 | return PyString_GET_SIZE(str); |
| 7249 | } |
| 7250 | |
| 7251 | /* Helpers for PyUnicode_Format() */ |
| 7252 | |
| 7253 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7254 | 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] | 7255 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7256 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7257 | if (argidx < arglen) { |
| 7258 | (*p_argidx)++; |
| 7259 | if (arglen < 0) |
| 7260 | return args; |
| 7261 | else |
| 7262 | return PyTuple_GetItem(args, argidx); |
| 7263 | } |
| 7264 | PyErr_SetString(PyExc_TypeError, |
| 7265 | "not enough arguments for format string"); |
| 7266 | return NULL; |
| 7267 | } |
| 7268 | |
| 7269 | #define F_LJUST (1<<0) |
| 7270 | #define F_SIGN (1<<1) |
| 7271 | #define F_BLANK (1<<2) |
| 7272 | #define F_ALT (1<<3) |
| 7273 | #define F_ZERO (1<<4) |
| 7274 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7275 | static Py_ssize_t |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7276 | strtounicode(Py_UNICODE *buffer, const char *charbuffer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7277 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7278 | register Py_ssize_t i; |
| 7279 | Py_ssize_t len = strlen(charbuffer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7280 | for (i = len - 1; i >= 0; i--) |
| 7281 | buffer[i] = (Py_UNICODE) charbuffer[i]; |
| 7282 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7283 | return len; |
| 7284 | } |
| 7285 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7286 | static int |
| 7287 | doubletounicode(Py_UNICODE *buffer, size_t len, const char *format, double x) |
| 7288 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 7289 | Py_ssize_t result; |
| 7290 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7291 | PyOS_ascii_formatd((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 7292 | result = strtounicode(buffer, (char *)buffer); |
| 7293 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7294 | } |
| 7295 | |
| 7296 | static int |
| 7297 | longtounicode(Py_UNICODE *buffer, size_t len, const char *format, long x) |
| 7298 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 7299 | Py_ssize_t result; |
| 7300 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7301 | PyOS_snprintf((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 7302 | result = strtounicode(buffer, (char *)buffer); |
| 7303 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7304 | } |
| 7305 | |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 7306 | /* XXX To save some code duplication, formatfloat/long/int could have been |
| 7307 | shared with stringobject.c, converting from 8-bit to Unicode after the |
| 7308 | formatting is done. */ |
| 7309 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7310 | static int |
| 7311 | formatfloat(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7312 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7313 | int flags, |
| 7314 | int prec, |
| 7315 | int type, |
| 7316 | PyObject *v) |
| 7317 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7318 | /* fmt = '%#.' + `prec` + `type` |
| 7319 | 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] | 7320 | char fmt[20]; |
| 7321 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7322 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7323 | x = PyFloat_AsDouble(v); |
| 7324 | if (x == -1.0 && PyErr_Occurred()) |
| 7325 | return -1; |
| 7326 | if (prec < 0) |
| 7327 | prec = 6; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7328 | if (type == 'f' && (fabs(x) / 1e25) >= 1e25) |
| 7329 | type = 'g'; |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 7330 | /* Worst case length calc to ensure no buffer overrun: |
| 7331 | |
| 7332 | 'g' formats: |
| 7333 | fmt = %#.<prec>g |
| 7334 | buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
| 7335 | for any double rep.) |
| 7336 | len = 1 + prec + 1 + 2 + 5 = 9 + prec |
| 7337 | |
| 7338 | 'f' formats: |
| 7339 | buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
| 7340 | len = 1 + 50 + 1 + prec = 52 + prec |
| 7341 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7342 | If prec=0 the effective precision is 1 (the leading digit is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7343 | always given), therefore increase the length by one. |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 7344 | |
| 7345 | */ |
Georg Brandl | c5db923 | 2007-07-12 08:38:04 +0000 | [diff] [blame] | 7346 | if (((type == 'g' || type == 'G') && |
| 7347 | buflen <= (size_t)10 + (size_t)prec) || |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 7348 | (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7349 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 7350 | "formatted float is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7351 | return -1; |
| 7352 | } |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 7353 | PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
| 7354 | (flags&F_ALT) ? "#" : "", |
| 7355 | prec, type); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7356 | return doubletounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7357 | } |
| 7358 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7359 | static PyObject* |
| 7360 | formatlong(PyObject *val, int flags, int prec, int type) |
| 7361 | { |
| 7362 | char *buf; |
| 7363 | int i, len; |
| 7364 | PyObject *str; /* temporary string object. */ |
| 7365 | PyUnicodeObject *result; |
| 7366 | |
| 7367 | str = _PyString_FormatLong(val, flags, prec, type, &buf, &len); |
| 7368 | if (!str) |
| 7369 | return NULL; |
| 7370 | result = _PyUnicode_New(len); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 7371 | if (!result) { |
| 7372 | Py_DECREF(str); |
| 7373 | return NULL; |
| 7374 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7375 | for (i = 0; i < len; i++) |
| 7376 | result->str[i] = buf[i]; |
| 7377 | result->str[len] = 0; |
| 7378 | Py_DECREF(str); |
| 7379 | return (PyObject*)result; |
| 7380 | } |
| 7381 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7382 | static int |
| 7383 | formatint(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7384 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7385 | int flags, |
| 7386 | int prec, |
| 7387 | int type, |
| 7388 | PyObject *v) |
| 7389 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7390 | /* fmt = '%#.' + `prec` + 'l' + `type` |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7391 | * worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
| 7392 | * + 1 + 1 |
| 7393 | * = 24 |
| 7394 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7395 | char fmt[64]; /* plenty big enough! */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7396 | char *sign; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7397 | long x; |
| 7398 | |
| 7399 | x = PyInt_AsLong(v); |
| 7400 | if (x == -1 && PyErr_Occurred()) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7401 | return -1; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7402 | if (x < 0 && type == 'u') { |
| 7403 | type = 'd'; |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 7404 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7405 | if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) |
| 7406 | sign = "-"; |
| 7407 | else |
| 7408 | sign = ""; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7409 | if (prec < 0) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7410 | prec = 1; |
| 7411 | |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7412 | /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) |
| 7413 | * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7414 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7415 | if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7416 | PyErr_SetString(PyExc_OverflowError, |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7417 | "formatted integer is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7418 | return -1; |
| 7419 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7420 | |
| 7421 | if ((flags & F_ALT) && |
| 7422 | (type == 'x' || type == 'X')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7423 | /* When converting under %#x or %#X, there are a number |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7424 | * of issues that cause pain: |
| 7425 | * - when 0 is being converted, the C standard leaves off |
| 7426 | * the '0x' or '0X', which is inconsistent with other |
| 7427 | * %#x/%#X conversions and inconsistent with Python's |
| 7428 | * hex() function |
| 7429 | * - there are platforms that violate the standard and |
| 7430 | * convert 0 with the '0x' or '0X' |
| 7431 | * (Metrowerks, Compaq Tru64) |
| 7432 | * - there are platforms that give '0x' when converting |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7433 | * under %#X, but convert 0 in accordance with the |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7434 | * standard (OS/2 EMX) |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7435 | * |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7436 | * We can achieve the desired consistency by inserting our |
| 7437 | * own '0x' or '0X' prefix, and substituting %x/%X in place |
| 7438 | * of %#x/%#X. |
| 7439 | * |
| 7440 | * Note that this is the same approach as used in |
| 7441 | * formatint() in stringobject.c |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 7442 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7443 | PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", |
| 7444 | sign, type, prec, type); |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 7445 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7446 | else { |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7447 | PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", |
| 7448 | sign, (flags&F_ALT) ? "#" : "", |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 7449 | prec, type); |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 7450 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7451 | if (sign[0]) |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7452 | return longtounicode(buf, buflen, fmt, -x); |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7453 | else |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 7454 | return longtounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7455 | } |
| 7456 | |
| 7457 | static int |
| 7458 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7459 | size_t buflen, |
| 7460 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7461 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7462 | /* presume that the buffer is at least 2 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7463 | if (PyUnicode_Check(v)) { |
| 7464 | if (PyUnicode_GET_SIZE(v) != 1) |
| 7465 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7466 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7467 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7468 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7469 | else if (PyString_Check(v)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7470 | if (PyString_GET_SIZE(v) != 1) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7471 | goto onError; |
| 7472 | buf[0] = (Py_UNICODE)PyString_AS_STRING(v)[0]; |
| 7473 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7474 | |
| 7475 | else { |
| 7476 | /* Integer input truncated to a character */ |
| 7477 | long x; |
| 7478 | x = PyInt_AsLong(v); |
| 7479 | if (x == -1 && PyErr_Occurred()) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7480 | goto onError; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 7481 | #ifdef Py_UNICODE_WIDE |
| 7482 | if (x < 0 || x > 0x10ffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 7483 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 7484 | "%c arg not in range(0x110000) " |
| 7485 | "(wide Python build)"); |
| 7486 | return -1; |
| 7487 | } |
| 7488 | #else |
| 7489 | if (x < 0 || x > 0xffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 7490 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 7491 | "%c arg not in range(0x10000) " |
| 7492 | "(narrow Python build)"); |
| 7493 | return -1; |
| 7494 | } |
| 7495 | #endif |
| 7496 | buf[0] = (Py_UNICODE) x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7497 | } |
| 7498 | buf[1] = '\0'; |
| 7499 | return 1; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 7500 | |
| 7501 | onError: |
| 7502 | PyErr_SetString(PyExc_TypeError, |
| 7503 | "%c requires int or char"); |
| 7504 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7505 | } |
| 7506 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7507 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 7508 | |
| 7509 | FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
| 7510 | chars are formatted. XXX This is a magic number. Each formatting |
| 7511 | routine does bounds checking to ensure no overflow, but a better |
| 7512 | solution may be to malloc a buffer of appropriate size for each |
| 7513 | format. For now, the current solution is sufficient. |
| 7514 | */ |
| 7515 | #define FORMATBUFLEN (size_t)120 |
| 7516 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7517 | PyObject *PyUnicode_Format(PyObject *format, |
| 7518 | PyObject *args) |
| 7519 | { |
| 7520 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7521 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7522 | int args_owned = 0; |
| 7523 | PyUnicodeObject *result = NULL; |
| 7524 | PyObject *dict = NULL; |
| 7525 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7526 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7527 | if (format == NULL || args == NULL) { |
| 7528 | PyErr_BadInternalCall(); |
| 7529 | return NULL; |
| 7530 | } |
| 7531 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7532 | if (uformat == NULL) |
| 7533 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7534 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 7535 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 7536 | |
| 7537 | reslen = rescnt = fmtcnt + 100; |
| 7538 | result = _PyUnicode_New(reslen); |
| 7539 | if (result == NULL) |
| 7540 | goto onError; |
| 7541 | res = PyUnicode_AS_UNICODE(result); |
| 7542 | |
| 7543 | if (PyTuple_Check(args)) { |
| 7544 | arglen = PyTuple_Size(args); |
| 7545 | argidx = 0; |
| 7546 | } |
| 7547 | else { |
| 7548 | arglen = -1; |
| 7549 | argidx = -2; |
| 7550 | } |
Neal Norwitz | 80a1bf4 | 2002-11-12 23:01:12 +0000 | [diff] [blame] | 7551 | if (args->ob_type->tp_as_mapping && !PyTuple_Check(args) && |
| 7552 | !PyObject_TypeCheck(args, &PyBaseString_Type)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7553 | dict = args; |
| 7554 | |
| 7555 | while (--fmtcnt >= 0) { |
| 7556 | if (*fmt != '%') { |
| 7557 | if (--rescnt < 0) { |
| 7558 | rescnt = fmtcnt + 100; |
| 7559 | reslen += rescnt; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7560 | if (_PyUnicode_Resize(&result, reslen) < 0) |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 7561 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7562 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 7563 | --rescnt; |
| 7564 | } |
| 7565 | *res++ = *fmt++; |
| 7566 | } |
| 7567 | else { |
| 7568 | /* Got a format specifier */ |
| 7569 | int flags = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7570 | Py_ssize_t width = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7571 | int prec = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7572 | Py_UNICODE c = '\0'; |
| 7573 | Py_UNICODE fill; |
| 7574 | PyObject *v = NULL; |
| 7575 | PyObject *temp = NULL; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7576 | Py_UNICODE *pbuf; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7577 | Py_UNICODE sign; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7578 | Py_ssize_t len; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7579 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7580 | |
| 7581 | fmt++; |
| 7582 | if (*fmt == '(') { |
| 7583 | Py_UNICODE *keystart; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7584 | Py_ssize_t keylen; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7585 | PyObject *key; |
| 7586 | int pcount = 1; |
| 7587 | |
| 7588 | if (dict == NULL) { |
| 7589 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7590 | "format requires a mapping"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7591 | goto onError; |
| 7592 | } |
| 7593 | ++fmt; |
| 7594 | --fmtcnt; |
| 7595 | keystart = fmt; |
| 7596 | /* Skip over balanced parentheses */ |
| 7597 | while (pcount > 0 && --fmtcnt >= 0) { |
| 7598 | if (*fmt == ')') |
| 7599 | --pcount; |
| 7600 | else if (*fmt == '(') |
| 7601 | ++pcount; |
| 7602 | fmt++; |
| 7603 | } |
| 7604 | keylen = fmt - keystart - 1; |
| 7605 | if (fmtcnt < 0 || pcount > 0) { |
| 7606 | PyErr_SetString(PyExc_ValueError, |
| 7607 | "incomplete format key"); |
| 7608 | goto onError; |
| 7609 | } |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 7610 | #if 0 |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7611 | /* keys are converted to strings using UTF-8 and |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7612 | then looked up since Python uses strings to hold |
| 7613 | variables names etc. in its namespaces and we |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7614 | wouldn't want to break common idioms. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7615 | key = PyUnicode_EncodeUTF8(keystart, |
| 7616 | keylen, |
| 7617 | NULL); |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 7618 | #else |
| 7619 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 7620 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7621 | if (key == NULL) |
| 7622 | goto onError; |
| 7623 | if (args_owned) { |
| 7624 | Py_DECREF(args); |
| 7625 | args_owned = 0; |
| 7626 | } |
| 7627 | args = PyObject_GetItem(dict, key); |
| 7628 | Py_DECREF(key); |
| 7629 | if (args == NULL) { |
| 7630 | goto onError; |
| 7631 | } |
| 7632 | args_owned = 1; |
| 7633 | arglen = -1; |
| 7634 | argidx = -2; |
| 7635 | } |
| 7636 | while (--fmtcnt >= 0) { |
| 7637 | switch (c = *fmt++) { |
| 7638 | case '-': flags |= F_LJUST; continue; |
| 7639 | case '+': flags |= F_SIGN; continue; |
| 7640 | case ' ': flags |= F_BLANK; continue; |
| 7641 | case '#': flags |= F_ALT; continue; |
| 7642 | case '0': flags |= F_ZERO; continue; |
| 7643 | } |
| 7644 | break; |
| 7645 | } |
| 7646 | if (c == '*') { |
| 7647 | v = getnextarg(args, arglen, &argidx); |
| 7648 | if (v == NULL) |
| 7649 | goto onError; |
| 7650 | if (!PyInt_Check(v)) { |
| 7651 | PyErr_SetString(PyExc_TypeError, |
| 7652 | "* wants int"); |
| 7653 | goto onError; |
| 7654 | } |
| 7655 | width = PyInt_AsLong(v); |
| 7656 | if (width < 0) { |
| 7657 | flags |= F_LJUST; |
| 7658 | width = -width; |
| 7659 | } |
| 7660 | if (--fmtcnt >= 0) |
| 7661 | c = *fmt++; |
| 7662 | } |
| 7663 | else if (c >= '0' && c <= '9') { |
| 7664 | width = c - '0'; |
| 7665 | while (--fmtcnt >= 0) { |
| 7666 | c = *fmt++; |
| 7667 | if (c < '0' || c > '9') |
| 7668 | break; |
| 7669 | if ((width*10) / 10 != width) { |
| 7670 | PyErr_SetString(PyExc_ValueError, |
| 7671 | "width too big"); |
| 7672 | goto onError; |
| 7673 | } |
| 7674 | width = width*10 + (c - '0'); |
| 7675 | } |
| 7676 | } |
| 7677 | if (c == '.') { |
| 7678 | prec = 0; |
| 7679 | if (--fmtcnt >= 0) |
| 7680 | c = *fmt++; |
| 7681 | if (c == '*') { |
| 7682 | v = getnextarg(args, arglen, &argidx); |
| 7683 | if (v == NULL) |
| 7684 | goto onError; |
| 7685 | if (!PyInt_Check(v)) { |
| 7686 | PyErr_SetString(PyExc_TypeError, |
| 7687 | "* wants int"); |
| 7688 | goto onError; |
| 7689 | } |
| 7690 | prec = PyInt_AsLong(v); |
| 7691 | if (prec < 0) |
| 7692 | prec = 0; |
| 7693 | if (--fmtcnt >= 0) |
| 7694 | c = *fmt++; |
| 7695 | } |
| 7696 | else if (c >= '0' && c <= '9') { |
| 7697 | prec = c - '0'; |
| 7698 | while (--fmtcnt >= 0) { |
| 7699 | c = Py_CHARMASK(*fmt++); |
| 7700 | if (c < '0' || c > '9') |
| 7701 | break; |
| 7702 | if ((prec*10) / 10 != prec) { |
| 7703 | PyErr_SetString(PyExc_ValueError, |
| 7704 | "prec too big"); |
| 7705 | goto onError; |
| 7706 | } |
| 7707 | prec = prec*10 + (c - '0'); |
| 7708 | } |
| 7709 | } |
| 7710 | } /* prec */ |
| 7711 | if (fmtcnt >= 0) { |
| 7712 | if (c == 'h' || c == 'l' || c == 'L') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7713 | if (--fmtcnt >= 0) |
| 7714 | c = *fmt++; |
| 7715 | } |
| 7716 | } |
| 7717 | if (fmtcnt < 0) { |
| 7718 | PyErr_SetString(PyExc_ValueError, |
| 7719 | "incomplete format"); |
| 7720 | goto onError; |
| 7721 | } |
| 7722 | if (c != '%') { |
| 7723 | v = getnextarg(args, arglen, &argidx); |
| 7724 | if (v == NULL) |
| 7725 | goto onError; |
| 7726 | } |
| 7727 | sign = 0; |
| 7728 | fill = ' '; |
| 7729 | switch (c) { |
| 7730 | |
| 7731 | case '%': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7732 | pbuf = formatbuf; |
| 7733 | /* presume that buffer length is at least 1 */ |
| 7734 | pbuf[0] = '%'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7735 | len = 1; |
| 7736 | break; |
| 7737 | |
| 7738 | case 's': |
| 7739 | case 'r': |
| 7740 | if (PyUnicode_Check(v) && c == 's') { |
| 7741 | temp = v; |
| 7742 | Py_INCREF(temp); |
| 7743 | } |
| 7744 | else { |
| 7745 | PyObject *unicode; |
| 7746 | if (c == 's') |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 7747 | temp = PyObject_Unicode(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7748 | else |
| 7749 | temp = PyObject_Repr(v); |
| 7750 | if (temp == NULL) |
| 7751 | goto onError; |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 7752 | if (PyUnicode_Check(temp)) |
| 7753 | /* nothing to do */; |
| 7754 | else if (PyString_Check(temp)) { |
| 7755 | /* convert to string to Unicode */ |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7756 | unicode = PyUnicode_Decode(PyString_AS_STRING(temp), |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7757 | PyString_GET_SIZE(temp), |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7758 | NULL, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7759 | "strict"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7760 | Py_DECREF(temp); |
| 7761 | temp = unicode; |
| 7762 | if (temp == NULL) |
| 7763 | goto onError; |
| 7764 | } |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 7765 | else { |
| 7766 | Py_DECREF(temp); |
| 7767 | PyErr_SetString(PyExc_TypeError, |
| 7768 | "%s argument has non-string str()"); |
| 7769 | goto onError; |
| 7770 | } |
| 7771 | } |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7772 | pbuf = PyUnicode_AS_UNICODE(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7773 | len = PyUnicode_GET_SIZE(temp); |
| 7774 | if (prec >= 0 && len > prec) |
| 7775 | len = prec; |
| 7776 | break; |
| 7777 | |
| 7778 | case 'i': |
| 7779 | case 'd': |
| 7780 | case 'u': |
| 7781 | case 'o': |
| 7782 | case 'x': |
| 7783 | case 'X': |
| 7784 | if (c == 'i') |
| 7785 | c = 'd'; |
Tim Peters | a3a3a03 | 2000-11-30 05:22:44 +0000 | [diff] [blame] | 7786 | if (PyLong_Check(v)) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7787 | temp = formatlong(v, flags, prec, c); |
| 7788 | if (!temp) |
| 7789 | goto onError; |
| 7790 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 7791 | len = PyUnicode_GET_SIZE(temp); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7792 | sign = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7793 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7794 | else { |
| 7795 | pbuf = formatbuf; |
| 7796 | len = formatint(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 7797 | flags, prec, c, v); |
| 7798 | if (len < 0) |
| 7799 | goto onError; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 7800 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7801 | } |
| 7802 | if (flags & F_ZERO) |
| 7803 | fill = '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7804 | break; |
| 7805 | |
| 7806 | case 'e': |
| 7807 | case 'E': |
| 7808 | case 'f': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 7809 | case 'F': |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7810 | case 'g': |
| 7811 | case 'G': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 7812 | if (c == 'F') |
| 7813 | c = 'f'; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7814 | pbuf = formatbuf; |
| 7815 | len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 7816 | flags, prec, c, v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7817 | if (len < 0) |
| 7818 | goto onError; |
| 7819 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7820 | if (flags & F_ZERO) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7821 | fill = '0'; |
| 7822 | break; |
| 7823 | |
| 7824 | case 'c': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7825 | pbuf = formatbuf; |
| 7826 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7827 | if (len < 0) |
| 7828 | goto onError; |
| 7829 | break; |
| 7830 | |
| 7831 | default: |
| 7832 | PyErr_Format(PyExc_ValueError, |
Andrew M. Kuchling | 6ca8917 | 2000-12-15 13:07:46 +0000 | [diff] [blame] | 7833 | "unsupported format character '%c' (0x%x) " |
Armin Rigo | 4b63c21 | 2006-10-04 11:44:06 +0000 | [diff] [blame] | 7834 | "at index %zd", |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7835 | (31<=c && c<=126) ? (char)c : '?', |
Marc-André Lemburg | 24e53b6 | 2002-09-24 09:32:14 +0000 | [diff] [blame] | 7836 | (int)c, |
Armin Rigo | 4b63c21 | 2006-10-04 11:44:06 +0000 | [diff] [blame] | 7837 | (Py_ssize_t)(fmt - 1 - |
| 7838 | PyUnicode_AS_UNICODE(uformat))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7839 | goto onError; |
| 7840 | } |
| 7841 | if (sign) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 7842 | if (*pbuf == '-' || *pbuf == '+') { |
| 7843 | sign = *pbuf++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7844 | len--; |
| 7845 | } |
| 7846 | else if (flags & F_SIGN) |
| 7847 | sign = '+'; |
| 7848 | else if (flags & F_BLANK) |
| 7849 | sign = ' '; |
| 7850 | else |
| 7851 | sign = 0; |
| 7852 | } |
| 7853 | if (width < len) |
| 7854 | width = len; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 7855 | if (rescnt - (sign != 0) < width) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7856 | reslen -= rescnt; |
| 7857 | rescnt = width + fmtcnt + 100; |
| 7858 | reslen += rescnt; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 7859 | if (reslen < 0) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 7860 | Py_XDECREF(temp); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7861 | PyErr_NoMemory(); |
| 7862 | goto onError; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 7863 | } |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7864 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 7865 | Py_XDECREF(temp); |
| 7866 | goto onError; |
| 7867 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7868 | res = PyUnicode_AS_UNICODE(result) |
| 7869 | + reslen - rescnt; |
| 7870 | } |
| 7871 | if (sign) { |
| 7872 | if (fill != ' ') |
| 7873 | *res++ = sign; |
| 7874 | rescnt--; |
| 7875 | if (width > len) |
| 7876 | width--; |
| 7877 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7878 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
| 7879 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 7880 | assert(pbuf[1] == c); |
| 7881 | if (fill != ' ') { |
| 7882 | *res++ = *pbuf++; |
| 7883 | *res++ = *pbuf++; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7884 | } |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 7885 | rescnt -= 2; |
| 7886 | width -= 2; |
| 7887 | if (width < 0) |
| 7888 | width = 0; |
| 7889 | len -= 2; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7890 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7891 | if (width > len && !(flags & F_LJUST)) { |
| 7892 | do { |
| 7893 | --rescnt; |
| 7894 | *res++ = fill; |
| 7895 | } while (--width > len); |
| 7896 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7897 | if (fill == ' ') { |
| 7898 | if (sign) |
| 7899 | *res++ = sign; |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 7900 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7901 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 7902 | assert(pbuf[1] == c); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 7903 | *res++ = *pbuf++; |
| 7904 | *res++ = *pbuf++; |
| 7905 | } |
| 7906 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 7907 | Py_UNICODE_COPY(res, pbuf, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7908 | res += len; |
| 7909 | rescnt -= len; |
| 7910 | while (--width >= len) { |
| 7911 | --rescnt; |
| 7912 | *res++ = ' '; |
| 7913 | } |
| 7914 | if (dict && (argidx < arglen) && c != '%') { |
| 7915 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 7916 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7917 | Py_XDECREF(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7918 | goto onError; |
| 7919 | } |
| 7920 | Py_XDECREF(temp); |
| 7921 | } /* '%' */ |
| 7922 | } /* until end */ |
| 7923 | if (argidx < arglen && !dict) { |
| 7924 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 7925 | "not all arguments converted during string formatting"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7926 | goto onError; |
| 7927 | } |
| 7928 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 7929 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
| 7930 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7931 | if (args_owned) { |
| 7932 | Py_DECREF(args); |
| 7933 | } |
| 7934 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7935 | return (PyObject *)result; |
| 7936 | |
| 7937 | onError: |
| 7938 | Py_XDECREF(result); |
| 7939 | Py_DECREF(uformat); |
| 7940 | if (args_owned) { |
| 7941 | Py_DECREF(args); |
| 7942 | } |
| 7943 | return NULL; |
| 7944 | } |
| 7945 | |
| 7946 | static PyBufferProcs unicode_as_buffer = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7947 | (readbufferproc) unicode_buffer_getreadbuf, |
| 7948 | (writebufferproc) unicode_buffer_getwritebuf, |
| 7949 | (segcountproc) unicode_buffer_getsegcount, |
| 7950 | (charbufferproc) unicode_buffer_getcharbuf, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7951 | }; |
| 7952 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 7953 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7954 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 7955 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7956 | static PyObject * |
| 7957 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 7958 | { |
| 7959 | PyObject *x = NULL; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 7960 | static char *kwlist[] = {"string", "encoding", "errors", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7961 | char *encoding = NULL; |
| 7962 | char *errors = NULL; |
| 7963 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7964 | if (type != &PyUnicode_Type) |
| 7965 | return unicode_subtype_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7966 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:unicode", |
| 7967 | kwlist, &x, &encoding, &errors)) |
| 7968 | return NULL; |
| 7969 | if (x == NULL) |
| 7970 | return (PyObject *)_PyUnicode_New(0); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 7971 | if (encoding == NULL && errors == NULL) |
| 7972 | return PyObject_Unicode(x); |
| 7973 | else |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 7974 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
| 7975 | } |
| 7976 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7977 | static PyObject * |
| 7978 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 7979 | { |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7980 | PyUnicodeObject *tmp, *pnew; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7981 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7982 | |
| 7983 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 7984 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 7985 | if (tmp == NULL) |
| 7986 | return NULL; |
| 7987 | assert(PyUnicode_Check(tmp)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7988 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 7989 | if (pnew == NULL) { |
| 7990 | Py_DECREF(tmp); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7991 | return NULL; |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 7992 | } |
Neal Norwitz | b3635f9 | 2008-03-18 04:17:36 +0000 | [diff] [blame] | 7993 | pnew->str = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 7994 | if (pnew->str == NULL) { |
| 7995 | _Py_ForgetReference((PyObject *)pnew); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 7996 | PyObject_Del(pnew); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 7997 | Py_DECREF(tmp); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 7998 | return PyErr_NoMemory(); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 7999 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8000 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 8001 | pnew->length = n; |
| 8002 | pnew->hash = tmp->hash; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8003 | Py_DECREF(tmp); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8004 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8005 | } |
| 8006 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8007 | PyDoc_STRVAR(unicode_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8008 | "unicode(string [, encoding[, errors]]) -> object\n\ |
| 8009 | \n\ |
| 8010 | Create a new Unicode object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 8011 | encoding defaults to the current default string encoding.\n\ |
| 8012 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8013 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8014 | PyTypeObject PyUnicode_Type = { |
| 8015 | PyObject_HEAD_INIT(&PyType_Type) |
| 8016 | 0, /* ob_size */ |
| 8017 | "unicode", /* tp_name */ |
| 8018 | sizeof(PyUnicodeObject), /* tp_size */ |
| 8019 | 0, /* tp_itemsize */ |
| 8020 | /* Slots */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 8021 | (destructor)unicode_dealloc, /* tp_dealloc */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8022 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8023 | 0, /* tp_getattr */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8024 | 0, /* tp_setattr */ |
Marc-André Lemburg | 040f76b | 2006-08-14 10:55:19 +0000 | [diff] [blame] | 8025 | 0, /* tp_compare */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 8026 | unicode_repr, /* tp_repr */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8027 | &unicode_as_number, /* tp_as_number */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8028 | &unicode_as_sequence, /* tp_as_sequence */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8029 | &unicode_as_mapping, /* tp_as_mapping */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8030 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 8031 | 0, /* tp_call*/ |
| 8032 | (reprfunc) unicode_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8033 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 8034 | 0, /* tp_setattro */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8035 | &unicode_as_buffer, /* tp_as_buffer */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8036 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
| 8037 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8038 | unicode_doc, /* tp_doc */ |
| 8039 | 0, /* tp_traverse */ |
| 8040 | 0, /* tp_clear */ |
Marc-André Lemburg | 040f76b | 2006-08-14 10:55:19 +0000 | [diff] [blame] | 8041 | PyUnicode_RichCompare, /* tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8042 | 0, /* tp_weaklistoffset */ |
| 8043 | 0, /* tp_iter */ |
| 8044 | 0, /* tp_iternext */ |
| 8045 | unicode_methods, /* tp_methods */ |
| 8046 | 0, /* tp_members */ |
| 8047 | 0, /* tp_getset */ |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 8048 | &PyBaseString_Type, /* tp_base */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8049 | 0, /* tp_dict */ |
| 8050 | 0, /* tp_descr_get */ |
| 8051 | 0, /* tp_descr_set */ |
| 8052 | 0, /* tp_dictoffset */ |
| 8053 | 0, /* tp_init */ |
| 8054 | 0, /* tp_alloc */ |
| 8055 | unicode_new, /* tp_new */ |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 8056 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8057 | }; |
| 8058 | |
| 8059 | /* Initialize the Unicode implementation */ |
| 8060 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 8061 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8062 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8063 | int i; |
| 8064 | |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 8065 | /* XXX - move this array to unicodectype.c ? */ |
| 8066 | Py_UNICODE linebreak[] = { |
| 8067 | 0x000A, /* LINE FEED */ |
| 8068 | 0x000D, /* CARRIAGE RETURN */ |
| 8069 | 0x001C, /* FILE SEPARATOR */ |
| 8070 | 0x001D, /* GROUP SEPARATOR */ |
| 8071 | 0x001E, /* RECORD SEPARATOR */ |
| 8072 | 0x0085, /* NEXT LINE */ |
| 8073 | 0x2028, /* LINE SEPARATOR */ |
| 8074 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 8075 | }; |
| 8076 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8077 | /* Init the implementation */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8078 | unicode_freelist = NULL; |
| 8079 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8080 | unicode_empty = _PyUnicode_New(0); |
Neal Norwitz | e1fdb32 | 2006-07-21 05:32:28 +0000 | [diff] [blame] | 8081 | if (!unicode_empty) |
| 8082 | return; |
| 8083 | |
Marc-André Lemburg | 90e8147 | 2000-06-07 09:13:21 +0000 | [diff] [blame] | 8084 | strcpy(unicode_default_encoding, "ascii"); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8085 | for (i = 0; i < 256; i++) |
| 8086 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 8087 | if (PyType_Ready(&PyUnicode_Type) < 0) |
| 8088 | Py_FatalError("Can't initialize 'unicode'"); |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 8089 | |
| 8090 | /* initialize the linebreak bloom filter */ |
| 8091 | bloom_linebreak = make_bloom_mask( |
| 8092 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 8093 | ); |
Neal Norwitz | de4c78a | 2006-06-13 08:28:19 +0000 | [diff] [blame] | 8094 | |
| 8095 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8096 | } |
| 8097 | |
| 8098 | /* Finalize the Unicode implementation */ |
| 8099 | |
| 8100 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 8101 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8102 | { |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 8103 | PyUnicodeObject *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8104 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8105 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 8106 | Py_XDECREF(unicode_empty); |
| 8107 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 8108 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8109 | for (i = 0; i < 256; i++) { |
| 8110 | if (unicode_latin1[i]) { |
| 8111 | Py_DECREF(unicode_latin1[i]); |
| 8112 | unicode_latin1[i] = NULL; |
| 8113 | } |
| 8114 | } |
| 8115 | |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 8116 | for (u = unicode_freelist; u != NULL;) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8117 | PyUnicodeObject *v = u; |
| 8118 | u = *(PyUnicodeObject **)u; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 8119 | if (v->str) |
Neal Norwitz | b3635f9 | 2008-03-18 04:17:36 +0000 | [diff] [blame] | 8120 | PyObject_DEL(v->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 8121 | Py_XDECREF(v->defenc); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 8122 | PyObject_Del(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8123 | } |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8124 | unicode_freelist = NULL; |
| 8125 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8126 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 8127 | |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 8128 | #ifdef __cplusplus |
| 8129 | } |
| 8130 | #endif |
| 8131 | |
| 8132 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 8133 | /* |
| 8134 | Local variables: |
| 8135 | c-basic-offset: 4 |
| 8136 | indent-tabs-mode: nil |
| 8137 | End: |
| 8138 | */ |