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 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +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 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +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" |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 44 | #include "bytes_methods.h" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 46 | #include "unicodeobject.h" |
Marc-André Lemburg | d49e5b4 | 2000-06-30 14:58:20 +0000 | [diff] [blame] | 47 | #include "ucnhash.h" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 48 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 49 | #include "formatter_unicode.h" |
| 50 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 51 | #ifdef MS_WINDOWS |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 52 | #include <windows.h> |
| 53 | #endif |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 54 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 55 | /* Limit for the Unicode object free list */ |
| 56 | |
| 57 | #define MAX_UNICODE_FREELIST_SIZE 1024 |
| 58 | |
| 59 | /* Limit for the Unicode object free list stay alive optimization. |
| 60 | |
| 61 | The implementation will keep allocated Unicode memory intact for |
| 62 | all objects on the free list having a size less than this |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 63 | limit. This reduces malloc() overhead for small Unicode objects. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 64 | |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 65 | At worst this will result in MAX_UNICODE_FREELIST_SIZE * |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 66 | (sizeof(PyUnicodeObject) + KEEPALIVE_SIZE_LIMIT + |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 67 | malloc()-overhead) bytes of unused garbage. |
| 68 | |
| 69 | Setting the limit to 0 effectively turns the feature off. |
| 70 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 71 | Note: This is an experimental feature ! If you get core dumps when |
| 72 | using Unicode objects, turn this feature off. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 73 | |
| 74 | */ |
| 75 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 76 | #define KEEPALIVE_SIZE_LIMIT 9 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 77 | |
| 78 | /* Endianness switches; defaults to little endian */ |
| 79 | |
| 80 | #ifdef WORDS_BIGENDIAN |
| 81 | # define BYTEORDER_IS_BIG_ENDIAN |
| 82 | #else |
| 83 | # define BYTEORDER_IS_LITTLE_ENDIAN |
| 84 | #endif |
| 85 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 86 | /* --- Globals ------------------------------------------------------------ |
| 87 | |
| 88 | The globals are initialized by the _PyUnicode_Init() API and should |
| 89 | not be used before calling that API. |
| 90 | |
| 91 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 92 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 93 | |
| 94 | #ifdef __cplusplus |
| 95 | extern "C" { |
| 96 | #endif |
| 97 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 98 | /* This dictionary holds all interned unicode strings. Note that references |
| 99 | to strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 100 | When the interned string reaches a refcnt of 0 the string deallocation |
| 101 | function will delete the reference from this dictionary. |
| 102 | |
| 103 | Another way to look at this is that to say that the actual reference |
| 104 | count of a string is: s->ob_refcnt + (s->ob_sstate?2:0) |
| 105 | */ |
| 106 | static PyObject *interned; |
| 107 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 108 | /* Free list for Unicode objects */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 109 | static PyUnicodeObject *unicode_freelist; |
| 110 | static int unicode_freelist_size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 111 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 112 | /* The empty Unicode object is shared to improve performance. */ |
| 113 | static PyUnicodeObject *unicode_empty; |
| 114 | |
| 115 | /* Single character Unicode strings in the Latin-1 range are being |
| 116 | shared as well. */ |
| 117 | static PyUnicodeObject *unicode_latin1[256]; |
| 118 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 119 | /* Default encoding to use and assume when NULL is passed as encoding |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 120 | parameter; it is fixed to "utf-8". Always use the |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 121 | PyUnicode_GetDefaultEncoding() API to access this global. |
| 122 | |
Alexandre Vassalotti | 3d2fd7f | 2007-10-16 00:26:33 +0000 | [diff] [blame] | 123 | Don't forget to alter Py_FileSystemDefaultEncoding if you change the |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 124 | hard coded default! |
| 125 | */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 126 | static const char unicode_default_encoding[] = "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 127 | |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 128 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 129 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 130 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 131 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 132 | return 0x10FFFF; |
| 133 | #else |
| 134 | /* This is actually an illegal character, so it should |
| 135 | not be passed to unichr. */ |
| 136 | return 0xFFFF; |
| 137 | #endif |
| 138 | } |
| 139 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 140 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 141 | |
| 142 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 143 | to keep things simple, we use a single bitmask, using the least 5 |
| 144 | bits from each unicode characters as the bit index. */ |
| 145 | |
| 146 | /* the linebreak mask is set up by Unicode_Init below */ |
| 147 | |
| 148 | #define BLOOM_MASK unsigned long |
| 149 | |
| 150 | static BLOOM_MASK bloom_linebreak; |
| 151 | |
| 152 | #define BLOOM(mask, ch) ((mask & (1 << ((ch) & 0x1F)))) |
| 153 | |
| 154 | #define BLOOM_LINEBREAK(ch)\ |
| 155 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK((ch))) |
| 156 | |
| 157 | Py_LOCAL_INLINE(BLOOM_MASK) make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len) |
| 158 | { |
| 159 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 160 | |
| 161 | long mask; |
| 162 | Py_ssize_t i; |
| 163 | |
| 164 | mask = 0; |
| 165 | for (i = 0; i < len; i++) |
| 166 | mask |= (1 << (ptr[i] & 0x1F)); |
| 167 | |
| 168 | return mask; |
| 169 | } |
| 170 | |
| 171 | Py_LOCAL_INLINE(int) unicode_member(Py_UNICODE chr, Py_UNICODE* set, Py_ssize_t setlen) |
| 172 | { |
| 173 | Py_ssize_t i; |
| 174 | |
| 175 | for (i = 0; i < setlen; i++) |
| 176 | if (set[i] == chr) |
| 177 | return 1; |
| 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | #define BLOOM_MEMBER(mask, chr, set, setlen)\ |
| 183 | BLOOM(mask, chr) && unicode_member(chr, set, setlen) |
| 184 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 185 | /* --- Unicode Object ----------------------------------------------------- */ |
| 186 | |
| 187 | static |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 188 | int unicode_resize(register PyUnicodeObject *unicode, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 189 | Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 190 | { |
| 191 | void *oldstr; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 192 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 193 | /* Shortcut if there's nothing much to do. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 194 | if (unicode->length == length) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 195 | goto reset; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 196 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 197 | /* Resizing shared object (unicode_empty or single character |
| 198 | objects) in-place is not allowed. Use PyUnicode_Resize() |
| 199 | instead ! */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 200 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 201 | if (unicode == unicode_empty || |
| 202 | (unicode->length == 1 && |
| 203 | unicode->str[0] < 256U && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 204 | unicode_latin1[unicode->str[0]] == unicode)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 205 | PyErr_SetString(PyExc_SystemError, |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 206 | "can't resize shared unicode objects"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 207 | return -1; |
| 208 | } |
| 209 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 210 | /* We allocate one more byte to make sure the string is Ux0000 terminated. |
| 211 | The overallocation is also used by fastsearch, which assumes that it's |
| 212 | safe to look at str[length] (without making any assumptions about what |
| 213 | it contains). */ |
| 214 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 215 | oldstr = unicode->str; |
| 216 | PyMem_RESIZE(unicode->str, Py_UNICODE, length + 1); |
| 217 | if (!unicode->str) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 218 | unicode->str = (Py_UNICODE *)oldstr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 219 | PyErr_NoMemory(); |
| 220 | return -1; |
| 221 | } |
| 222 | unicode->str[length] = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 223 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 224 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 225 | reset: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 226 | /* Reset the object caches */ |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 227 | if (unicode->defenc) { |
| 228 | Py_DECREF(unicode->defenc); |
| 229 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 230 | } |
| 231 | unicode->hash = -1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 232 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | /* We allocate one more byte to make sure the string is |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 237 | Ux0000 terminated; some code (e.g. new_identifier) |
| 238 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 239 | |
| 240 | XXX This allocator could further be enhanced by assuring that the |
| 241 | free list never reduces its size below 1. |
| 242 | |
| 243 | */ |
| 244 | |
| 245 | static |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 246 | PyUnicodeObject *_PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 247 | { |
| 248 | register PyUnicodeObject *unicode; |
| 249 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 250 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 251 | if (length == 0 && unicode_empty != NULL) { |
| 252 | Py_INCREF(unicode_empty); |
| 253 | return unicode_empty; |
| 254 | } |
| 255 | |
| 256 | /* Unicode freelist & memory allocation */ |
| 257 | if (unicode_freelist) { |
| 258 | unicode = unicode_freelist; |
Marc-André Lemburg | bea47e7 | 2000-06-17 20:31:17 +0000 | [diff] [blame] | 259 | unicode_freelist = *(PyUnicodeObject **)unicode; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 260 | unicode_freelist_size--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 261 | if (unicode->str) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 262 | /* Keep-Alive optimization: we only upsize the buffer, |
| 263 | never downsize it. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 264 | if ((unicode->length < length) && |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 265 | unicode_resize(unicode, length) < 0) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 266 | PyMem_DEL(unicode->str); |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 267 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 268 | } |
| 269 | } |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 270 | else { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 271 | unicode->str = PyMem_NEW(Py_UNICODE, length + 1); |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 272 | } |
| 273 | PyObject_INIT(unicode, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 274 | } |
| 275 | else { |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 276 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 277 | if (unicode == NULL) |
| 278 | return NULL; |
| 279 | unicode->str = PyMem_NEW(Py_UNICODE, length + 1); |
| 280 | } |
| 281 | |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 282 | if (!unicode->str) { |
| 283 | PyErr_NoMemory(); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 284 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 285 | } |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 286 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 287 | * the caller fails before initializing str -- unicode_resize() |
| 288 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 289 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 290 | * We don't want unicode_resize to read uninitialized memory in |
| 291 | * that case. |
| 292 | */ |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 293 | unicode->str[0] = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 294 | unicode->str[length] = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 295 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 296 | unicode->hash = -1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 297 | unicode->state = 0; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 298 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 299 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 300 | |
| 301 | onError: |
| 302 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 303 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 304 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | static |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 308 | void unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 309 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 310 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
| 311 | case SSTATE_NOT_INTERNED: |
| 312 | break; |
| 313 | |
| 314 | case SSTATE_INTERNED_MORTAL: |
| 315 | /* revive dead object temporarily for DelItem */ |
Martin v. Löwis | 5d7428b | 2007-07-21 18:47:48 +0000 | [diff] [blame] | 316 | Py_Refcnt(unicode) = 3; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 317 | if (PyDict_DelItem(interned, (PyObject *)unicode) != 0) |
| 318 | Py_FatalError( |
| 319 | "deletion of interned unicode string failed"); |
| 320 | break; |
| 321 | |
| 322 | case SSTATE_INTERNED_IMMORTAL: |
| 323 | Py_FatalError("Immortal interned unicode string died."); |
| 324 | |
| 325 | default: |
| 326 | Py_FatalError("Inconsistent interned unicode string state."); |
| 327 | } |
| 328 | |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 329 | if (PyUnicode_CheckExact(unicode) && |
| 330 | unicode_freelist_size < MAX_UNICODE_FREELIST_SIZE) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 331 | /* Keep-Alive optimization */ |
| 332 | if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 333 | PyMem_DEL(unicode->str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 334 | unicode->str = NULL; |
| 335 | unicode->length = 0; |
| 336 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 337 | if (unicode->defenc) { |
| 338 | Py_DECREF(unicode->defenc); |
| 339 | unicode->defenc = NULL; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 340 | } |
| 341 | /* Add to free list */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 342 | *(PyUnicodeObject **)unicode = unicode_freelist; |
| 343 | unicode_freelist = unicode; |
| 344 | unicode_freelist_size++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 345 | } |
| 346 | else { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 347 | PyMem_DEL(unicode->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 348 | Py_XDECREF(unicode->defenc); |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 349 | Py_Type(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 353 | int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 354 | { |
| 355 | register PyUnicodeObject *v; |
| 356 | |
| 357 | /* Argument checks */ |
| 358 | if (unicode == NULL) { |
| 359 | PyErr_BadInternalCall(); |
| 360 | return -1; |
| 361 | } |
| 362 | v = (PyUnicodeObject *)*unicode; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 363 | if (v == NULL || !PyUnicode_Check(v) || Py_Refcnt(v) != 1 || length < 0) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 364 | PyErr_BadInternalCall(); |
| 365 | return -1; |
| 366 | } |
| 367 | |
| 368 | /* Resizing unicode_empty and single character objects is not |
| 369 | possible since these are being shared. We simply return a fresh |
| 370 | copy with the same Unicode content. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 371 | if (v->length != length && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 372 | (v == unicode_empty || v->length == 1)) { |
| 373 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 374 | if (w == NULL) |
| 375 | return -1; |
| 376 | Py_UNICODE_COPY(w->str, v->str, |
| 377 | length < v->length ? length : v->length); |
Raymond Hettinger | c8df578 | 2003-03-09 07:30:43 +0000 | [diff] [blame] | 378 | Py_DECREF(*unicode); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 379 | *unicode = (PyObject *)w; |
| 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 384 | objects, since we can modify them in-place. */ |
| 385 | return unicode_resize(v, length); |
| 386 | } |
| 387 | |
| 388 | /* Internal API for use in unicodeobject.c only ! */ |
| 389 | #define _PyUnicode_Resize(unicodevar, length) \ |
| 390 | PyUnicode_Resize(((PyObject **)(unicodevar)), length) |
| 391 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 392 | PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 393 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 394 | { |
| 395 | PyUnicodeObject *unicode; |
| 396 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 397 | /* If the Unicode data is known at construction time, we can apply |
| 398 | some optimizations which share commonly used objects. */ |
| 399 | if (u != NULL) { |
| 400 | |
| 401 | /* Optimization for empty strings */ |
| 402 | if (size == 0 && unicode_empty != NULL) { |
| 403 | Py_INCREF(unicode_empty); |
| 404 | return (PyObject *)unicode_empty; |
| 405 | } |
| 406 | |
| 407 | /* Single character Unicode objects in the Latin-1 range are |
| 408 | shared when using this constructor */ |
| 409 | if (size == 1 && *u < 256) { |
| 410 | unicode = unicode_latin1[*u]; |
| 411 | if (!unicode) { |
| 412 | unicode = _PyUnicode_New(1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 413 | if (!unicode) |
| 414 | return NULL; |
Marc-André Lemburg | 8879a33 | 2001-06-07 12:26:56 +0000 | [diff] [blame] | 415 | unicode->str[0] = *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 416 | unicode_latin1[*u] = unicode; |
| 417 | } |
| 418 | Py_INCREF(unicode); |
| 419 | return (PyObject *)unicode; |
| 420 | } |
| 421 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 422 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 423 | unicode = _PyUnicode_New(size); |
| 424 | if (!unicode) |
| 425 | return NULL; |
| 426 | |
| 427 | /* Copy the Unicode data into the new object */ |
| 428 | if (u != NULL) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 429 | Py_UNICODE_COPY(unicode->str, u, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 430 | |
| 431 | return (PyObject *)unicode; |
| 432 | } |
| 433 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 434 | PyObject *PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 435 | { |
| 436 | PyUnicodeObject *unicode; |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 437 | /* If the Unicode data is known at construction time, we can apply |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 438 | some optimizations which share commonly used objects. |
| 439 | Also, this means the input must be UTF-8, so fall back to the |
| 440 | UTF-8 decoder at the end. */ |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 441 | if (u != NULL) { |
| 442 | |
| 443 | /* Optimization for empty strings */ |
| 444 | if (size == 0 && unicode_empty != NULL) { |
| 445 | Py_INCREF(unicode_empty); |
| 446 | return (PyObject *)unicode_empty; |
| 447 | } |
| 448 | |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 449 | /* Single characters are shared when using this constructor. |
| 450 | Restrict to ASCII, since the input must be UTF-8. */ |
| 451 | if (size == 1 && Py_CHARMASK(*u) < 128) { |
Guido van Rossum | 00058aa | 2007-07-19 18:21:28 +0000 | [diff] [blame] | 452 | unicode = unicode_latin1[Py_CHARMASK(*u)]; |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 453 | if (!unicode) { |
| 454 | unicode = _PyUnicode_New(1); |
| 455 | if (!unicode) |
| 456 | return NULL; |
Guido van Rossum | 00058aa | 2007-07-19 18:21:28 +0000 | [diff] [blame] | 457 | unicode->str[0] = Py_CHARMASK(*u); |
| 458 | unicode_latin1[Py_CHARMASK(*u)] = unicode; |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 459 | } |
| 460 | Py_INCREF(unicode); |
| 461 | return (PyObject *)unicode; |
| 462 | } |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 463 | |
| 464 | return PyUnicode_DecodeUTF8(u, size, NULL); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Walter Dörwald | 5550731 | 2007-05-18 13:12:10 +0000 | [diff] [blame] | 467 | unicode = _PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 468 | if (!unicode) |
| 469 | return NULL; |
| 470 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 471 | return (PyObject *)unicode; |
| 472 | } |
| 473 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 474 | PyObject *PyUnicode_FromString(const char *u) |
| 475 | { |
| 476 | size_t size = strlen(u); |
| 477 | if (size > PY_SSIZE_T_MAX) { |
| 478 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 479 | return NULL; |
| 480 | } |
| 481 | |
| 482 | return PyUnicode_FromStringAndSize(u, size); |
| 483 | } |
| 484 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 485 | #ifdef HAVE_WCHAR_H |
| 486 | |
| 487 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 488 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 489 | { |
| 490 | PyUnicodeObject *unicode; |
| 491 | |
| 492 | if (w == NULL) { |
| 493 | PyErr_BadInternalCall(); |
| 494 | return NULL; |
| 495 | } |
| 496 | |
| 497 | unicode = _PyUnicode_New(size); |
| 498 | if (!unicode) |
| 499 | return NULL; |
| 500 | |
| 501 | /* Copy the wchar_t data into the new object */ |
| 502 | #ifdef HAVE_USABLE_WCHAR_T |
| 503 | memcpy(unicode->str, w, size * sizeof(wchar_t)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 504 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 505 | { |
| 506 | register Py_UNICODE *u; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 507 | register Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 508 | u = PyUnicode_AS_UNICODE(unicode); |
Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 509 | for (i = size; i > 0; i--) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 510 | *u++ = *w++; |
| 511 | } |
| 512 | #endif |
| 513 | |
| 514 | return (PyObject *)unicode; |
| 515 | } |
| 516 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 517 | static void |
| 518 | makefmt(char *fmt, int longflag, int size_tflag, int zeropad, int width, int precision, char c) |
| 519 | { |
| 520 | *fmt++ = '%'; |
| 521 | if (width) { |
| 522 | if (zeropad) |
| 523 | *fmt++ = '0'; |
| 524 | fmt += sprintf(fmt, "%d", width); |
| 525 | } |
| 526 | if (precision) |
| 527 | fmt += sprintf(fmt, ".%d", precision); |
| 528 | if (longflag) |
| 529 | *fmt++ = 'l'; |
| 530 | else if (size_tflag) { |
| 531 | char *f = PY_FORMAT_SIZE_T; |
| 532 | while (*f) |
| 533 | *fmt++ = *f++; |
| 534 | } |
| 535 | *fmt++ = c; |
| 536 | *fmt = '\0'; |
| 537 | } |
| 538 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 539 | #define appendstring(string) {for (copy = string;*copy;) *s++ = *copy++;} |
| 540 | |
| 541 | PyObject * |
| 542 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 543 | { |
| 544 | va_list count; |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 545 | Py_ssize_t callcount = 0; |
| 546 | PyObject **callresults = NULL; |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 547 | PyObject **callresult = NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 548 | Py_ssize_t n = 0; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 549 | int width = 0; |
| 550 | int precision = 0; |
| 551 | int zeropad; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 552 | const char* f; |
| 553 | Py_UNICODE *s; |
| 554 | PyObject *string; |
| 555 | /* used by sprintf */ |
| 556 | char buffer[21]; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 557 | /* use abuffer instead of buffer, if we need more space |
| 558 | * (which can happen if there's a format specifier with width). */ |
| 559 | char *abuffer = NULL; |
| 560 | char *realbuffer; |
| 561 | Py_ssize_t abuffersize = 0; |
| 562 | char fmt[60]; /* should be enough for %0width.precisionld */ |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 563 | const char *copy; |
| 564 | |
| 565 | #ifdef VA_LIST_IS_ARRAY |
| 566 | Py_MEMCPY(count, vargs, sizeof(va_list)); |
| 567 | #else |
| 568 | #ifdef __va_copy |
| 569 | __va_copy(count, vargs); |
| 570 | #else |
| 571 | count = vargs; |
| 572 | #endif |
| 573 | #endif |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 574 | /* step 1: count the number of %S/%R format specifications |
| 575 | * (we call PyObject_Unicode()/PyObject_Repr() for these objects |
| 576 | * once during step 3 and put the result in an array) */ |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 577 | for (f = format; *f; f++) { |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 578 | if (*f == '%' && (*(f+1)=='S' || *(f+1)=='R')) |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 579 | ++callcount; |
| 580 | } |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 581 | /* step 2: allocate memory for the results of |
| 582 | * PyObject_Unicode()/PyObject_Repr() calls */ |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 583 | if (callcount) { |
| 584 | callresults = PyMem_Malloc(sizeof(PyObject *)*callcount); |
| 585 | if (!callresults) { |
| 586 | PyErr_NoMemory(); |
| 587 | return NULL; |
| 588 | } |
| 589 | callresult = callresults; |
| 590 | } |
| 591 | /* step 3: figure out how large a buffer we need */ |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 592 | for (f = format; *f; f++) { |
| 593 | if (*f == '%') { |
| 594 | const char* p = f; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 595 | width = 0; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 596 | while (ISDIGIT(*f)) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 597 | width = (width*10) + *f++ - '0'; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 598 | while (*++f && *f != '%' && !ISALPHA(*f)) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 599 | ; |
| 600 | |
| 601 | /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
| 602 | * they don't affect the amount of space we reserve. |
| 603 | */ |
| 604 | if ((*f == 'l' || *f == 'z') && |
| 605 | (f[1] == 'd' || f[1] == 'u')) |
Eric Smith | ddd2582 | 2007-08-27 11:33:42 +0000 | [diff] [blame] | 606 | ++f; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 607 | |
| 608 | switch (*f) { |
| 609 | case 'c': |
| 610 | (void)va_arg(count, int); |
| 611 | /* fall through... */ |
| 612 | case '%': |
| 613 | n++; |
| 614 | break; |
| 615 | case 'd': case 'u': case 'i': case 'x': |
| 616 | (void) va_arg(count, int); |
| 617 | /* 20 bytes is enough to hold a 64-bit |
| 618 | integer. Decimal takes the most space. |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 619 | This isn't enough for octal. |
| 620 | If a width is specified we need more |
| 621 | (which we allocate later). */ |
| 622 | if (width < 20) |
| 623 | width = 20; |
| 624 | n += width; |
| 625 | if (abuffersize < width) |
| 626 | abuffersize = width; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 627 | break; |
| 628 | case 's': |
Martin v. Löwis | 90d1fcd | 2007-08-31 11:01:23 +0000 | [diff] [blame] | 629 | { |
| 630 | /* UTF-8 */ |
| 631 | unsigned char*s; |
| 632 | s = va_arg(count, unsigned char*); |
| 633 | while (*s) { |
| 634 | if (*s < 128) { |
| 635 | n++; s++; |
| 636 | } else if (*s < 0xc0) { |
| 637 | /* invalid UTF-8 */ |
| 638 | n++; s++; |
| 639 | } else if (*s < 0xc0) { |
| 640 | n++; |
| 641 | s++; if(!*s)break; |
| 642 | s++; |
| 643 | } else if (*s < 0xe0) { |
| 644 | n++; |
| 645 | s++; if(!*s)break; |
| 646 | s++; if(!*s)break; |
| 647 | s++; |
| 648 | } else { |
| 649 | #ifdef Py_UNICODE_WIDE |
| 650 | n++; |
| 651 | #else |
| 652 | n+=2; |
| 653 | #endif |
| 654 | s++; if(!*s)break; |
| 655 | s++; if(!*s)break; |
| 656 | s++; if(!*s)break; |
| 657 | s++; |
| 658 | } |
| 659 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 660 | break; |
Martin v. Löwis | 90d1fcd | 2007-08-31 11:01:23 +0000 | [diff] [blame] | 661 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 662 | case 'U': |
| 663 | { |
| 664 | PyObject *obj = va_arg(count, PyObject *); |
| 665 | assert(obj && PyUnicode_Check(obj)); |
| 666 | n += PyUnicode_GET_SIZE(obj); |
| 667 | break; |
| 668 | } |
Walter Dörwald | d7fb764 | 2007-06-11 16:36:59 +0000 | [diff] [blame] | 669 | case 'V': |
| 670 | { |
| 671 | PyObject *obj = va_arg(count, PyObject *); |
| 672 | const char *str = va_arg(count, const char *); |
| 673 | assert(obj || str); |
| 674 | assert(!obj || PyUnicode_Check(obj)); |
| 675 | if (obj) |
| 676 | n += PyUnicode_GET_SIZE(obj); |
| 677 | else |
| 678 | n += strlen(str); |
| 679 | break; |
| 680 | } |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 681 | case 'S': |
| 682 | { |
| 683 | PyObject *obj = va_arg(count, PyObject *); |
| 684 | PyObject *str; |
| 685 | assert(obj); |
| 686 | str = PyObject_Unicode(obj); |
| 687 | if (!str) |
| 688 | goto fail; |
| 689 | n += PyUnicode_GET_SIZE(str); |
| 690 | /* Remember the str and switch to the next slot */ |
| 691 | *callresult++ = str; |
| 692 | break; |
| 693 | } |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 694 | case 'R': |
| 695 | { |
| 696 | PyObject *obj = va_arg(count, PyObject *); |
| 697 | PyObject *repr; |
| 698 | assert(obj); |
| 699 | repr = PyObject_Repr(obj); |
| 700 | if (!repr) |
| 701 | goto fail; |
| 702 | n += PyUnicode_GET_SIZE(repr); |
| 703 | /* Remember the repr and switch to the next slot */ |
| 704 | *callresult++ = repr; |
| 705 | break; |
| 706 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 707 | case 'p': |
| 708 | (void) va_arg(count, int); |
| 709 | /* maximum 64-bit pointer representation: |
| 710 | * 0xffffffffffffffff |
| 711 | * so 19 characters is enough. |
| 712 | * XXX I count 18 -- what's the extra for? |
| 713 | */ |
| 714 | n += 19; |
| 715 | break; |
| 716 | default: |
| 717 | /* if we stumble upon an unknown |
| 718 | formatting code, copy the rest of |
| 719 | the format string to the output |
| 720 | string. (we cannot just skip the |
| 721 | code, since there's no way to know |
| 722 | what's in the argument list) */ |
| 723 | n += strlen(p); |
| 724 | goto expand; |
| 725 | } |
| 726 | } else |
| 727 | n++; |
| 728 | } |
| 729 | expand: |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 730 | if (abuffersize > 20) { |
| 731 | abuffer = PyMem_Malloc(abuffersize); |
| 732 | if (!abuffer) { |
| 733 | PyErr_NoMemory(); |
| 734 | goto fail; |
| 735 | } |
| 736 | realbuffer = abuffer; |
| 737 | } |
| 738 | else |
| 739 | realbuffer = buffer; |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 740 | /* step 4: fill the buffer */ |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 741 | /* Since we've analyzed how much space we need for the worst case, |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 742 | we don't have to resize the string. |
| 743 | There can be no errors beyond this point. */ |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 744 | string = PyUnicode_FromUnicode(NULL, n); |
| 745 | if (!string) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 746 | goto fail; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 747 | |
| 748 | s = PyUnicode_AS_UNICODE(string); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 749 | callresult = callresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 750 | |
| 751 | for (f = format; *f; f++) { |
| 752 | if (*f == '%') { |
| 753 | const char* p = f++; |
| 754 | int longflag = 0; |
| 755 | int size_tflag = 0; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 756 | zeropad = (*f == '0'); |
| 757 | /* parse the width.precision part */ |
| 758 | width = 0; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 759 | while (ISDIGIT(*f)) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 760 | width = (width*10) + *f++ - '0'; |
| 761 | precision = 0; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 762 | if (*f == '.') { |
| 763 | f++; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 764 | while (ISDIGIT(*f)) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 765 | precision = (precision*10) + *f++ - '0'; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 766 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 767 | /* handle the long flag, but only for %ld and %lu. |
| 768 | others can be added when necessary. */ |
| 769 | if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) { |
| 770 | longflag = 1; |
| 771 | ++f; |
| 772 | } |
| 773 | /* handle the size_t flag. */ |
| 774 | if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
| 775 | size_tflag = 1; |
| 776 | ++f; |
| 777 | } |
| 778 | |
| 779 | switch (*f) { |
| 780 | case 'c': |
| 781 | *s++ = va_arg(vargs, int); |
| 782 | break; |
| 783 | case 'd': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 784 | makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'd'); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 785 | if (longflag) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 786 | sprintf(realbuffer, fmt, va_arg(vargs, long)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 787 | else if (size_tflag) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 788 | sprintf(realbuffer, fmt, va_arg(vargs, Py_ssize_t)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 789 | else |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 790 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 791 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 792 | break; |
| 793 | case 'u': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 794 | makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'u'); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 795 | if (longflag) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 796 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned long)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 797 | else if (size_tflag) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 798 | sprintf(realbuffer, fmt, va_arg(vargs, size_t)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 799 | else |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 800 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned int)); |
| 801 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 802 | break; |
| 803 | case 'i': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 804 | makefmt(fmt, 0, 0, zeropad, width, precision, 'i'); |
| 805 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 806 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 807 | break; |
| 808 | case 'x': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 809 | makefmt(fmt, 0, 0, zeropad, width, precision, 'x'); |
| 810 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 811 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 812 | break; |
| 813 | case 's': |
Martin v. Löwis | 90d1fcd | 2007-08-31 11:01:23 +0000 | [diff] [blame] | 814 | { |
| 815 | /* Parameter must be UTF-8 encoded. |
| 816 | In case of encoding errors, use |
| 817 | the replacement character. */ |
| 818 | PyObject *u; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 819 | p = va_arg(vargs, char*); |
Martin v. Löwis | 90d1fcd | 2007-08-31 11:01:23 +0000 | [diff] [blame] | 820 | u = PyUnicode_DecodeUTF8(p, strlen(p), |
| 821 | "replace"); |
| 822 | if (!u) |
| 823 | goto fail; |
| 824 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(u), |
| 825 | PyUnicode_GET_SIZE(u)); |
| 826 | s += PyUnicode_GET_SIZE(u); |
| 827 | Py_DECREF(u); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 828 | break; |
Martin v. Löwis | 90d1fcd | 2007-08-31 11:01:23 +0000 | [diff] [blame] | 829 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 830 | case 'U': |
| 831 | { |
| 832 | PyObject *obj = va_arg(vargs, PyObject *); |
Walter Dörwald | 5c2fab6 | 2007-05-24 19:51:02 +0000 | [diff] [blame] | 833 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 834 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 835 | s += size; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 836 | break; |
| 837 | } |
Walter Dörwald | d7fb764 | 2007-06-11 16:36:59 +0000 | [diff] [blame] | 838 | case 'V': |
| 839 | { |
| 840 | PyObject *obj = va_arg(vargs, PyObject *); |
| 841 | const char *str = va_arg(vargs, const char *); |
| 842 | if (obj) { |
| 843 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 844 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 845 | s += size; |
| 846 | } else { |
| 847 | appendstring(str); |
| 848 | } |
| 849 | break; |
| 850 | } |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 851 | case 'S': |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 852 | case 'R': |
| 853 | { |
Guido van Rossum | 755114a | 2007-06-13 01:04:27 +0000 | [diff] [blame] | 854 | Py_UNICODE *ucopy; |
| 855 | Py_ssize_t usize; |
| 856 | Py_ssize_t upos; |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 857 | /* unused, since we already have the result */ |
| 858 | (void) va_arg(vargs, PyObject *); |
Guido van Rossum | 755114a | 2007-06-13 01:04:27 +0000 | [diff] [blame] | 859 | ucopy = PyUnicode_AS_UNICODE(*callresult); |
| 860 | usize = PyUnicode_GET_SIZE(*callresult); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 861 | for (upos = 0; upos<usize;) |
| 862 | *s++ = ucopy[upos++]; |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 863 | /* We're done with the unicode()/repr() => forget it */ |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 864 | Py_DECREF(*callresult); |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 865 | /* switch to next unicode()/repr() result */ |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 866 | ++callresult; |
| 867 | break; |
| 868 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 869 | case 'p': |
| 870 | sprintf(buffer, "%p", va_arg(vargs, void*)); |
| 871 | /* %p is ill-defined: ensure leading 0x. */ |
| 872 | if (buffer[1] == 'X') |
| 873 | buffer[1] = 'x'; |
| 874 | else if (buffer[1] != 'x') { |
| 875 | memmove(buffer+2, buffer, strlen(buffer)+1); |
| 876 | buffer[0] = '0'; |
| 877 | buffer[1] = 'x'; |
| 878 | } |
| 879 | appendstring(buffer); |
| 880 | break; |
| 881 | case '%': |
| 882 | *s++ = '%'; |
| 883 | break; |
| 884 | default: |
| 885 | appendstring(p); |
| 886 | goto end; |
| 887 | } |
| 888 | } else |
| 889 | *s++ = *f; |
| 890 | } |
| 891 | |
| 892 | end: |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 893 | if (callresults) |
| 894 | PyMem_Free(callresults); |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 895 | if (abuffer) |
| 896 | PyMem_Free(abuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 897 | _PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string)); |
| 898 | return string; |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 899 | fail: |
| 900 | if (callresults) { |
| 901 | PyObject **callresult2 = callresults; |
Guido van Rossum | 307fa8c | 2007-07-16 20:46:27 +0000 | [diff] [blame] | 902 | while (callresult2 < callresult) { |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 903 | Py_DECREF(*callresult2); |
| 904 | ++callresult2; |
| 905 | } |
| 906 | PyMem_Free(callresults); |
| 907 | } |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 908 | if (abuffer) |
| 909 | PyMem_Free(abuffer); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 910 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | #undef appendstring |
| 914 | |
| 915 | PyObject * |
| 916 | PyUnicode_FromFormat(const char *format, ...) |
| 917 | { |
| 918 | PyObject* ret; |
| 919 | va_list vargs; |
| 920 | |
| 921 | #ifdef HAVE_STDARG_PROTOTYPES |
| 922 | va_start(vargs, format); |
| 923 | #else |
| 924 | va_start(vargs); |
| 925 | #endif |
| 926 | ret = PyUnicode_FromFormatV(format, vargs); |
| 927 | va_end(vargs); |
| 928 | return ret; |
| 929 | } |
| 930 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 931 | Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
| 932 | wchar_t *w, |
| 933 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 934 | { |
| 935 | if (unicode == NULL) { |
| 936 | PyErr_BadInternalCall(); |
| 937 | return -1; |
| 938 | } |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 939 | |
| 940 | /* If possible, try to copy the 0-termination as well */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 941 | if (size > PyUnicode_GET_SIZE(unicode)) |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 942 | size = PyUnicode_GET_SIZE(unicode) + 1; |
| 943 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 944 | #ifdef HAVE_USABLE_WCHAR_T |
| 945 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 946 | #else |
| 947 | { |
| 948 | register Py_UNICODE *u; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 949 | register Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 950 | u = PyUnicode_AS_UNICODE(unicode); |
Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 951 | for (i = size; i > 0; i--) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 952 | *w++ = *u++; |
| 953 | } |
| 954 | #endif |
| 955 | |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 956 | if (size > PyUnicode_GET_SIZE(unicode)) |
| 957 | return PyUnicode_GET_SIZE(unicode); |
| 958 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 959 | return size; |
| 960 | } |
| 961 | |
| 962 | #endif |
| 963 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 964 | PyObject *PyUnicode_FromOrdinal(int ordinal) |
| 965 | { |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 966 | Py_UNICODE s[2]; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 967 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 968 | if (ordinal < 0 || ordinal > 0x10ffff) { |
| 969 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 970 | "chr() arg not in range(0x110000)"); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 971 | return NULL; |
| 972 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 973 | |
| 974 | #ifndef Py_UNICODE_WIDE |
| 975 | if (ordinal > 0xffff) { |
| 976 | ordinal -= 0x10000; |
| 977 | s[0] = 0xD800 | (ordinal >> 10); |
| 978 | s[1] = 0xDC00 | (ordinal & 0x3FF); |
| 979 | return PyUnicode_FromUnicode(s, 2); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 980 | } |
| 981 | #endif |
| 982 | |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 983 | s[0] = (Py_UNICODE)ordinal; |
| 984 | return PyUnicode_FromUnicode(s, 1); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 985 | } |
| 986 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 987 | PyObject *PyUnicode_FromObject(register PyObject *obj) |
| 988 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 989 | /* XXX Perhaps we should make this API an alias of |
| 990 | PyObject_Unicode() instead ?! */ |
| 991 | if (PyUnicode_CheckExact(obj)) { |
| 992 | Py_INCREF(obj); |
| 993 | return obj; |
| 994 | } |
| 995 | if (PyUnicode_Check(obj)) { |
| 996 | /* For a Unicode subtype that's not a Unicode object, |
| 997 | return a true Unicode object with the same data. */ |
| 998 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 999 | PyUnicode_GET_SIZE(obj)); |
| 1000 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1001 | return PyUnicode_FromEncodedObject(obj, NULL, "strict"); |
| 1002 | } |
| 1003 | |
| 1004 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
| 1005 | const char *encoding, |
| 1006 | const char *errors) |
| 1007 | { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1008 | const char *s = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1009 | Py_ssize_t len; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1010 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1011 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1012 | if (obj == NULL) { |
| 1013 | PyErr_BadInternalCall(); |
| 1014 | return NULL; |
| 1015 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1016 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1017 | if (PyUnicode_Check(obj)) { |
| 1018 | PyErr_SetString(PyExc_TypeError, |
| 1019 | "decoding Unicode is not supported"); |
| 1020 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1021 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1022 | |
| 1023 | /* Coerce object */ |
| 1024 | if (PyString_Check(obj)) { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1025 | s = PyString_AS_STRING(obj); |
| 1026 | len = PyString_GET_SIZE(obj); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1027 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1028 | else if (PyObject_AsCharBuffer(obj, &s, &len)) { |
| 1029 | /* Overwrite the error message with something more useful in |
| 1030 | case of a TypeError. */ |
| 1031 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1032 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1033 | "coercing to Unicode: need string or buffer, " |
| 1034 | "%.80s found", |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1035 | Py_Type(obj)->tp_name); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1036 | goto onError; |
| 1037 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1038 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1039 | /* Convert to Unicode */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1040 | if (len == 0) { |
| 1041 | Py_INCREF(unicode_empty); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1042 | v = (PyObject *)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1043 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1044 | else |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1045 | v = PyUnicode_Decode(s, len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 1046 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1047 | return v; |
| 1048 | |
| 1049 | onError: |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1050 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | PyObject *PyUnicode_Decode(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1054 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1055 | const char *encoding, |
| 1056 | const char *errors) |
| 1057 | { |
| 1058 | PyObject *buffer = NULL, *unicode; |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1059 | Py_buffer info; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1060 | char lower[20]; /* Enough for any encoding name we recognize */ |
| 1061 | char *l; |
| 1062 | const char *e; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1063 | |
| 1064 | if (encoding == NULL) |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1065 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1066 | |
| 1067 | /* Convert encoding to lower case and replace '_' with '-' in order to |
| 1068 | catch e.g. UTF_8 */ |
| 1069 | e = encoding; |
| 1070 | l = lower; |
| 1071 | while (*e && l < &lower[(sizeof lower) - 2]) { |
| 1072 | if (ISUPPER(*e)) { |
| 1073 | *l++ = TOLOWER(*e++); |
| 1074 | } |
| 1075 | else if (*e == '_') { |
| 1076 | *l++ = '-'; |
| 1077 | e++; |
| 1078 | } |
| 1079 | else { |
| 1080 | *l++ = *e++; |
| 1081 | } |
| 1082 | } |
| 1083 | *l = '\0'; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1084 | |
| 1085 | /* Shortcuts for common default encodings */ |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1086 | if (strcmp(lower, "utf-8") == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1087 | return PyUnicode_DecodeUTF8(s, size, errors); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1088 | else if ((strcmp(lower, "latin-1") == 0) || |
| 1089 | (strcmp(lower, "iso-8859-1") == 0)) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1090 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1091 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1092 | else if (strcmp(lower, "mbcs") == 0) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1093 | return PyUnicode_DecodeMBCS(s, size, errors); |
| 1094 | #endif |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1095 | else if (strcmp(lower, "ascii") == 0) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1096 | return PyUnicode_DecodeASCII(s, size, errors); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1097 | else if (strcmp(lower, "utf-16") == 0) |
| 1098 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 1099 | else if (strcmp(lower, "utf-32") == 0) |
| 1100 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1101 | |
| 1102 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1103 | buffer = NULL; |
| 1104 | if (PyBuffer_FillInfo(&info, (void *)s, size, 1, PyBUF_SIMPLE) < 0) |
| 1105 | goto onError; |
| 1106 | buffer = PyMemoryView_FromMemory(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1107 | if (buffer == NULL) |
| 1108 | goto onError; |
| 1109 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 1110 | if (unicode == NULL) |
| 1111 | goto onError; |
| 1112 | if (!PyUnicode_Check(unicode)) { |
| 1113 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 1114 | "decoder did not return an unicode object (type=%.400s)", |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1115 | Py_Type(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1116 | Py_DECREF(unicode); |
| 1117 | goto onError; |
| 1118 | } |
| 1119 | Py_DECREF(buffer); |
| 1120 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1121 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1122 | onError: |
| 1123 | Py_XDECREF(buffer); |
| 1124 | return NULL; |
| 1125 | } |
| 1126 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1127 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 1128 | const char *encoding, |
| 1129 | const char *errors) |
| 1130 | { |
| 1131 | PyObject *v; |
| 1132 | |
| 1133 | if (!PyUnicode_Check(unicode)) { |
| 1134 | PyErr_BadArgument(); |
| 1135 | goto onError; |
| 1136 | } |
| 1137 | |
| 1138 | if (encoding == NULL) |
| 1139 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1140 | |
| 1141 | /* Decode via the codec registry */ |
| 1142 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1143 | if (v == NULL) |
| 1144 | goto onError; |
| 1145 | return v; |
| 1146 | |
| 1147 | onError: |
| 1148 | return NULL; |
| 1149 | } |
| 1150 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1151 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1152 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1153 | const char *encoding, |
| 1154 | const char *errors) |
| 1155 | { |
| 1156 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1157 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1158 | unicode = PyUnicode_FromUnicode(s, size); |
| 1159 | if (unicode == NULL) |
| 1160 | return NULL; |
| 1161 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 1162 | Py_DECREF(unicode); |
| 1163 | return v; |
| 1164 | } |
| 1165 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1166 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 1167 | const char *encoding, |
| 1168 | const char *errors) |
| 1169 | { |
| 1170 | PyObject *v; |
| 1171 | |
| 1172 | if (!PyUnicode_Check(unicode)) { |
| 1173 | PyErr_BadArgument(); |
| 1174 | goto onError; |
| 1175 | } |
| 1176 | |
| 1177 | if (encoding == NULL) |
| 1178 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1179 | |
| 1180 | /* Encode via the codec registry */ |
| 1181 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1182 | if (v == NULL) |
| 1183 | goto onError; |
| 1184 | return v; |
| 1185 | |
| 1186 | onError: |
| 1187 | return NULL; |
| 1188 | } |
| 1189 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1190 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 1191 | const char *encoding, |
| 1192 | const char *errors) |
| 1193 | { |
| 1194 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1195 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1196 | if (!PyUnicode_Check(unicode)) { |
| 1197 | PyErr_BadArgument(); |
| 1198 | goto onError; |
| 1199 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1200 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1201 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1202 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1203 | |
| 1204 | /* Shortcuts for common default encodings */ |
| 1205 | if (errors == NULL) { |
| 1206 | if (strcmp(encoding, "utf-8") == 0) |
Jeremy Hylton | 9cea41c | 2001-05-29 17:13:15 +0000 | [diff] [blame] | 1207 | return PyUnicode_AsUTF8String(unicode); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1208 | else if (strcmp(encoding, "latin-1") == 0) |
| 1209 | return PyUnicode_AsLatin1String(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1210 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 1211 | else if (strcmp(encoding, "mbcs") == 0) |
| 1212 | return PyUnicode_AsMBCSString(unicode); |
| 1213 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1214 | else if (strcmp(encoding, "ascii") == 0) |
| 1215 | return PyUnicode_AsASCIIString(unicode); |
| 1216 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1217 | |
| 1218 | /* Encode via the codec registry */ |
| 1219 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1220 | if (v == NULL) |
| 1221 | goto onError; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1222 | if (!PyBytes_Check(v)) { |
| 1223 | if (PyString_Check(v)) { |
| 1224 | /* Old codec, turn it into bytes */ |
| 1225 | PyObject *b = PyBytes_FromObject(v); |
| 1226 | Py_DECREF(v); |
| 1227 | return b; |
| 1228 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1229 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1230 | "encoder did not return a bytes object " |
| 1231 | "(type=%.400s, encoding=%.20s, errors=%.20s)", |
| 1232 | v->ob_type->tp_name, |
| 1233 | encoding ? encoding : "NULL", |
| 1234 | errors ? errors : "NULL"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1235 | Py_DECREF(v); |
| 1236 | goto onError; |
| 1237 | } |
| 1238 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1239 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1240 | onError: |
| 1241 | return NULL; |
| 1242 | } |
| 1243 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1244 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
| 1245 | const char *errors) |
| 1246 | { |
| 1247 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1248 | PyObject *b; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1249 | if (v) |
| 1250 | return v; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1251 | if (errors != NULL) |
| 1252 | Py_FatalError("non-NULL encoding in _PyUnicode_AsDefaultEncodedString"); |
Guido van Rossum | 0661009 | 2007-08-16 21:02:22 +0000 | [diff] [blame] | 1253 | b = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 1254 | PyUnicode_GET_SIZE(unicode), |
| 1255 | NULL); |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1256 | if (!b) |
| 1257 | return NULL; |
| 1258 | v = PyString_FromStringAndSize(PyBytes_AsString(b), |
| 1259 | PyBytes_Size(b)); |
| 1260 | Py_DECREF(b); |
Guido van Rossum | e7a0d39 | 2007-07-12 07:53:00 +0000 | [diff] [blame] | 1261 | ((PyUnicodeObject *)unicode)->defenc = v; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1262 | return v; |
| 1263 | } |
| 1264 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1265 | PyObject* |
| 1266 | PyUnicode_DecodeFSDefault(const char *s) |
| 1267 | { |
| 1268 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
| 1269 | |
| 1270 | /* During the early bootstrapping process, Py_FileSystemDefaultEncoding |
| 1271 | can be undefined. If it is case, decode using UTF-8. The following assumes |
| 1272 | that Py_FileSystemDefaultEncoding is set to a built-in encoding during the |
| 1273 | bootstrapping process where the codecs aren't ready yet. |
| 1274 | */ |
| 1275 | if (Py_FileSystemDefaultEncoding) { |
| 1276 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 1277 | if (strcmp(Py_FileSystemDefaultEncoding, "mbcs")) { |
| 1278 | return PyUnicode_DecodeMBCS(s, size, "replace"); |
| 1279 | } |
| 1280 | #elif defined(__APPLE__) |
| 1281 | if (strcmp(Py_FileSystemDefaultEncoding, "utf-8")) { |
| 1282 | return PyUnicode_DecodeUTF8(s, size, "replace"); |
| 1283 | } |
| 1284 | #endif |
| 1285 | return PyUnicode_Decode(s, size, |
| 1286 | Py_FileSystemDefaultEncoding, |
| 1287 | "replace"); |
| 1288 | } |
| 1289 | else { |
| 1290 | return PyUnicode_DecodeUTF8(s, size, "replace"); |
| 1291 | } |
| 1292 | } |
| 1293 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1294 | char* |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1295 | PyUnicode_AsStringAndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1296 | { |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1297 | PyObject *str8; |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 1298 | if (!PyUnicode_Check(unicode)) { |
| 1299 | PyErr_BadArgument(); |
| 1300 | return NULL; |
| 1301 | } |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1302 | str8 = _PyUnicode_AsDefaultEncodedString(unicode, NULL); |
| 1303 | if (str8 == NULL) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1304 | return NULL; |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1305 | if (psize != NULL) |
| 1306 | *psize = PyString_GET_SIZE(str8); |
| 1307 | return PyString_AS_STRING(str8); |
| 1308 | } |
| 1309 | |
| 1310 | char* |
| 1311 | PyUnicode_AsString(PyObject *unicode) |
| 1312 | { |
| 1313 | return PyUnicode_AsStringAndSize(unicode, NULL); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1314 | } |
| 1315 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1316 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 1317 | { |
| 1318 | if (!PyUnicode_Check(unicode)) { |
| 1319 | PyErr_BadArgument(); |
| 1320 | goto onError; |
| 1321 | } |
| 1322 | return PyUnicode_AS_UNICODE(unicode); |
| 1323 | |
| 1324 | onError: |
| 1325 | return NULL; |
| 1326 | } |
| 1327 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1328 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1329 | { |
| 1330 | if (!PyUnicode_Check(unicode)) { |
| 1331 | PyErr_BadArgument(); |
| 1332 | goto onError; |
| 1333 | } |
| 1334 | return PyUnicode_GET_SIZE(unicode); |
| 1335 | |
| 1336 | onError: |
| 1337 | return -1; |
| 1338 | } |
| 1339 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 1340 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1341 | { |
| 1342 | return unicode_default_encoding; |
| 1343 | } |
| 1344 | |
| 1345 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 1346 | { |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1347 | if (strcmp(encoding, unicode_default_encoding) != 0) { |
| 1348 | PyErr_Format(PyExc_ValueError, |
| 1349 | "Can only set default encoding to %s", |
| 1350 | unicode_default_encoding); |
| 1351 | return -1; |
| 1352 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1353 | return 0; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1354 | } |
| 1355 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1356 | /* error handling callback helper: |
| 1357 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 1358 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1359 | and adjust various state variables. |
| 1360 | return 0 on success, -1 on error |
| 1361 | */ |
| 1362 | |
| 1363 | static |
| 1364 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
| 1365 | const char *encoding, const char *reason, |
Walter Dörwald | a651d3d | 2007-08-30 15:29:21 +0000 | [diff] [blame] | 1366 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 1367 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1368 | PyObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1369 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1370 | 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] | 1371 | |
| 1372 | PyObject *restuple = NULL; |
| 1373 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1374 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1375 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1376 | Py_ssize_t requiredsize; |
| 1377 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1378 | Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1379 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1380 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1381 | int res = -1; |
| 1382 | |
| 1383 | if (*errorHandler == NULL) { |
| 1384 | *errorHandler = PyCodec_LookupError(errors); |
| 1385 | if (*errorHandler == NULL) |
| 1386 | goto onError; |
| 1387 | } |
| 1388 | |
| 1389 | if (*exceptionObject == NULL) { |
| 1390 | *exceptionObject = PyUnicodeDecodeError_Create( |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1391 | encoding, *input, *inend-*input, *startinpos, *endinpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1392 | if (*exceptionObject == NULL) |
| 1393 | goto onError; |
| 1394 | } |
| 1395 | else { |
| 1396 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 1397 | goto onError; |
| 1398 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
| 1399 | goto onError; |
| 1400 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 1401 | goto onError; |
| 1402 | } |
| 1403 | |
| 1404 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 1405 | if (restuple == NULL) |
| 1406 | goto onError; |
| 1407 | if (!PyTuple_Check(restuple)) { |
| 1408 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 1409 | goto onError; |
| 1410 | } |
| 1411 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
| 1412 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1413 | |
| 1414 | /* Copy back the bytes variables, which might have been modified by the |
| 1415 | callback */ |
| 1416 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 1417 | if (!inputobj) |
| 1418 | goto onError; |
| 1419 | if (!PyBytes_Check(inputobj)) { |
| 1420 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
| 1421 | } |
| 1422 | *input = PyBytes_AS_STRING(inputobj); |
| 1423 | insize = PyBytes_GET_SIZE(inputobj); |
| 1424 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 1425 | /* we can DECREF safely, as the exception has another reference, |
| 1426 | so the object won't go away. */ |
| 1427 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1428 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1429 | if (newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1430 | newpos = insize+newpos; |
| 1431 | if (newpos<0 || newpos>insize) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 1432 | 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] | 1433 | goto onError; |
| 1434 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1435 | |
| 1436 | /* need more space? (at least enough for what we |
| 1437 | have+the replacement+the rest of the string (starting |
| 1438 | at the new input position), so we won't have to check space |
| 1439 | when there are no errors in the rest of the string) */ |
| 1440 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 1441 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 1442 | requiredsize = *outpos + repsize + insize-newpos; |
| 1443 | if (requiredsize > outsize) { |
| 1444 | if (requiredsize<2*outsize) |
| 1445 | requiredsize = 2*outsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1446 | if (PyUnicode_Resize(output, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1447 | goto onError; |
| 1448 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
| 1449 | } |
| 1450 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1451 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1452 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 1453 | *outptr += repsize; |
| 1454 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1455 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1456 | /* we made it! */ |
| 1457 | res = 0; |
| 1458 | |
| 1459 | onError: |
| 1460 | Py_XDECREF(restuple); |
| 1461 | return res; |
| 1462 | } |
| 1463 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1464 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 1465 | |
| 1466 | /* see RFC2152 for details */ |
| 1467 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1468 | static |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1469 | char utf7_special[128] = { |
| 1470 | /* indicate whether a UTF-7 character is special i.e. cannot be directly |
| 1471 | encoded: |
| 1472 | 0 - not special |
| 1473 | 1 - special |
| 1474 | 2 - whitespace (optional) |
| 1475 | 3 - RFC2152 Set O (optional) */ |
| 1476 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, |
| 1477 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1478 | 2, 3, 3, 3, 3, 3, 3, 0, 0, 0, 3, 1, 0, 0, 0, 1, |
| 1479 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, |
| 1480 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1481 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, |
| 1482 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1483 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 1, 1, |
| 1484 | |
| 1485 | }; |
| 1486 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1487 | /* Note: The comparison (c) <= 0 is a trick to work-around gcc |
| 1488 | warnings about the comparison always being false; since |
| 1489 | utf7_special[0] is 1, we can safely make that one comparison |
| 1490 | true */ |
| 1491 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1492 | #define SPECIAL(c, encodeO, encodeWS) \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1493 | ((c) > 127 || (c) <= 0 || utf7_special[(c)] == 1 || \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1494 | (encodeWS && (utf7_special[(c)] == 2)) || \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1495 | (encodeO && (utf7_special[(c)] == 3))) |
| 1496 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1497 | #define B64(n) \ |
| 1498 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 1499 | #define B64CHAR(c) \ |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1500 | (ISALNUM(c) || (c) == '+' || (c) == '/') |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1501 | #define UB64(c) \ |
| 1502 | ((c) == '+' ? 62 : (c) == '/' ? 63 : (c) >= 'a' ? \ |
| 1503 | (c) - 71 : (c) >= 'A' ? (c) - 65 : (c) + 4 ) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1504 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1505 | #define ENCODE(out, ch, bits) \ |
| 1506 | while (bits >= 6) { \ |
| 1507 | *out++ = B64(ch >> (bits-6)); \ |
| 1508 | bits -= 6; \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1511 | #define DECODE(out, ch, bits, surrogate) \ |
| 1512 | while (bits >= 16) { \ |
| 1513 | Py_UNICODE outCh = (Py_UNICODE) ((ch >> (bits-16)) & 0xffff); \ |
| 1514 | bits -= 16; \ |
| 1515 | if (surrogate) { \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1516 | /* We have already generated an error for the high surrogate \ |
| 1517 | 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] | 1518 | surrogate = 0; \ |
| 1519 | } else if (0xDC00 <= outCh && outCh <= 0xDFFF) { \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1520 | /* This is a surrogate pair. Unfortunately we can't represent \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1521 | it in a 16-bit character */ \ |
| 1522 | surrogate = 1; \ |
| 1523 | errmsg = "code pairs are not supported"; \ |
| 1524 | goto utf7Error; \ |
| 1525 | } else { \ |
| 1526 | *out++ = outCh; \ |
| 1527 | } \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1528 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1529 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1530 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1531 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1532 | const char *errors) |
| 1533 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1534 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1535 | Py_ssize_t startinpos; |
| 1536 | Py_ssize_t endinpos; |
| 1537 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1538 | const char *e; |
| 1539 | PyUnicodeObject *unicode; |
| 1540 | Py_UNICODE *p; |
| 1541 | const char *errmsg = ""; |
| 1542 | int inShift = 0; |
| 1543 | unsigned int bitsleft = 0; |
| 1544 | unsigned long charsleft = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1545 | int surrogate = 0; |
| 1546 | PyObject *errorHandler = NULL; |
| 1547 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1548 | |
| 1549 | unicode = _PyUnicode_New(size); |
| 1550 | if (!unicode) |
| 1551 | return NULL; |
| 1552 | if (size == 0) |
| 1553 | return (PyObject *)unicode; |
| 1554 | |
| 1555 | p = unicode->str; |
| 1556 | e = s + size; |
| 1557 | |
| 1558 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1559 | Py_UNICODE ch; |
| 1560 | restart: |
| 1561 | ch = *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1562 | |
| 1563 | if (inShift) { |
| 1564 | if ((ch == '-') || !B64CHAR(ch)) { |
| 1565 | inShift = 0; |
| 1566 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1567 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1568 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1569 | if (bitsleft >= 6) { |
| 1570 | /* The shift sequence has a partial character in it. If |
| 1571 | bitsleft < 6 then we could just classify it as padding |
| 1572 | but that is not the case here */ |
| 1573 | |
| 1574 | errmsg = "partial character in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1575 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1576 | } |
| 1577 | /* According to RFC2152 the remaining bits should be zero. We |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1578 | choose to signal an error/insert a replacement character |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1579 | here so indicate the potential of a misencoded character. */ |
| 1580 | |
| 1581 | /* On x86, a << b == a << (b%32) so make sure that bitsleft != 0 */ |
| 1582 | if (bitsleft && charsleft << (sizeof(charsleft) * 8 - bitsleft)) { |
| 1583 | errmsg = "non-zero padding bits in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1584 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1585 | } |
| 1586 | |
| 1587 | if (ch == '-') { |
| 1588 | if ((s < e) && (*(s) == '-')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1589 | *p++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1590 | inShift = 1; |
| 1591 | } |
| 1592 | } else if (SPECIAL(ch,0,0)) { |
| 1593 | errmsg = "unexpected special character"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1594 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1595 | } else { |
| 1596 | *p++ = ch; |
| 1597 | } |
| 1598 | } else { |
| 1599 | charsleft = (charsleft << 6) | UB64(ch); |
| 1600 | bitsleft += 6; |
| 1601 | s++; |
| 1602 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1603 | } |
| 1604 | } |
| 1605 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1606 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1607 | s++; |
| 1608 | if (s < e && *s == '-') { |
| 1609 | s++; |
| 1610 | *p++ = '+'; |
| 1611 | } else |
| 1612 | { |
| 1613 | inShift = 1; |
| 1614 | bitsleft = 0; |
| 1615 | } |
| 1616 | } |
| 1617 | else if (SPECIAL(ch,0,0)) { |
Walter Dörwald | 2b65c75 | 2007-08-30 15:35:26 +0000 | [diff] [blame] | 1618 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1619 | errmsg = "unexpected special character"; |
| 1620 | s++; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1621 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1622 | } |
| 1623 | else { |
| 1624 | *p++ = ch; |
| 1625 | s++; |
| 1626 | } |
| 1627 | continue; |
| 1628 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1629 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1630 | endinpos = s-starts; |
| 1631 | if (unicode_decode_call_errorhandler( |
| 1632 | errors, &errorHandler, |
| 1633 | "utf7", errmsg, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1634 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1635 | (PyObject **)&unicode, &outpos, &p)) |
| 1636 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1637 | } |
| 1638 | |
| 1639 | if (inShift) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1640 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1641 | endinpos = size; |
| 1642 | if (unicode_decode_call_errorhandler( |
| 1643 | errors, &errorHandler, |
| 1644 | "utf7", "unterminated shift sequence", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1645 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1646 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1647 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1648 | if (s < e) |
| 1649 | goto restart; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1650 | } |
| 1651 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1652 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1653 | goto onError; |
| 1654 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1655 | Py_XDECREF(errorHandler); |
| 1656 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1657 | return (PyObject *)unicode; |
| 1658 | |
| 1659 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1660 | Py_XDECREF(errorHandler); |
| 1661 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1662 | Py_DECREF(unicode); |
| 1663 | return NULL; |
| 1664 | } |
| 1665 | |
| 1666 | |
| 1667 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1668 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1669 | int encodeSetO, |
| 1670 | int encodeWhiteSpace, |
| 1671 | const char *errors) |
| 1672 | { |
| 1673 | PyObject *v; |
| 1674 | /* It might be possible to tighten this worst case */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1675 | Py_ssize_t cbAllocated = 5 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1676 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1677 | Py_ssize_t i = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1678 | unsigned int bitsleft = 0; |
| 1679 | unsigned long charsleft = 0; |
| 1680 | char * out; |
| 1681 | char * start; |
| 1682 | |
| 1683 | if (size == 0) |
Walter Dörwald | 51ab414 | 2007-05-05 14:43:36 +0000 | [diff] [blame] | 1684 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1685 | |
Walter Dörwald | 51ab414 | 2007-05-05 14:43:36 +0000 | [diff] [blame] | 1686 | v = PyBytes_FromStringAndSize(NULL, cbAllocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1687 | if (v == NULL) |
| 1688 | return NULL; |
| 1689 | |
Walter Dörwald | 51ab414 | 2007-05-05 14:43:36 +0000 | [diff] [blame] | 1690 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1691 | for (;i < size; ++i) { |
| 1692 | Py_UNICODE ch = s[i]; |
| 1693 | |
| 1694 | if (!inShift) { |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1695 | if (ch == '+') { |
| 1696 | *out++ = '+'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1697 | *out++ = '-'; |
| 1698 | } else if (SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1699 | charsleft = ch; |
| 1700 | bitsleft = 16; |
| 1701 | *out++ = '+'; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1702 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1703 | inShift = bitsleft > 0; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1704 | } else { |
| 1705 | *out++ = (char) ch; |
| 1706 | } |
| 1707 | } else { |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1708 | if (!SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1709 | *out++ = B64(charsleft << (6-bitsleft)); |
| 1710 | charsleft = 0; |
| 1711 | bitsleft = 0; |
| 1712 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 1713 | so no '-' is required, except if the character is itself a '-' */ |
| 1714 | if (B64CHAR(ch) || ch == '-') { |
| 1715 | *out++ = '-'; |
| 1716 | } |
| 1717 | inShift = 0; |
| 1718 | *out++ = (char) ch; |
| 1719 | } else { |
| 1720 | bitsleft += 16; |
| 1721 | charsleft = (charsleft << 16) | ch; |
| 1722 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
| 1723 | |
| 1724 | /* If the next character is special then we dont' need to terminate |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1725 | 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] | 1726 | or '-' then the shift sequence will be terminated implicitly and we |
| 1727 | don't have to insert a '-'. */ |
| 1728 | |
| 1729 | if (bitsleft == 0) { |
| 1730 | if (i + 1 < size) { |
| 1731 | Py_UNICODE ch2 = s[i+1]; |
| 1732 | |
| 1733 | if (SPECIAL(ch2, encodeSetO, encodeWhiteSpace)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1734 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1735 | } else if (B64CHAR(ch2) || ch2 == '-') { |
| 1736 | *out++ = '-'; |
| 1737 | inShift = 0; |
| 1738 | } else { |
| 1739 | inShift = 0; |
| 1740 | } |
| 1741 | |
| 1742 | } |
| 1743 | else { |
| 1744 | *out++ = '-'; |
| 1745 | inShift = 0; |
| 1746 | } |
| 1747 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1748 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1749 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1750 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1751 | if (bitsleft) { |
| 1752 | *out++= B64(charsleft << (6-bitsleft) ); |
| 1753 | *out++ = '-'; |
| 1754 | } |
| 1755 | |
Walter Dörwald | 51ab414 | 2007-05-05 14:43:36 +0000 | [diff] [blame] | 1756 | if (PyBytes_Resize(v, out - start)) { |
| 1757 | Py_DECREF(v); |
| 1758 | return NULL; |
| 1759 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1760 | return v; |
| 1761 | } |
| 1762 | |
| 1763 | #undef SPECIAL |
| 1764 | #undef B64 |
| 1765 | #undef B64CHAR |
| 1766 | #undef UB64 |
| 1767 | #undef ENCODE |
| 1768 | #undef DECODE |
| 1769 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1770 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 1771 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1772 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1773 | char utf8_code_length[256] = { |
| 1774 | /* Map UTF-8 encoded prefix byte to sequence length. zero means |
| 1775 | illegal prefix. see RFC 2279 for details */ |
| 1776 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1777 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1778 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1779 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1780 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1781 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1782 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1783 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1784 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1785 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1786 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1787 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1788 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1789 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1790 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 1791 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 |
| 1792 | }; |
| 1793 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1794 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1795 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1796 | const char *errors) |
| 1797 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1798 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 1799 | } |
| 1800 | |
| 1801 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1802 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1803 | const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1804 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1805 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1806 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1807 | int n; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1808 | Py_ssize_t startinpos; |
| 1809 | Py_ssize_t endinpos; |
| 1810 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1811 | const char *e; |
| 1812 | PyUnicodeObject *unicode; |
| 1813 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1814 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1815 | PyObject *errorHandler = NULL; |
| 1816 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1817 | |
| 1818 | /* Note: size will always be longer than the resulting Unicode |
| 1819 | character count */ |
| 1820 | unicode = _PyUnicode_New(size); |
| 1821 | if (!unicode) |
| 1822 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1823 | if (size == 0) { |
| 1824 | if (consumed) |
| 1825 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1826 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1827 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1828 | |
| 1829 | /* Unpack UTF-8 encoded data */ |
| 1830 | p = unicode->str; |
| 1831 | e = s + size; |
| 1832 | |
| 1833 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1834 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1835 | |
| 1836 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1837 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1838 | s++; |
| 1839 | continue; |
| 1840 | } |
| 1841 | |
| 1842 | n = utf8_code_length[ch]; |
| 1843 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1844 | if (s + n > e) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1845 | if (consumed) |
| 1846 | break; |
| 1847 | else { |
| 1848 | errmsg = "unexpected end of data"; |
| 1849 | startinpos = s-starts; |
| 1850 | endinpos = size; |
| 1851 | goto utf8Error; |
| 1852 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1853 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1854 | |
| 1855 | switch (n) { |
| 1856 | |
| 1857 | case 0: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1858 | errmsg = "unexpected code byte"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1859 | startinpos = s-starts; |
| 1860 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1861 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1862 | |
| 1863 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1864 | errmsg = "internal error"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1865 | startinpos = s-starts; |
| 1866 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1867 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1868 | |
| 1869 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1870 | if ((s[1] & 0xc0) != 0x80) { |
| 1871 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1872 | startinpos = s-starts; |
| 1873 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1874 | goto utf8Error; |
| 1875 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1876 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1877 | if (ch < 0x80) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1878 | startinpos = s-starts; |
| 1879 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1880 | errmsg = "illegal encoding"; |
| 1881 | goto utf8Error; |
| 1882 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1883 | else |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1884 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1885 | break; |
| 1886 | |
| 1887 | case 3: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1888 | if ((s[1] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1889 | (s[2] & 0xc0) != 0x80) { |
| 1890 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1891 | startinpos = s-starts; |
| 1892 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1893 | goto utf8Error; |
| 1894 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1895 | 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] | 1896 | if (ch < 0x0800) { |
| 1897 | /* Note: UTF-8 encodings of surrogates are considered |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1898 | legal UTF-8 sequences; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1899 | |
| 1900 | XXX For wide builds (UCS-4) we should probably try |
| 1901 | to recombine the surrogates into a single code |
| 1902 | unit. |
| 1903 | */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1904 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1905 | startinpos = s-starts; |
| 1906 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1907 | goto utf8Error; |
| 1908 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1909 | else |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1910 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1911 | break; |
| 1912 | |
| 1913 | case 4: |
| 1914 | if ((s[1] & 0xc0) != 0x80 || |
| 1915 | (s[2] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1916 | (s[3] & 0xc0) != 0x80) { |
| 1917 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1918 | startinpos = s-starts; |
| 1919 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1920 | goto utf8Error; |
| 1921 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1922 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 1923 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 1924 | /* validate and convert to UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1925 | if ((ch < 0x10000) /* minimum value allowed for 4 |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1926 | byte encoding */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1927 | || (ch > 0x10ffff)) /* maximum value allowed for |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1928 | UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1929 | { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1930 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1931 | startinpos = s-starts; |
| 1932 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1933 | goto utf8Error; |
| 1934 | } |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1935 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1936 | *p++ = (Py_UNICODE)ch; |
| 1937 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1938 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1939 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1940 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 1941 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1942 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1943 | /* high surrogate = top 10 bits added to D800 */ |
| 1944 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1945 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1946 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 1947 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1948 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1949 | break; |
| 1950 | |
| 1951 | default: |
| 1952 | /* Other sizes are only needed for UCS-4 */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1953 | errmsg = "unsupported Unicode code range"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1954 | startinpos = s-starts; |
| 1955 | endinpos = startinpos+n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1956 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1957 | } |
| 1958 | s += n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1959 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1960 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1961 | utf8Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1962 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1963 | if (unicode_decode_call_errorhandler( |
| 1964 | errors, &errorHandler, |
| 1965 | "utf8", errmsg, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1966 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1967 | (PyObject **)&unicode, &outpos, &p)) |
| 1968 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1969 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1970 | if (consumed) |
| 1971 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1972 | |
| 1973 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1974 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1975 | goto onError; |
| 1976 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1977 | Py_XDECREF(errorHandler); |
| 1978 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1979 | return (PyObject *)unicode; |
| 1980 | |
| 1981 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1982 | Py_XDECREF(errorHandler); |
| 1983 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1984 | Py_DECREF(unicode); |
| 1985 | return NULL; |
| 1986 | } |
| 1987 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1988 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 1989 | and allocate exactly as much space needed at the end. Else allocate the |
| 1990 | maximum possible needed (4 result bytes per Unicode character), and return |
| 1991 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1992 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1993 | PyObject * |
| 1994 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1995 | Py_ssize_t size, |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1996 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1997 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1998 | #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] | 1999 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2000 | Py_ssize_t i; /* index into s of next input byte */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2001 | PyObject *v; /* result string object */ |
| 2002 | char *p; /* next free byte in output buffer */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2003 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2004 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2005 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2006 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2007 | assert(s != NULL); |
| 2008 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2009 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2010 | if (size <= MAX_SHORT_UNICHARS) { |
| 2011 | /* Write into the stack buffer; nallocated can't overflow. |
| 2012 | * At the end, we'll allocate exactly as much heap space as it |
| 2013 | * turns out we need. |
| 2014 | */ |
| 2015 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
| 2016 | v = NULL; /* will allocate after we're done */ |
| 2017 | p = stackbuf; |
| 2018 | } |
| 2019 | else { |
| 2020 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 2021 | nallocated = size * 4; |
| 2022 | if (nallocated / 4 != size) /* overflow! */ |
| 2023 | return PyErr_NoMemory(); |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 2024 | v = PyBytes_FromStringAndSize(NULL, nallocated); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2025 | if (v == NULL) |
| 2026 | return NULL; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 2027 | p = PyBytes_AS_STRING(v); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2028 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2029 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2030 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2031 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2032 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2033 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2034 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2035 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2036 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2037 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2038 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 2039 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 2040 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2041 | } |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2042 | else { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2043 | /* Encode UCS2 Unicode ordinals */ |
| 2044 | if (ch < 0x10000) { |
| 2045 | /* Special case: check for high surrogate */ |
| 2046 | if (0xD800 <= ch && ch <= 0xDBFF && i != size) { |
| 2047 | Py_UCS4 ch2 = s[i]; |
| 2048 | /* Check for low surrogate and combine the two to |
| 2049 | form a UCS4 value */ |
| 2050 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2051 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2052 | i++; |
| 2053 | goto encodeUCS4; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2054 | } |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2055 | /* Fall through: handles isolated high surrogates */ |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2056 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2057 | *p++ = (char)(0xe0 | (ch >> 12)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2058 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2059 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2060 | continue; |
| 2061 | } |
| 2062 | encodeUCS4: |
| 2063 | /* Encode UCS4 Unicode ordinals */ |
| 2064 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2065 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 2066 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2067 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2068 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2069 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 2070 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2071 | if (v == NULL) { |
| 2072 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2073 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2074 | assert(nneeded <= nallocated); |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 2075 | v = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2076 | } |
| 2077 | else { |
| 2078 | /* Cut back to size actually needed. */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 2079 | nneeded = p - PyBytes_AS_STRING(v); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2080 | assert(nneeded <= nallocated); |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 2081 | PyBytes_Resize(v, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2082 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2083 | return v; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2084 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2085 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2086 | } |
| 2087 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2088 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 2089 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2090 | if (!PyUnicode_Check(unicode)) { |
| 2091 | PyErr_BadArgument(); |
| 2092 | return NULL; |
| 2093 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 2094 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 2095 | PyUnicode_GET_SIZE(unicode), |
| 2096 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2097 | } |
| 2098 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2099 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 2100 | |
| 2101 | PyObject * |
| 2102 | PyUnicode_DecodeUTF32(const char *s, |
| 2103 | Py_ssize_t size, |
| 2104 | const char *errors, |
| 2105 | int *byteorder) |
| 2106 | { |
| 2107 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 2108 | } |
| 2109 | |
| 2110 | PyObject * |
| 2111 | PyUnicode_DecodeUTF32Stateful(const char *s, |
| 2112 | Py_ssize_t size, |
| 2113 | const char *errors, |
| 2114 | int *byteorder, |
| 2115 | Py_ssize_t *consumed) |
| 2116 | { |
| 2117 | const char *starts = s; |
| 2118 | Py_ssize_t startinpos; |
| 2119 | Py_ssize_t endinpos; |
| 2120 | Py_ssize_t outpos; |
| 2121 | PyUnicodeObject *unicode; |
| 2122 | Py_UNICODE *p; |
| 2123 | #ifndef Py_UNICODE_WIDE |
| 2124 | int i, pairs; |
| 2125 | #else |
| 2126 | const int pairs = 0; |
| 2127 | #endif |
| 2128 | const unsigned char *q, *e; |
| 2129 | int bo = 0; /* assume native ordering by default */ |
| 2130 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2131 | /* Offsets from q for retrieving bytes in the right order. */ |
| 2132 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2133 | int iorder[] = {0, 1, 2, 3}; |
| 2134 | #else |
| 2135 | int iorder[] = {3, 2, 1, 0}; |
| 2136 | #endif |
| 2137 | PyObject *errorHandler = NULL; |
| 2138 | PyObject *exc = NULL; |
Guido van Rossum | 8d991ed | 2007-08-17 15:41:00 +0000 | [diff] [blame] | 2139 | /* On narrow builds we split characters outside the BMP into two |
| 2140 | codepoints => count how much extra space we need. */ |
| 2141 | #ifndef Py_UNICODE_WIDE |
| 2142 | for (i = pairs = 0; i < size/4; i++) |
| 2143 | if (((Py_UCS4 *)s)[i] >= 0x10000) |
| 2144 | pairs++; |
| 2145 | #endif |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2146 | |
| 2147 | /* This might be one to much, because of a BOM */ |
| 2148 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 2149 | if (!unicode) |
| 2150 | return NULL; |
| 2151 | if (size == 0) |
| 2152 | return (PyObject *)unicode; |
| 2153 | |
| 2154 | /* Unpack UTF-32 encoded data */ |
| 2155 | p = unicode->str; |
| 2156 | q = (unsigned char *)s; |
| 2157 | e = q + size; |
| 2158 | |
| 2159 | if (byteorder) |
| 2160 | bo = *byteorder; |
| 2161 | |
| 2162 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2163 | byte order setting accordingly. In native mode, the leading BOM |
| 2164 | mark is skipped, in all other modes, it is copied to the output |
| 2165 | stream as-is (giving a ZWNBSP character). */ |
| 2166 | if (bo == 0) { |
| 2167 | if (size >= 4) { |
| 2168 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2169 | (q[iorder[1]] << 8) | q[iorder[0]]; |
| 2170 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2171 | if (bom == 0x0000FEFF) { |
| 2172 | q += 4; |
| 2173 | bo = -1; |
| 2174 | } |
| 2175 | else if (bom == 0xFFFE0000) { |
| 2176 | q += 4; |
| 2177 | bo = 1; |
| 2178 | } |
| 2179 | #else |
| 2180 | if (bom == 0x0000FEFF) { |
| 2181 | q += 4; |
| 2182 | bo = 1; |
| 2183 | } |
| 2184 | else if (bom == 0xFFFE0000) { |
| 2185 | q += 4; |
| 2186 | bo = -1; |
| 2187 | } |
| 2188 | #endif |
| 2189 | } |
| 2190 | } |
| 2191 | |
| 2192 | if (bo == -1) { |
| 2193 | /* force LE */ |
| 2194 | iorder[0] = 0; |
| 2195 | iorder[1] = 1; |
| 2196 | iorder[2] = 2; |
| 2197 | iorder[3] = 3; |
| 2198 | } |
| 2199 | else if (bo == 1) { |
| 2200 | /* force BE */ |
| 2201 | iorder[0] = 3; |
| 2202 | iorder[1] = 2; |
| 2203 | iorder[2] = 1; |
| 2204 | iorder[3] = 0; |
| 2205 | } |
| 2206 | |
| 2207 | while (q < e) { |
| 2208 | Py_UCS4 ch; |
| 2209 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 2210 | if (e-q<4) { |
| 2211 | if (consumed) |
| 2212 | break; |
| 2213 | errmsg = "truncated data"; |
| 2214 | startinpos = ((const char *)q)-starts; |
| 2215 | endinpos = ((const char *)e)-starts; |
| 2216 | goto utf32Error; |
| 2217 | /* The remaining input chars are ignored if the callback |
| 2218 | chooses to skip the input */ |
| 2219 | } |
| 2220 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2221 | (q[iorder[1]] << 8) | q[iorder[0]]; |
| 2222 | |
| 2223 | if (ch >= 0x110000) |
| 2224 | { |
| 2225 | errmsg = "codepoint not in range(0x110000)"; |
| 2226 | startinpos = ((const char *)q)-starts; |
| 2227 | endinpos = startinpos+4; |
| 2228 | goto utf32Error; |
| 2229 | } |
| 2230 | #ifndef Py_UNICODE_WIDE |
| 2231 | if (ch >= 0x10000) |
| 2232 | { |
| 2233 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 2234 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2235 | } |
| 2236 | else |
| 2237 | #endif |
| 2238 | *p++ = ch; |
| 2239 | q += 4; |
| 2240 | continue; |
| 2241 | utf32Error: |
| 2242 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2243 | if (unicode_decode_call_errorhandler( |
| 2244 | errors, &errorHandler, |
| 2245 | "utf32", errmsg, |
| 2246 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 2247 | (PyObject **)&unicode, &outpos, &p)) |
| 2248 | goto onError; |
| 2249 | } |
| 2250 | |
| 2251 | if (byteorder) |
| 2252 | *byteorder = bo; |
| 2253 | |
| 2254 | if (consumed) |
| 2255 | *consumed = (const char *)q-starts; |
| 2256 | |
| 2257 | /* Adjust length */ |
| 2258 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
| 2259 | goto onError; |
| 2260 | |
| 2261 | Py_XDECREF(errorHandler); |
| 2262 | Py_XDECREF(exc); |
| 2263 | return (PyObject *)unicode; |
| 2264 | |
| 2265 | onError: |
| 2266 | Py_DECREF(unicode); |
| 2267 | Py_XDECREF(errorHandler); |
| 2268 | Py_XDECREF(exc); |
| 2269 | return NULL; |
| 2270 | } |
| 2271 | |
| 2272 | PyObject * |
| 2273 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 2274 | Py_ssize_t size, |
| 2275 | const char *errors, |
| 2276 | int byteorder) |
| 2277 | { |
| 2278 | PyObject *v; |
| 2279 | unsigned char *p; |
| 2280 | #ifndef Py_UNICODE_WIDE |
| 2281 | int i, pairs; |
| 2282 | #else |
| 2283 | const int pairs = 0; |
| 2284 | #endif |
| 2285 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2286 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2287 | int iorder[] = {0, 1, 2, 3}; |
| 2288 | #else |
| 2289 | int iorder[] = {3, 2, 1, 0}; |
| 2290 | #endif |
| 2291 | |
| 2292 | #define STORECHAR(CH) \ |
| 2293 | do { \ |
| 2294 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 2295 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 2296 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 2297 | p[iorder[0]] = (CH) & 0xff; \ |
| 2298 | p += 4; \ |
| 2299 | } while(0) |
| 2300 | |
| 2301 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 2302 | so we need less space. */ |
| 2303 | #ifndef Py_UNICODE_WIDE |
| 2304 | for (i = pairs = 0; i < size-1; i++) |
| 2305 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 2306 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 2307 | pairs++; |
| 2308 | #endif |
| 2309 | v = PyBytes_FromStringAndSize(NULL, |
| 2310 | 4 * (size - pairs + (byteorder == 0))); |
| 2311 | if (v == NULL) |
| 2312 | return NULL; |
| 2313 | |
| 2314 | p = (unsigned char *)PyBytes_AS_STRING(v); |
| 2315 | if (byteorder == 0) |
| 2316 | STORECHAR(0xFEFF); |
| 2317 | if (size == 0) |
| 2318 | return v; |
| 2319 | |
| 2320 | if (byteorder == -1) { |
| 2321 | /* force LE */ |
| 2322 | iorder[0] = 0; |
| 2323 | iorder[1] = 1; |
| 2324 | iorder[2] = 2; |
| 2325 | iorder[3] = 3; |
| 2326 | } |
| 2327 | else if (byteorder == 1) { |
| 2328 | /* force BE */ |
| 2329 | iorder[0] = 3; |
| 2330 | iorder[1] = 2; |
| 2331 | iorder[2] = 1; |
| 2332 | iorder[3] = 0; |
| 2333 | } |
| 2334 | |
| 2335 | while (size-- > 0) { |
| 2336 | Py_UCS4 ch = *s++; |
| 2337 | #ifndef Py_UNICODE_WIDE |
| 2338 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 2339 | Py_UCS4 ch2 = *s; |
| 2340 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 2341 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 2342 | s++; |
| 2343 | size--; |
| 2344 | } |
| 2345 | } |
| 2346 | #endif |
| 2347 | STORECHAR(ch); |
| 2348 | } |
| 2349 | return v; |
| 2350 | #undef STORECHAR |
| 2351 | } |
| 2352 | |
| 2353 | PyObject *PyUnicode_AsUTF32String(PyObject *unicode) |
| 2354 | { |
| 2355 | if (!PyUnicode_Check(unicode)) { |
| 2356 | PyErr_BadArgument(); |
| 2357 | return NULL; |
| 2358 | } |
| 2359 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
| 2360 | PyUnicode_GET_SIZE(unicode), |
| 2361 | NULL, |
| 2362 | 0); |
| 2363 | } |
| 2364 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2365 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 2366 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2367 | PyObject * |
| 2368 | PyUnicode_DecodeUTF16(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2369 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2370 | const char *errors, |
| 2371 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2372 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2373 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 2374 | } |
| 2375 | |
| 2376 | PyObject * |
| 2377 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2378 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2379 | const char *errors, |
| 2380 | int *byteorder, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2381 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2382 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2383 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2384 | Py_ssize_t startinpos; |
| 2385 | Py_ssize_t endinpos; |
| 2386 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2387 | PyUnicodeObject *unicode; |
| 2388 | Py_UNICODE *p; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2389 | const unsigned char *q, *e; |
| 2390 | int bo = 0; /* assume native ordering by default */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2391 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2392 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 2393 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2394 | int ihi = 1, ilo = 0; |
| 2395 | #else |
| 2396 | int ihi = 0, ilo = 1; |
| 2397 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2398 | PyObject *errorHandler = NULL; |
| 2399 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2400 | |
| 2401 | /* Note: size will always be longer than the resulting Unicode |
| 2402 | character count */ |
| 2403 | unicode = _PyUnicode_New(size); |
| 2404 | if (!unicode) |
| 2405 | return NULL; |
| 2406 | if (size == 0) |
| 2407 | return (PyObject *)unicode; |
| 2408 | |
| 2409 | /* Unpack UTF-16 encoded data */ |
| 2410 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2411 | q = (unsigned char *)s; |
| 2412 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2413 | |
| 2414 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2415 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2416 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2417 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2418 | byte order setting accordingly. In native mode, the leading BOM |
| 2419 | mark is skipped, in all other modes, it is copied to the output |
| 2420 | stream as-is (giving a ZWNBSP character). */ |
| 2421 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2422 | if (size >= 2) { |
| 2423 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2424 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2425 | if (bom == 0xFEFF) { |
| 2426 | q += 2; |
| 2427 | bo = -1; |
| 2428 | } |
| 2429 | else if (bom == 0xFFFE) { |
| 2430 | q += 2; |
| 2431 | bo = 1; |
| 2432 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2433 | #else |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2434 | if (bom == 0xFEFF) { |
| 2435 | q += 2; |
| 2436 | bo = 1; |
| 2437 | } |
| 2438 | else if (bom == 0xFFFE) { |
| 2439 | q += 2; |
| 2440 | bo = -1; |
| 2441 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2442 | #endif |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2443 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2444 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2445 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2446 | if (bo == -1) { |
| 2447 | /* force LE */ |
| 2448 | ihi = 1; |
| 2449 | ilo = 0; |
| 2450 | } |
| 2451 | else if (bo == 1) { |
| 2452 | /* force BE */ |
| 2453 | ihi = 0; |
| 2454 | ilo = 1; |
| 2455 | } |
| 2456 | |
| 2457 | while (q < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2458 | Py_UNICODE ch; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2459 | /* remaining bytes at the end? (size should be even) */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2460 | if (e-q<2) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2461 | if (consumed) |
| 2462 | break; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2463 | errmsg = "truncated data"; |
| 2464 | startinpos = ((const char *)q)-starts; |
| 2465 | endinpos = ((const char *)e)-starts; |
| 2466 | goto utf16Error; |
| 2467 | /* The remaining input chars are ignored if the callback |
| 2468 | chooses to skip the input */ |
| 2469 | } |
| 2470 | ch = (q[ihi] << 8) | q[ilo]; |
| 2471 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2472 | q += 2; |
| 2473 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2474 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 2475 | *p++ = ch; |
| 2476 | continue; |
| 2477 | } |
| 2478 | |
| 2479 | /* UTF-16 code pair: */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2480 | if (q >= e) { |
| 2481 | errmsg = "unexpected end of data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2482 | startinpos = (((const char *)q)-2)-starts; |
| 2483 | endinpos = ((const char *)e)-starts; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2484 | goto utf16Error; |
| 2485 | } |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 2486 | if (0xD800 <= ch && ch <= 0xDBFF) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2487 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 2488 | q += 2; |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 2489 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2490 | #ifndef Py_UNICODE_WIDE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2491 | *p++ = ch; |
| 2492 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2493 | #else |
| 2494 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2495 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2496 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2497 | } |
| 2498 | else { |
| 2499 | errmsg = "illegal UTF-16 surrogate"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2500 | startinpos = (((const char *)q)-4)-starts; |
| 2501 | endinpos = startinpos+2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2502 | goto utf16Error; |
| 2503 | } |
| 2504 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2505 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2506 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2507 | startinpos = (((const char *)q)-2)-starts; |
| 2508 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2509 | /* Fall through to report the error */ |
| 2510 | |
| 2511 | utf16Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2512 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2513 | if (unicode_decode_call_errorhandler( |
| 2514 | errors, &errorHandler, |
| 2515 | "utf16", errmsg, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2516 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2517 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2518 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2519 | } |
| 2520 | |
| 2521 | if (byteorder) |
| 2522 | *byteorder = bo; |
| 2523 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2524 | if (consumed) |
| 2525 | *consumed = (const char *)q-starts; |
| 2526 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2527 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2528 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2529 | goto onError; |
| 2530 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2531 | Py_XDECREF(errorHandler); |
| 2532 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2533 | return (PyObject *)unicode; |
| 2534 | |
| 2535 | onError: |
| 2536 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2537 | Py_XDECREF(errorHandler); |
| 2538 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2539 | return NULL; |
| 2540 | } |
| 2541 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2542 | PyObject * |
| 2543 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2544 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2545 | const char *errors, |
| 2546 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2547 | { |
| 2548 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2549 | unsigned char *p; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2550 | #ifdef Py_UNICODE_WIDE |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2551 | int i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2552 | #else |
| 2553 | const int pairs = 0; |
| 2554 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2555 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2556 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2557 | int ihi = 1, ilo = 0; |
| 2558 | #else |
| 2559 | int ihi = 0, ilo = 1; |
| 2560 | #endif |
| 2561 | |
| 2562 | #define STORECHAR(CH) \ |
| 2563 | do { \ |
| 2564 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 2565 | p[ilo] = (CH) & 0xff; \ |
| 2566 | p += 2; \ |
| 2567 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2568 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2569 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2570 | for (i = pairs = 0; i < size; i++) |
| 2571 | if (s[i] >= 0x10000) |
| 2572 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2573 | #endif |
Walter Dörwald | 3cc3452 | 2007-05-04 10:48:27 +0000 | [diff] [blame] | 2574 | v = PyBytes_FromStringAndSize(NULL, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2575 | 2 * (size + pairs + (byteorder == 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2576 | if (v == NULL) |
| 2577 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2578 | |
Walter Dörwald | 3cc3452 | 2007-05-04 10:48:27 +0000 | [diff] [blame] | 2579 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2580 | if (byteorder == 0) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2581 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2582 | if (size == 0) |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 2583 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2584 | |
| 2585 | if (byteorder == -1) { |
| 2586 | /* force LE */ |
| 2587 | ihi = 1; |
| 2588 | ilo = 0; |
| 2589 | } |
| 2590 | else if (byteorder == 1) { |
| 2591 | /* force BE */ |
| 2592 | ihi = 0; |
| 2593 | ilo = 1; |
| 2594 | } |
| 2595 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2596 | while (size-- > 0) { |
| 2597 | Py_UNICODE ch = *s++; |
| 2598 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2599 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2600 | if (ch >= 0x10000) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2601 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2602 | ch = 0xD800 | ((ch-0x10000) >> 10); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2603 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2604 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2605 | STORECHAR(ch); |
| 2606 | if (ch2) |
| 2607 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2608 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2609 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2610 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2611 | } |
| 2612 | |
| 2613 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 2614 | { |
| 2615 | if (!PyUnicode_Check(unicode)) { |
| 2616 | PyErr_BadArgument(); |
| 2617 | return NULL; |
| 2618 | } |
| 2619 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
| 2620 | PyUnicode_GET_SIZE(unicode), |
| 2621 | NULL, |
| 2622 | 0); |
| 2623 | } |
| 2624 | |
| 2625 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 2626 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2627 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 2628 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2629 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2630 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2631 | const char *errors) |
| 2632 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2633 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2634 | Py_ssize_t startinpos; |
| 2635 | Py_ssize_t endinpos; |
| 2636 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2637 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2638 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2639 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2640 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2641 | char* message; |
| 2642 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2643 | PyObject *errorHandler = NULL; |
| 2644 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2645 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2646 | /* Escaped strings will always be longer than the resulting |
| 2647 | 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] | 2648 | length after conversion to the true value. |
| 2649 | (but if the error callback returns a long replacement string |
| 2650 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2651 | v = _PyUnicode_New(size); |
| 2652 | if (v == NULL) |
| 2653 | goto onError; |
| 2654 | if (size == 0) |
| 2655 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2656 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2657 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2658 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2659 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2660 | while (s < end) { |
| 2661 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2662 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2663 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2664 | |
| 2665 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 2666 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2667 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2668 | continue; |
| 2669 | } |
| 2670 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2671 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2672 | /* \ - Escapes */ |
| 2673 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame^] | 2674 | c = *s++; |
| 2675 | if (s > end) |
| 2676 | c = '\0'; /* Invalid after \ */ |
| 2677 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2678 | |
| 2679 | /* \x escapes */ |
| 2680 | case '\n': break; |
| 2681 | case '\\': *p++ = '\\'; break; |
| 2682 | case '\'': *p++ = '\''; break; |
| 2683 | case '\"': *p++ = '\"'; break; |
| 2684 | case 'b': *p++ = '\b'; break; |
| 2685 | case 'f': *p++ = '\014'; break; /* FF */ |
| 2686 | case 't': *p++ = '\t'; break; |
| 2687 | case 'n': *p++ = '\n'; break; |
| 2688 | case 'r': *p++ = '\r'; break; |
| 2689 | case 'v': *p++ = '\013'; break; /* VT */ |
| 2690 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 2691 | |
| 2692 | /* \OOO (octal) escapes */ |
| 2693 | case '0': case '1': case '2': case '3': |
| 2694 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2695 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame^] | 2696 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2697 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame^] | 2698 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2699 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2700 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2701 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2702 | break; |
| 2703 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2704 | /* hex escapes */ |
| 2705 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2706 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2707 | digits = 2; |
| 2708 | message = "truncated \\xXX escape"; |
| 2709 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2710 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2711 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2712 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2713 | digits = 4; |
| 2714 | message = "truncated \\uXXXX escape"; |
| 2715 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2716 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2717 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2718 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2719 | digits = 8; |
| 2720 | message = "truncated \\UXXXXXXXX escape"; |
| 2721 | hexescape: |
| 2722 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2723 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2724 | if (s+digits>end) { |
| 2725 | endinpos = size; |
| 2726 | if (unicode_decode_call_errorhandler( |
| 2727 | errors, &errorHandler, |
| 2728 | "unicodeescape", "end of string in escape sequence", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2729 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2730 | (PyObject **)&v, &outpos, &p)) |
| 2731 | goto onError; |
| 2732 | goto nextByte; |
| 2733 | } |
| 2734 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2735 | c = (unsigned char) s[i]; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2736 | if (!ISXDIGIT(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2737 | endinpos = (s+i+1)-starts; |
| 2738 | if (unicode_decode_call_errorhandler( |
| 2739 | errors, &errorHandler, |
| 2740 | "unicodeescape", message, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2741 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2742 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2743 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2744 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2745 | } |
| 2746 | chr = (chr<<4) & ~0xF; |
| 2747 | if (c >= '0' && c <= '9') |
| 2748 | chr += c - '0'; |
| 2749 | else if (c >= 'a' && c <= 'f') |
| 2750 | chr += 10 + c - 'a'; |
| 2751 | else |
| 2752 | chr += 10 + c - 'A'; |
| 2753 | } |
| 2754 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 2755 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2756 | /* _decoding_error will have already written into the |
| 2757 | target buffer. */ |
| 2758 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2759 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2760 | /* when we get here, chr is a 32-bit unicode character */ |
| 2761 | if (chr <= 0xffff) |
| 2762 | /* UCS-2 character */ |
| 2763 | *p++ = (Py_UNICODE) chr; |
| 2764 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2765 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2766 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2767 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2768 | *p++ = chr; |
| 2769 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2770 | chr -= 0x10000L; |
| 2771 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2772 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2773 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2774 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2775 | endinpos = s-starts; |
| 2776 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2777 | if (unicode_decode_call_errorhandler( |
| 2778 | errors, &errorHandler, |
| 2779 | "unicodeescape", "illegal Unicode character", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2780 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2781 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2782 | goto onError; |
| 2783 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2784 | break; |
| 2785 | |
| 2786 | /* \N{name} */ |
| 2787 | case 'N': |
| 2788 | message = "malformed \\N character escape"; |
| 2789 | if (ucnhash_CAPI == NULL) { |
| 2790 | /* load the unicode data module */ |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2791 | PyObject *m, *api; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2792 | m = PyImport_ImportModule("unicodedata"); |
| 2793 | if (m == NULL) |
| 2794 | goto ucnhashError; |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2795 | api = PyObject_GetAttrString(m, "ucnhash_CAPI"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2796 | Py_DECREF(m); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2797 | if (api == NULL) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2798 | goto ucnhashError; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2799 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCObject_AsVoidPtr(api); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2800 | Py_DECREF(api); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2801 | if (ucnhash_CAPI == NULL) |
| 2802 | goto ucnhashError; |
| 2803 | } |
| 2804 | if (*s == '{') { |
| 2805 | const char *start = s+1; |
| 2806 | /* look for the closing brace */ |
| 2807 | while (*s != '}' && s < end) |
| 2808 | s++; |
| 2809 | if (s > start && s < end && *s == '}') { |
| 2810 | /* found a name. look it up in the unicode database */ |
| 2811 | message = "unknown Unicode character name"; |
| 2812 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 2813 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2814 | goto store; |
| 2815 | } |
| 2816 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2817 | endinpos = s-starts; |
| 2818 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2819 | if (unicode_decode_call_errorhandler( |
| 2820 | errors, &errorHandler, |
| 2821 | "unicodeescape", message, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2822 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2823 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2824 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2825 | break; |
| 2826 | |
| 2827 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2828 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2829 | message = "\\ at end of string"; |
| 2830 | s--; |
| 2831 | endinpos = s-starts; |
| 2832 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2833 | if (unicode_decode_call_errorhandler( |
| 2834 | errors, &errorHandler, |
| 2835 | "unicodeescape", message, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2836 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2837 | (PyObject **)&v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2838 | goto onError; |
| 2839 | } |
| 2840 | else { |
| 2841 | *p++ = '\\'; |
| 2842 | *p++ = (unsigned char)s[-1]; |
| 2843 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2844 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2845 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2846 | nextByte: |
| 2847 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2848 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2849 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2850 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 2851 | Py_XDECREF(errorHandler); |
| 2852 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2853 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2854 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2855 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2856 | PyErr_SetString( |
| 2857 | PyExc_UnicodeError, |
| 2858 | "\\N escapes not supported (can't load unicodedata module)" |
| 2859 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2860 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2861 | Py_XDECREF(errorHandler); |
| 2862 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 2863 | return NULL; |
| 2864 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2865 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2866 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2867 | Py_XDECREF(errorHandler); |
| 2868 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2869 | return NULL; |
| 2870 | } |
| 2871 | |
| 2872 | /* Return a Unicode-Escape string version of the Unicode object. |
| 2873 | |
| 2874 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 2875 | appropriate. |
| 2876 | |
| 2877 | */ |
| 2878 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2879 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
| 2880 | Py_ssize_t size, |
| 2881 | Py_UNICODE ch) |
| 2882 | { |
| 2883 | /* like wcschr, but doesn't stop at NULL characters */ |
| 2884 | |
| 2885 | while (size-- > 0) { |
| 2886 | if (*s == ch) |
| 2887 | return s; |
| 2888 | s++; |
| 2889 | } |
| 2890 | |
| 2891 | return NULL; |
| 2892 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 2893 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2894 | static const char *hexdigits = "0123456789abcdef"; |
| 2895 | |
| 2896 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 2897 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2898 | { |
| 2899 | PyObject *repr; |
| 2900 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2901 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2902 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 2903 | better to choose a different scheme. Perhaps scan the |
| 2904 | first N-chars of the string and allocate based on that size. |
| 2905 | */ |
| 2906 | /* Initial allocation is based on the longest-possible unichr |
| 2907 | escape. |
| 2908 | |
| 2909 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 2910 | unichr, so in this case it's the longest unichr escape. In |
| 2911 | narrow (UTF-16) builds this is five chars per source unichr |
| 2912 | since there are two unichrs in the surrogate pair, so in narrow |
| 2913 | (UTF-16) builds it's not the longest unichr escape. |
| 2914 | |
| 2915 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 2916 | so in the narrow (UTF-16) build case it's the longest unichr |
| 2917 | escape. |
| 2918 | */ |
| 2919 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2920 | repr = PyBytes_FromStringAndSize(NULL, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2921 | #ifdef Py_UNICODE_WIDE |
| 2922 | + 10*size |
| 2923 | #else |
| 2924 | + 6*size |
| 2925 | #endif |
| 2926 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2927 | if (repr == NULL) |
| 2928 | return NULL; |
| 2929 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2930 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2931 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2932 | while (size-- > 0) { |
| 2933 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2934 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2935 | /* Escape backslashes */ |
| 2936 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2937 | *p++ = '\\'; |
| 2938 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2939 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2940 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2941 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 2942 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2943 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 2944 | else if (ch >= 0x10000) { |
| 2945 | *p++ = '\\'; |
| 2946 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2947 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 2948 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 2949 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 2950 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 2951 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 2952 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 2953 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 2954 | *p++ = hexdigits[ch & 0x0000000F]; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2955 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2956 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2957 | #else |
| 2958 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2959 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 2960 | Py_UNICODE ch2; |
| 2961 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2962 | |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2963 | ch2 = *s++; |
| 2964 | size--; |
| 2965 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 2966 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 2967 | *p++ = '\\'; |
| 2968 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2969 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 2970 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 2971 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 2972 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 2973 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 2974 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 2975 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 2976 | *p++ = hexdigits[ucs & 0x0000000F]; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2977 | continue; |
| 2978 | } |
| 2979 | /* Fall through: isolated surrogates are copied as-is */ |
| 2980 | s--; |
| 2981 | size++; |
| 2982 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2983 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2984 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2985 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2986 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2987 | *p++ = '\\'; |
| 2988 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2989 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 2990 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 2991 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 2992 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2993 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2994 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2995 | /* Map special whitespace to '\t', \n', '\r' */ |
| 2996 | else if (ch == '\t') { |
| 2997 | *p++ = '\\'; |
| 2998 | *p++ = 't'; |
| 2999 | } |
| 3000 | else if (ch == '\n') { |
| 3001 | *p++ = '\\'; |
| 3002 | *p++ = 'n'; |
| 3003 | } |
| 3004 | else if (ch == '\r') { |
| 3005 | *p++ = '\\'; |
| 3006 | *p++ = 'r'; |
| 3007 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3008 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3009 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 3010 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3011 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3012 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3013 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 3014 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3015 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3016 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3017 | /* Copy everything else as-is */ |
| 3018 | else |
| 3019 | *p++ = (char) ch; |
| 3020 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3021 | |
| 3022 | *p = '\0'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3023 | if (PyBytes_Resize(repr, p - PyBytes_AS_STRING(repr))) { |
| 3024 | Py_DECREF(repr); |
| 3025 | return NULL; |
| 3026 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3027 | return repr; |
| 3028 | } |
| 3029 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3030 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
| 3031 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3032 | PyObject *s, *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3033 | if (!PyUnicode_Check(unicode)) { |
| 3034 | PyErr_BadArgument(); |
| 3035 | return NULL; |
| 3036 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3037 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3038 | PyUnicode_GET_SIZE(unicode)); |
| 3039 | |
| 3040 | if (!s) |
| 3041 | return NULL; |
| 3042 | result = PyString_FromStringAndSize(PyBytes_AS_STRING(s), |
| 3043 | PyBytes_GET_SIZE(s)); |
| 3044 | Py_DECREF(s); |
| 3045 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3046 | } |
| 3047 | |
| 3048 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 3049 | |
| 3050 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3051 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3052 | const char *errors) |
| 3053 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3054 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3055 | Py_ssize_t startinpos; |
| 3056 | Py_ssize_t endinpos; |
| 3057 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3058 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3059 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3060 | const char *end; |
| 3061 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3062 | PyObject *errorHandler = NULL; |
| 3063 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3064 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3065 | /* Escaped strings will always be longer than the resulting |
| 3066 | 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] | 3067 | length after conversion to the true value. (But decoding error |
| 3068 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3069 | v = _PyUnicode_New(size); |
| 3070 | if (v == NULL) |
| 3071 | goto onError; |
| 3072 | if (size == 0) |
| 3073 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3074 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3075 | end = s + size; |
| 3076 | while (s < end) { |
| 3077 | unsigned char c; |
Martin v. Löwis | 047c05e | 2002-03-21 08:55:28 +0000 | [diff] [blame] | 3078 | Py_UCS4 x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3079 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3080 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3081 | |
| 3082 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3083 | if (*s != '\\') { |
| 3084 | *p++ = (unsigned char)*s++; |
| 3085 | continue; |
| 3086 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3087 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3088 | |
| 3089 | /* \u-escapes are only interpreted iff the number of leading |
| 3090 | backslashes if odd */ |
| 3091 | bs = s; |
| 3092 | for (;s < end;) { |
| 3093 | if (*s != '\\') |
| 3094 | break; |
| 3095 | *p++ = (unsigned char)*s++; |
| 3096 | } |
| 3097 | if (((s - bs) & 1) == 0 || |
| 3098 | s >= end || |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3099 | (*s != 'u' && *s != 'U')) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3100 | continue; |
| 3101 | } |
| 3102 | p--; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3103 | count = *s=='u' ? 4 : 8; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3104 | s++; |
| 3105 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3106 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3107 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3108 | for (x = 0, i = 0; i < count; ++i, ++s) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3109 | c = (unsigned char)*s; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3110 | if (!ISXDIGIT(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3111 | endinpos = s-starts; |
| 3112 | if (unicode_decode_call_errorhandler( |
| 3113 | errors, &errorHandler, |
| 3114 | "rawunicodeescape", "truncated \\uXXXX", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3115 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3116 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3117 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3118 | goto nextByte; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3119 | } |
| 3120 | x = (x<<4) & ~0xF; |
| 3121 | if (c >= '0' && c <= '9') |
| 3122 | x += c - '0'; |
| 3123 | else if (c >= 'a' && c <= 'f') |
| 3124 | x += 10 + c - 'a'; |
| 3125 | else |
| 3126 | x += 10 + c - 'A'; |
| 3127 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3128 | #ifndef Py_UNICODE_WIDE |
| 3129 | if (x > 0x10000) { |
| 3130 | if (unicode_decode_call_errorhandler( |
| 3131 | errors, &errorHandler, |
| 3132 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3133 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3134 | (PyObject **)&v, &outpos, &p)) |
| 3135 | goto onError; |
| 3136 | } |
| 3137 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3138 | *p++ = x; |
| 3139 | nextByte: |
| 3140 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3141 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3142 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3143 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3144 | Py_XDECREF(errorHandler); |
| 3145 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3146 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3147 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3148 | onError: |
| 3149 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3150 | Py_XDECREF(errorHandler); |
| 3151 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3152 | return NULL; |
| 3153 | } |
| 3154 | |
| 3155 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3156 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3157 | { |
| 3158 | PyObject *repr; |
| 3159 | char *p; |
| 3160 | char *q; |
| 3161 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3162 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3163 | repr = PyBytes_FromStringAndSize(NULL, 10 * size); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3164 | #else |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3165 | repr = PyBytes_FromStringAndSize(NULL, 6 * size); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3166 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3167 | if (repr == NULL) |
| 3168 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3169 | if (size == 0) |
| 3170 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3171 | |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3172 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3173 | while (size-- > 0) { |
| 3174 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3175 | #ifdef Py_UNICODE_WIDE |
| 3176 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 3177 | if (ch >= 0x10000) { |
| 3178 | *p++ = '\\'; |
| 3179 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 3180 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 3181 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 3182 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 3183 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 3184 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 3185 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 3186 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 3187 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3188 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3189 | else |
| 3190 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3191 | /* Map 16-bit characters to '\uxxxx' */ |
| 3192 | if (ch >= 256) { |
| 3193 | *p++ = '\\'; |
| 3194 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 3195 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 3196 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 3197 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 3198 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3199 | } |
| 3200 | /* Copy everything else as-is */ |
| 3201 | else |
| 3202 | *p++ = (char) ch; |
| 3203 | } |
| 3204 | *p = '\0'; |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3205 | if (PyBytes_Resize(repr, p - q)) { |
| 3206 | Py_DECREF(repr); |
| 3207 | return NULL; |
| 3208 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3209 | return repr; |
| 3210 | } |
| 3211 | |
| 3212 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 3213 | { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3214 | PyObject *s, *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3215 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3216 | PyErr_BadArgument(); |
| 3217 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3218 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3219 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3220 | PyUnicode_GET_SIZE(unicode)); |
| 3221 | |
| 3222 | if (!s) |
| 3223 | return NULL; |
| 3224 | result = PyString_FromStringAndSize(PyBytes_AS_STRING(s), |
| 3225 | PyBytes_GET_SIZE(s)); |
| 3226 | Py_DECREF(s); |
| 3227 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3228 | } |
| 3229 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3230 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 3231 | |
| 3232 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3233 | Py_ssize_t size, |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3234 | const char *errors) |
| 3235 | { |
| 3236 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3237 | Py_ssize_t startinpos; |
| 3238 | Py_ssize_t endinpos; |
| 3239 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3240 | PyUnicodeObject *v; |
| 3241 | Py_UNICODE *p; |
| 3242 | const char *end; |
| 3243 | const char *reason; |
| 3244 | PyObject *errorHandler = NULL; |
| 3245 | PyObject *exc = NULL; |
| 3246 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 3247 | #ifdef Py_UNICODE_WIDE |
| 3248 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 3249 | #endif |
| 3250 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3251 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3252 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 3253 | if (v == NULL) |
| 3254 | goto onError; |
| 3255 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
| 3256 | return (PyObject *)v; |
| 3257 | p = PyUnicode_AS_UNICODE(v); |
| 3258 | end = s + size; |
| 3259 | |
| 3260 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3261 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3262 | /* We have to sanity check the raw data, otherwise doom looms for |
| 3263 | some malformed UCS-4 data. */ |
| 3264 | if ( |
| 3265 | #ifdef Py_UNICODE_WIDE |
| 3266 | *p > unimax || *p < 0 || |
| 3267 | #endif |
| 3268 | end-s < Py_UNICODE_SIZE |
| 3269 | ) |
| 3270 | { |
| 3271 | startinpos = s - starts; |
| 3272 | if (end-s < Py_UNICODE_SIZE) { |
| 3273 | endinpos = end-starts; |
| 3274 | reason = "truncated input"; |
| 3275 | } |
| 3276 | else { |
| 3277 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 3278 | reason = "illegal code point (> 0x10FFFF)"; |
| 3279 | } |
| 3280 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 3281 | if (unicode_decode_call_errorhandler( |
| 3282 | errors, &errorHandler, |
| 3283 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3284 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3285 | (PyObject **)&v, &outpos, &p)) { |
| 3286 | goto onError; |
| 3287 | } |
| 3288 | } |
| 3289 | else { |
| 3290 | p++; |
| 3291 | s += Py_UNICODE_SIZE; |
| 3292 | } |
| 3293 | } |
| 3294 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3295 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3296 | goto onError; |
| 3297 | Py_XDECREF(errorHandler); |
| 3298 | Py_XDECREF(exc); |
| 3299 | return (PyObject *)v; |
| 3300 | |
| 3301 | onError: |
| 3302 | Py_XDECREF(v); |
| 3303 | Py_XDECREF(errorHandler); |
| 3304 | Py_XDECREF(exc); |
| 3305 | return NULL; |
| 3306 | } |
| 3307 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3308 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 3309 | |
| 3310 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3311 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3312 | const char *errors) |
| 3313 | { |
| 3314 | PyUnicodeObject *v; |
| 3315 | Py_UNICODE *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3316 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3317 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3318 | if (size == 1) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3319 | Py_UNICODE r = *(unsigned char*)s; |
| 3320 | return PyUnicode_FromUnicode(&r, 1); |
| 3321 | } |
| 3322 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3323 | v = _PyUnicode_New(size); |
| 3324 | if (v == NULL) |
| 3325 | goto onError; |
| 3326 | if (size == 0) |
| 3327 | return (PyObject *)v; |
| 3328 | p = PyUnicode_AS_UNICODE(v); |
| 3329 | while (size-- > 0) |
| 3330 | *p++ = (unsigned char)*s++; |
| 3331 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3332 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3333 | onError: |
| 3334 | Py_XDECREF(v); |
| 3335 | return NULL; |
| 3336 | } |
| 3337 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3338 | /* create or adjust a UnicodeEncodeError */ |
| 3339 | static void make_encode_exception(PyObject **exceptionObject, |
| 3340 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3341 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3342 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3343 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3344 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3345 | if (*exceptionObject == NULL) { |
| 3346 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 3347 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3348 | } |
| 3349 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3350 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 3351 | goto onError; |
| 3352 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 3353 | goto onError; |
| 3354 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 3355 | goto onError; |
| 3356 | return; |
| 3357 | onError: |
| 3358 | Py_DECREF(*exceptionObject); |
| 3359 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3360 | } |
| 3361 | } |
| 3362 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3363 | /* raises a UnicodeEncodeError */ |
| 3364 | static void raise_encode_exception(PyObject **exceptionObject, |
| 3365 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3366 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3367 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3368 | const char *reason) |
| 3369 | { |
| 3370 | make_encode_exception(exceptionObject, |
| 3371 | encoding, unicode, size, startpos, endpos, reason); |
| 3372 | if (*exceptionObject != NULL) |
| 3373 | PyCodec_StrictErrors(*exceptionObject); |
| 3374 | } |
| 3375 | |
| 3376 | /* error handling callback helper: |
| 3377 | build arguments, call the callback and check the arguments, |
| 3378 | put the result into newpos and return the replacement string, which |
| 3379 | has to be freed by the caller */ |
| 3380 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
| 3381 | PyObject **errorHandler, |
| 3382 | const char *encoding, const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3383 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 3384 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3385 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3386 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3387 | 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] | 3388 | |
| 3389 | PyObject *restuple; |
| 3390 | PyObject *resunicode; |
| 3391 | |
| 3392 | if (*errorHandler == NULL) { |
| 3393 | *errorHandler = PyCodec_LookupError(errors); |
| 3394 | if (*errorHandler == NULL) |
| 3395 | return NULL; |
| 3396 | } |
| 3397 | |
| 3398 | make_encode_exception(exceptionObject, |
| 3399 | encoding, unicode, size, startpos, endpos, reason); |
| 3400 | if (*exceptionObject == NULL) |
| 3401 | return NULL; |
| 3402 | |
| 3403 | restuple = PyObject_CallFunctionObjArgs( |
| 3404 | *errorHandler, *exceptionObject, NULL); |
| 3405 | if (restuple == NULL) |
| 3406 | return NULL; |
| 3407 | if (!PyTuple_Check(restuple)) { |
| 3408 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 3409 | Py_DECREF(restuple); |
| 3410 | return NULL; |
| 3411 | } |
| 3412 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
| 3413 | &resunicode, newpos)) { |
| 3414 | Py_DECREF(restuple); |
| 3415 | return NULL; |
| 3416 | } |
| 3417 | if (*newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3418 | *newpos = size+*newpos; |
| 3419 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 3420 | 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] | 3421 | Py_DECREF(restuple); |
| 3422 | return NULL; |
| 3423 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3424 | Py_INCREF(resunicode); |
| 3425 | Py_DECREF(restuple); |
| 3426 | return resunicode; |
| 3427 | } |
| 3428 | |
| 3429 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3430 | Py_ssize_t size, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3431 | const char *errors, |
| 3432 | int limit) |
| 3433 | { |
| 3434 | /* output object */ |
| 3435 | PyObject *res; |
| 3436 | /* pointers to the beginning and end+1 of input */ |
| 3437 | const Py_UNICODE *startp = p; |
| 3438 | const Py_UNICODE *endp = p + size; |
| 3439 | /* pointer to the beginning of the unencodable characters */ |
| 3440 | /* const Py_UNICODE *badp = NULL; */ |
| 3441 | /* pointer into the output */ |
| 3442 | char *str; |
| 3443 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3444 | Py_ssize_t respos = 0; |
| 3445 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3446 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 3447 | 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] | 3448 | PyObject *errorHandler = NULL; |
| 3449 | PyObject *exc = NULL; |
| 3450 | /* the following variable is used for caching string comparisons |
| 3451 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 3452 | int known_errorHandler = -1; |
| 3453 | |
| 3454 | /* allocate enough for a simple encoding without |
| 3455 | replacements, if we need more, we'll resize */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3456 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3457 | if (res == NULL) |
| 3458 | goto onError; |
| 3459 | if (size == 0) |
| 3460 | return res; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3461 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3462 | ressize = size; |
| 3463 | |
| 3464 | while (p<endp) { |
| 3465 | Py_UNICODE c = *p; |
| 3466 | |
| 3467 | /* can we encode this? */ |
| 3468 | if (c<limit) { |
| 3469 | /* no overflow check, because we know that the space is enough */ |
| 3470 | *str++ = (char)c; |
| 3471 | ++p; |
| 3472 | } |
| 3473 | else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3474 | Py_ssize_t unicodepos = p-startp; |
| 3475 | Py_ssize_t requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3476 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3477 | Py_ssize_t repsize; |
| 3478 | Py_ssize_t newpos; |
| 3479 | Py_ssize_t respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3480 | Py_UNICODE *uni2; |
| 3481 | /* startpos for collecting unencodable chars */ |
| 3482 | const Py_UNICODE *collstart = p; |
| 3483 | const Py_UNICODE *collend = p; |
| 3484 | /* find all unecodable characters */ |
| 3485 | while ((collend < endp) && ((*collend)>=limit)) |
| 3486 | ++collend; |
| 3487 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 3488 | if (known_errorHandler==-1) { |
| 3489 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3490 | known_errorHandler = 1; |
| 3491 | else if (!strcmp(errors, "replace")) |
| 3492 | known_errorHandler = 2; |
| 3493 | else if (!strcmp(errors, "ignore")) |
| 3494 | known_errorHandler = 3; |
| 3495 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3496 | known_errorHandler = 4; |
| 3497 | else |
| 3498 | known_errorHandler = 0; |
| 3499 | } |
| 3500 | switch (known_errorHandler) { |
| 3501 | case 1: /* strict */ |
| 3502 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 3503 | goto onError; |
| 3504 | case 2: /* replace */ |
| 3505 | while (collstart++<collend) |
| 3506 | *str++ = '?'; /* fall through */ |
| 3507 | case 3: /* ignore */ |
| 3508 | p = collend; |
| 3509 | break; |
| 3510 | case 4: /* xmlcharrefreplace */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3511 | respos = str - PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3512 | /* determine replacement size (temporarily (mis)uses p) */ |
| 3513 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 3514 | if (*p<10) |
| 3515 | repsize += 2+1+1; |
| 3516 | else if (*p<100) |
| 3517 | repsize += 2+2+1; |
| 3518 | else if (*p<1000) |
| 3519 | repsize += 2+3+1; |
| 3520 | else if (*p<10000) |
| 3521 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 3522 | #ifndef Py_UNICODE_WIDE |
| 3523 | else |
| 3524 | repsize += 2+5+1; |
| 3525 | #else |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3526 | else if (*p<100000) |
| 3527 | repsize += 2+5+1; |
| 3528 | else if (*p<1000000) |
| 3529 | repsize += 2+6+1; |
| 3530 | else |
| 3531 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3532 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3533 | } |
| 3534 | requiredsize = respos+repsize+(endp-collend); |
| 3535 | if (requiredsize > ressize) { |
| 3536 | if (requiredsize<2*ressize) |
| 3537 | requiredsize = 2*ressize; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3538 | if (PyBytes_Resize(res, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3539 | goto onError; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3540 | str = PyBytes_AS_STRING(res) + respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3541 | ressize = requiredsize; |
| 3542 | } |
| 3543 | /* generate replacement (temporarily (mis)uses p) */ |
| 3544 | for (p = collstart; p < collend; ++p) { |
| 3545 | str += sprintf(str, "&#%d;", (int)*p); |
| 3546 | } |
| 3547 | p = collend; |
| 3548 | break; |
| 3549 | default: |
| 3550 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 3551 | encoding, reason, startp, size, &exc, |
| 3552 | collstart-startp, collend-startp, &newpos); |
| 3553 | if (repunicode == NULL) |
| 3554 | goto onError; |
| 3555 | /* need more space? (at least enough for what we |
| 3556 | have+the replacement+the rest of the string, so |
| 3557 | we won't have to check space for encodable characters) */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3558 | respos = str - PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3559 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3560 | requiredsize = respos+repsize+(endp-collend); |
| 3561 | if (requiredsize > ressize) { |
| 3562 | if (requiredsize<2*ressize) |
| 3563 | requiredsize = 2*ressize; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3564 | if (PyBytes_Resize(res, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3565 | Py_DECREF(repunicode); |
| 3566 | goto onError; |
| 3567 | } |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3568 | str = PyBytes_AS_STRING(res) + respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3569 | ressize = requiredsize; |
| 3570 | } |
| 3571 | /* check if there is anything unencodable in the replacement |
| 3572 | and copy it to the output */ |
| 3573 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 3574 | c = *uni2; |
| 3575 | if (c >= limit) { |
| 3576 | raise_encode_exception(&exc, encoding, startp, size, |
| 3577 | unicodepos, unicodepos+1, reason); |
| 3578 | Py_DECREF(repunicode); |
| 3579 | goto onError; |
| 3580 | } |
| 3581 | *str = (char)c; |
| 3582 | } |
| 3583 | p = startp + newpos; |
| 3584 | Py_DECREF(repunicode); |
| 3585 | } |
| 3586 | } |
| 3587 | } |
| 3588 | /* Resize if we allocated to much */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3589 | respos = str - PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3590 | if (respos<ressize) |
| 3591 | /* If this falls res will be NULL */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3592 | PyBytes_Resize(res, respos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3593 | Py_XDECREF(errorHandler); |
| 3594 | Py_XDECREF(exc); |
| 3595 | return res; |
| 3596 | |
| 3597 | onError: |
| 3598 | Py_XDECREF(res); |
| 3599 | Py_XDECREF(errorHandler); |
| 3600 | Py_XDECREF(exc); |
| 3601 | return NULL; |
| 3602 | } |
| 3603 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3604 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3605 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3606 | const char *errors) |
| 3607 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3608 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3609 | } |
| 3610 | |
| 3611 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 3612 | { |
| 3613 | if (!PyUnicode_Check(unicode)) { |
| 3614 | PyErr_BadArgument(); |
| 3615 | return NULL; |
| 3616 | } |
| 3617 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 3618 | PyUnicode_GET_SIZE(unicode), |
| 3619 | NULL); |
| 3620 | } |
| 3621 | |
| 3622 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 3623 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3624 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3625 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3626 | const char *errors) |
| 3627 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3628 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3629 | PyUnicodeObject *v; |
| 3630 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3631 | Py_ssize_t startinpos; |
| 3632 | Py_ssize_t endinpos; |
| 3633 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3634 | const char *e; |
| 3635 | PyObject *errorHandler = NULL; |
| 3636 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3637 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3638 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3639 | if (size == 1 && *(unsigned char*)s < 128) { |
| 3640 | Py_UNICODE r = *(unsigned char*)s; |
| 3641 | return PyUnicode_FromUnicode(&r, 1); |
| 3642 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3643 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3644 | v = _PyUnicode_New(size); |
| 3645 | if (v == NULL) |
| 3646 | goto onError; |
| 3647 | if (size == 0) |
| 3648 | return (PyObject *)v; |
| 3649 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3650 | e = s + size; |
| 3651 | while (s < e) { |
| 3652 | register unsigned char c = (unsigned char)*s; |
| 3653 | if (c < 128) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3654 | *p++ = c; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3655 | ++s; |
| 3656 | } |
| 3657 | else { |
| 3658 | startinpos = s-starts; |
| 3659 | endinpos = startinpos + 1; |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 3660 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3661 | if (unicode_decode_call_errorhandler( |
| 3662 | errors, &errorHandler, |
| 3663 | "ascii", "ordinal not in range(128)", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3664 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3665 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3666 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3667 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3668 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3669 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3670 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3671 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3672 | Py_XDECREF(errorHandler); |
| 3673 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3674 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3675 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3676 | onError: |
| 3677 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3678 | Py_XDECREF(errorHandler); |
| 3679 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3680 | return NULL; |
| 3681 | } |
| 3682 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3683 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3684 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3685 | const char *errors) |
| 3686 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3687 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3688 | } |
| 3689 | |
| 3690 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 3691 | { |
| 3692 | if (!PyUnicode_Check(unicode)) { |
| 3693 | PyErr_BadArgument(); |
| 3694 | return NULL; |
| 3695 | } |
| 3696 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 3697 | PyUnicode_GET_SIZE(unicode), |
| 3698 | NULL); |
| 3699 | } |
| 3700 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3701 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3702 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3703 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3704 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3705 | #if SIZEOF_INT < SIZEOF_SSIZE_T |
| 3706 | #define NEED_RETRY |
| 3707 | #endif |
| 3708 | |
| 3709 | /* XXX This code is limited to "true" double-byte encodings, as |
| 3710 | a) it assumes an incomplete character consists of a single byte, and |
| 3711 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
| 3712 | encodings, see IsDBCSLeadByteEx documentation. */ |
| 3713 | |
| 3714 | static int is_dbcs_lead_byte(const char *s, int offset) |
| 3715 | { |
| 3716 | const char *curr = s + offset; |
| 3717 | |
| 3718 | if (IsDBCSLeadByte(*curr)) { |
| 3719 | const char *prev = CharPrev(s, curr); |
| 3720 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
| 3721 | } |
| 3722 | return 0; |
| 3723 | } |
| 3724 | |
| 3725 | /* |
| 3726 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 3727 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 3728 | */ |
| 3729 | static int decode_mbcs(PyUnicodeObject **v, |
| 3730 | const char *s, /* MBCS string */ |
| 3731 | int size, /* sizeof MBCS string */ |
| 3732 | int final) |
| 3733 | { |
| 3734 | Py_UNICODE *p; |
| 3735 | Py_ssize_t n = 0; |
| 3736 | int usize = 0; |
| 3737 | |
| 3738 | assert(size >= 0); |
| 3739 | |
| 3740 | /* Skip trailing lead-byte unless 'final' is set */ |
| 3741 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
| 3742 | --size; |
| 3743 | |
| 3744 | /* First get the size of the result */ |
| 3745 | if (size > 0) { |
| 3746 | usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); |
| 3747 | if (usize == 0) { |
| 3748 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3749 | return -1; |
| 3750 | } |
| 3751 | } |
| 3752 | |
| 3753 | if (*v == NULL) { |
| 3754 | /* Create unicode object */ |
| 3755 | *v = _PyUnicode_New(usize); |
| 3756 | if (*v == NULL) |
| 3757 | return -1; |
| 3758 | } |
| 3759 | else { |
| 3760 | /* Extend unicode object */ |
| 3761 | n = PyUnicode_GET_SIZE(*v); |
| 3762 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 3763 | return -1; |
| 3764 | } |
| 3765 | |
| 3766 | /* Do the conversion */ |
| 3767 | if (size > 0) { |
| 3768 | p = PyUnicode_AS_UNICODE(*v) + n; |
| 3769 | if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { |
| 3770 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3771 | return -1; |
| 3772 | } |
| 3773 | } |
| 3774 | |
| 3775 | return size; |
| 3776 | } |
| 3777 | |
| 3778 | PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
| 3779 | Py_ssize_t size, |
| 3780 | const char *errors, |
| 3781 | Py_ssize_t *consumed) |
| 3782 | { |
| 3783 | PyUnicodeObject *v = NULL; |
| 3784 | int done; |
| 3785 | |
| 3786 | if (consumed) |
| 3787 | *consumed = 0; |
| 3788 | |
| 3789 | #ifdef NEED_RETRY |
| 3790 | retry: |
| 3791 | if (size > INT_MAX) |
| 3792 | done = decode_mbcs(&v, s, INT_MAX, 0); |
| 3793 | else |
| 3794 | #endif |
| 3795 | done = decode_mbcs(&v, s, (int)size, !consumed); |
| 3796 | |
| 3797 | if (done < 0) { |
| 3798 | Py_XDECREF(v); |
| 3799 | return NULL; |
| 3800 | } |
| 3801 | |
| 3802 | if (consumed) |
| 3803 | *consumed += done; |
| 3804 | |
| 3805 | #ifdef NEED_RETRY |
| 3806 | if (size > INT_MAX) { |
| 3807 | s += done; |
| 3808 | size -= done; |
| 3809 | goto retry; |
| 3810 | } |
| 3811 | #endif |
| 3812 | |
| 3813 | return (PyObject *)v; |
| 3814 | } |
| 3815 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3816 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3817 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3818 | const char *errors) |
| 3819 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3820 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 3821 | } |
| 3822 | |
| 3823 | /* |
| 3824 | * Convert unicode into string object (MBCS). |
| 3825 | * Returns 0 if succeed, -1 otherwise. |
| 3826 | */ |
| 3827 | static int encode_mbcs(PyObject **repr, |
| 3828 | const Py_UNICODE *p, /* unicode */ |
| 3829 | int size) /* size of unicode */ |
| 3830 | { |
| 3831 | int mbcssize = 0; |
| 3832 | Py_ssize_t n = 0; |
| 3833 | |
| 3834 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3835 | |
| 3836 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3837 | if (size > 0) { |
| 3838 | mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); |
| 3839 | if (mbcssize == 0) { |
| 3840 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3841 | return -1; |
| 3842 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3843 | } |
| 3844 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3845 | if (*repr == NULL) { |
| 3846 | /* Create string object */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3847 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3848 | if (*repr == NULL) |
| 3849 | return -1; |
| 3850 | } |
| 3851 | else { |
| 3852 | /* Extend string object */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3853 | n = PyBytes_Size(*repr); |
| 3854 | if (PyBytes_Resize(*repr, n + mbcssize) < 0) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3855 | return -1; |
| 3856 | } |
| 3857 | |
| 3858 | /* Do the conversion */ |
| 3859 | if (size > 0) { |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3860 | char *s = PyBytes_AS_STRING(*repr) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3861 | if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { |
| 3862 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3863 | return -1; |
| 3864 | } |
| 3865 | } |
| 3866 | |
| 3867 | return 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3868 | } |
| 3869 | |
| 3870 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3871 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3872 | const char *errors) |
| 3873 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3874 | PyObject *repr = NULL; |
| 3875 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 3876 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3877 | #ifdef NEED_RETRY |
| 3878 | retry: |
| 3879 | if (size > INT_MAX) |
| 3880 | ret = encode_mbcs(&repr, p, INT_MAX); |
| 3881 | else |
| 3882 | #endif |
| 3883 | ret = encode_mbcs(&repr, p, (int)size); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3884 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3885 | if (ret < 0) { |
| 3886 | Py_XDECREF(repr); |
| 3887 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3888 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3889 | |
| 3890 | #ifdef NEED_RETRY |
| 3891 | if (size > INT_MAX) { |
| 3892 | p += INT_MAX; |
| 3893 | size -= INT_MAX; |
| 3894 | goto retry; |
| 3895 | } |
| 3896 | #endif |
| 3897 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3898 | return repr; |
| 3899 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3900 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3901 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 3902 | { |
| 3903 | if (!PyUnicode_Check(unicode)) { |
| 3904 | PyErr_BadArgument(); |
| 3905 | return NULL; |
| 3906 | } |
| 3907 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 3908 | PyUnicode_GET_SIZE(unicode), |
| 3909 | NULL); |
| 3910 | } |
| 3911 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3912 | #undef NEED_RETRY |
| 3913 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3914 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3915 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3916 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 3917 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3918 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3919 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3920 | PyObject *mapping, |
| 3921 | const char *errors) |
| 3922 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3923 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3924 | Py_ssize_t startinpos; |
| 3925 | Py_ssize_t endinpos; |
| 3926 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3927 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3928 | PyUnicodeObject *v; |
| 3929 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3930 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3931 | PyObject *errorHandler = NULL; |
| 3932 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3933 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3934 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3935 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3936 | /* Default to Latin-1 */ |
| 3937 | if (mapping == NULL) |
| 3938 | return PyUnicode_DecodeLatin1(s, size, errors); |
| 3939 | |
| 3940 | v = _PyUnicode_New(size); |
| 3941 | if (v == NULL) |
| 3942 | goto onError; |
| 3943 | if (size == 0) |
| 3944 | return (PyObject *)v; |
| 3945 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3946 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3947 | if (PyUnicode_CheckExact(mapping)) { |
| 3948 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 3949 | maplen = PyUnicode_GET_SIZE(mapping); |
| 3950 | while (s < e) { |
| 3951 | unsigned char ch = *s; |
| 3952 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3953 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3954 | if (ch < maplen) |
| 3955 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3956 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3957 | if (x == 0xfffe) { |
| 3958 | /* undefined mapping */ |
| 3959 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3960 | startinpos = s-starts; |
| 3961 | endinpos = startinpos+1; |
| 3962 | if (unicode_decode_call_errorhandler( |
| 3963 | errors, &errorHandler, |
| 3964 | "charmap", "character maps to <undefined>", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3965 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3966 | (PyObject **)&v, &outpos, &p)) { |
| 3967 | goto onError; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 3968 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3969 | continue; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 3970 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3971 | *p++ = x; |
| 3972 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3973 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3974 | } |
| 3975 | else { |
| 3976 | while (s < e) { |
| 3977 | unsigned char ch = *s; |
| 3978 | PyObject *w, *x; |
| 3979 | |
| 3980 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 3981 | w = PyInt_FromLong((long)ch); |
| 3982 | if (w == NULL) |
| 3983 | goto onError; |
| 3984 | x = PyObject_GetItem(mapping, w); |
| 3985 | Py_DECREF(w); |
| 3986 | if (x == NULL) { |
| 3987 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 3988 | /* No mapping found means: mapping is undefined. */ |
| 3989 | PyErr_Clear(); |
| 3990 | x = Py_None; |
| 3991 | Py_INCREF(x); |
| 3992 | } else |
| 3993 | goto onError; |
| 3994 | } |
| 3995 | |
| 3996 | /* Apply mapping */ |
| 3997 | if (PyInt_Check(x)) { |
| 3998 | long value = PyInt_AS_LONG(x); |
| 3999 | if (value < 0 || value > 65535) { |
| 4000 | PyErr_SetString(PyExc_TypeError, |
| 4001 | "character mapping must be in range(65536)"); |
| 4002 | Py_DECREF(x); |
| 4003 | goto onError; |
| 4004 | } |
| 4005 | *p++ = (Py_UNICODE)value; |
| 4006 | } |
| 4007 | else if (x == Py_None) { |
| 4008 | /* undefined mapping */ |
| 4009 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4010 | startinpos = s-starts; |
| 4011 | endinpos = startinpos+1; |
| 4012 | if (unicode_decode_call_errorhandler( |
| 4013 | errors, &errorHandler, |
| 4014 | "charmap", "character maps to <undefined>", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4015 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4016 | (PyObject **)&v, &outpos, &p)) { |
| 4017 | Py_DECREF(x); |
| 4018 | goto onError; |
| 4019 | } |
Walter Dörwald | d4fff17 | 2005-11-28 22:15:56 +0000 | [diff] [blame] | 4020 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4021 | continue; |
| 4022 | } |
| 4023 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4024 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4025 | |
| 4026 | if (targetsize == 1) |
| 4027 | /* 1-1 mapping */ |
| 4028 | *p++ = *PyUnicode_AS_UNICODE(x); |
| 4029 | |
| 4030 | else if (targetsize > 1) { |
| 4031 | /* 1-n mapping */ |
| 4032 | if (targetsize > extrachars) { |
| 4033 | /* resize first */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4034 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 4035 | Py_ssize_t needed = (targetsize - extrachars) + \ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4036 | (targetsize << 2); |
| 4037 | extrachars += needed; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4038 | /* XXX overflow detection missing */ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4039 | if (_PyUnicode_Resize(&v, |
| 4040 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 4041 | Py_DECREF(x); |
| 4042 | goto onError; |
| 4043 | } |
| 4044 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 4045 | } |
| 4046 | Py_UNICODE_COPY(p, |
| 4047 | PyUnicode_AS_UNICODE(x), |
| 4048 | targetsize); |
| 4049 | p += targetsize; |
| 4050 | extrachars -= targetsize; |
| 4051 | } |
| 4052 | /* 1-0 mapping: skip the character */ |
| 4053 | } |
| 4054 | else { |
| 4055 | /* wrong return value */ |
| 4056 | PyErr_SetString(PyExc_TypeError, |
| 4057 | "character mapping must return integer, None or unicode"); |
| 4058 | Py_DECREF(x); |
| 4059 | goto onError; |
| 4060 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4061 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4062 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4063 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4064 | } |
| 4065 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4066 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4067 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4068 | Py_XDECREF(errorHandler); |
| 4069 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4070 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4071 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4072 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4073 | Py_XDECREF(errorHandler); |
| 4074 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4075 | Py_XDECREF(v); |
| 4076 | return NULL; |
| 4077 | } |
| 4078 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4079 | /* Charmap encoding: the lookup table */ |
| 4080 | |
| 4081 | struct encoding_map{ |
| 4082 | PyObject_HEAD |
| 4083 | unsigned char level1[32]; |
| 4084 | int count2, count3; |
| 4085 | unsigned char level23[1]; |
| 4086 | }; |
| 4087 | |
| 4088 | static PyObject* |
| 4089 | encoding_map_size(PyObject *obj, PyObject* args) |
| 4090 | { |
| 4091 | struct encoding_map *map = (struct encoding_map*)obj; |
| 4092 | return PyInt_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
| 4093 | 128*map->count3); |
| 4094 | } |
| 4095 | |
| 4096 | static PyMethodDef encoding_map_methods[] = { |
| 4097 | {"size", encoding_map_size, METH_NOARGS, |
| 4098 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 4099 | { 0 } |
| 4100 | }; |
| 4101 | |
| 4102 | static void |
| 4103 | encoding_map_dealloc(PyObject* o) |
| 4104 | { |
| 4105 | PyObject_FREE(o); |
| 4106 | } |
| 4107 | |
| 4108 | static PyTypeObject EncodingMapType = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 4109 | PyVarObject_HEAD_INIT(NULL, 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4110 | "EncodingMap", /*tp_name*/ |
| 4111 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 4112 | 0, /*tp_itemsize*/ |
| 4113 | /* methods */ |
| 4114 | encoding_map_dealloc, /*tp_dealloc*/ |
| 4115 | 0, /*tp_print*/ |
| 4116 | 0, /*tp_getattr*/ |
| 4117 | 0, /*tp_setattr*/ |
| 4118 | 0, /*tp_compare*/ |
| 4119 | 0, /*tp_repr*/ |
| 4120 | 0, /*tp_as_number*/ |
| 4121 | 0, /*tp_as_sequence*/ |
| 4122 | 0, /*tp_as_mapping*/ |
| 4123 | 0, /*tp_hash*/ |
| 4124 | 0, /*tp_call*/ |
| 4125 | 0, /*tp_str*/ |
| 4126 | 0, /*tp_getattro*/ |
| 4127 | 0, /*tp_setattro*/ |
| 4128 | 0, /*tp_as_buffer*/ |
| 4129 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 4130 | 0, /*tp_doc*/ |
| 4131 | 0, /*tp_traverse*/ |
| 4132 | 0, /*tp_clear*/ |
| 4133 | 0, /*tp_richcompare*/ |
| 4134 | 0, /*tp_weaklistoffset*/ |
| 4135 | 0, /*tp_iter*/ |
| 4136 | 0, /*tp_iternext*/ |
| 4137 | encoding_map_methods, /*tp_methods*/ |
| 4138 | 0, /*tp_members*/ |
| 4139 | 0, /*tp_getset*/ |
| 4140 | 0, /*tp_base*/ |
| 4141 | 0, /*tp_dict*/ |
| 4142 | 0, /*tp_descr_get*/ |
| 4143 | 0, /*tp_descr_set*/ |
| 4144 | 0, /*tp_dictoffset*/ |
| 4145 | 0, /*tp_init*/ |
| 4146 | 0, /*tp_alloc*/ |
| 4147 | 0, /*tp_new*/ |
| 4148 | 0, /*tp_free*/ |
| 4149 | 0, /*tp_is_gc*/ |
| 4150 | }; |
| 4151 | |
| 4152 | PyObject* |
| 4153 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 4154 | { |
| 4155 | Py_UNICODE *decode; |
| 4156 | PyObject *result; |
| 4157 | struct encoding_map *mresult; |
| 4158 | int i; |
| 4159 | int need_dict = 0; |
| 4160 | unsigned char level1[32]; |
| 4161 | unsigned char level2[512]; |
| 4162 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 4163 | int count2 = 0, count3 = 0; |
| 4164 | |
| 4165 | if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { |
| 4166 | PyErr_BadArgument(); |
| 4167 | return NULL; |
| 4168 | } |
| 4169 | decode = PyUnicode_AS_UNICODE(string); |
| 4170 | memset(level1, 0xFF, sizeof level1); |
| 4171 | memset(level2, 0xFF, sizeof level2); |
| 4172 | |
| 4173 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 4174 | or if there are non-BMP characters, we need to use |
| 4175 | a mapping dictionary. */ |
| 4176 | if (decode[0] != 0) |
| 4177 | need_dict = 1; |
| 4178 | for (i = 1; i < 256; i++) { |
| 4179 | int l1, l2; |
| 4180 | if (decode[i] == 0 |
| 4181 | #ifdef Py_UNICODE_WIDE |
| 4182 | || decode[i] > 0xFFFF |
| 4183 | #endif |
| 4184 | ) { |
| 4185 | need_dict = 1; |
| 4186 | break; |
| 4187 | } |
| 4188 | if (decode[i] == 0xFFFE) |
| 4189 | /* unmapped character */ |
| 4190 | continue; |
| 4191 | l1 = decode[i] >> 11; |
| 4192 | l2 = decode[i] >> 7; |
| 4193 | if (level1[l1] == 0xFF) |
| 4194 | level1[l1] = count2++; |
| 4195 | if (level2[l2] == 0xFF) |
| 4196 | level2[l2] = count3++; |
| 4197 | } |
| 4198 | |
| 4199 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 4200 | need_dict = 1; |
| 4201 | |
| 4202 | if (need_dict) { |
| 4203 | PyObject *result = PyDict_New(); |
| 4204 | PyObject *key, *value; |
| 4205 | if (!result) |
| 4206 | return NULL; |
| 4207 | for (i = 0; i < 256; i++) { |
| 4208 | key = value = NULL; |
| 4209 | key = PyInt_FromLong(decode[i]); |
| 4210 | value = PyInt_FromLong(i); |
| 4211 | if (!key || !value) |
| 4212 | goto failed1; |
| 4213 | if (PyDict_SetItem(result, key, value) == -1) |
| 4214 | goto failed1; |
| 4215 | Py_DECREF(key); |
| 4216 | Py_DECREF(value); |
| 4217 | } |
| 4218 | return result; |
| 4219 | failed1: |
| 4220 | Py_XDECREF(key); |
| 4221 | Py_XDECREF(value); |
| 4222 | Py_DECREF(result); |
| 4223 | return NULL; |
| 4224 | } |
| 4225 | |
| 4226 | /* Create a three-level trie */ |
| 4227 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 4228 | 16*count2 + 128*count3 - 1); |
| 4229 | if (!result) |
| 4230 | return PyErr_NoMemory(); |
| 4231 | PyObject_Init(result, &EncodingMapType); |
| 4232 | mresult = (struct encoding_map*)result; |
| 4233 | mresult->count2 = count2; |
| 4234 | mresult->count3 = count3; |
| 4235 | mlevel1 = mresult->level1; |
| 4236 | mlevel2 = mresult->level23; |
| 4237 | mlevel3 = mresult->level23 + 16*count2; |
| 4238 | memcpy(mlevel1, level1, 32); |
| 4239 | memset(mlevel2, 0xFF, 16*count2); |
| 4240 | memset(mlevel3, 0, 128*count3); |
| 4241 | count3 = 0; |
| 4242 | for (i = 1; i < 256; i++) { |
| 4243 | int o1, o2, o3, i2, i3; |
| 4244 | if (decode[i] == 0xFFFE) |
| 4245 | /* unmapped character */ |
| 4246 | continue; |
| 4247 | o1 = decode[i]>>11; |
| 4248 | o2 = (decode[i]>>7) & 0xF; |
| 4249 | i2 = 16*mlevel1[o1] + o2; |
| 4250 | if (mlevel2[i2] == 0xFF) |
| 4251 | mlevel2[i2] = count3++; |
| 4252 | o3 = decode[i] & 0x7F; |
| 4253 | i3 = 128*mlevel2[i2] + o3; |
| 4254 | mlevel3[i3] = i; |
| 4255 | } |
| 4256 | return result; |
| 4257 | } |
| 4258 | |
| 4259 | static int |
| 4260 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 4261 | { |
| 4262 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 4263 | int l1 = c>>11; |
| 4264 | int l2 = (c>>7) & 0xF; |
| 4265 | int l3 = c & 0x7F; |
| 4266 | int i; |
| 4267 | |
| 4268 | #ifdef Py_UNICODE_WIDE |
| 4269 | if (c > 0xFFFF) { |
| 4270 | return -1; |
| 4271 | } |
| 4272 | #endif |
| 4273 | if (c == 0) |
| 4274 | return 0; |
| 4275 | /* level 1*/ |
| 4276 | i = map->level1[l1]; |
| 4277 | if (i == 0xFF) { |
| 4278 | return -1; |
| 4279 | } |
| 4280 | /* level 2*/ |
| 4281 | i = map->level23[16*i+l2]; |
| 4282 | if (i == 0xFF) { |
| 4283 | return -1; |
| 4284 | } |
| 4285 | /* level 3 */ |
| 4286 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 4287 | if (i == 0) { |
| 4288 | return -1; |
| 4289 | } |
| 4290 | return i; |
| 4291 | } |
| 4292 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4293 | /* Lookup the character ch in the mapping. If the character |
| 4294 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 4295 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4296 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4297 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4298 | PyObject *w = PyInt_FromLong((long)c); |
| 4299 | PyObject *x; |
| 4300 | |
| 4301 | if (w == NULL) |
| 4302 | return NULL; |
| 4303 | x = PyObject_GetItem(mapping, w); |
| 4304 | Py_DECREF(w); |
| 4305 | if (x == NULL) { |
| 4306 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4307 | /* No mapping found means: mapping is undefined. */ |
| 4308 | PyErr_Clear(); |
| 4309 | x = Py_None; |
| 4310 | Py_INCREF(x); |
| 4311 | return x; |
| 4312 | } else |
| 4313 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4314 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 4315 | else if (x == Py_None) |
| 4316 | return x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4317 | else if (PyInt_Check(x)) { |
| 4318 | long value = PyInt_AS_LONG(x); |
| 4319 | if (value < 0 || value > 255) { |
| 4320 | PyErr_SetString(PyExc_TypeError, |
| 4321 | "character mapping must be in range(256)"); |
| 4322 | Py_DECREF(x); |
| 4323 | return NULL; |
| 4324 | } |
| 4325 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4326 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4327 | else if (PyString_Check(x)) |
| 4328 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4329 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4330 | /* wrong return value */ |
Walter Dörwald | 580ceed | 2007-05-09 10:39:19 +0000 | [diff] [blame] | 4331 | PyErr_Format(PyExc_TypeError, |
| 4332 | "character mapping must return integer, None or str8, not %.400s", |
| 4333 | x->ob_type->tp_name); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4334 | Py_DECREF(x); |
| 4335 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4336 | } |
| 4337 | } |
| 4338 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4339 | static int |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4340 | charmapencode_resize(PyObject *outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4341 | { |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4342 | Py_ssize_t outsize = PyBytes_GET_SIZE( outobj); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4343 | /* exponentially overallocate to minimize reallocations */ |
| 4344 | if (requiredsize < 2*outsize) |
| 4345 | requiredsize = 2*outsize; |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4346 | if (PyBytes_Resize(outobj, requiredsize)) { |
| 4347 | Py_DECREF(outobj); |
| 4348 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4349 | } |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4350 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4351 | } |
| 4352 | |
| 4353 | typedef enum charmapencode_result { |
| 4354 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
| 4355 | }charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4356 | /* lookup the character, put the result in the output string and adjust |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4357 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4358 | space is available. Return a new reference to the object that |
| 4359 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 4360 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 4361 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4362 | static |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4363 | charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4364 | PyObject *outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4365 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4366 | PyObject *rep; |
| 4367 | char *outstart; |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4368 | Py_ssize_t outsize = PyBytes_GET_SIZE(outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4369 | |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 4370 | if (Py_Type(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4371 | int res = encoding_map_lookup(c, mapping); |
| 4372 | Py_ssize_t requiredsize = *outpos+1; |
| 4373 | if (res == -1) |
| 4374 | return enc_FAILED; |
| 4375 | if (outsize<requiredsize) |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4376 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4377 | return enc_EXCEPTION; |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4378 | outstart = PyBytes_AS_STRING(outobj); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4379 | outstart[(*outpos)++] = (char)res; |
| 4380 | return enc_SUCCESS; |
| 4381 | } |
| 4382 | |
| 4383 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4384 | if (rep==NULL) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4385 | return enc_EXCEPTION; |
| 4386 | else if (rep==Py_None) { |
| 4387 | Py_DECREF(rep); |
| 4388 | return enc_FAILED; |
| 4389 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4390 | if (PyInt_Check(rep)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4391 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4392 | if (outsize<requiredsize) |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4393 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4394 | Py_DECREF(rep); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4395 | return enc_EXCEPTION; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4396 | } |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4397 | outstart = PyBytes_AS_STRING(outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4398 | outstart[(*outpos)++] = (char)PyInt_AS_LONG(rep); |
| 4399 | } |
| 4400 | else { |
| 4401 | const char *repchars = PyString_AS_STRING(rep); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4402 | Py_ssize_t repsize = PyString_GET_SIZE(rep); |
| 4403 | Py_ssize_t requiredsize = *outpos+repsize; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4404 | if (outsize<requiredsize) |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4405 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4406 | Py_DECREF(rep); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4407 | return enc_EXCEPTION; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4408 | } |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4409 | outstart = PyBytes_AS_STRING(outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4410 | memcpy(outstart + *outpos, repchars, repsize); |
| 4411 | *outpos += repsize; |
| 4412 | } |
| 4413 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4414 | Py_DECREF(rep); |
| 4415 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4416 | } |
| 4417 | |
| 4418 | /* handle an error in PyUnicode_EncodeCharmap |
| 4419 | Return 0 on success, -1 on error */ |
| 4420 | static |
| 4421 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4422 | 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] | 4423 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4424 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4425 | PyObject *res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4426 | { |
| 4427 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4428 | Py_ssize_t repsize; |
| 4429 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4430 | Py_UNICODE *uni2; |
| 4431 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4432 | Py_ssize_t collstartpos = *inpos; |
| 4433 | Py_ssize_t collendpos = *inpos+1; |
| 4434 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4435 | char *encoding = "charmap"; |
| 4436 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4437 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4438 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4439 | /* find all unencodable characters */ |
| 4440 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4441 | PyObject *rep; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 4442 | if (Py_Type(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4443 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 4444 | if (res != -1) |
| 4445 | break; |
| 4446 | ++collendpos; |
| 4447 | continue; |
| 4448 | } |
| 4449 | |
| 4450 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 4451 | if (rep==NULL) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4452 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4453 | else if (rep!=Py_None) { |
| 4454 | Py_DECREF(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4455 | break; |
| 4456 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4457 | Py_DECREF(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4458 | ++collendpos; |
| 4459 | } |
| 4460 | /* cache callback name lookup |
| 4461 | * (if not done yet, i.e. it's the first error) */ |
| 4462 | if (*known_errorHandler==-1) { |
| 4463 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4464 | *known_errorHandler = 1; |
| 4465 | else if (!strcmp(errors, "replace")) |
| 4466 | *known_errorHandler = 2; |
| 4467 | else if (!strcmp(errors, "ignore")) |
| 4468 | *known_errorHandler = 3; |
| 4469 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4470 | *known_errorHandler = 4; |
| 4471 | else |
| 4472 | *known_errorHandler = 0; |
| 4473 | } |
| 4474 | switch (*known_errorHandler) { |
| 4475 | case 1: /* strict */ |
| 4476 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4477 | return -1; |
| 4478 | case 2: /* replace */ |
| 4479 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
| 4480 | x = charmapencode_output('?', mapping, res, respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4481 | if (x==enc_EXCEPTION) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4482 | return -1; |
| 4483 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4484 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4485 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4486 | return -1; |
| 4487 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4488 | } |
| 4489 | /* fall through */ |
| 4490 | case 3: /* ignore */ |
| 4491 | *inpos = collendpos; |
| 4492 | break; |
| 4493 | case 4: /* xmlcharrefreplace */ |
| 4494 | /* generate replacement (temporarily (mis)uses p) */ |
| 4495 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
| 4496 | char buffer[2+29+1+1]; |
| 4497 | char *cp; |
| 4498 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 4499 | for (cp = buffer; *cp; ++cp) { |
| 4500 | x = charmapencode_output(*cp, mapping, res, respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4501 | if (x==enc_EXCEPTION) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4502 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4503 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4504 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4505 | return -1; |
| 4506 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4507 | } |
| 4508 | } |
| 4509 | *inpos = collendpos; |
| 4510 | break; |
| 4511 | default: |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4512 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4513 | encoding, reason, p, size, exceptionObject, |
| 4514 | collstartpos, collendpos, &newpos); |
| 4515 | if (repunicode == NULL) |
| 4516 | return -1; |
| 4517 | /* generate replacement */ |
| 4518 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4519 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 4520 | x = charmapencode_output(*uni2, mapping, res, respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4521 | if (x==enc_EXCEPTION) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4522 | return -1; |
| 4523 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4524 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4525 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4526 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4527 | return -1; |
| 4528 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4529 | } |
| 4530 | *inpos = newpos; |
| 4531 | Py_DECREF(repunicode); |
| 4532 | } |
| 4533 | return 0; |
| 4534 | } |
| 4535 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4536 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4537 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4538 | PyObject *mapping, |
| 4539 | const char *errors) |
| 4540 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4541 | /* output object */ |
| 4542 | PyObject *res = NULL; |
| 4543 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4544 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4545 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4546 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4547 | PyObject *errorHandler = NULL; |
| 4548 | PyObject *exc = NULL; |
| 4549 | /* the following variable is used for caching string comparisons |
| 4550 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 4551 | * 3=ignore, 4=xmlcharrefreplace */ |
| 4552 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4553 | |
| 4554 | /* Default to Latin-1 */ |
| 4555 | if (mapping == NULL) |
| 4556 | return PyUnicode_EncodeLatin1(p, size, errors); |
| 4557 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4558 | /* allocate enough for a simple encoding without |
| 4559 | replacements, if we need more, we'll resize */ |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4560 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4561 | if (res == NULL) |
| 4562 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 4563 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4564 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4565 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4566 | while (inpos<size) { |
| 4567 | /* try to encode it */ |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4568 | charmapencode_result x = charmapencode_output(p[inpos], mapping, res, &respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4569 | if (x==enc_EXCEPTION) /* error */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4570 | goto onError; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4571 | if (x==enc_FAILED) { /* unencodable character */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4572 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 4573 | &exc, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4574 | &known_errorHandler, &errorHandler, errors, |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4575 | res, &respos)) { |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 4576 | goto onError; |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 4577 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4578 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4579 | else |
| 4580 | /* done with this character => adjust input position */ |
| 4581 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4582 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4583 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4584 | /* Resize if we allocated to much */ |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4585 | if (respos<PyBytes_GET_SIZE(res)) { |
| 4586 | if (PyBytes_Resize(res, respos)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4587 | goto onError; |
| 4588 | } |
| 4589 | Py_XDECREF(exc); |
| 4590 | Py_XDECREF(errorHandler); |
| 4591 | return res; |
| 4592 | |
| 4593 | onError: |
| 4594 | Py_XDECREF(res); |
| 4595 | Py_XDECREF(exc); |
| 4596 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4597 | return NULL; |
| 4598 | } |
| 4599 | |
| 4600 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
| 4601 | PyObject *mapping) |
| 4602 | { |
| 4603 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
| 4604 | PyErr_BadArgument(); |
| 4605 | return NULL; |
| 4606 | } |
| 4607 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
| 4608 | PyUnicode_GET_SIZE(unicode), |
| 4609 | mapping, |
| 4610 | NULL); |
| 4611 | } |
| 4612 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4613 | /* create or adjust a UnicodeTranslateError */ |
| 4614 | static void make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4615 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4616 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4617 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4618 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4619 | if (*exceptionObject == NULL) { |
| 4620 | *exceptionObject = PyUnicodeTranslateError_Create( |
| 4621 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4622 | } |
| 4623 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4624 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 4625 | goto onError; |
| 4626 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 4627 | goto onError; |
| 4628 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 4629 | goto onError; |
| 4630 | return; |
| 4631 | onError: |
| 4632 | Py_DECREF(*exceptionObject); |
| 4633 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4634 | } |
| 4635 | } |
| 4636 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4637 | /* raises a UnicodeTranslateError */ |
| 4638 | static void raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4639 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4640 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4641 | const char *reason) |
| 4642 | { |
| 4643 | make_translate_exception(exceptionObject, |
| 4644 | unicode, size, startpos, endpos, reason); |
| 4645 | if (*exceptionObject != NULL) |
| 4646 | PyCodec_StrictErrors(*exceptionObject); |
| 4647 | } |
| 4648 | |
| 4649 | /* error handling callback helper: |
| 4650 | build arguments, call the callback and check the arguments, |
| 4651 | put the result into newpos and return the replacement string, which |
| 4652 | has to be freed by the caller */ |
| 4653 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
| 4654 | PyObject **errorHandler, |
| 4655 | const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4656 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 4657 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4658 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4659 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4660 | 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] | 4661 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4662 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4663 | PyObject *restuple; |
| 4664 | PyObject *resunicode; |
| 4665 | |
| 4666 | if (*errorHandler == NULL) { |
| 4667 | *errorHandler = PyCodec_LookupError(errors); |
| 4668 | if (*errorHandler == NULL) |
| 4669 | return NULL; |
| 4670 | } |
| 4671 | |
| 4672 | make_translate_exception(exceptionObject, |
| 4673 | unicode, size, startpos, endpos, reason); |
| 4674 | if (*exceptionObject == NULL) |
| 4675 | return NULL; |
| 4676 | |
| 4677 | restuple = PyObject_CallFunctionObjArgs( |
| 4678 | *errorHandler, *exceptionObject, NULL); |
| 4679 | if (restuple == NULL) |
| 4680 | return NULL; |
| 4681 | if (!PyTuple_Check(restuple)) { |
| 4682 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 4683 | Py_DECREF(restuple); |
| 4684 | return NULL; |
| 4685 | } |
| 4686 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4687 | &resunicode, &i_newpos)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4688 | Py_DECREF(restuple); |
| 4689 | return NULL; |
| 4690 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4691 | if (i_newpos<0) |
| 4692 | *newpos = size+i_newpos; |
| 4693 | else |
| 4694 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4695 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 4696 | 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] | 4697 | Py_DECREF(restuple); |
| 4698 | return NULL; |
| 4699 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4700 | Py_INCREF(resunicode); |
| 4701 | Py_DECREF(restuple); |
| 4702 | return resunicode; |
| 4703 | } |
| 4704 | |
| 4705 | /* Lookup the character ch in the mapping and put the result in result, |
| 4706 | which must be decrefed by the caller. |
| 4707 | Return 0 on success, -1 on error */ |
| 4708 | static |
| 4709 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 4710 | { |
| 4711 | PyObject *w = PyInt_FromLong((long)c); |
| 4712 | PyObject *x; |
| 4713 | |
| 4714 | if (w == NULL) |
| 4715 | return -1; |
| 4716 | x = PyObject_GetItem(mapping, w); |
| 4717 | Py_DECREF(w); |
| 4718 | if (x == NULL) { |
| 4719 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4720 | /* No mapping found means: use 1:1 mapping. */ |
| 4721 | PyErr_Clear(); |
| 4722 | *result = NULL; |
| 4723 | return 0; |
| 4724 | } else |
| 4725 | return -1; |
| 4726 | } |
| 4727 | else if (x == Py_None) { |
| 4728 | *result = x; |
| 4729 | return 0; |
| 4730 | } |
| 4731 | else if (PyInt_Check(x)) { |
| 4732 | long value = PyInt_AS_LONG(x); |
| 4733 | long max = PyUnicode_GetMax(); |
| 4734 | if (value < 0 || value > max) { |
| 4735 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 4736 | "character mapping must be in range(0x%x)", max+1); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4737 | Py_DECREF(x); |
| 4738 | return -1; |
| 4739 | } |
| 4740 | *result = x; |
| 4741 | return 0; |
| 4742 | } |
| 4743 | else if (PyUnicode_Check(x)) { |
| 4744 | *result = x; |
| 4745 | return 0; |
| 4746 | } |
| 4747 | else { |
| 4748 | /* wrong return value */ |
| 4749 | PyErr_SetString(PyExc_TypeError, |
| 4750 | "character mapping must return integer, None or unicode"); |
Walter Dörwald | 150523e | 2003-08-15 16:52:19 +0000 | [diff] [blame] | 4751 | Py_DECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4752 | return -1; |
| 4753 | } |
| 4754 | } |
| 4755 | /* ensure that *outobj is at least requiredsize characters long, |
| 4756 | if not reallocate and adjust various state variables. |
| 4757 | Return 0 on success, -1 on error */ |
| 4758 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4759 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4760 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4761 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4762 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4763 | if (requiredsize > oldsize) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4764 | /* remember old output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4765 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4766 | /* exponentially overallocate to minimize reallocations */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4767 | if (requiredsize < 2 * oldsize) |
| 4768 | requiredsize = 2 * oldsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 4769 | if (_PyUnicode_Resize(outobj, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4770 | return -1; |
| 4771 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4772 | } |
| 4773 | return 0; |
| 4774 | } |
| 4775 | /* lookup the character, put the result in the output string and adjust |
| 4776 | various state variables. Return a new reference to the object that |
| 4777 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 4778 | undefined (in which case no character was written). |
| 4779 | The called must decref result. |
| 4780 | Return 0 on success, -1 on error. */ |
| 4781 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4782 | 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] | 4783 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4784 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4785 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4786 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4787 | return -1; |
| 4788 | if (*res==NULL) { |
| 4789 | /* not found => default to 1:1 mapping */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4790 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4791 | } |
| 4792 | else if (*res==Py_None) |
| 4793 | ; |
| 4794 | else if (PyInt_Check(*res)) { |
| 4795 | /* no overflow check, because we know that the space is enough */ |
| 4796 | *(*outp)++ = (Py_UNICODE)PyInt_AS_LONG(*res); |
| 4797 | } |
| 4798 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4799 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4800 | if (repsize==1) { |
| 4801 | /* no overflow check, because we know that the space is enough */ |
| 4802 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 4803 | } |
| 4804 | else if (repsize!=0) { |
| 4805 | /* more than one character */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4806 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
Walter Dörwald | cd736e7 | 2004-02-05 17:36:00 +0000 | [diff] [blame] | 4807 | (insize - (curinp-startinp)) + |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4808 | repsize - 1; |
| 4809 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4810 | return -1; |
| 4811 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 4812 | *outp += repsize; |
| 4813 | } |
| 4814 | } |
| 4815 | else |
| 4816 | return -1; |
| 4817 | return 0; |
| 4818 | } |
| 4819 | |
| 4820 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4821 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4822 | PyObject *mapping, |
| 4823 | const char *errors) |
| 4824 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4825 | /* output object */ |
| 4826 | PyObject *res = NULL; |
| 4827 | /* pointers to the beginning and end+1 of input */ |
| 4828 | const Py_UNICODE *startp = p; |
| 4829 | const Py_UNICODE *endp = p + size; |
| 4830 | /* pointer into the output */ |
| 4831 | Py_UNICODE *str; |
| 4832 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4833 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4834 | char *reason = "character maps to <undefined>"; |
| 4835 | PyObject *errorHandler = NULL; |
| 4836 | PyObject *exc = NULL; |
| 4837 | /* the following variable is used for caching string comparisons |
| 4838 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 4839 | * 3=ignore, 4=xmlcharrefreplace */ |
| 4840 | int known_errorHandler = -1; |
| 4841 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4842 | if (mapping == NULL) { |
| 4843 | PyErr_BadArgument(); |
| 4844 | return NULL; |
| 4845 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4846 | |
| 4847 | /* allocate enough for a simple 1:1 translation without |
| 4848 | replacements, if we need more, we'll resize */ |
| 4849 | res = PyUnicode_FromUnicode(NULL, size); |
| 4850 | if (res == NULL) |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4851 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4852 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4853 | return res; |
| 4854 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4855 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4856 | while (p<endp) { |
| 4857 | /* try to encode it */ |
| 4858 | PyObject *x = NULL; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4859 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4860 | Py_XDECREF(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4861 | goto onError; |
| 4862 | } |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 4863 | Py_XDECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4864 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 4865 | ++p; |
| 4866 | else { /* untranslatable character */ |
| 4867 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4868 | Py_ssize_t repsize; |
| 4869 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4870 | Py_UNICODE *uni2; |
| 4871 | /* startpos for collecting untranslatable chars */ |
| 4872 | const Py_UNICODE *collstart = p; |
| 4873 | const Py_UNICODE *collend = p+1; |
| 4874 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4875 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4876 | /* find all untranslatable characters */ |
| 4877 | while (collend < endp) { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4878 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4879 | goto onError; |
| 4880 | Py_XDECREF(x); |
| 4881 | if (x!=Py_None) |
| 4882 | break; |
| 4883 | ++collend; |
| 4884 | } |
| 4885 | /* cache callback name lookup |
| 4886 | * (if not done yet, i.e. it's the first error) */ |
| 4887 | if (known_errorHandler==-1) { |
| 4888 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4889 | known_errorHandler = 1; |
| 4890 | else if (!strcmp(errors, "replace")) |
| 4891 | known_errorHandler = 2; |
| 4892 | else if (!strcmp(errors, "ignore")) |
| 4893 | known_errorHandler = 3; |
| 4894 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4895 | known_errorHandler = 4; |
| 4896 | else |
| 4897 | known_errorHandler = 0; |
| 4898 | } |
| 4899 | switch (known_errorHandler) { |
| 4900 | case 1: /* strict */ |
| 4901 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
| 4902 | goto onError; |
| 4903 | case 2: /* replace */ |
| 4904 | /* No need to check for space, this is a 1:1 replacement */ |
| 4905 | for (coll = collstart; coll<collend; ++coll) |
| 4906 | *str++ = '?'; |
| 4907 | /* fall through */ |
| 4908 | case 3: /* ignore */ |
| 4909 | p = collend; |
| 4910 | break; |
| 4911 | case 4: /* xmlcharrefreplace */ |
| 4912 | /* generate replacement (temporarily (mis)uses p) */ |
| 4913 | for (p = collstart; p < collend; ++p) { |
| 4914 | char buffer[2+29+1+1]; |
| 4915 | char *cp; |
| 4916 | sprintf(buffer, "&#%d;", (int)*p); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4917 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4918 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 4919 | goto onError; |
| 4920 | for (cp = buffer; *cp; ++cp) |
| 4921 | *str++ = *cp; |
| 4922 | } |
| 4923 | p = collend; |
| 4924 | break; |
| 4925 | default: |
| 4926 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 4927 | reason, startp, size, &exc, |
| 4928 | collstart-startp, collend-startp, &newpos); |
| 4929 | if (repunicode == NULL) |
| 4930 | goto onError; |
| 4931 | /* generate replacement */ |
| 4932 | repsize = PyUnicode_GET_SIZE(repunicode); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4933 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4934 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 4935 | Py_DECREF(repunicode); |
| 4936 | goto onError; |
| 4937 | } |
| 4938 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 4939 | *str++ = *uni2; |
| 4940 | p = startp + newpos; |
| 4941 | Py_DECREF(repunicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4942 | } |
| 4943 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4944 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4945 | /* Resize if we allocated to much */ |
| 4946 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4947 | if (respos<PyUnicode_GET_SIZE(res)) { |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 4948 | if (_PyUnicode_Resize(&res, respos) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 4949 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4950 | } |
| 4951 | Py_XDECREF(exc); |
| 4952 | Py_XDECREF(errorHandler); |
| 4953 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4954 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4955 | onError: |
| 4956 | Py_XDECREF(res); |
| 4957 | Py_XDECREF(exc); |
| 4958 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4959 | return NULL; |
| 4960 | } |
| 4961 | |
| 4962 | PyObject *PyUnicode_Translate(PyObject *str, |
| 4963 | PyObject *mapping, |
| 4964 | const char *errors) |
| 4965 | { |
| 4966 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4967 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4968 | str = PyUnicode_FromObject(str); |
| 4969 | if (str == NULL) |
| 4970 | goto onError; |
| 4971 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
| 4972 | PyUnicode_GET_SIZE(str), |
| 4973 | mapping, |
| 4974 | errors); |
| 4975 | Py_DECREF(str); |
| 4976 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4977 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4978 | onError: |
| 4979 | Py_XDECREF(str); |
| 4980 | return NULL; |
| 4981 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4982 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4983 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 4984 | |
| 4985 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4986 | Py_ssize_t length, |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4987 | char *output, |
| 4988 | const char *errors) |
| 4989 | { |
| 4990 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4991 | PyObject *errorHandler = NULL; |
| 4992 | PyObject *exc = NULL; |
| 4993 | const char *encoding = "decimal"; |
| 4994 | const char *reason = "invalid decimal Unicode string"; |
| 4995 | /* the following variable is used for caching string comparisons |
| 4996 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 4997 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4998 | |
| 4999 | if (output == NULL) { |
| 5000 | PyErr_BadArgument(); |
| 5001 | return -1; |
| 5002 | } |
| 5003 | |
| 5004 | p = s; |
| 5005 | end = s + length; |
| 5006 | while (p < end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5007 | register Py_UNICODE ch = *p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5008 | int decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5009 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5010 | Py_ssize_t repsize; |
| 5011 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5012 | Py_UNICODE *uni2; |
| 5013 | Py_UNICODE *collstart; |
| 5014 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5015 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5016 | if (Py_UNICODE_ISSPACE(ch)) { |
| 5017 | *output++ = ' '; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5018 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5019 | continue; |
| 5020 | } |
| 5021 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5022 | if (decimal >= 0) { |
| 5023 | *output++ = '0' + decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5024 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5025 | continue; |
| 5026 | } |
Guido van Rossum | ba47704 | 2000-04-06 18:18:10 +0000 | [diff] [blame] | 5027 | if (0 < ch && ch < 256) { |
Guido van Rossum | 42c29aa | 2000-05-03 23:58:29 +0000 | [diff] [blame] | 5028 | *output++ = (char)ch; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5029 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5030 | continue; |
| 5031 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5032 | /* All other characters are considered unencodable */ |
| 5033 | collstart = p; |
| 5034 | collend = p+1; |
| 5035 | while (collend < end) { |
| 5036 | if ((0 < *collend && *collend < 256) || |
| 5037 | !Py_UNICODE_ISSPACE(*collend) || |
| 5038 | Py_UNICODE_TODECIMAL(*collend)) |
| 5039 | break; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5040 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5041 | /* cache callback name lookup |
| 5042 | * (if not done yet, i.e. it's the first error) */ |
| 5043 | if (known_errorHandler==-1) { |
| 5044 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5045 | known_errorHandler = 1; |
| 5046 | else if (!strcmp(errors, "replace")) |
| 5047 | known_errorHandler = 2; |
| 5048 | else if (!strcmp(errors, "ignore")) |
| 5049 | known_errorHandler = 3; |
| 5050 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5051 | known_errorHandler = 4; |
| 5052 | else |
| 5053 | known_errorHandler = 0; |
| 5054 | } |
| 5055 | switch (known_errorHandler) { |
| 5056 | case 1: /* strict */ |
| 5057 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 5058 | goto onError; |
| 5059 | case 2: /* replace */ |
| 5060 | for (p = collstart; p < collend; ++p) |
| 5061 | *output++ = '?'; |
| 5062 | /* fall through */ |
| 5063 | case 3: /* ignore */ |
| 5064 | p = collend; |
| 5065 | break; |
| 5066 | case 4: /* xmlcharrefreplace */ |
| 5067 | /* generate replacement (temporarily (mis)uses p) */ |
| 5068 | for (p = collstart; p < collend; ++p) |
| 5069 | output += sprintf(output, "&#%d;", (int)*p); |
| 5070 | p = collend; |
| 5071 | break; |
| 5072 | default: |
| 5073 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 5074 | encoding, reason, s, length, &exc, |
| 5075 | collstart-s, collend-s, &newpos); |
| 5076 | if (repunicode == NULL) |
| 5077 | goto onError; |
| 5078 | /* generate replacement */ |
| 5079 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5080 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 5081 | Py_UNICODE ch = *uni2; |
| 5082 | if (Py_UNICODE_ISSPACE(ch)) |
| 5083 | *output++ = ' '; |
| 5084 | else { |
| 5085 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5086 | if (decimal >= 0) |
| 5087 | *output++ = '0' + decimal; |
| 5088 | else if (0 < ch && ch < 256) |
| 5089 | *output++ = (char)ch; |
| 5090 | else { |
| 5091 | Py_DECREF(repunicode); |
| 5092 | raise_encode_exception(&exc, encoding, |
| 5093 | s, length, collstart-s, collend-s, reason); |
| 5094 | goto onError; |
| 5095 | } |
| 5096 | } |
| 5097 | } |
| 5098 | p = s + newpos; |
| 5099 | Py_DECREF(repunicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5100 | } |
| 5101 | } |
| 5102 | /* 0-terminate the output string */ |
| 5103 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5104 | Py_XDECREF(exc); |
| 5105 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5106 | return 0; |
| 5107 | |
| 5108 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5109 | Py_XDECREF(exc); |
| 5110 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5111 | return -1; |
| 5112 | } |
| 5113 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5114 | /* --- Helpers ------------------------------------------------------------ */ |
| 5115 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 5116 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5117 | |
| 5118 | #include "stringlib/fastsearch.h" |
| 5119 | |
| 5120 | #include "stringlib/count.h" |
| 5121 | #include "stringlib/find.h" |
| 5122 | #include "stringlib/partition.h" |
| 5123 | |
| 5124 | /* helper macro to fixup start/end slice values */ |
| 5125 | #define FIX_START_END(obj) \ |
| 5126 | if (start < 0) \ |
| 5127 | start += (obj)->length; \ |
| 5128 | if (start < 0) \ |
| 5129 | start = 0; \ |
| 5130 | if (end > (obj)->length) \ |
| 5131 | end = (obj)->length; \ |
| 5132 | if (end < 0) \ |
| 5133 | end += (obj)->length; \ |
| 5134 | if (end < 0) \ |
| 5135 | end = 0; |
| 5136 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5137 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5138 | PyObject *substr, |
| 5139 | Py_ssize_t start, |
| 5140 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5141 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5142 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5143 | PyUnicodeObject* str_obj; |
| 5144 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5145 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5146 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 5147 | if (!str_obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5148 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5149 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 5150 | if (!sub_obj) { |
| 5151 | Py_DECREF(str_obj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5152 | return -1; |
| 5153 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5154 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5155 | FIX_START_END(str_obj); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5156 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5157 | result = stringlib_count( |
| 5158 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length |
| 5159 | ); |
| 5160 | |
| 5161 | Py_DECREF(sub_obj); |
| 5162 | Py_DECREF(str_obj); |
| 5163 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5164 | return result; |
| 5165 | } |
| 5166 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5167 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5168 | PyObject *sub, |
| 5169 | Py_ssize_t start, |
| 5170 | Py_ssize_t end, |
| 5171 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5172 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5173 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5174 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5175 | str = PyUnicode_FromObject(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5176 | if (!str) |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 5177 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5178 | sub = PyUnicode_FromObject(sub); |
| 5179 | if (!sub) { |
Marc-André Lemburg | 4164439 | 2002-05-29 13:46:29 +0000 | [diff] [blame] | 5180 | Py_DECREF(str); |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 5181 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5182 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5183 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5184 | if (direction > 0) |
| 5185 | result = stringlib_find_slice( |
| 5186 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5187 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5188 | start, end |
| 5189 | ); |
| 5190 | else |
| 5191 | result = stringlib_rfind_slice( |
| 5192 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5193 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5194 | start, end |
| 5195 | ); |
| 5196 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5197 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5198 | Py_DECREF(sub); |
| 5199 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5200 | return result; |
| 5201 | } |
| 5202 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5203 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5204 | int tailmatch(PyUnicodeObject *self, |
| 5205 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5206 | Py_ssize_t start, |
| 5207 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5208 | int direction) |
| 5209 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5210 | if (substring->length == 0) |
| 5211 | return 1; |
| 5212 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5213 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5214 | |
| 5215 | end -= substring->length; |
| 5216 | if (end < start) |
| 5217 | return 0; |
| 5218 | |
| 5219 | if (direction > 0) { |
| 5220 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 5221 | return 1; |
| 5222 | } else { |
| 5223 | if (Py_UNICODE_MATCH(self, start, substring)) |
| 5224 | return 1; |
| 5225 | } |
| 5226 | |
| 5227 | return 0; |
| 5228 | } |
| 5229 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5230 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5231 | PyObject *substr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5232 | Py_ssize_t start, |
| 5233 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5234 | int direction) |
| 5235 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5236 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5237 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5238 | str = PyUnicode_FromObject(str); |
| 5239 | if (str == NULL) |
| 5240 | return -1; |
| 5241 | substr = PyUnicode_FromObject(substr); |
| 5242 | if (substr == NULL) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5243 | Py_DECREF(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5244 | return -1; |
| 5245 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5246 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5247 | result = tailmatch((PyUnicodeObject *)str, |
| 5248 | (PyUnicodeObject *)substr, |
| 5249 | start, end, direction); |
| 5250 | Py_DECREF(str); |
| 5251 | Py_DECREF(substr); |
| 5252 | return result; |
| 5253 | } |
| 5254 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5255 | /* Apply fixfct filter to the Unicode object self and return a |
| 5256 | reference to the modified object */ |
| 5257 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5258 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5259 | PyObject *fixup(PyUnicodeObject *self, |
| 5260 | int (*fixfct)(PyUnicodeObject *s)) |
| 5261 | { |
| 5262 | |
| 5263 | PyUnicodeObject *u; |
| 5264 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5265 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5266 | if (u == NULL) |
| 5267 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5268 | |
| 5269 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5270 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5271 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5272 | /* fixfct should return TRUE if it modified the buffer. If |
| 5273 | FALSE, return a reference to the original buffer instead |
| 5274 | (to save space, not time) */ |
| 5275 | Py_INCREF(self); |
| 5276 | Py_DECREF(u); |
| 5277 | return (PyObject*) self; |
| 5278 | } |
| 5279 | return (PyObject*) u; |
| 5280 | } |
| 5281 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5282 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5283 | int fixupper(PyUnicodeObject *self) |
| 5284 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5285 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5286 | Py_UNICODE *s = self->str; |
| 5287 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5288 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5289 | while (len-- > 0) { |
| 5290 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5291 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5292 | ch = Py_UNICODE_TOUPPER(*s); |
| 5293 | if (ch != *s) { |
| 5294 | status = 1; |
| 5295 | *s = ch; |
| 5296 | } |
| 5297 | s++; |
| 5298 | } |
| 5299 | |
| 5300 | return status; |
| 5301 | } |
| 5302 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5303 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5304 | int fixlower(PyUnicodeObject *self) |
| 5305 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5306 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5307 | Py_UNICODE *s = self->str; |
| 5308 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5309 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5310 | while (len-- > 0) { |
| 5311 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5312 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5313 | ch = Py_UNICODE_TOLOWER(*s); |
| 5314 | if (ch != *s) { |
| 5315 | status = 1; |
| 5316 | *s = ch; |
| 5317 | } |
| 5318 | s++; |
| 5319 | } |
| 5320 | |
| 5321 | return status; |
| 5322 | } |
| 5323 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5324 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5325 | int fixswapcase(PyUnicodeObject *self) |
| 5326 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5327 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5328 | Py_UNICODE *s = self->str; |
| 5329 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5330 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5331 | while (len-- > 0) { |
| 5332 | if (Py_UNICODE_ISUPPER(*s)) { |
| 5333 | *s = Py_UNICODE_TOLOWER(*s); |
| 5334 | status = 1; |
| 5335 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 5336 | *s = Py_UNICODE_TOUPPER(*s); |
| 5337 | status = 1; |
| 5338 | } |
| 5339 | s++; |
| 5340 | } |
| 5341 | |
| 5342 | return status; |
| 5343 | } |
| 5344 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5345 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5346 | int fixcapitalize(PyUnicodeObject *self) |
| 5347 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5348 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5349 | Py_UNICODE *s = self->str; |
| 5350 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5351 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5352 | if (len == 0) |
| 5353 | return 0; |
| 5354 | if (Py_UNICODE_ISLOWER(*s)) { |
| 5355 | *s = Py_UNICODE_TOUPPER(*s); |
| 5356 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5357 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5358 | s++; |
| 5359 | while (--len > 0) { |
| 5360 | if (Py_UNICODE_ISUPPER(*s)) { |
| 5361 | *s = Py_UNICODE_TOLOWER(*s); |
| 5362 | status = 1; |
| 5363 | } |
| 5364 | s++; |
| 5365 | } |
| 5366 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5367 | } |
| 5368 | |
| 5369 | static |
| 5370 | int fixtitle(PyUnicodeObject *self) |
| 5371 | { |
| 5372 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5373 | register Py_UNICODE *e; |
| 5374 | int previous_is_cased; |
| 5375 | |
| 5376 | /* Shortcut for single character strings */ |
| 5377 | if (PyUnicode_GET_SIZE(self) == 1) { |
| 5378 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 5379 | if (*p != ch) { |
| 5380 | *p = ch; |
| 5381 | return 1; |
| 5382 | } |
| 5383 | else |
| 5384 | return 0; |
| 5385 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5386 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5387 | e = p + PyUnicode_GET_SIZE(self); |
| 5388 | previous_is_cased = 0; |
| 5389 | for (; p < e; p++) { |
| 5390 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5391 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5392 | if (previous_is_cased) |
| 5393 | *p = Py_UNICODE_TOLOWER(ch); |
| 5394 | else |
| 5395 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5396 | |
| 5397 | if (Py_UNICODE_ISLOWER(ch) || |
| 5398 | Py_UNICODE_ISUPPER(ch) || |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5399 | Py_UNICODE_ISTITLE(ch)) |
| 5400 | previous_is_cased = 1; |
| 5401 | else |
| 5402 | previous_is_cased = 0; |
| 5403 | } |
| 5404 | return 1; |
| 5405 | } |
| 5406 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5407 | PyObject * |
| 5408 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5409 | { |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5410 | PyObject *internal_separator = NULL; |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 5411 | const Py_UNICODE blank = ' '; |
| 5412 | const Py_UNICODE *sep = ␣ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5413 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5414 | PyUnicodeObject *res = NULL; /* the result */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5415 | Py_ssize_t res_alloc = 100; /* # allocated bytes for string in res */ |
| 5416 | Py_ssize_t res_used; /* # used bytes */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5417 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 5418 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5419 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5420 | PyObject *item; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5421 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5422 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5423 | fseq = PySequence_Fast(seq, ""); |
| 5424 | if (fseq == NULL) { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5425 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5426 | } |
| 5427 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5428 | /* Grrrr. A codec may be invoked to convert str objects to |
| 5429 | * Unicode, and so it's possible to call back into Python code |
| 5430 | * during PyUnicode_FromObject(), and so it's possible for a sick |
| 5431 | * codec to change the size of fseq (if seq is a list). Therefore |
| 5432 | * we have to keep refetching the size -- can't assume seqlen |
| 5433 | * is invariant. |
| 5434 | */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5435 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 5436 | /* If empty sequence, return u"". */ |
| 5437 | if (seqlen == 0) { |
| 5438 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 5439 | goto Done; |
| 5440 | } |
| 5441 | /* If singleton sequence with an exact Unicode, return that. */ |
| 5442 | if (seqlen == 1) { |
| 5443 | item = PySequence_Fast_GET_ITEM(fseq, 0); |
| 5444 | if (PyUnicode_CheckExact(item)) { |
| 5445 | Py_INCREF(item); |
| 5446 | res = (PyUnicodeObject *)item; |
| 5447 | goto Done; |
| 5448 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5449 | } |
| 5450 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5451 | /* At least two items to join, or one that isn't exact Unicode. */ |
| 5452 | if (seqlen > 1) { |
| 5453 | /* Set up sep and seplen -- they're needed. */ |
| 5454 | if (separator == NULL) { |
| 5455 | sep = ␣ |
| 5456 | seplen = 1; |
| 5457 | } |
| 5458 | else { |
| 5459 | internal_separator = PyUnicode_FromObject(separator); |
| 5460 | if (internal_separator == NULL) |
| 5461 | goto onError; |
| 5462 | sep = PyUnicode_AS_UNICODE(internal_separator); |
| 5463 | seplen = PyUnicode_GET_SIZE(internal_separator); |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5464 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 5465 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5466 | } |
| 5467 | } |
| 5468 | |
| 5469 | /* Get space. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5470 | res = _PyUnicode_New(res_alloc); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5471 | if (res == NULL) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5472 | goto onError; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5473 | res_p = PyUnicode_AS_UNICODE(res); |
| 5474 | res_used = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5475 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5476 | for (i = 0; i < seqlen; ++i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5477 | Py_ssize_t itemlen; |
| 5478 | Py_ssize_t new_res_used; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5479 | |
| 5480 | item = PySequence_Fast_GET_ITEM(fseq, i); |
| 5481 | /* Convert item to Unicode. */ |
Guido van Rossum | f104429 | 2007-09-27 18:01:22 +0000 | [diff] [blame] | 5482 | if (!PyString_Check(item) && !PyUnicode_Check(item)) |
| 5483 | { |
| 5484 | if (PyBytes_Check(item)) |
| 5485 | { |
| 5486 | PyErr_Format(PyExc_TypeError, |
| 5487 | "sequence item %d: join() will not operate on " |
| 5488 | "bytes objects", i); |
| 5489 | goto onError; |
| 5490 | } |
| 5491 | item = PyObject_Unicode(item); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5492 | } |
Guido van Rossum | f104429 | 2007-09-27 18:01:22 +0000 | [diff] [blame] | 5493 | else |
| 5494 | item = PyUnicode_FromObject(item); |
| 5495 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5496 | if (item == NULL) |
| 5497 | goto onError; |
| 5498 | /* We own a reference to item from here on. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5499 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5500 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 5501 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 5502 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5503 | /* 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] | 5504 | itemlen = PyUnicode_GET_SIZE(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5505 | new_res_used = res_used + itemlen; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5506 | if (new_res_used < 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5507 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5508 | if (i < seqlen - 1) { |
| 5509 | new_res_used += seplen; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5510 | if (new_res_used < 0) |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5511 | goto Overflow; |
| 5512 | } |
| 5513 | if (new_res_used > res_alloc) { |
| 5514 | /* double allocated size until it's big enough */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5515 | do { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5516 | res_alloc += res_alloc; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5517 | if (res_alloc <= 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5518 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5519 | } while (new_res_used > res_alloc); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5520 | if (_PyUnicode_Resize(&res, res_alloc) < 0) { |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 5521 | Py_DECREF(item); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5522 | goto onError; |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 5523 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5524 | res_p = PyUnicode_AS_UNICODE(res) + res_used; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5525 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5526 | |
| 5527 | /* Copy item, and maybe the separator. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5528 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5529 | res_p += itemlen; |
| 5530 | if (i < seqlen - 1) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5531 | Py_UNICODE_COPY(res_p, sep, seplen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5532 | res_p += seplen; |
| 5533 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5534 | Py_DECREF(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5535 | res_used = new_res_used; |
| 5536 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5537 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5538 | /* Shrink res to match the used area; this probably can't fail, |
| 5539 | * but it's cheap to check. |
| 5540 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5541 | if (_PyUnicode_Resize(&res, res_used) < 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5542 | goto onError; |
| 5543 | |
| 5544 | Done: |
| 5545 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5546 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5547 | return (PyObject *)res; |
| 5548 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5549 | Overflow: |
| 5550 | PyErr_SetString(PyExc_OverflowError, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5551 | "join() result is too long for a Python string"); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5552 | Py_DECREF(item); |
| 5553 | /* fall through */ |
| 5554 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5555 | onError: |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5556 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5557 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5558 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5559 | return NULL; |
| 5560 | } |
| 5561 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5562 | static |
| 5563 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5564 | Py_ssize_t left, |
| 5565 | Py_ssize_t right, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5566 | Py_UNICODE fill) |
| 5567 | { |
| 5568 | PyUnicodeObject *u; |
| 5569 | |
| 5570 | if (left < 0) |
| 5571 | left = 0; |
| 5572 | if (right < 0) |
| 5573 | right = 0; |
| 5574 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5575 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5576 | Py_INCREF(self); |
| 5577 | return self; |
| 5578 | } |
| 5579 | |
| 5580 | u = _PyUnicode_New(left + self->length + right); |
| 5581 | if (u) { |
| 5582 | if (left) |
| 5583 | Py_UNICODE_FILL(u->str, fill, left); |
| 5584 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 5585 | if (right) |
| 5586 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 5587 | } |
| 5588 | |
| 5589 | return u; |
| 5590 | } |
| 5591 | |
| 5592 | #define SPLIT_APPEND(data, left, right) \ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5593 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5594 | if (!str) \ |
| 5595 | goto onError; \ |
| 5596 | if (PyList_Append(list, str)) { \ |
| 5597 | Py_DECREF(str); \ |
| 5598 | goto onError; \ |
| 5599 | } \ |
| 5600 | else \ |
| 5601 | Py_DECREF(str); |
| 5602 | |
| 5603 | static |
| 5604 | PyObject *split_whitespace(PyUnicodeObject *self, |
| 5605 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5606 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5607 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5608 | register Py_ssize_t i; |
| 5609 | register Py_ssize_t j; |
| 5610 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5611 | PyObject *str; |
| 5612 | |
| 5613 | for (i = j = 0; i < len; ) { |
| 5614 | /* find a token */ |
| 5615 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 5616 | i++; |
| 5617 | j = i; |
| 5618 | while (i < len && !Py_UNICODE_ISSPACE(self->str[i])) |
| 5619 | i++; |
| 5620 | if (j < i) { |
| 5621 | if (maxcount-- <= 0) |
| 5622 | break; |
| 5623 | SPLIT_APPEND(self->str, j, i); |
| 5624 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 5625 | i++; |
| 5626 | j = i; |
| 5627 | } |
| 5628 | } |
| 5629 | if (j < len) { |
| 5630 | SPLIT_APPEND(self->str, j, len); |
| 5631 | } |
| 5632 | return list; |
| 5633 | |
| 5634 | onError: |
| 5635 | Py_DECREF(list); |
| 5636 | return NULL; |
| 5637 | } |
| 5638 | |
| 5639 | PyObject *PyUnicode_Splitlines(PyObject *string, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5640 | int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5641 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5642 | register Py_ssize_t i; |
| 5643 | register Py_ssize_t j; |
| 5644 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5645 | PyObject *list; |
| 5646 | PyObject *str; |
| 5647 | Py_UNICODE *data; |
| 5648 | |
| 5649 | string = PyUnicode_FromObject(string); |
| 5650 | if (string == NULL) |
| 5651 | return NULL; |
| 5652 | data = PyUnicode_AS_UNICODE(string); |
| 5653 | len = PyUnicode_GET_SIZE(string); |
| 5654 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5655 | list = PyList_New(0); |
| 5656 | if (!list) |
| 5657 | goto onError; |
| 5658 | |
| 5659 | for (i = j = 0; i < len; ) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5660 | Py_ssize_t eol; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5661 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5662 | /* Find a line and append it */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5663 | while (i < len && !BLOOM_LINEBREAK(data[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5664 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5665 | |
| 5666 | /* Skip the line break reading CRLF as one line break */ |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5667 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5668 | if (i < len) { |
| 5669 | if (data[i] == '\r' && i + 1 < len && |
| 5670 | data[i+1] == '\n') |
| 5671 | i += 2; |
| 5672 | else |
| 5673 | i++; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5674 | if (keepends) |
| 5675 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5676 | } |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5677 | SPLIT_APPEND(data, j, eol); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5678 | j = i; |
| 5679 | } |
| 5680 | if (j < len) { |
| 5681 | SPLIT_APPEND(data, j, len); |
| 5682 | } |
| 5683 | |
| 5684 | Py_DECREF(string); |
| 5685 | return list; |
| 5686 | |
| 5687 | onError: |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5688 | Py_XDECREF(list); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5689 | Py_DECREF(string); |
| 5690 | return NULL; |
| 5691 | } |
| 5692 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5693 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5694 | PyObject *split_char(PyUnicodeObject *self, |
| 5695 | PyObject *list, |
| 5696 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5697 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5698 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5699 | register Py_ssize_t i; |
| 5700 | register Py_ssize_t j; |
| 5701 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5702 | PyObject *str; |
| 5703 | |
| 5704 | for (i = j = 0; i < len; ) { |
| 5705 | if (self->str[i] == ch) { |
| 5706 | if (maxcount-- <= 0) |
| 5707 | break; |
| 5708 | SPLIT_APPEND(self->str, j, i); |
| 5709 | i = j = i + 1; |
| 5710 | } else |
| 5711 | i++; |
| 5712 | } |
| 5713 | if (j <= len) { |
| 5714 | SPLIT_APPEND(self->str, j, len); |
| 5715 | } |
| 5716 | return list; |
| 5717 | |
| 5718 | onError: |
| 5719 | Py_DECREF(list); |
| 5720 | return NULL; |
| 5721 | } |
| 5722 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5723 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5724 | PyObject *split_substring(PyUnicodeObject *self, |
| 5725 | PyObject *list, |
| 5726 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5727 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5728 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5729 | register Py_ssize_t i; |
| 5730 | register Py_ssize_t j; |
| 5731 | Py_ssize_t len = self->length; |
| 5732 | Py_ssize_t sublen = substring->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5733 | PyObject *str; |
| 5734 | |
Guido van Rossum | cda4f9a | 2000-12-19 02:23:19 +0000 | [diff] [blame] | 5735 | for (i = j = 0; i <= len - sublen; ) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5736 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 5737 | if (maxcount-- <= 0) |
| 5738 | break; |
| 5739 | SPLIT_APPEND(self->str, j, i); |
| 5740 | i = j = i + sublen; |
| 5741 | } else |
| 5742 | i++; |
| 5743 | } |
| 5744 | if (j <= len) { |
| 5745 | SPLIT_APPEND(self->str, j, len); |
| 5746 | } |
| 5747 | return list; |
| 5748 | |
| 5749 | onError: |
| 5750 | Py_DECREF(list); |
| 5751 | return NULL; |
| 5752 | } |
| 5753 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5754 | static |
| 5755 | PyObject *rsplit_whitespace(PyUnicodeObject *self, |
| 5756 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5757 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5758 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5759 | register Py_ssize_t i; |
| 5760 | register Py_ssize_t j; |
| 5761 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5762 | PyObject *str; |
| 5763 | |
| 5764 | for (i = j = len - 1; i >= 0; ) { |
| 5765 | /* find a token */ |
| 5766 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 5767 | i--; |
| 5768 | j = i; |
| 5769 | while (i >= 0 && !Py_UNICODE_ISSPACE(self->str[i])) |
| 5770 | i--; |
| 5771 | if (j > i) { |
| 5772 | if (maxcount-- <= 0) |
| 5773 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5774 | SPLIT_APPEND(self->str, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5775 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 5776 | i--; |
| 5777 | j = i; |
| 5778 | } |
| 5779 | } |
| 5780 | if (j >= 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5781 | SPLIT_APPEND(self->str, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5782 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5783 | if (PyList_Reverse(list) < 0) |
| 5784 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5785 | return list; |
| 5786 | |
| 5787 | onError: |
| 5788 | Py_DECREF(list); |
| 5789 | return NULL; |
| 5790 | } |
| 5791 | |
| 5792 | static |
| 5793 | PyObject *rsplit_char(PyUnicodeObject *self, |
| 5794 | PyObject *list, |
| 5795 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5796 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5797 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5798 | register Py_ssize_t i; |
| 5799 | register Py_ssize_t j; |
| 5800 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5801 | PyObject *str; |
| 5802 | |
| 5803 | for (i = j = len - 1; i >= 0; ) { |
| 5804 | if (self->str[i] == ch) { |
| 5805 | if (maxcount-- <= 0) |
| 5806 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5807 | SPLIT_APPEND(self->str, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5808 | j = i = i - 1; |
| 5809 | } else |
| 5810 | i--; |
| 5811 | } |
Hye-Shik Chang | 7fc4cf5 | 2003-12-23 09:10:16 +0000 | [diff] [blame] | 5812 | if (j >= -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5813 | SPLIT_APPEND(self->str, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5814 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5815 | if (PyList_Reverse(list) < 0) |
| 5816 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5817 | return list; |
| 5818 | |
| 5819 | onError: |
| 5820 | Py_DECREF(list); |
| 5821 | return NULL; |
| 5822 | } |
| 5823 | |
| 5824 | static |
| 5825 | PyObject *rsplit_substring(PyUnicodeObject *self, |
| 5826 | PyObject *list, |
| 5827 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5828 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5829 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5830 | register Py_ssize_t i; |
| 5831 | register Py_ssize_t j; |
| 5832 | Py_ssize_t len = self->length; |
| 5833 | Py_ssize_t sublen = substring->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5834 | PyObject *str; |
| 5835 | |
| 5836 | for (i = len - sublen, j = len; i >= 0; ) { |
| 5837 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 5838 | if (maxcount-- <= 0) |
| 5839 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5840 | SPLIT_APPEND(self->str, i + sublen, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5841 | j = i; |
| 5842 | i -= sublen; |
| 5843 | } else |
| 5844 | i--; |
| 5845 | } |
| 5846 | if (j >= 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5847 | SPLIT_APPEND(self->str, 0, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5848 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5849 | if (PyList_Reverse(list) < 0) |
| 5850 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5851 | return list; |
| 5852 | |
| 5853 | onError: |
| 5854 | Py_DECREF(list); |
| 5855 | return NULL; |
| 5856 | } |
| 5857 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5858 | #undef SPLIT_APPEND |
| 5859 | |
| 5860 | static |
| 5861 | PyObject *split(PyUnicodeObject *self, |
| 5862 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5863 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5864 | { |
| 5865 | PyObject *list; |
| 5866 | |
| 5867 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5868 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5869 | |
| 5870 | list = PyList_New(0); |
| 5871 | if (!list) |
| 5872 | return NULL; |
| 5873 | |
| 5874 | if (substring == NULL) |
| 5875 | return split_whitespace(self,list,maxcount); |
| 5876 | |
| 5877 | else if (substring->length == 1) |
| 5878 | return split_char(self,list,substring->str[0],maxcount); |
| 5879 | |
| 5880 | else if (substring->length == 0) { |
| 5881 | Py_DECREF(list); |
| 5882 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 5883 | return NULL; |
| 5884 | } |
| 5885 | else |
| 5886 | return split_substring(self,list,substring,maxcount); |
| 5887 | } |
| 5888 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5889 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5890 | PyObject *rsplit(PyUnicodeObject *self, |
| 5891 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5892 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5893 | { |
| 5894 | PyObject *list; |
| 5895 | |
| 5896 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5897 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5898 | |
| 5899 | list = PyList_New(0); |
| 5900 | if (!list) |
| 5901 | return NULL; |
| 5902 | |
| 5903 | if (substring == NULL) |
| 5904 | return rsplit_whitespace(self,list,maxcount); |
| 5905 | |
| 5906 | else if (substring->length == 1) |
| 5907 | return rsplit_char(self,list,substring->str[0],maxcount); |
| 5908 | |
| 5909 | else if (substring->length == 0) { |
| 5910 | Py_DECREF(list); |
| 5911 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 5912 | return NULL; |
| 5913 | } |
| 5914 | else |
| 5915 | return rsplit_substring(self,list,substring,maxcount); |
| 5916 | } |
| 5917 | |
| 5918 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5919 | PyObject *replace(PyUnicodeObject *self, |
| 5920 | PyUnicodeObject *str1, |
| 5921 | PyUnicodeObject *str2, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5922 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5923 | { |
| 5924 | PyUnicodeObject *u; |
| 5925 | |
| 5926 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5927 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5928 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5929 | if (str1->length == str2->length) { |
| 5930 | /* same length */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5931 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5932 | if (str1->length == 1) { |
| 5933 | /* replace characters */ |
| 5934 | Py_UNICODE u1, u2; |
| 5935 | if (!findchar(self->str, self->length, str1->str[0])) |
| 5936 | goto nothing; |
| 5937 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 5938 | if (!u) |
| 5939 | return NULL; |
| 5940 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5941 | u1 = str1->str[0]; |
| 5942 | u2 = str2->str[0]; |
| 5943 | for (i = 0; i < u->length; i++) |
| 5944 | if (u->str[i] == u1) { |
| 5945 | if (--maxcount < 0) |
| 5946 | break; |
| 5947 | u->str[i] = u2; |
| 5948 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5949 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5950 | i = fastsearch( |
| 5951 | self->str, self->length, str1->str, str1->length, FAST_SEARCH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5952 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5953 | if (i < 0) |
| 5954 | goto nothing; |
| 5955 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 5956 | if (!u) |
| 5957 | return NULL; |
| 5958 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5959 | while (i <= self->length - str1->length) |
| 5960 | if (Py_UNICODE_MATCH(self, i, str1)) { |
| 5961 | if (--maxcount < 0) |
| 5962 | break; |
| 5963 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 5964 | i += str1->length; |
| 5965 | } else |
| 5966 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5967 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5968 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5969 | |
| 5970 | Py_ssize_t n, i, j, e; |
| 5971 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5972 | Py_UNICODE *p; |
| 5973 | |
| 5974 | /* replace strings */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5975 | n = stringlib_count(self->str, self->length, str1->str, str1->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5976 | if (n > maxcount) |
| 5977 | n = maxcount; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5978 | if (n == 0) |
| 5979 | goto nothing; |
| 5980 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 5981 | delta = (str2->length - str1->length); |
| 5982 | if (delta == 0) { |
| 5983 | new_size = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5984 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5985 | product = n * (str2->length - str1->length); |
| 5986 | if ((product / (str2->length - str1->length)) != n) { |
| 5987 | PyErr_SetString(PyExc_OverflowError, |
| 5988 | "replace string is too long"); |
| 5989 | return NULL; |
| 5990 | } |
| 5991 | new_size = self->length + product; |
| 5992 | if (new_size < 0) { |
| 5993 | PyErr_SetString(PyExc_OverflowError, |
| 5994 | "replace string is too long"); |
| 5995 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5996 | } |
| 5997 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5998 | u = _PyUnicode_New(new_size); |
| 5999 | if (!u) |
| 6000 | return NULL; |
| 6001 | i = 0; |
| 6002 | p = u->str; |
| 6003 | e = self->length - str1->length; |
| 6004 | if (str1->length > 0) { |
| 6005 | while (n-- > 0) { |
| 6006 | /* look for next match */ |
| 6007 | j = i; |
| 6008 | while (j <= e) { |
| 6009 | if (Py_UNICODE_MATCH(self, j, str1)) |
| 6010 | break; |
| 6011 | j++; |
| 6012 | } |
| 6013 | if (j > i) { |
| 6014 | if (j > e) |
| 6015 | break; |
| 6016 | /* copy unchanged part [i:j] */ |
| 6017 | Py_UNICODE_COPY(p, self->str+i, j-i); |
| 6018 | p += j - i; |
| 6019 | } |
| 6020 | /* copy substitution string */ |
| 6021 | if (str2->length > 0) { |
| 6022 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6023 | p += str2->length; |
| 6024 | } |
| 6025 | i = j + str1->length; |
| 6026 | } |
| 6027 | if (i < self->length) |
| 6028 | /* copy tail [i:] */ |
| 6029 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6030 | } else { |
| 6031 | /* interleave */ |
| 6032 | while (n > 0) { |
| 6033 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6034 | p += str2->length; |
| 6035 | if (--n <= 0) |
| 6036 | break; |
| 6037 | *p++ = self->str[i++]; |
| 6038 | } |
| 6039 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6040 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6041 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6042 | return (PyObject *) u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6043 | |
| 6044 | nothing: |
| 6045 | /* nothing to replace; return original string (when possible) */ |
| 6046 | if (PyUnicode_CheckExact(self)) { |
| 6047 | Py_INCREF(self); |
| 6048 | return (PyObject *) self; |
| 6049 | } |
| 6050 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6051 | } |
| 6052 | |
| 6053 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 6054 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6055 | PyDoc_STRVAR(title__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6056 | "S.title() -> unicode\n\ |
| 6057 | \n\ |
| 6058 | 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] | 6059 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6060 | |
| 6061 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6062 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6063 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6064 | return fixup(self, fixtitle); |
| 6065 | } |
| 6066 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6067 | PyDoc_STRVAR(capitalize__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6068 | "S.capitalize() -> unicode\n\ |
| 6069 | \n\ |
| 6070 | 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] | 6071 | have upper case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6072 | |
| 6073 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6074 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6075 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6076 | return fixup(self, fixcapitalize); |
| 6077 | } |
| 6078 | |
| 6079 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6080 | PyDoc_STRVAR(capwords__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6081 | "S.capwords() -> unicode\n\ |
| 6082 | \n\ |
| 6083 | 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] | 6084 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6085 | |
| 6086 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6087 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6088 | { |
| 6089 | PyObject *list; |
| 6090 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6091 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6092 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6093 | /* Split into words */ |
| 6094 | list = split(self, NULL, -1); |
| 6095 | if (!list) |
| 6096 | return NULL; |
| 6097 | |
| 6098 | /* Capitalize each word */ |
| 6099 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 6100 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
| 6101 | fixcapitalize); |
| 6102 | if (item == NULL) |
| 6103 | goto onError; |
| 6104 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 6105 | PyList_SET_ITEM(list, i, item); |
| 6106 | } |
| 6107 | |
| 6108 | /* Join the words to form a new string */ |
| 6109 | item = PyUnicode_Join(NULL, list); |
| 6110 | |
| 6111 | onError: |
| 6112 | Py_DECREF(list); |
| 6113 | return (PyObject *)item; |
| 6114 | } |
| 6115 | #endif |
| 6116 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6117 | /* Argument converter. Coerces to a single unicode character */ |
| 6118 | |
| 6119 | static int |
| 6120 | convert_uc(PyObject *obj, void *addr) |
| 6121 | { |
| 6122 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 6123 | PyObject *uniobj; |
| 6124 | Py_UNICODE *unistr; |
| 6125 | |
| 6126 | uniobj = PyUnicode_FromObject(obj); |
| 6127 | if (uniobj == NULL) { |
| 6128 | PyErr_SetString(PyExc_TypeError, |
| 6129 | "The fill character cannot be converted to Unicode"); |
| 6130 | return 0; |
| 6131 | } |
| 6132 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 6133 | PyErr_SetString(PyExc_TypeError, |
| 6134 | "The fill character must be exactly one character long"); |
| 6135 | Py_DECREF(uniobj); |
| 6136 | return 0; |
| 6137 | } |
| 6138 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 6139 | *fillcharloc = unistr[0]; |
| 6140 | Py_DECREF(uniobj); |
| 6141 | return 1; |
| 6142 | } |
| 6143 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6144 | PyDoc_STRVAR(center__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6145 | "S.center(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6146 | \n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6147 | Return S centered in a Unicode string of length width. Padding is\n\ |
| 6148 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6149 | |
| 6150 | static PyObject * |
| 6151 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 6152 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6153 | Py_ssize_t marg, left; |
| 6154 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6155 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6156 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 6157 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6158 | return NULL; |
| 6159 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6160 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6161 | Py_INCREF(self); |
| 6162 | return (PyObject*) self; |
| 6163 | } |
| 6164 | |
| 6165 | marg = width - self->length; |
| 6166 | left = marg / 2 + (marg & width & 1); |
| 6167 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6168 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6169 | } |
| 6170 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6171 | #if 0 |
| 6172 | |
| 6173 | /* This code should go into some future Unicode collation support |
| 6174 | module. The basic comparison should compare ordinals on a naive |
Trent Mick | 20abf57 | 2000-08-12 22:14:34 +0000 | [diff] [blame] | 6175 | basis (this is what Java does and thus JPython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6176 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6177 | /* speedy UTF-16 code point order comparison */ |
| 6178 | /* gleaned from: */ |
| 6179 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 6180 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6181 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6182 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6183 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6184 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 6185 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6186 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6187 | }; |
| 6188 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6189 | static int |
| 6190 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6191 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6192 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6193 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6194 | Py_UNICODE *s1 = str1->str; |
| 6195 | Py_UNICODE *s2 = str2->str; |
| 6196 | |
| 6197 | len1 = str1->length; |
| 6198 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6199 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6200 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6201 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6202 | |
| 6203 | c1 = *s1++; |
| 6204 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6205 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6206 | if (c1 > (1<<11) * 26) |
| 6207 | c1 += utf16Fixup[c1>>11]; |
| 6208 | if (c2 > (1<<11) * 26) |
| 6209 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6210 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6211 | |
| 6212 | if (c1 != c2) |
| 6213 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6214 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6215 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6216 | } |
| 6217 | |
| 6218 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6219 | } |
| 6220 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6221 | #else |
| 6222 | |
| 6223 | static int |
| 6224 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6225 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6226 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6227 | |
| 6228 | Py_UNICODE *s1 = str1->str; |
| 6229 | Py_UNICODE *s2 = str2->str; |
| 6230 | |
| 6231 | len1 = str1->length; |
| 6232 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6233 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6234 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6235 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6236 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6237 | c1 = *s1++; |
| 6238 | c2 = *s2++; |
| 6239 | |
| 6240 | if (c1 != c2) |
| 6241 | return (c1 < c2) ? -1 : 1; |
| 6242 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6243 | len1--; len2--; |
| 6244 | } |
| 6245 | |
| 6246 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6247 | } |
| 6248 | |
| 6249 | #endif |
| 6250 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6251 | int PyUnicode_Compare(PyObject *left, |
| 6252 | PyObject *right) |
| 6253 | { |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6254 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) |
| 6255 | return unicode_compare((PyUnicodeObject *)left, |
| 6256 | (PyUnicodeObject *)right); |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6257 | PyErr_Format(PyExc_TypeError, |
| 6258 | "Can't compare %.100s and %.100s", |
| 6259 | left->ob_type->tp_name, |
| 6260 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6261 | return -1; |
| 6262 | } |
| 6263 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6264 | int |
| 6265 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 6266 | { |
| 6267 | int i; |
| 6268 | Py_UNICODE *id; |
| 6269 | assert(PyUnicode_Check(uni)); |
| 6270 | id = PyUnicode_AS_UNICODE(uni); |
| 6271 | /* Compare Unicode string and source character set string */ |
| 6272 | for (i = 0; id[i] && str[i]; i++) |
| 6273 | if (id[i] != str[i]) |
| 6274 | return ((int)id[i] < (int)str[i]) ? -1 : 1; |
| 6275 | if (id[i]) |
| 6276 | return 1; /* uni is longer */ |
| 6277 | if (str[i]) |
| 6278 | return -1; /* str is longer */ |
| 6279 | return 0; |
| 6280 | } |
| 6281 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6282 | PyObject *PyUnicode_RichCompare(PyObject *left, |
| 6283 | PyObject *right, |
| 6284 | int op) |
| 6285 | { |
| 6286 | int result; |
| 6287 | |
| 6288 | result = PyUnicode_Compare(left, right); |
| 6289 | if (result == -1 && PyErr_Occurred()) |
| 6290 | goto onError; |
| 6291 | |
| 6292 | /* Convert the return value to a Boolean */ |
| 6293 | switch (op) { |
| 6294 | case Py_EQ: |
| 6295 | result = (result == 0); |
| 6296 | break; |
| 6297 | case Py_NE: |
| 6298 | result = (result != 0); |
| 6299 | break; |
| 6300 | case Py_LE: |
| 6301 | result = (result <= 0); |
| 6302 | break; |
| 6303 | case Py_GE: |
| 6304 | result = (result >= 0); |
| 6305 | break; |
| 6306 | case Py_LT: |
| 6307 | result = (result == -1); |
| 6308 | break; |
| 6309 | case Py_GT: |
| 6310 | result = (result == 1); |
| 6311 | break; |
| 6312 | } |
| 6313 | return PyBool_FromLong(result); |
| 6314 | |
| 6315 | onError: |
| 6316 | |
| 6317 | /* Standard case |
| 6318 | |
| 6319 | Type errors mean that PyUnicode_FromObject() could not convert |
| 6320 | one of the arguments (usually the right hand side) to Unicode, |
| 6321 | ie. we can't handle the comparison request. However, it is |
| 6322 | possible that the other object knows a comparison method, which |
| 6323 | is why we return Py_NotImplemented to give the other object a |
| 6324 | chance. |
| 6325 | |
| 6326 | */ |
| 6327 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 6328 | PyErr_Clear(); |
| 6329 | Py_INCREF(Py_NotImplemented); |
| 6330 | return Py_NotImplemented; |
| 6331 | } |
| 6332 | if (op != Py_EQ && op != Py_NE) |
| 6333 | return NULL; |
| 6334 | |
| 6335 | /* Equality comparison. |
| 6336 | |
| 6337 | This is a special case: we silence any PyExc_UnicodeDecodeError |
| 6338 | and instead turn it into a PyErr_UnicodeWarning. |
| 6339 | |
| 6340 | */ |
| 6341 | if (!PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) |
| 6342 | return NULL; |
| 6343 | PyErr_Clear(); |
Skip Montanaro | 46fc337 | 2007-08-12 11:44:53 +0000 | [diff] [blame] | 6344 | if (PyErr_WarnEx(PyExc_UnicodeWarning, |
| 6345 | (op == Py_EQ) ? |
| 6346 | "Unicode equal comparison " |
| 6347 | "failed to convert both arguments to Unicode - " |
| 6348 | "interpreting them as being unequal" |
| 6349 | : |
| 6350 | "Unicode unequal comparison " |
| 6351 | "failed to convert both arguments to Unicode - " |
| 6352 | "interpreting them as being unequal", |
| 6353 | 1) < 0) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6354 | return NULL; |
| 6355 | result = (op == Py_NE); |
| 6356 | return PyBool_FromLong(result); |
| 6357 | } |
| 6358 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6359 | int PyUnicode_Contains(PyObject *container, |
| 6360 | PyObject *element) |
| 6361 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6362 | PyObject *str, *sub; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6363 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6364 | |
| 6365 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6366 | sub = PyUnicode_FromObject(element); |
| 6367 | if (!sub) { |
Walter Dörwald | 26e0f51 | 2007-06-12 16:51:31 +0000 | [diff] [blame] | 6368 | PyErr_Format(PyExc_TypeError, |
| 6369 | "'in <string>' requires string as left operand, not %s", |
| 6370 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6371 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6372 | } |
| 6373 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6374 | str = PyUnicode_FromObject(container); |
| 6375 | if (!str) { |
| 6376 | Py_DECREF(sub); |
| 6377 | return -1; |
| 6378 | } |
| 6379 | |
| 6380 | result = stringlib_contains_obj(str, sub); |
| 6381 | |
| 6382 | Py_DECREF(str); |
| 6383 | Py_DECREF(sub); |
| 6384 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6385 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6386 | } |
| 6387 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6388 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 6389 | |
| 6390 | PyObject *PyUnicode_Concat(PyObject *left, |
| 6391 | PyObject *right) |
| 6392 | { |
| 6393 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 6394 | |
Guido van Rossum | 84d79dd | 2007-04-13 02:23:57 +0000 | [diff] [blame] | 6395 | if (PyBytes_Check(left) || PyBytes_Check(right)) |
| 6396 | return PyBytes_Concat(left, right); |
| 6397 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6398 | /* Coerce the two arguments */ |
| 6399 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 6400 | if (u == NULL) |
| 6401 | goto onError; |
| 6402 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 6403 | if (v == NULL) |
| 6404 | goto onError; |
| 6405 | |
| 6406 | /* Shortcuts */ |
| 6407 | if (v == unicode_empty) { |
| 6408 | Py_DECREF(v); |
| 6409 | return (PyObject *)u; |
| 6410 | } |
| 6411 | if (u == unicode_empty) { |
| 6412 | Py_DECREF(u); |
| 6413 | return (PyObject *)v; |
| 6414 | } |
| 6415 | |
| 6416 | /* Concat the two Unicode strings */ |
| 6417 | w = _PyUnicode_New(u->length + v->length); |
| 6418 | if (w == NULL) |
| 6419 | goto onError; |
| 6420 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 6421 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 6422 | |
| 6423 | Py_DECREF(u); |
| 6424 | Py_DECREF(v); |
| 6425 | return (PyObject *)w; |
| 6426 | |
| 6427 | onError: |
| 6428 | Py_XDECREF(u); |
| 6429 | Py_XDECREF(v); |
| 6430 | return NULL; |
| 6431 | } |
| 6432 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 6433 | void |
| 6434 | PyUnicode_Append(PyObject **pleft, PyObject *right) |
| 6435 | { |
| 6436 | PyObject *new; |
| 6437 | if (*pleft == NULL) |
| 6438 | return; |
| 6439 | if (right == NULL || !PyUnicode_Check(*pleft)) { |
| 6440 | Py_DECREF(*pleft); |
| 6441 | *pleft = NULL; |
| 6442 | return; |
| 6443 | } |
| 6444 | new = PyUnicode_Concat(*pleft, right); |
| 6445 | Py_DECREF(*pleft); |
| 6446 | *pleft = new; |
| 6447 | } |
| 6448 | |
| 6449 | void |
| 6450 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 6451 | { |
| 6452 | PyUnicode_Append(pleft, right); |
| 6453 | Py_XDECREF(right); |
| 6454 | } |
| 6455 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6456 | PyDoc_STRVAR(count__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6457 | "S.count(sub[, start[, end]]) -> int\n\ |
| 6458 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6459 | Return the number of non-overlapping occurrences of substring sub in\n\ |
| 6460 | 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] | 6461 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6462 | |
| 6463 | static PyObject * |
| 6464 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 6465 | { |
| 6466 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6467 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6468 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6469 | PyObject *result; |
| 6470 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6471 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
| 6472 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6473 | return NULL; |
| 6474 | |
| 6475 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6476 | (PyObject *)substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6477 | if (substring == NULL) |
| 6478 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6479 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6480 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6481 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6482 | result = PyInt_FromSsize_t( |
| 6483 | stringlib_count(self->str + start, end - start, |
| 6484 | substring->str, substring->length) |
| 6485 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6486 | |
| 6487 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6488 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6489 | return result; |
| 6490 | } |
| 6491 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6492 | PyDoc_STRVAR(encode__doc__, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6493 | "S.encode([encoding[,errors]]) -> string or unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6494 | \n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6495 | Encodes S using the codec registered for encoding. encoding defaults\n\ |
| 6496 | 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] | 6497 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6498 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 6499 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 6500 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6501 | |
| 6502 | static PyObject * |
| 6503 | unicode_encode(PyUnicodeObject *self, PyObject *args) |
| 6504 | { |
| 6505 | char *encoding = NULL; |
| 6506 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6507 | PyObject *v; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 6508 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6509 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 6510 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6511 | v = PyUnicode_AsEncodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6512 | if (v == NULL) |
| 6513 | goto onError; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 6514 | if (!PyBytes_Check(v)) { |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6515 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 6516 | "encoder did not return a bytes object " |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6517 | "(type=%.400s)", |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 6518 | Py_Type(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6519 | Py_DECREF(v); |
| 6520 | return NULL; |
| 6521 | } |
| 6522 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6523 | |
| 6524 | onError: |
| 6525 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6526 | } |
| 6527 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6528 | PyDoc_STRVAR(expandtabs__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6529 | "S.expandtabs([tabsize]) -> unicode\n\ |
| 6530 | \n\ |
| 6531 | 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] | 6532 | 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] | 6533 | |
| 6534 | static PyObject* |
| 6535 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 6536 | { |
| 6537 | Py_UNICODE *e; |
| 6538 | Py_UNICODE *p; |
| 6539 | Py_UNICODE *q; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6540 | Py_ssize_t i, j, old_j; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6541 | PyUnicodeObject *u; |
| 6542 | int tabsize = 8; |
| 6543 | |
| 6544 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
| 6545 | return NULL; |
| 6546 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6547 | /* First pass: determine size of output string */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6548 | i = j = old_j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6549 | e = self->str + self->length; |
| 6550 | for (p = self->str; p < e; p++) |
| 6551 | if (*p == '\t') { |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6552 | if (tabsize > 0) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6553 | j += tabsize - (j % tabsize); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6554 | if (old_j > j) { |
| 6555 | PyErr_SetString(PyExc_OverflowError, |
| 6556 | "new string is too long"); |
| 6557 | return NULL; |
| 6558 | } |
| 6559 | old_j = j; |
| 6560 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6561 | } |
| 6562 | else { |
| 6563 | j++; |
| 6564 | if (*p == '\n' || *p == '\r') { |
| 6565 | i += j; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6566 | old_j = j = 0; |
| 6567 | if (i < 0) { |
| 6568 | PyErr_SetString(PyExc_OverflowError, |
| 6569 | "new string is too long"); |
| 6570 | return NULL; |
| 6571 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6572 | } |
| 6573 | } |
| 6574 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6575 | if ((i + j) < 0) { |
| 6576 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 6577 | return NULL; |
| 6578 | } |
| 6579 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6580 | /* Second pass: create output string and fill it */ |
| 6581 | u = _PyUnicode_New(i + j); |
| 6582 | if (!u) |
| 6583 | return NULL; |
| 6584 | |
| 6585 | j = 0; |
| 6586 | q = u->str; |
| 6587 | |
| 6588 | for (p = self->str; p < e; p++) |
| 6589 | if (*p == '\t') { |
| 6590 | if (tabsize > 0) { |
| 6591 | i = tabsize - (j % tabsize); |
| 6592 | j += i; |
| 6593 | while (i--) |
| 6594 | *q++ = ' '; |
| 6595 | } |
| 6596 | } |
| 6597 | else { |
| 6598 | j++; |
| 6599 | *q++ = *p; |
| 6600 | if (*p == '\n' || *p == '\r') |
| 6601 | j = 0; |
| 6602 | } |
| 6603 | |
| 6604 | return (PyObject*) u; |
| 6605 | } |
| 6606 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6607 | PyDoc_STRVAR(find__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6608 | "S.find(sub [,start [,end]]) -> int\n\ |
| 6609 | \n\ |
| 6610 | Return the lowest index in S where substring sub is found,\n\ |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 6611 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6612 | arguments start and end are interpreted as in slice notation.\n\ |
| 6613 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6614 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6615 | |
| 6616 | static PyObject * |
| 6617 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 6618 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6619 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6620 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6621 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6622 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6623 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6624 | if (!PyArg_ParseTuple(args, "O|O&O&:find", &substring, |
| 6625 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6626 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6627 | substring = PyUnicode_FromObject(substring); |
| 6628 | if (!substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6629 | return NULL; |
| 6630 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6631 | result = stringlib_find_slice( |
| 6632 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6633 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6634 | start, end |
| 6635 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6636 | |
| 6637 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6638 | |
| 6639 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6640 | } |
| 6641 | |
| 6642 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6643 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6644 | { |
| 6645 | if (index < 0 || index >= self->length) { |
| 6646 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 6647 | return NULL; |
| 6648 | } |
| 6649 | |
| 6650 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 6651 | } |
| 6652 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 6653 | /* Believe it or not, this produces the same value for ASCII strings |
| 6654 | as string_hash(). */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6655 | static long |
Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 6656 | unicode_hash(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6657 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 6658 | Py_ssize_t len; |
| 6659 | Py_UNICODE *p; |
| 6660 | long x; |
| 6661 | |
| 6662 | if (self->hash != -1) |
| 6663 | return self->hash; |
| 6664 | len = Py_Size(self); |
| 6665 | p = self->str; |
| 6666 | x = *p << 7; |
| 6667 | while (--len >= 0) |
| 6668 | x = (1000003*x) ^ *p++; |
| 6669 | x ^= Py_Size(self); |
| 6670 | if (x == -1) |
| 6671 | x = -2; |
| 6672 | self->hash = x; |
| 6673 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6674 | } |
| 6675 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6676 | PyDoc_STRVAR(index__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6677 | "S.index(sub [,start [,end]]) -> int\n\ |
| 6678 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6679 | 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] | 6680 | |
| 6681 | static PyObject * |
| 6682 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 6683 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6684 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6685 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6686 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6687 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6688 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6689 | if (!PyArg_ParseTuple(args, "O|O&O&:index", &substring, |
| 6690 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6691 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6692 | substring = PyUnicode_FromObject(substring); |
| 6693 | if (!substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6694 | return NULL; |
| 6695 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6696 | result = stringlib_find_slice( |
| 6697 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6698 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6699 | start, end |
| 6700 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6701 | |
| 6702 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6703 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6704 | if (result < 0) { |
| 6705 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 6706 | return NULL; |
| 6707 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6708 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6709 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6710 | } |
| 6711 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6712 | PyDoc_STRVAR(islower__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6713 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6714 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6715 | 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] | 6716 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6717 | |
| 6718 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6719 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6720 | { |
| 6721 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6722 | register const Py_UNICODE *e; |
| 6723 | int cased; |
| 6724 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6725 | /* Shortcut for single character strings */ |
| 6726 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6727 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6728 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6729 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6730 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6731 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6732 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6733 | e = p + PyUnicode_GET_SIZE(self); |
| 6734 | cased = 0; |
| 6735 | for (; p < e; p++) { |
| 6736 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6737 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6738 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6739 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6740 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 6741 | cased = 1; |
| 6742 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6743 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6744 | } |
| 6745 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6746 | PyDoc_STRVAR(isupper__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6747 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6748 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6749 | 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] | 6750 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6751 | |
| 6752 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6753 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6754 | { |
| 6755 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6756 | register const Py_UNICODE *e; |
| 6757 | int cased; |
| 6758 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6759 | /* Shortcut for single character strings */ |
| 6760 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6761 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6762 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6763 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6764 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6765 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6766 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6767 | e = p + PyUnicode_GET_SIZE(self); |
| 6768 | cased = 0; |
| 6769 | for (; p < e; p++) { |
| 6770 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6771 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6772 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6773 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6774 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 6775 | cased = 1; |
| 6776 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6777 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6778 | } |
| 6779 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6780 | PyDoc_STRVAR(istitle__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6781 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6782 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6783 | Return True if S is a titlecased string and there is at least one\n\ |
| 6784 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 6785 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 6786 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6787 | |
| 6788 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6789 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6790 | { |
| 6791 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6792 | register const Py_UNICODE *e; |
| 6793 | int cased, previous_is_cased; |
| 6794 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6795 | /* Shortcut for single character strings */ |
| 6796 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6797 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 6798 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6799 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6800 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6801 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6802 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6803 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6804 | e = p + PyUnicode_GET_SIZE(self); |
| 6805 | cased = 0; |
| 6806 | previous_is_cased = 0; |
| 6807 | for (; p < e; p++) { |
| 6808 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6809 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6810 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 6811 | if (previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6812 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6813 | previous_is_cased = 1; |
| 6814 | cased = 1; |
| 6815 | } |
| 6816 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 6817 | if (!previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6818 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6819 | previous_is_cased = 1; |
| 6820 | cased = 1; |
| 6821 | } |
| 6822 | else |
| 6823 | previous_is_cased = 0; |
| 6824 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6825 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6826 | } |
| 6827 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6828 | PyDoc_STRVAR(isspace__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6829 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6830 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6831 | Return True if all characters in S are whitespace\n\ |
| 6832 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6833 | |
| 6834 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6835 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6836 | { |
| 6837 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6838 | register const Py_UNICODE *e; |
| 6839 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6840 | /* Shortcut for single character strings */ |
| 6841 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6842 | Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6843 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6844 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6845 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6846 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6847 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6848 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6849 | e = p + PyUnicode_GET_SIZE(self); |
| 6850 | for (; p < e; p++) { |
| 6851 | if (!Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6852 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6853 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6854 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6855 | } |
| 6856 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6857 | PyDoc_STRVAR(isalpha__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6858 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6859 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6860 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6861 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6862 | |
| 6863 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6864 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6865 | { |
| 6866 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6867 | register const Py_UNICODE *e; |
| 6868 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6869 | /* Shortcut for single character strings */ |
| 6870 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6871 | Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6872 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6873 | |
| 6874 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6875 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6876 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6877 | |
| 6878 | e = p + PyUnicode_GET_SIZE(self); |
| 6879 | for (; p < e; p++) { |
| 6880 | if (!Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6881 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6882 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6883 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6884 | } |
| 6885 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6886 | PyDoc_STRVAR(isalnum__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6887 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6888 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6889 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6890 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6891 | |
| 6892 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6893 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6894 | { |
| 6895 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6896 | register const Py_UNICODE *e; |
| 6897 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6898 | /* Shortcut for single character strings */ |
| 6899 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6900 | Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6901 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6902 | |
| 6903 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6904 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6905 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6906 | |
| 6907 | e = p + PyUnicode_GET_SIZE(self); |
| 6908 | for (; p < e; p++) { |
| 6909 | if (!Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6910 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6911 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6912 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6913 | } |
| 6914 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6915 | PyDoc_STRVAR(isdecimal__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6916 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6917 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6918 | 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] | 6919 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6920 | |
| 6921 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6922 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6923 | { |
| 6924 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6925 | register const Py_UNICODE *e; |
| 6926 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6927 | /* Shortcut for single character strings */ |
| 6928 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6929 | Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6930 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6931 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6932 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6933 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6934 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6935 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6936 | e = p + PyUnicode_GET_SIZE(self); |
| 6937 | for (; p < e; p++) { |
| 6938 | if (!Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6939 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6940 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6941 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6942 | } |
| 6943 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6944 | PyDoc_STRVAR(isdigit__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6945 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6946 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6947 | Return True if all characters in S are digits\n\ |
| 6948 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6949 | |
| 6950 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6951 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6952 | { |
| 6953 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6954 | register const Py_UNICODE *e; |
| 6955 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6956 | /* Shortcut for single character strings */ |
| 6957 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6958 | Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6959 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6960 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6961 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6962 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6963 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6964 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6965 | e = p + PyUnicode_GET_SIZE(self); |
| 6966 | for (; p < e; p++) { |
| 6967 | if (!Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6968 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6969 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6970 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6971 | } |
| 6972 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6973 | PyDoc_STRVAR(isnumeric__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6974 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6975 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6976 | 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] | 6977 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6978 | |
| 6979 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6980 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6981 | { |
| 6982 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6983 | register const Py_UNICODE *e; |
| 6984 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6985 | /* Shortcut for single character strings */ |
| 6986 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6987 | Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6988 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6989 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6990 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6991 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6992 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6993 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6994 | e = p + PyUnicode_GET_SIZE(self); |
| 6995 | for (; p < e; p++) { |
| 6996 | if (!Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6997 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6998 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6999 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7000 | } |
| 7001 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7002 | int |
| 7003 | PyUnicode_IsIdentifier(PyObject *self) |
| 7004 | { |
| 7005 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE((PyUnicodeObject*)self); |
| 7006 | register const Py_UNICODE *e; |
| 7007 | |
| 7008 | /* Special case for empty strings */ |
| 7009 | if (PyUnicode_GET_SIZE(self) == 0) |
| 7010 | return 0; |
| 7011 | |
| 7012 | /* PEP 3131 says that the first character must be in |
| 7013 | XID_Start and subsequent characters in XID_Continue, |
| 7014 | and for the ASCII range, the 2.x rules apply (i.e |
| 7015 | start with letters and underscore, continue with |
| 7016 | letters, digits, underscore). However, given the current |
| 7017 | definition of XID_Start and XID_Continue, it is sufficient |
| 7018 | to check just for these, except that _ must be allowed |
| 7019 | as starting an identifier. */ |
| 7020 | if (!_PyUnicode_IsXidStart(*p) && *p != 0x5F /* LOW LINE */) |
| 7021 | return 0; |
| 7022 | |
| 7023 | e = p + PyUnicode_GET_SIZE(self); |
| 7024 | for (p++; p < e; p++) { |
| 7025 | if (!_PyUnicode_IsXidContinue(*p)) |
| 7026 | return 0; |
| 7027 | } |
| 7028 | return 1; |
| 7029 | } |
| 7030 | |
| 7031 | PyDoc_STRVAR(isidentifier__doc__, |
| 7032 | "S.isidentifier() -> bool\n\ |
| 7033 | \n\ |
| 7034 | Return True if S is a valid identifier according\n\ |
| 7035 | to the language definition."); |
| 7036 | |
| 7037 | static PyObject* |
| 7038 | unicode_isidentifier(PyObject *self) |
| 7039 | { |
| 7040 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 7041 | } |
| 7042 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7043 | PyDoc_STRVAR(join__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7044 | "S.join(sequence) -> unicode\n\ |
| 7045 | \n\ |
| 7046 | 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] | 7047 | sequence. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7048 | |
| 7049 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7050 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7051 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7052 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7053 | } |
| 7054 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7055 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7056 | unicode_length(PyUnicodeObject *self) |
| 7057 | { |
| 7058 | return self->length; |
| 7059 | } |
| 7060 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7061 | PyDoc_STRVAR(ljust__doc__, |
Hye-Shik Chang | 974ed7c | 2004-06-02 16:49:17 +0000 | [diff] [blame] | 7062 | "S.ljust(width[, fillchar]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7063 | \n\ |
| 7064 | 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] | 7065 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7066 | |
| 7067 | static PyObject * |
| 7068 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 7069 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7070 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7071 | Py_UNICODE fillchar = ' '; |
| 7072 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7073 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7074 | return NULL; |
| 7075 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7076 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7077 | Py_INCREF(self); |
| 7078 | return (PyObject*) self; |
| 7079 | } |
| 7080 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7081 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7082 | } |
| 7083 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7084 | PyDoc_STRVAR(lower__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7085 | "S.lower() -> unicode\n\ |
| 7086 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7087 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7088 | |
| 7089 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7090 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7091 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7092 | return fixup(self, fixlower); |
| 7093 | } |
| 7094 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7095 | #define LEFTSTRIP 0 |
| 7096 | #define RIGHTSTRIP 1 |
| 7097 | #define BOTHSTRIP 2 |
| 7098 | |
| 7099 | /* Arrays indexed by above */ |
| 7100 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 7101 | |
| 7102 | #define STRIPNAME(i) (stripformat[i]+3) |
| 7103 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7104 | /* externally visible for str.strip(unicode) */ |
| 7105 | PyObject * |
| 7106 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 7107 | { |
| 7108 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7109 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7110 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7111 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 7112 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7113 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7114 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
| 7115 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7116 | i = 0; |
| 7117 | if (striptype != RIGHTSTRIP) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7118 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 7119 | i++; |
| 7120 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7121 | } |
| 7122 | |
| 7123 | j = len; |
| 7124 | if (striptype != LEFTSTRIP) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7125 | do { |
| 7126 | j--; |
| 7127 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 7128 | j++; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7129 | } |
| 7130 | |
| 7131 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7132 | Py_INCREF(self); |
| 7133 | return (PyObject*)self; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7134 | } |
| 7135 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7136 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7137 | } |
| 7138 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7139 | |
| 7140 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7141 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7142 | { |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7143 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7144 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7145 | |
| 7146 | i = 0; |
| 7147 | if (striptype != RIGHTSTRIP) { |
| 7148 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 7149 | i++; |
| 7150 | } |
| 7151 | } |
| 7152 | |
| 7153 | j = len; |
| 7154 | if (striptype != LEFTSTRIP) { |
| 7155 | do { |
| 7156 | j--; |
| 7157 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 7158 | j++; |
| 7159 | } |
| 7160 | |
| 7161 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 7162 | Py_INCREF(self); |
| 7163 | return (PyObject*)self; |
| 7164 | } |
| 7165 | else |
| 7166 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7167 | } |
| 7168 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7169 | |
| 7170 | static PyObject * |
| 7171 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 7172 | { |
| 7173 | PyObject *sep = NULL; |
| 7174 | |
| 7175 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 7176 | return NULL; |
| 7177 | |
| 7178 | if (sep != NULL && sep != Py_None) { |
| 7179 | if (PyUnicode_Check(sep)) |
| 7180 | return _PyUnicode_XStrip(self, striptype, sep); |
| 7181 | else if (PyString_Check(sep)) { |
| 7182 | PyObject *res; |
| 7183 | sep = PyUnicode_FromObject(sep); |
| 7184 | if (sep==NULL) |
| 7185 | return NULL; |
| 7186 | res = _PyUnicode_XStrip(self, striptype, sep); |
| 7187 | Py_DECREF(sep); |
| 7188 | return res; |
| 7189 | } |
| 7190 | else { |
| 7191 | PyErr_Format(PyExc_TypeError, |
| 7192 | "%s arg must be None, unicode or str", |
| 7193 | STRIPNAME(striptype)); |
| 7194 | return NULL; |
| 7195 | } |
| 7196 | } |
| 7197 | |
| 7198 | return do_strip(self, striptype); |
| 7199 | } |
| 7200 | |
| 7201 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7202 | PyDoc_STRVAR(strip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7203 | "S.strip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7204 | \n\ |
| 7205 | Return a copy of the string S with leading and trailing\n\ |
| 7206 | whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7207 | If chars is given and not None, remove characters in chars instead.\n\ |
| 7208 | 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] | 7209 | |
| 7210 | static PyObject * |
| 7211 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 7212 | { |
| 7213 | if (PyTuple_GET_SIZE(args) == 0) |
| 7214 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 7215 | else |
| 7216 | return do_argstrip(self, BOTHSTRIP, args); |
| 7217 | } |
| 7218 | |
| 7219 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7220 | PyDoc_STRVAR(lstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7221 | "S.lstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7222 | \n\ |
| 7223 | Return a copy of the string S with leading whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7224 | If chars is given and not None, remove characters in chars instead.\n\ |
| 7225 | 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] | 7226 | |
| 7227 | static PyObject * |
| 7228 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 7229 | { |
| 7230 | if (PyTuple_GET_SIZE(args) == 0) |
| 7231 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 7232 | else |
| 7233 | return do_argstrip(self, LEFTSTRIP, args); |
| 7234 | } |
| 7235 | |
| 7236 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7237 | PyDoc_STRVAR(rstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7238 | "S.rstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7239 | \n\ |
| 7240 | Return a copy of the string S with trailing whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7241 | If chars is given and not None, remove characters in chars instead.\n\ |
| 7242 | 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] | 7243 | |
| 7244 | static PyObject * |
| 7245 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 7246 | { |
| 7247 | if (PyTuple_GET_SIZE(args) == 0) |
| 7248 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 7249 | else |
| 7250 | return do_argstrip(self, RIGHTSTRIP, args); |
| 7251 | } |
| 7252 | |
| 7253 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7254 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7255 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7256 | { |
| 7257 | PyUnicodeObject *u; |
| 7258 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7259 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7260 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7261 | |
| 7262 | if (len < 0) |
| 7263 | len = 0; |
| 7264 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7265 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7266 | /* no repeat, return original string */ |
| 7267 | Py_INCREF(str); |
| 7268 | return (PyObject*) str; |
| 7269 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7270 | |
| 7271 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 7272 | * needed doesn't overflow size_t |
| 7273 | */ |
| 7274 | nchars = len * str->length; |
| 7275 | if (len && nchars / len != str->length) { |
| 7276 | PyErr_SetString(PyExc_OverflowError, |
| 7277 | "repeated string is too long"); |
| 7278 | return NULL; |
| 7279 | } |
| 7280 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 7281 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 7282 | PyErr_SetString(PyExc_OverflowError, |
| 7283 | "repeated string is too long"); |
| 7284 | return NULL; |
| 7285 | } |
| 7286 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7287 | if (!u) |
| 7288 | return NULL; |
| 7289 | |
| 7290 | p = u->str; |
| 7291 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7292 | if (str->length == 1 && len > 0) { |
| 7293 | Py_UNICODE_FILL(p, str->str[0], len); |
| 7294 | } else { |
| 7295 | Py_ssize_t done = 0; /* number of characters copied this far */ |
| 7296 | if (done < nchars) { |
| 7297 | Py_UNICODE_COPY(p, str->str, str->length); |
| 7298 | done = str->length; |
| 7299 | } |
| 7300 | while (done < nchars) { |
| 7301 | int n = (done <= nchars-done) ? done : nchars-done; |
| 7302 | Py_UNICODE_COPY(p+done, p, n); |
| 7303 | done += n; |
| 7304 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7305 | } |
| 7306 | |
| 7307 | return (PyObject*) u; |
| 7308 | } |
| 7309 | |
| 7310 | PyObject *PyUnicode_Replace(PyObject *obj, |
| 7311 | PyObject *subobj, |
| 7312 | PyObject *replobj, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7313 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7314 | { |
| 7315 | PyObject *self; |
| 7316 | PyObject *str1; |
| 7317 | PyObject *str2; |
| 7318 | PyObject *result; |
| 7319 | |
| 7320 | self = PyUnicode_FromObject(obj); |
| 7321 | if (self == NULL) |
| 7322 | return NULL; |
| 7323 | str1 = PyUnicode_FromObject(subobj); |
| 7324 | if (str1 == NULL) { |
| 7325 | Py_DECREF(self); |
| 7326 | return NULL; |
| 7327 | } |
| 7328 | str2 = PyUnicode_FromObject(replobj); |
| 7329 | if (str2 == NULL) { |
| 7330 | Py_DECREF(self); |
| 7331 | Py_DECREF(str1); |
| 7332 | return NULL; |
| 7333 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7334 | result = replace((PyUnicodeObject *)self, |
| 7335 | (PyUnicodeObject *)str1, |
| 7336 | (PyUnicodeObject *)str2, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7337 | maxcount); |
| 7338 | Py_DECREF(self); |
| 7339 | Py_DECREF(str1); |
| 7340 | Py_DECREF(str2); |
| 7341 | return result; |
| 7342 | } |
| 7343 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7344 | PyDoc_STRVAR(replace__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7345 | "S.replace (old, new[, maxsplit]) -> unicode\n\ |
| 7346 | \n\ |
| 7347 | Return a copy of S with all occurrences of substring\n\ |
| 7348 | 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] | 7349 | given, only the first maxsplit occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7350 | |
| 7351 | static PyObject* |
| 7352 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 7353 | { |
| 7354 | PyUnicodeObject *str1; |
| 7355 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7356 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7357 | PyObject *result; |
| 7358 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7359 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7360 | return NULL; |
| 7361 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 7362 | if (str1 == NULL) |
| 7363 | return NULL; |
| 7364 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7365 | if (str2 == NULL) { |
| 7366 | Py_DECREF(str1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7367 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7368 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7369 | |
| 7370 | result = replace(self, str1, str2, maxcount); |
| 7371 | |
| 7372 | Py_DECREF(str1); |
| 7373 | Py_DECREF(str2); |
| 7374 | return result; |
| 7375 | } |
| 7376 | |
| 7377 | static |
| 7378 | PyObject *unicode_repr(PyObject *unicode) |
| 7379 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7380 | PyObject *repr; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7381 | Py_UNICODE *p; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7382 | Py_UNICODE *s = PyUnicode_AS_UNICODE(unicode); |
| 7383 | Py_ssize_t size = PyUnicode_GET_SIZE(unicode); |
| 7384 | |
| 7385 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 7386 | better to choose a different scheme. Perhaps scan the |
| 7387 | first N-chars of the string and allocate based on that size. |
| 7388 | */ |
| 7389 | /* Initial allocation is based on the longest-possible unichr |
| 7390 | escape. |
| 7391 | |
| 7392 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 7393 | unichr, so in this case it's the longest unichr escape. In |
| 7394 | narrow (UTF-16) builds this is five chars per source unichr |
| 7395 | since there are two unichrs in the surrogate pair, so in narrow |
| 7396 | (UTF-16) builds it's not the longest unichr escape. |
| 7397 | |
| 7398 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 7399 | so in the narrow (UTF-16) build case it's the longest unichr |
| 7400 | escape. |
| 7401 | */ |
| 7402 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7403 | repr = PyUnicode_FromUnicode(NULL, |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7404 | 2 /* quotes */ |
| 7405 | #ifdef Py_UNICODE_WIDE |
| 7406 | + 10*size |
| 7407 | #else |
| 7408 | + 6*size |
| 7409 | #endif |
| 7410 | + 1); |
| 7411 | if (repr == NULL) |
| 7412 | return NULL; |
| 7413 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7414 | p = PyUnicode_AS_UNICODE(repr); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7415 | |
| 7416 | /* Add quote */ |
| 7417 | *p++ = (findchar(s, size, '\'') && |
| 7418 | !findchar(s, size, '"')) ? '"' : '\''; |
| 7419 | while (size-- > 0) { |
| 7420 | Py_UNICODE ch = *s++; |
| 7421 | |
| 7422 | /* Escape quotes and backslashes */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7423 | if ((ch == PyUnicode_AS_UNICODE(repr)[0]) || (ch == '\\')) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7424 | *p++ = '\\'; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7425 | *p++ = ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7426 | continue; |
| 7427 | } |
| 7428 | |
| 7429 | #ifdef Py_UNICODE_WIDE |
| 7430 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 7431 | else if (ch >= 0x10000) { |
| 7432 | *p++ = '\\'; |
| 7433 | *p++ = 'U'; |
| 7434 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 7435 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 7436 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 7437 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 7438 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 7439 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 7440 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 7441 | *p++ = hexdigits[ch & 0x0000000F]; |
| 7442 | continue; |
| 7443 | } |
| 7444 | #else |
| 7445 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 7446 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 7447 | Py_UNICODE ch2; |
| 7448 | Py_UCS4 ucs; |
| 7449 | |
| 7450 | ch2 = *s++; |
| 7451 | size--; |
| 7452 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 7453 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 7454 | *p++ = '\\'; |
| 7455 | *p++ = 'U'; |
| 7456 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 7457 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 7458 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 7459 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 7460 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 7461 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 7462 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 7463 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 7464 | continue; |
| 7465 | } |
| 7466 | /* Fall through: isolated surrogates are copied as-is */ |
| 7467 | s--; |
| 7468 | size++; |
| 7469 | } |
| 7470 | #endif |
| 7471 | |
| 7472 | /* Map 16-bit characters to '\uxxxx' */ |
| 7473 | if (ch >= 256) { |
| 7474 | *p++ = '\\'; |
| 7475 | *p++ = 'u'; |
| 7476 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 7477 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 7478 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 7479 | *p++ = hexdigits[ch & 0x000F]; |
| 7480 | } |
| 7481 | |
| 7482 | /* Map special whitespace to '\t', \n', '\r' */ |
| 7483 | else if (ch == '\t') { |
| 7484 | *p++ = '\\'; |
| 7485 | *p++ = 't'; |
| 7486 | } |
| 7487 | else if (ch == '\n') { |
| 7488 | *p++ = '\\'; |
| 7489 | *p++ = 'n'; |
| 7490 | } |
| 7491 | else if (ch == '\r') { |
| 7492 | *p++ = '\\'; |
| 7493 | *p++ = 'r'; |
| 7494 | } |
| 7495 | |
| 7496 | /* Map non-printable US ASCII to '\xhh' */ |
| 7497 | else if (ch < ' ' || ch >= 0x7F) { |
| 7498 | *p++ = '\\'; |
| 7499 | *p++ = 'x'; |
| 7500 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 7501 | *p++ = hexdigits[ch & 0x000F]; |
| 7502 | } |
| 7503 | |
| 7504 | /* Copy everything else as-is */ |
| 7505 | else |
| 7506 | *p++ = (char) ch; |
| 7507 | } |
| 7508 | /* Add quote */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7509 | *p++ = PyUnicode_AS_UNICODE(repr)[0]; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7510 | |
| 7511 | *p = '\0'; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7512 | _PyUnicode_Resize(&repr, p - PyUnicode_AS_UNICODE(repr)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7513 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7514 | } |
| 7515 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7516 | PyDoc_STRVAR(rfind__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7517 | "S.rfind(sub [,start [,end]]) -> int\n\ |
| 7518 | \n\ |
| 7519 | Return the highest index in S where substring sub is found,\n\ |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 7520 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7521 | arguments start and end are interpreted as in slice notation.\n\ |
| 7522 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7523 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7524 | |
| 7525 | static PyObject * |
| 7526 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 7527 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7528 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7529 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7530 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7531 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7532 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 7533 | if (!PyArg_ParseTuple(args, "O|O&O&:rfind", &substring, |
| 7534 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7535 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7536 | substring = PyUnicode_FromObject(substring); |
| 7537 | if (!substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7538 | return NULL; |
| 7539 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7540 | result = stringlib_rfind_slice( |
| 7541 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7542 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7543 | start, end |
| 7544 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7545 | |
| 7546 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7547 | |
| 7548 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7549 | } |
| 7550 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7551 | PyDoc_STRVAR(rindex__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7552 | "S.rindex(sub [,start [,end]]) -> int\n\ |
| 7553 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7554 | 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] | 7555 | |
| 7556 | static PyObject * |
| 7557 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 7558 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7559 | PyObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7560 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7561 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7562 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7563 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 7564 | if (!PyArg_ParseTuple(args, "O|O&O&:rindex", &substring, |
| 7565 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7566 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7567 | substring = PyUnicode_FromObject(substring); |
| 7568 | if (!substring) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7569 | return NULL; |
| 7570 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7571 | result = stringlib_rfind_slice( |
| 7572 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7573 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7574 | start, end |
| 7575 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7576 | |
| 7577 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7578 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7579 | if (result < 0) { |
| 7580 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 7581 | return NULL; |
| 7582 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7583 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7584 | } |
| 7585 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7586 | PyDoc_STRVAR(rjust__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7587 | "S.rjust(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7588 | \n\ |
| 7589 | 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] | 7590 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7591 | |
| 7592 | static PyObject * |
| 7593 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 7594 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7595 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7596 | Py_UNICODE fillchar = ' '; |
| 7597 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7598 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7599 | return NULL; |
| 7600 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7601 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7602 | Py_INCREF(self); |
| 7603 | return (PyObject*) self; |
| 7604 | } |
| 7605 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7606 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7607 | } |
| 7608 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7609 | PyObject *PyUnicode_Split(PyObject *s, |
| 7610 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7611 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7612 | { |
| 7613 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7614 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7615 | s = PyUnicode_FromObject(s); |
| 7616 | if (s == NULL) |
| 7617 | return NULL; |
| 7618 | if (sep != NULL) { |
| 7619 | sep = PyUnicode_FromObject(sep); |
| 7620 | if (sep == NULL) { |
| 7621 | Py_DECREF(s); |
| 7622 | return NULL; |
| 7623 | } |
| 7624 | } |
| 7625 | |
| 7626 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 7627 | |
| 7628 | Py_DECREF(s); |
| 7629 | Py_XDECREF(sep); |
| 7630 | return result; |
| 7631 | } |
| 7632 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7633 | PyDoc_STRVAR(split__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7634 | "S.split([sep [,maxsplit]]) -> list of strings\n\ |
| 7635 | \n\ |
| 7636 | Return a list of the words in S, using sep as the\n\ |
| 7637 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Thomas Heller | ca0d2cb | 2004-09-15 11:41:32 +0000 | [diff] [blame] | 7638 | 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] | 7639 | any whitespace string is a separator."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7640 | |
| 7641 | static PyObject* |
| 7642 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 7643 | { |
| 7644 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7645 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7646 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7647 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7648 | return NULL; |
| 7649 | |
| 7650 | if (substring == Py_None) |
| 7651 | return split(self, NULL, maxcount); |
| 7652 | else if (PyUnicode_Check(substring)) |
| 7653 | return split(self, (PyUnicodeObject *)substring, maxcount); |
| 7654 | else |
| 7655 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
| 7656 | } |
| 7657 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7658 | PyObject * |
| 7659 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 7660 | { |
| 7661 | PyObject* str_obj; |
| 7662 | PyObject* sep_obj; |
| 7663 | PyObject* out; |
| 7664 | |
| 7665 | str_obj = PyUnicode_FromObject(str_in); |
| 7666 | if (!str_obj) |
| 7667 | return NULL; |
| 7668 | sep_obj = PyUnicode_FromObject(sep_in); |
| 7669 | if (!sep_obj) { |
| 7670 | Py_DECREF(str_obj); |
| 7671 | return NULL; |
| 7672 | } |
| 7673 | |
| 7674 | out = stringlib_partition( |
| 7675 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 7676 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 7677 | ); |
| 7678 | |
| 7679 | Py_DECREF(sep_obj); |
| 7680 | Py_DECREF(str_obj); |
| 7681 | |
| 7682 | return out; |
| 7683 | } |
| 7684 | |
| 7685 | |
| 7686 | PyObject * |
| 7687 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 7688 | { |
| 7689 | PyObject* str_obj; |
| 7690 | PyObject* sep_obj; |
| 7691 | PyObject* out; |
| 7692 | |
| 7693 | str_obj = PyUnicode_FromObject(str_in); |
| 7694 | if (!str_obj) |
| 7695 | return NULL; |
| 7696 | sep_obj = PyUnicode_FromObject(sep_in); |
| 7697 | if (!sep_obj) { |
| 7698 | Py_DECREF(str_obj); |
| 7699 | return NULL; |
| 7700 | } |
| 7701 | |
| 7702 | out = stringlib_rpartition( |
| 7703 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 7704 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 7705 | ); |
| 7706 | |
| 7707 | Py_DECREF(sep_obj); |
| 7708 | Py_DECREF(str_obj); |
| 7709 | |
| 7710 | return out; |
| 7711 | } |
| 7712 | |
| 7713 | PyDoc_STRVAR(partition__doc__, |
| 7714 | "S.partition(sep) -> (head, sep, tail)\n\ |
| 7715 | \n\ |
| 7716 | Searches for the separator sep in S, and returns the part before it,\n\ |
| 7717 | the separator itself, and the part after it. If the separator is not\n\ |
| 7718 | found, returns S and two empty strings."); |
| 7719 | |
| 7720 | static PyObject* |
| 7721 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
| 7722 | { |
| 7723 | return PyUnicode_Partition((PyObject *)self, separator); |
| 7724 | } |
| 7725 | |
| 7726 | PyDoc_STRVAR(rpartition__doc__, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7727 | "S.rpartition(sep) -> (tail, sep, head)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7728 | \n\ |
| 7729 | Searches for the separator sep in S, starting at the end of S, and returns\n\ |
| 7730 | the part before it, the separator itself, and the part after it. If the\n\ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7731 | separator is not found, returns two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7732 | |
| 7733 | static PyObject* |
| 7734 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 7735 | { |
| 7736 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 7737 | } |
| 7738 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7739 | PyObject *PyUnicode_RSplit(PyObject *s, |
| 7740 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7741 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7742 | { |
| 7743 | PyObject *result; |
| 7744 | |
| 7745 | s = PyUnicode_FromObject(s); |
| 7746 | if (s == NULL) |
| 7747 | return NULL; |
| 7748 | if (sep != NULL) { |
| 7749 | sep = PyUnicode_FromObject(sep); |
| 7750 | if (sep == NULL) { |
| 7751 | Py_DECREF(s); |
| 7752 | return NULL; |
| 7753 | } |
| 7754 | } |
| 7755 | |
| 7756 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 7757 | |
| 7758 | Py_DECREF(s); |
| 7759 | Py_XDECREF(sep); |
| 7760 | return result; |
| 7761 | } |
| 7762 | |
| 7763 | PyDoc_STRVAR(rsplit__doc__, |
| 7764 | "S.rsplit([sep [,maxsplit]]) -> list of strings\n\ |
| 7765 | \n\ |
| 7766 | Return a list of the words in S, using sep as the\n\ |
| 7767 | delimiter string, starting at the end of the string and\n\ |
| 7768 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 7769 | splits are done. If sep is not specified, any whitespace string\n\ |
| 7770 | is a separator."); |
| 7771 | |
| 7772 | static PyObject* |
| 7773 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 7774 | { |
| 7775 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7776 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7777 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7778 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7779 | return NULL; |
| 7780 | |
| 7781 | if (substring == Py_None) |
| 7782 | return rsplit(self, NULL, maxcount); |
| 7783 | else if (PyUnicode_Check(substring)) |
| 7784 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
| 7785 | else |
| 7786 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
| 7787 | } |
| 7788 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7789 | PyDoc_STRVAR(splitlines__doc__, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7790 | "S.splitlines([keepends]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7791 | \n\ |
| 7792 | 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] | 7793 | 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] | 7794 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7795 | |
| 7796 | static PyObject* |
| 7797 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 7798 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7799 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7800 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7801 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7802 | return NULL; |
| 7803 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7804 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7805 | } |
| 7806 | |
| 7807 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 7808 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7809 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 7810 | if (PyUnicode_CheckExact(self)) { |
| 7811 | Py_INCREF(self); |
| 7812 | return self; |
| 7813 | } else |
| 7814 | /* Subtype -- return genuine unicode string with the same value. */ |
| 7815 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(self), |
| 7816 | PyUnicode_GET_SIZE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7817 | } |
| 7818 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7819 | PyDoc_STRVAR(swapcase__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7820 | "S.swapcase() -> unicode\n\ |
| 7821 | \n\ |
| 7822 | 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] | 7823 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7824 | |
| 7825 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7826 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7827 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7828 | return fixup(self, fixswapcase); |
| 7829 | } |
| 7830 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7831 | PyDoc_STRVAR(translate__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7832 | "S.translate(table) -> unicode\n\ |
| 7833 | \n\ |
| 7834 | Return a copy of the string S, where all characters have been mapped\n\ |
| 7835 | 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] | 7836 | Unicode ordinals to Unicode ordinals, Unicode strings or None.\n\ |
| 7837 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 7838 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7839 | |
| 7840 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7841 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7842 | { |
Georg Brandl | 94c2c75 | 2007-10-23 06:52:59 +0000 | [diff] [blame] | 7843 | PyObject *newtable = NULL; |
| 7844 | Py_ssize_t i = 0; |
| 7845 | PyObject *key, *value, *result; |
| 7846 | |
| 7847 | if (!PyDict_Check(table)) { |
| 7848 | PyErr_SetString(PyExc_TypeError, "translate argument must be a dict"); |
| 7849 | return NULL; |
| 7850 | } |
| 7851 | /* fixup the table -- allow size-1 string keys instead of only int keys */ |
| 7852 | newtable = PyDict_Copy(table); |
| 7853 | if (!newtable) return NULL; |
| 7854 | while (PyDict_Next(table, &i, &key, &value)) { |
| 7855 | if (PyUnicode_Check(key)) { |
| 7856 | /* convert string keys to integer keys */ |
| 7857 | PyObject *newkey; |
| 7858 | int res; |
| 7859 | if (PyUnicode_GET_SIZE(key) != 1) { |
| 7860 | PyErr_SetString(PyExc_ValueError, "string items in translate " |
| 7861 | "table must be 1 element long"); |
| 7862 | goto err; |
| 7863 | } |
| 7864 | newkey = PyInt_FromLong(PyUnicode_AS_UNICODE(key)[0]); |
| 7865 | if (!newkey) |
| 7866 | goto err; |
| 7867 | res = PyDict_SetItem(newtable, newkey, value); |
| 7868 | Py_DECREF(newkey); |
| 7869 | if (res < 0) |
| 7870 | goto err; |
| 7871 | } else if (PyInt_Check(key)) { |
| 7872 | /* just keep integer keys */ |
| 7873 | if (PyDict_SetItem(newtable, key, value) < 0) |
| 7874 | goto err; |
| 7875 | } else { |
| 7876 | PyErr_SetString(PyExc_TypeError, "items in translate table must be " |
| 7877 | "strings or integers"); |
| 7878 | goto err; |
| 7879 | } |
| 7880 | } |
| 7881 | |
| 7882 | result = PyUnicode_TranslateCharmap(self->str, |
| 7883 | self->length, |
| 7884 | newtable, |
| 7885 | "ignore"); |
| 7886 | Py_DECREF(newtable); |
| 7887 | return result; |
| 7888 | err: |
| 7889 | Py_DECREF(newtable); |
| 7890 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7891 | } |
| 7892 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7893 | PyDoc_STRVAR(upper__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7894 | "S.upper() -> unicode\n\ |
| 7895 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7896 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7897 | |
| 7898 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7899 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7900 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7901 | return fixup(self, fixupper); |
| 7902 | } |
| 7903 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7904 | PyDoc_STRVAR(zfill__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7905 | "S.zfill(width) -> unicode\n\ |
| 7906 | \n\ |
| 7907 | 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] | 7908 | of the specified width. The string x is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7909 | |
| 7910 | static PyObject * |
| 7911 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 7912 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7913 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7914 | PyUnicodeObject *u; |
| 7915 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7916 | Py_ssize_t width; |
| 7917 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7918 | return NULL; |
| 7919 | |
| 7920 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 7921 | if (PyUnicode_CheckExact(self)) { |
| 7922 | Py_INCREF(self); |
| 7923 | return (PyObject*) self; |
| 7924 | } |
| 7925 | else |
| 7926 | return PyUnicode_FromUnicode( |
| 7927 | PyUnicode_AS_UNICODE(self), |
| 7928 | PyUnicode_GET_SIZE(self) |
| 7929 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7930 | } |
| 7931 | |
| 7932 | fill = width - self->length; |
| 7933 | |
| 7934 | u = pad(self, fill, 0, '0'); |
| 7935 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 7936 | if (u == NULL) |
| 7937 | return NULL; |
| 7938 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7939 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 7940 | /* move sign to beginning of string */ |
| 7941 | u->str[0] = u->str[fill]; |
| 7942 | u->str[fill] = '0'; |
| 7943 | } |
| 7944 | |
| 7945 | return (PyObject*) u; |
| 7946 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7947 | |
| 7948 | #if 0 |
| 7949 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7950 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7951 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7952 | return PyInt_FromLong(unicode_freelist_size); |
| 7953 | } |
| 7954 | #endif |
| 7955 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7956 | PyDoc_STRVAR(startswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7957 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7958 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 7959 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 7960 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7961 | With optional end, stop comparing S at that position.\n\ |
| 7962 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7963 | |
| 7964 | static PyObject * |
| 7965 | unicode_startswith(PyUnicodeObject *self, |
| 7966 | PyObject *args) |
| 7967 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7968 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7969 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7970 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7971 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7972 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7973 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7974 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 7975 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7976 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7977 | if (PyTuple_Check(subobj)) { |
| 7978 | Py_ssize_t i; |
| 7979 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 7980 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 7981 | PyTuple_GET_ITEM(subobj, i)); |
| 7982 | if (substring == NULL) |
| 7983 | return NULL; |
| 7984 | result = tailmatch(self, substring, start, end, -1); |
| 7985 | Py_DECREF(substring); |
| 7986 | if (result) { |
| 7987 | Py_RETURN_TRUE; |
| 7988 | } |
| 7989 | } |
| 7990 | /* nothing matched */ |
| 7991 | Py_RETURN_FALSE; |
| 7992 | } |
| 7993 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7994 | if (substring == NULL) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7995 | return NULL; |
| 7996 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7997 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7998 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7999 | } |
| 8000 | |
| 8001 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8002 | PyDoc_STRVAR(endswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 8003 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8004 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 8005 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 8006 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8007 | With optional end, stop comparing S at that position.\n\ |
| 8008 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8009 | |
| 8010 | static PyObject * |
| 8011 | unicode_endswith(PyUnicodeObject *self, |
| 8012 | PyObject *args) |
| 8013 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8014 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8015 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8016 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8017 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8018 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8019 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8020 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
| 8021 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8022 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8023 | if (PyTuple_Check(subobj)) { |
| 8024 | Py_ssize_t i; |
| 8025 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 8026 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 8027 | PyTuple_GET_ITEM(subobj, i)); |
| 8028 | if (substring == NULL) |
| 8029 | return NULL; |
| 8030 | result = tailmatch(self, substring, start, end, +1); |
| 8031 | Py_DECREF(substring); |
| 8032 | if (result) { |
| 8033 | Py_RETURN_TRUE; |
| 8034 | } |
| 8035 | } |
| 8036 | Py_RETURN_FALSE; |
| 8037 | } |
| 8038 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8039 | if (substring == NULL) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8040 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8041 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8042 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8043 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8044 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8045 | } |
| 8046 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8047 | #include "stringlib/string_format.h" |
| 8048 | |
| 8049 | PyDoc_STRVAR(format__doc__, |
| 8050 | "S.format(*args, **kwargs) -> unicode\n\ |
| 8051 | \n\ |
| 8052 | "); |
| 8053 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8054 | PyDoc_STRVAR(p_format__doc__, |
| 8055 | "S.__format__(format_spec) -> unicode\n\ |
| 8056 | \n\ |
| 8057 | "); |
| 8058 | |
| 8059 | static PyObject * |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8060 | unicode_getnewargs(PyUnicodeObject *v) |
| 8061 | { |
| 8062 | return Py_BuildValue("(u#)", v->str, v->length); |
| 8063 | } |
| 8064 | |
| 8065 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8066 | static PyMethodDef unicode_methods[] = { |
| 8067 | |
| 8068 | /* Order is according to common usage: often used methods should |
| 8069 | appear first, since lookup is done sequentially. */ |
| 8070 | |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8071 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS, encode__doc__}, |
| 8072 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 8073 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8074 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8075 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 8076 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 8077 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 8078 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 8079 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 8080 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 8081 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8082 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8083 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 8084 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 8085 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8086 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8087 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 8088 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 8089 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8090 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8091 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8092 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8093 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8094 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 8095 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 8096 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 8097 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 8098 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 8099 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 8100 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 8101 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 8102 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 8103 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 8104 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 8105 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 8106 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 8107 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 8108 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8109 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 8110 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
| 8111 | {"__format__", (PyCFunction) unicode_unicode__format__, METH_VARARGS, p_format__doc__}, |
Eric Smith | f6db409 | 2007-08-27 23:52:26 +0000 | [diff] [blame] | 8112 | {"_formatter_field_name_split", (PyCFunction) formatter_field_name_split, METH_NOARGS}, |
| 8113 | {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8114 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8115 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8116 | #endif |
| 8117 | |
| 8118 | #if 0 |
| 8119 | /* This one is just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8120 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8121 | #endif |
| 8122 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8123 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8124 | {NULL, NULL} |
| 8125 | }; |
| 8126 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8127 | static PyObject * |
| 8128 | unicode_mod(PyObject *v, PyObject *w) |
| 8129 | { |
| 8130 | if (!PyUnicode_Check(v)) { |
| 8131 | Py_INCREF(Py_NotImplemented); |
| 8132 | return Py_NotImplemented; |
| 8133 | } |
| 8134 | return PyUnicode_Format(v, w); |
| 8135 | } |
| 8136 | |
| 8137 | static PyNumberMethods unicode_as_number = { |
| 8138 | 0, /*nb_add*/ |
| 8139 | 0, /*nb_subtract*/ |
| 8140 | 0, /*nb_multiply*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8141 | unicode_mod, /*nb_remainder*/ |
| 8142 | }; |
| 8143 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8144 | static PySequenceMethods unicode_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8145 | (lenfunc) unicode_length, /* sq_length */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8146 | PyUnicode_Concat, /* sq_concat */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8147 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 8148 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
Thomas Wouters | d2cf20e | 2007-08-30 22:57:53 +0000 | [diff] [blame] | 8149 | 0, /* sq_slice */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8150 | 0, /* sq_ass_item */ |
| 8151 | 0, /* sq_ass_slice */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8152 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8153 | }; |
| 8154 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8155 | static PyObject* |
| 8156 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 8157 | { |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 8158 | if (PyIndex_Check(item)) { |
| 8159 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8160 | if (i == -1 && PyErr_Occurred()) |
| 8161 | return NULL; |
| 8162 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8163 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8164 | return unicode_getitem(self, i); |
| 8165 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8166 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8167 | Py_UNICODE* source_buf; |
| 8168 | Py_UNICODE* result_buf; |
| 8169 | PyObject* result; |
| 8170 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8171 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8172 | &start, &stop, &step, &slicelength) < 0) { |
| 8173 | return NULL; |
| 8174 | } |
| 8175 | |
| 8176 | if (slicelength <= 0) { |
| 8177 | return PyUnicode_FromUnicode(NULL, 0); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 8178 | } else if (start == 0 && step == 1 && slicelength == self->length && |
| 8179 | PyUnicode_CheckExact(self)) { |
| 8180 | Py_INCREF(self); |
| 8181 | return (PyObject *)self; |
| 8182 | } else if (step == 1) { |
| 8183 | return PyUnicode_FromUnicode(self->str + start, slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8184 | } else { |
| 8185 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8186 | result_buf = (Py_UNICODE *)PyMem_MALLOC(slicelength* |
| 8187 | sizeof(Py_UNICODE)); |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8188 | |
| 8189 | if (result_buf == NULL) |
| 8190 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8191 | |
| 8192 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 8193 | result_buf[i] = source_buf[cur]; |
| 8194 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8195 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8196 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
| 8197 | PyMem_FREE(result_buf); |
| 8198 | return result; |
| 8199 | } |
| 8200 | } else { |
| 8201 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 8202 | return NULL; |
| 8203 | } |
| 8204 | } |
| 8205 | |
| 8206 | static PyMappingMethods unicode_as_mapping = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8207 | (lenfunc)unicode_length, /* mp_length */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8208 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 8209 | (objobjargproc)0, /* mp_ass_subscript */ |
| 8210 | }; |
| 8211 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8212 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8213 | /* Helpers for PyUnicode_Format() */ |
| 8214 | |
| 8215 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8216 | 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] | 8217 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8218 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8219 | if (argidx < arglen) { |
| 8220 | (*p_argidx)++; |
| 8221 | if (arglen < 0) |
| 8222 | return args; |
| 8223 | else |
| 8224 | return PyTuple_GetItem(args, argidx); |
| 8225 | } |
| 8226 | PyErr_SetString(PyExc_TypeError, |
| 8227 | "not enough arguments for format string"); |
| 8228 | return NULL; |
| 8229 | } |
| 8230 | |
| 8231 | #define F_LJUST (1<<0) |
| 8232 | #define F_SIGN (1<<1) |
| 8233 | #define F_BLANK (1<<2) |
| 8234 | #define F_ALT (1<<3) |
| 8235 | #define F_ZERO (1<<4) |
| 8236 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8237 | static Py_ssize_t |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8238 | strtounicode(Py_UNICODE *buffer, const char *charbuffer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8239 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8240 | register Py_ssize_t i; |
| 8241 | Py_ssize_t len = strlen(charbuffer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8242 | for (i = len - 1; i >= 0; i--) |
| 8243 | buffer[i] = (Py_UNICODE) charbuffer[i]; |
| 8244 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8245 | return len; |
| 8246 | } |
| 8247 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8248 | static int |
| 8249 | doubletounicode(Py_UNICODE *buffer, size_t len, const char *format, double x) |
| 8250 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8251 | Py_ssize_t result; |
| 8252 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8253 | PyOS_ascii_formatd((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8254 | result = strtounicode(buffer, (char *)buffer); |
| 8255 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8256 | } |
| 8257 | |
| 8258 | static int |
| 8259 | longtounicode(Py_UNICODE *buffer, size_t len, const char *format, long x) |
| 8260 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8261 | Py_ssize_t result; |
| 8262 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8263 | PyOS_snprintf((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8264 | result = strtounicode(buffer, (char *)buffer); |
| 8265 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8266 | } |
| 8267 | |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 8268 | /* XXX To save some code duplication, formatfloat/long/int could have been |
| 8269 | shared with stringobject.c, converting from 8-bit to Unicode after the |
| 8270 | formatting is done. */ |
| 8271 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8272 | static int |
| 8273 | formatfloat(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8274 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8275 | int flags, |
| 8276 | int prec, |
| 8277 | int type, |
| 8278 | PyObject *v) |
| 8279 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8280 | /* fmt = '%#.' + `prec` + `type` |
| 8281 | 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] | 8282 | char fmt[20]; |
| 8283 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8284 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8285 | x = PyFloat_AsDouble(v); |
| 8286 | if (x == -1.0 && PyErr_Occurred()) |
| 8287 | return -1; |
| 8288 | if (prec < 0) |
| 8289 | prec = 6; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8290 | if (type == 'f' && (fabs(x) / 1e25) >= 1e25) |
| 8291 | type = 'g'; |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8292 | /* Worst case length calc to ensure no buffer overrun: |
| 8293 | |
| 8294 | 'g' formats: |
| 8295 | fmt = %#.<prec>g |
| 8296 | buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
| 8297 | for any double rep.) |
| 8298 | len = 1 + prec + 1 + 2 + 5 = 9 + prec |
| 8299 | |
| 8300 | 'f' formats: |
| 8301 | buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
| 8302 | len = 1 + 50 + 1 + prec = 52 + prec |
| 8303 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8304 | If prec=0 the effective precision is 1 (the leading digit is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8305 | always given), therefore increase the length by one. |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8306 | |
| 8307 | */ |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 8308 | if (((type == 'g' || type == 'G') && |
| 8309 | buflen <= (size_t)10 + (size_t)prec) || |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8310 | (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8311 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8312 | "formatted float is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8313 | return -1; |
| 8314 | } |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8315 | PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
| 8316 | (flags&F_ALT) ? "#" : "", |
| 8317 | prec, type); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8318 | return doubletounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8319 | } |
| 8320 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8321 | static PyObject* |
| 8322 | formatlong(PyObject *val, int flags, int prec, int type) |
| 8323 | { |
| 8324 | char *buf; |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8325 | int len; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8326 | PyObject *str; /* temporary string object. */ |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8327 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8328 | |
| 8329 | str = _PyString_FormatLong(val, flags, prec, type, &buf, &len); |
| 8330 | if (!str) |
| 8331 | return NULL; |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8332 | result = PyUnicode_FromStringAndSize(buf, len); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8333 | Py_DECREF(str); |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8334 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8335 | } |
| 8336 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8337 | static int |
| 8338 | formatint(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8339 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8340 | int flags, |
| 8341 | int prec, |
| 8342 | int type, |
| 8343 | PyObject *v) |
| 8344 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8345 | /* fmt = '%#.' + `prec` + 'l' + `type` |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8346 | * worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
| 8347 | * + 1 + 1 |
| 8348 | * = 24 |
| 8349 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8350 | char fmt[64]; /* plenty big enough! */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8351 | char *sign; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8352 | long x; |
| 8353 | |
| 8354 | x = PyInt_AsLong(v); |
| 8355 | if (x == -1 && PyErr_Occurred()) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8356 | return -1; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8357 | if (x < 0 && type == 'u') { |
| 8358 | type = 'd'; |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 8359 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8360 | if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) |
| 8361 | sign = "-"; |
| 8362 | else |
| 8363 | sign = ""; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8364 | if (prec < 0) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8365 | prec = 1; |
| 8366 | |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8367 | /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) |
| 8368 | * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8369 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8370 | if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8371 | PyErr_SetString(PyExc_OverflowError, |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8372 | "formatted integer is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8373 | return -1; |
| 8374 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8375 | |
| 8376 | if ((flags & F_ALT) && |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 8377 | (type == 'x' || type == 'X' || type == 'o')) { |
| 8378 | /* When converting under %#o, %#x or %#X, there are a number |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8379 | * of issues that cause pain: |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 8380 | * - for %#o, we want a different base marker than C |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8381 | * - when 0 is being converted, the C standard leaves off |
| 8382 | * the '0x' or '0X', which is inconsistent with other |
| 8383 | * %#x/%#X conversions and inconsistent with Python's |
| 8384 | * hex() function |
| 8385 | * - there are platforms that violate the standard and |
| 8386 | * convert 0 with the '0x' or '0X' |
| 8387 | * (Metrowerks, Compaq Tru64) |
| 8388 | * - there are platforms that give '0x' when converting |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8389 | * under %#X, but convert 0 in accordance with the |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8390 | * standard (OS/2 EMX) |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8391 | * |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8392 | * We can achieve the desired consistency by inserting our |
| 8393 | * own '0x' or '0X' prefix, and substituting %x/%X in place |
| 8394 | * of %#x/%#X. |
| 8395 | * |
| 8396 | * Note that this is the same approach as used in |
| 8397 | * formatint() in stringobject.c |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 8398 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8399 | PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", |
| 8400 | sign, type, prec, type); |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 8401 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8402 | else { |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8403 | PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", |
| 8404 | sign, (flags&F_ALT) ? "#" : "", |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8405 | prec, type); |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 8406 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8407 | if (sign[0]) |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8408 | return longtounicode(buf, buflen, fmt, -x); |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8409 | else |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8410 | return longtounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8411 | } |
| 8412 | |
| 8413 | static int |
| 8414 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8415 | size_t buflen, |
| 8416 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8417 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8418 | /* presume that the buffer is at least 2 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8419 | if (PyUnicode_Check(v)) { |
| 8420 | if (PyUnicode_GET_SIZE(v) != 1) |
| 8421 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8422 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8423 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8424 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8425 | else if (PyString_Check(v)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8426 | if (PyString_GET_SIZE(v) != 1) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8427 | goto onError; |
| 8428 | buf[0] = (Py_UNICODE)PyString_AS_STRING(v)[0]; |
| 8429 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8430 | |
| 8431 | else { |
| 8432 | /* Integer input truncated to a character */ |
| 8433 | long x; |
| 8434 | x = PyInt_AsLong(v); |
| 8435 | if (x == -1 && PyErr_Occurred()) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8436 | goto onError; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8437 | #ifdef Py_UNICODE_WIDE |
| 8438 | if (x < 0 || x > 0x10ffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 8439 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8440 | "%c arg not in range(0x110000) " |
| 8441 | "(wide Python build)"); |
| 8442 | return -1; |
| 8443 | } |
| 8444 | #else |
| 8445 | if (x < 0 || x > 0xffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 8446 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8447 | "%c arg not in range(0x10000) " |
| 8448 | "(narrow Python build)"); |
| 8449 | return -1; |
| 8450 | } |
| 8451 | #endif |
| 8452 | buf[0] = (Py_UNICODE) x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8453 | } |
| 8454 | buf[1] = '\0'; |
| 8455 | return 1; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8456 | |
| 8457 | onError: |
| 8458 | PyErr_SetString(PyExc_TypeError, |
| 8459 | "%c requires int or char"); |
| 8460 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8461 | } |
| 8462 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8463 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 8464 | |
| 8465 | FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
| 8466 | chars are formatted. XXX This is a magic number. Each formatting |
| 8467 | routine does bounds checking to ensure no overflow, but a better |
| 8468 | solution may be to malloc a buffer of appropriate size for each |
| 8469 | format. For now, the current solution is sufficient. |
| 8470 | */ |
| 8471 | #define FORMATBUFLEN (size_t)120 |
| 8472 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8473 | PyObject *PyUnicode_Format(PyObject *format, |
| 8474 | PyObject *args) |
| 8475 | { |
| 8476 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8477 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8478 | int args_owned = 0; |
| 8479 | PyUnicodeObject *result = NULL; |
| 8480 | PyObject *dict = NULL; |
| 8481 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8482 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8483 | if (format == NULL || args == NULL) { |
| 8484 | PyErr_BadInternalCall(); |
| 8485 | return NULL; |
| 8486 | } |
| 8487 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8488 | if (uformat == NULL) |
| 8489 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8490 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 8491 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 8492 | |
| 8493 | reslen = rescnt = fmtcnt + 100; |
| 8494 | result = _PyUnicode_New(reslen); |
| 8495 | if (result == NULL) |
| 8496 | goto onError; |
| 8497 | res = PyUnicode_AS_UNICODE(result); |
| 8498 | |
| 8499 | if (PyTuple_Check(args)) { |
| 8500 | arglen = PyTuple_Size(args); |
| 8501 | argidx = 0; |
| 8502 | } |
| 8503 | else { |
| 8504 | arglen = -1; |
| 8505 | argidx = -2; |
| 8506 | } |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 8507 | if (Py_Type(args)->tp_as_mapping && !PyTuple_Check(args) && |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 8508 | !PyString_Check(args) && !PyUnicode_Check(args)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8509 | dict = args; |
| 8510 | |
| 8511 | while (--fmtcnt >= 0) { |
| 8512 | if (*fmt != '%') { |
| 8513 | if (--rescnt < 0) { |
| 8514 | rescnt = fmtcnt + 100; |
| 8515 | reslen += rescnt; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8516 | if (_PyUnicode_Resize(&result, reslen) < 0) |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 8517 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8518 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 8519 | --rescnt; |
| 8520 | } |
| 8521 | *res++ = *fmt++; |
| 8522 | } |
| 8523 | else { |
| 8524 | /* Got a format specifier */ |
| 8525 | int flags = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8526 | Py_ssize_t width = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8527 | int prec = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8528 | Py_UNICODE c = '\0'; |
| 8529 | Py_UNICODE fill; |
| 8530 | PyObject *v = NULL; |
| 8531 | PyObject *temp = NULL; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8532 | Py_UNICODE *pbuf; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8533 | Py_UNICODE sign; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8534 | Py_ssize_t len; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8535 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8536 | |
| 8537 | fmt++; |
| 8538 | if (*fmt == '(') { |
| 8539 | Py_UNICODE *keystart; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8540 | Py_ssize_t keylen; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8541 | PyObject *key; |
| 8542 | int pcount = 1; |
| 8543 | |
| 8544 | if (dict == NULL) { |
| 8545 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8546 | "format requires a mapping"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8547 | goto onError; |
| 8548 | } |
| 8549 | ++fmt; |
| 8550 | --fmtcnt; |
| 8551 | keystart = fmt; |
| 8552 | /* Skip over balanced parentheses */ |
| 8553 | while (pcount > 0 && --fmtcnt >= 0) { |
| 8554 | if (*fmt == ')') |
| 8555 | --pcount; |
| 8556 | else if (*fmt == '(') |
| 8557 | ++pcount; |
| 8558 | fmt++; |
| 8559 | } |
| 8560 | keylen = fmt - keystart - 1; |
| 8561 | if (fmtcnt < 0 || pcount > 0) { |
| 8562 | PyErr_SetString(PyExc_ValueError, |
| 8563 | "incomplete format key"); |
| 8564 | goto onError; |
| 8565 | } |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 8566 | #if 0 |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8567 | /* keys are converted to strings using UTF-8 and |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8568 | then looked up since Python uses strings to hold |
| 8569 | variables names etc. in its namespaces and we |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8570 | wouldn't want to break common idioms. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8571 | key = PyUnicode_EncodeUTF8(keystart, |
| 8572 | keylen, |
| 8573 | NULL); |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 8574 | #else |
| 8575 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 8576 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8577 | if (key == NULL) |
| 8578 | goto onError; |
| 8579 | if (args_owned) { |
| 8580 | Py_DECREF(args); |
| 8581 | args_owned = 0; |
| 8582 | } |
| 8583 | args = PyObject_GetItem(dict, key); |
| 8584 | Py_DECREF(key); |
| 8585 | if (args == NULL) { |
| 8586 | goto onError; |
| 8587 | } |
| 8588 | args_owned = 1; |
| 8589 | arglen = -1; |
| 8590 | argidx = -2; |
| 8591 | } |
| 8592 | while (--fmtcnt >= 0) { |
| 8593 | switch (c = *fmt++) { |
| 8594 | case '-': flags |= F_LJUST; continue; |
| 8595 | case '+': flags |= F_SIGN; continue; |
| 8596 | case ' ': flags |= F_BLANK; continue; |
| 8597 | case '#': flags |= F_ALT; continue; |
| 8598 | case '0': flags |= F_ZERO; continue; |
| 8599 | } |
| 8600 | break; |
| 8601 | } |
| 8602 | if (c == '*') { |
| 8603 | v = getnextarg(args, arglen, &argidx); |
| 8604 | if (v == NULL) |
| 8605 | goto onError; |
| 8606 | if (!PyInt_Check(v)) { |
| 8607 | PyErr_SetString(PyExc_TypeError, |
| 8608 | "* wants int"); |
| 8609 | goto onError; |
| 8610 | } |
| 8611 | width = PyInt_AsLong(v); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 8612 | if (width == -1 && PyErr_Occurred()) |
| 8613 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8614 | if (width < 0) { |
| 8615 | flags |= F_LJUST; |
| 8616 | width = -width; |
| 8617 | } |
| 8618 | if (--fmtcnt >= 0) |
| 8619 | c = *fmt++; |
| 8620 | } |
| 8621 | else if (c >= '0' && c <= '9') { |
| 8622 | width = c - '0'; |
| 8623 | while (--fmtcnt >= 0) { |
| 8624 | c = *fmt++; |
| 8625 | if (c < '0' || c > '9') |
| 8626 | break; |
| 8627 | if ((width*10) / 10 != width) { |
| 8628 | PyErr_SetString(PyExc_ValueError, |
| 8629 | "width too big"); |
| 8630 | goto onError; |
| 8631 | } |
| 8632 | width = width*10 + (c - '0'); |
| 8633 | } |
| 8634 | } |
| 8635 | if (c == '.') { |
| 8636 | prec = 0; |
| 8637 | if (--fmtcnt >= 0) |
| 8638 | c = *fmt++; |
| 8639 | if (c == '*') { |
| 8640 | v = getnextarg(args, arglen, &argidx); |
| 8641 | if (v == NULL) |
| 8642 | goto onError; |
| 8643 | if (!PyInt_Check(v)) { |
| 8644 | PyErr_SetString(PyExc_TypeError, |
| 8645 | "* wants int"); |
| 8646 | goto onError; |
| 8647 | } |
| 8648 | prec = PyInt_AsLong(v); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 8649 | if (prec == -1 && PyErr_Occurred()) |
| 8650 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8651 | if (prec < 0) |
| 8652 | prec = 0; |
| 8653 | if (--fmtcnt >= 0) |
| 8654 | c = *fmt++; |
| 8655 | } |
| 8656 | else if (c >= '0' && c <= '9') { |
| 8657 | prec = c - '0'; |
| 8658 | while (--fmtcnt >= 0) { |
| 8659 | c = Py_CHARMASK(*fmt++); |
| 8660 | if (c < '0' || c > '9') |
| 8661 | break; |
| 8662 | if ((prec*10) / 10 != prec) { |
| 8663 | PyErr_SetString(PyExc_ValueError, |
| 8664 | "prec too big"); |
| 8665 | goto onError; |
| 8666 | } |
| 8667 | prec = prec*10 + (c - '0'); |
| 8668 | } |
| 8669 | } |
| 8670 | } /* prec */ |
| 8671 | if (fmtcnt >= 0) { |
| 8672 | if (c == 'h' || c == 'l' || c == 'L') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8673 | if (--fmtcnt >= 0) |
| 8674 | c = *fmt++; |
| 8675 | } |
| 8676 | } |
| 8677 | if (fmtcnt < 0) { |
| 8678 | PyErr_SetString(PyExc_ValueError, |
| 8679 | "incomplete format"); |
| 8680 | goto onError; |
| 8681 | } |
| 8682 | if (c != '%') { |
| 8683 | v = getnextarg(args, arglen, &argidx); |
| 8684 | if (v == NULL) |
| 8685 | goto onError; |
| 8686 | } |
| 8687 | sign = 0; |
| 8688 | fill = ' '; |
| 8689 | switch (c) { |
| 8690 | |
| 8691 | case '%': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8692 | pbuf = formatbuf; |
| 8693 | /* presume that buffer length is at least 1 */ |
| 8694 | pbuf[0] = '%'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8695 | len = 1; |
| 8696 | break; |
| 8697 | |
| 8698 | case 's': |
| 8699 | case 'r': |
| 8700 | if (PyUnicode_Check(v) && c == 's') { |
| 8701 | temp = v; |
| 8702 | Py_INCREF(temp); |
| 8703 | } |
| 8704 | else { |
| 8705 | PyObject *unicode; |
| 8706 | if (c == 's') |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8707 | temp = PyObject_Unicode(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8708 | else |
| 8709 | temp = PyObject_Repr(v); |
| 8710 | if (temp == NULL) |
| 8711 | goto onError; |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8712 | if (PyUnicode_Check(temp)) |
| 8713 | /* nothing to do */; |
| 8714 | else if (PyString_Check(temp)) { |
| 8715 | /* convert to string to Unicode */ |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8716 | unicode = PyUnicode_Decode(PyString_AS_STRING(temp), |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8717 | PyString_GET_SIZE(temp), |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8718 | NULL, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8719 | "strict"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8720 | Py_DECREF(temp); |
| 8721 | temp = unicode; |
| 8722 | if (temp == NULL) |
| 8723 | goto onError; |
| 8724 | } |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8725 | else { |
| 8726 | Py_DECREF(temp); |
| 8727 | PyErr_SetString(PyExc_TypeError, |
| 8728 | "%s argument has non-string str()"); |
| 8729 | goto onError; |
| 8730 | } |
| 8731 | } |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8732 | pbuf = PyUnicode_AS_UNICODE(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8733 | len = PyUnicode_GET_SIZE(temp); |
| 8734 | if (prec >= 0 && len > prec) |
| 8735 | len = prec; |
| 8736 | break; |
| 8737 | |
| 8738 | case 'i': |
| 8739 | case 'd': |
| 8740 | case 'u': |
| 8741 | case 'o': |
| 8742 | case 'x': |
| 8743 | case 'X': |
| 8744 | if (c == 'i') |
| 8745 | c = 'd'; |
Tim Peters | a3a3a03 | 2000-11-30 05:22:44 +0000 | [diff] [blame] | 8746 | if (PyLong_Check(v)) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8747 | temp = formatlong(v, flags, prec, c); |
| 8748 | if (!temp) |
| 8749 | goto onError; |
| 8750 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 8751 | len = PyUnicode_GET_SIZE(temp); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8752 | sign = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8753 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8754 | else { |
| 8755 | pbuf = formatbuf; |
| 8756 | len = formatint(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 8757 | flags, prec, c, v); |
| 8758 | if (len < 0) |
| 8759 | goto onError; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8760 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8761 | } |
| 8762 | if (flags & F_ZERO) |
| 8763 | fill = '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8764 | break; |
| 8765 | |
| 8766 | case 'e': |
| 8767 | case 'E': |
| 8768 | case 'f': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 8769 | case 'F': |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8770 | case 'g': |
| 8771 | case 'G': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 8772 | if (c == 'F') |
| 8773 | c = 'f'; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8774 | pbuf = formatbuf; |
| 8775 | len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 8776 | flags, prec, c, v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8777 | if (len < 0) |
| 8778 | goto onError; |
| 8779 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8780 | if (flags & F_ZERO) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8781 | fill = '0'; |
| 8782 | break; |
| 8783 | |
| 8784 | case 'c': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8785 | pbuf = formatbuf; |
| 8786 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8787 | if (len < 0) |
| 8788 | goto onError; |
| 8789 | break; |
| 8790 | |
| 8791 | default: |
| 8792 | PyErr_Format(PyExc_ValueError, |
Andrew M. Kuchling | 6ca8917 | 2000-12-15 13:07:46 +0000 | [diff] [blame] | 8793 | "unsupported format character '%c' (0x%x) " |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 8794 | "at index %zd", |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8795 | (31<=c && c<=126) ? (char)c : '?', |
Marc-André Lemburg | 24e53b6 | 2002-09-24 09:32:14 +0000 | [diff] [blame] | 8796 | (int)c, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 8797 | (Py_ssize_t)(fmt - 1 - |
| 8798 | PyUnicode_AS_UNICODE(uformat))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8799 | goto onError; |
| 8800 | } |
| 8801 | if (sign) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8802 | if (*pbuf == '-' || *pbuf == '+') { |
| 8803 | sign = *pbuf++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8804 | len--; |
| 8805 | } |
| 8806 | else if (flags & F_SIGN) |
| 8807 | sign = '+'; |
| 8808 | else if (flags & F_BLANK) |
| 8809 | sign = ' '; |
| 8810 | else |
| 8811 | sign = 0; |
| 8812 | } |
| 8813 | if (width < len) |
| 8814 | width = len; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8815 | if (rescnt - (sign != 0) < width) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8816 | reslen -= rescnt; |
| 8817 | rescnt = width + fmtcnt + 100; |
| 8818 | reslen += rescnt; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8819 | if (reslen < 0) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 8820 | Py_XDECREF(temp); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8821 | PyErr_NoMemory(); |
| 8822 | goto onError; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8823 | } |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8824 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 8825 | Py_XDECREF(temp); |
| 8826 | goto onError; |
| 8827 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8828 | res = PyUnicode_AS_UNICODE(result) |
| 8829 | + reslen - rescnt; |
| 8830 | } |
| 8831 | if (sign) { |
| 8832 | if (fill != ' ') |
| 8833 | *res++ = sign; |
| 8834 | rescnt--; |
| 8835 | if (width > len) |
| 8836 | width--; |
| 8837 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 8838 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8839 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8840 | assert(pbuf[1] == c); |
| 8841 | if (fill != ' ') { |
| 8842 | *res++ = *pbuf++; |
| 8843 | *res++ = *pbuf++; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8844 | } |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8845 | rescnt -= 2; |
| 8846 | width -= 2; |
| 8847 | if (width < 0) |
| 8848 | width = 0; |
| 8849 | len -= 2; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8850 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8851 | if (width > len && !(flags & F_LJUST)) { |
| 8852 | do { |
| 8853 | --rescnt; |
| 8854 | *res++ = fill; |
| 8855 | } while (--width > len); |
| 8856 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8857 | if (fill == ' ') { |
| 8858 | if (sign) |
| 8859 | *res++ = sign; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 8860 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8861 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8862 | assert(pbuf[1] == c); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8863 | *res++ = *pbuf++; |
| 8864 | *res++ = *pbuf++; |
| 8865 | } |
| 8866 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8867 | Py_UNICODE_COPY(res, pbuf, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8868 | res += len; |
| 8869 | rescnt -= len; |
| 8870 | while (--width >= len) { |
| 8871 | --rescnt; |
| 8872 | *res++ = ' '; |
| 8873 | } |
| 8874 | if (dict && (argidx < arglen) && c != '%') { |
| 8875 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 8876 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8877 | Py_XDECREF(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8878 | goto onError; |
| 8879 | } |
| 8880 | Py_XDECREF(temp); |
| 8881 | } /* '%' */ |
| 8882 | } /* until end */ |
| 8883 | if (argidx < arglen && !dict) { |
| 8884 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 8885 | "not all arguments converted during string formatting"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8886 | goto onError; |
| 8887 | } |
| 8888 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8889 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
| 8890 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8891 | if (args_owned) { |
| 8892 | Py_DECREF(args); |
| 8893 | } |
| 8894 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8895 | return (PyObject *)result; |
| 8896 | |
| 8897 | onError: |
| 8898 | Py_XDECREF(result); |
| 8899 | Py_DECREF(uformat); |
| 8900 | if (args_owned) { |
| 8901 | Py_DECREF(args); |
| 8902 | } |
| 8903 | return NULL; |
| 8904 | } |
| 8905 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 8906 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8907 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 8908 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8909 | static PyObject * |
| 8910 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 8911 | { |
| 8912 | PyObject *x = NULL; |
Guido van Rossum | 55b4a7b | 2007-07-11 09:28:11 +0000 | [diff] [blame] | 8913 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8914 | char *encoding = NULL; |
| 8915 | char *errors = NULL; |
| 8916 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8917 | if (type != &PyUnicode_Type) |
| 8918 | return unicode_subtype_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8919 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:unicode", |
| 8920 | kwlist, &x, &encoding, &errors)) |
| 8921 | return NULL; |
| 8922 | if (x == NULL) |
| 8923 | return (PyObject *)_PyUnicode_New(0); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 8924 | if (encoding == NULL && errors == NULL) |
| 8925 | return PyObject_Unicode(x); |
| 8926 | else |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8927 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
| 8928 | } |
| 8929 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8930 | static PyObject * |
| 8931 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 8932 | { |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8933 | PyUnicodeObject *tmp, *pnew; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8934 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8935 | |
| 8936 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 8937 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 8938 | if (tmp == NULL) |
| 8939 | return NULL; |
| 8940 | assert(PyUnicode_Check(tmp)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8941 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 8942 | if (pnew == NULL) { |
| 8943 | Py_DECREF(tmp); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8944 | return NULL; |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 8945 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8946 | pnew->str = PyMem_NEW(Py_UNICODE, n+1); |
| 8947 | if (pnew->str == NULL) { |
| 8948 | _Py_ForgetReference((PyObject *)pnew); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 8949 | PyObject_Del(pnew); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 8950 | Py_DECREF(tmp); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 8951 | return PyErr_NoMemory(); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8952 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8953 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 8954 | pnew->length = n; |
| 8955 | pnew->hash = tmp->hash; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8956 | Py_DECREF(tmp); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8957 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8958 | } |
| 8959 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8960 | PyDoc_STRVAR(unicode_doc, |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 8961 | "str(string [, encoding[, errors]]) -> object\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8962 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 8963 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 8964 | encoding defaults to the current default string encoding.\n\ |
| 8965 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8966 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 8967 | static PyObject *unicode_iter(PyObject *seq); |
| 8968 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8969 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 8970 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | 84fc66d | 2007-05-03 17:18:26 +0000 | [diff] [blame] | 8971 | "str", /* tp_name */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8972 | sizeof(PyUnicodeObject), /* tp_size */ |
| 8973 | 0, /* tp_itemsize */ |
| 8974 | /* Slots */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 8975 | (destructor)unicode_dealloc, /* tp_dealloc */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8976 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8977 | 0, /* tp_getattr */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8978 | 0, /* tp_setattr */ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 8979 | 0, /* tp_compare */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8980 | unicode_repr, /* tp_repr */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8981 | &unicode_as_number, /* tp_as_number */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8982 | &unicode_as_sequence, /* tp_as_sequence */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8983 | &unicode_as_mapping, /* tp_as_mapping */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8984 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 8985 | 0, /* tp_call*/ |
| 8986 | (reprfunc) unicode_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8987 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 8988 | 0, /* tp_setattro */ |
Alexandre Vassalotti | 70a2371 | 2007-10-14 02:05:51 +0000 | [diff] [blame] | 8989 | 0, /* tp_as_buffer */ |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 8990 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 8991 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8992 | unicode_doc, /* tp_doc */ |
| 8993 | 0, /* tp_traverse */ |
| 8994 | 0, /* tp_clear */ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 8995 | PyUnicode_RichCompare, /* tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8996 | 0, /* tp_weaklistoffset */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 8997 | unicode_iter, /* tp_iter */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8998 | 0, /* tp_iternext */ |
| 8999 | unicode_methods, /* tp_methods */ |
| 9000 | 0, /* tp_members */ |
| 9001 | 0, /* tp_getset */ |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 9002 | &PyBaseObject_Type, /* tp_base */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9003 | 0, /* tp_dict */ |
| 9004 | 0, /* tp_descr_get */ |
| 9005 | 0, /* tp_descr_set */ |
| 9006 | 0, /* tp_dictoffset */ |
| 9007 | 0, /* tp_init */ |
| 9008 | 0, /* tp_alloc */ |
| 9009 | unicode_new, /* tp_new */ |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 9010 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9011 | }; |
| 9012 | |
| 9013 | /* Initialize the Unicode implementation */ |
| 9014 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9015 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9016 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9017 | int i; |
| 9018 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9019 | /* XXX - move this array to unicodectype.c ? */ |
| 9020 | Py_UNICODE linebreak[] = { |
| 9021 | 0x000A, /* LINE FEED */ |
| 9022 | 0x000D, /* CARRIAGE RETURN */ |
| 9023 | 0x001C, /* FILE SEPARATOR */ |
| 9024 | 0x001D, /* GROUP SEPARATOR */ |
| 9025 | 0x001E, /* RECORD SEPARATOR */ |
| 9026 | 0x0085, /* NEXT LINE */ |
| 9027 | 0x2028, /* LINE SEPARATOR */ |
| 9028 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 9029 | }; |
| 9030 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 9031 | /* Init the implementation */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 9032 | unicode_freelist = NULL; |
| 9033 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9034 | unicode_empty = _PyUnicode_New(0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9035 | if (!unicode_empty) |
| 9036 | return; |
| 9037 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9038 | for (i = 0; i < 256; i++) |
| 9039 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 9040 | if (PyType_Ready(&PyUnicode_Type) < 0) |
| 9041 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9042 | |
| 9043 | /* initialize the linebreak bloom filter */ |
| 9044 | bloom_linebreak = make_bloom_mask( |
| 9045 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 9046 | ); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9047 | |
| 9048 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9049 | } |
| 9050 | |
| 9051 | /* Finalize the Unicode implementation */ |
| 9052 | |
| 9053 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9054 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9055 | { |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 9056 | PyUnicodeObject *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9057 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9058 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 9059 | Py_XDECREF(unicode_empty); |
| 9060 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 9061 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9062 | for (i = 0; i < 256; i++) { |
| 9063 | if (unicode_latin1[i]) { |
| 9064 | Py_DECREF(unicode_latin1[i]); |
| 9065 | unicode_latin1[i] = NULL; |
| 9066 | } |
| 9067 | } |
| 9068 | |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 9069 | for (u = unicode_freelist; u != NULL;) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9070 | PyUnicodeObject *v = u; |
| 9071 | u = *(PyUnicodeObject **)u; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 9072 | if (v->str) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 9073 | PyMem_DEL(v->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 9074 | Py_XDECREF(v->defenc); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 9075 | PyObject_Del(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9076 | } |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 9077 | unicode_freelist = NULL; |
| 9078 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9079 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9080 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9081 | void |
| 9082 | PyUnicode_InternInPlace(PyObject **p) |
| 9083 | { |
| 9084 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 9085 | PyObject *t; |
| 9086 | if (s == NULL || !PyUnicode_Check(s)) |
| 9087 | Py_FatalError( |
| 9088 | "PyUnicode_InternInPlace: unicode strings only please!"); |
| 9089 | /* If it's a subclass, we don't really know what putting |
| 9090 | it in the interned dict might do. */ |
| 9091 | if (!PyUnicode_CheckExact(s)) |
| 9092 | return; |
| 9093 | if (PyUnicode_CHECK_INTERNED(s)) |
| 9094 | return; |
| 9095 | if (interned == NULL) { |
| 9096 | interned = PyDict_New(); |
| 9097 | if (interned == NULL) { |
| 9098 | PyErr_Clear(); /* Don't leave an exception */ |
| 9099 | return; |
| 9100 | } |
| 9101 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9102 | /* It might be that the GetItem call fails even |
| 9103 | though the key is present in the dictionary, |
| 9104 | namely when this happens during a stack overflow. */ |
| 9105 | Py_ALLOW_RECURSION |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9106 | t = PyDict_GetItem(interned, (PyObject *)s); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9107 | Py_END_ALLOW_RECURSION |
| 9108 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9109 | if (t) { |
| 9110 | Py_INCREF(t); |
| 9111 | Py_DECREF(*p); |
| 9112 | *p = t; |
| 9113 | return; |
| 9114 | } |
| 9115 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9116 | PyThreadState_GET()->recursion_critical = 1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9117 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 9118 | PyErr_Clear(); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9119 | PyThreadState_GET()->recursion_critical = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9120 | return; |
| 9121 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9122 | PyThreadState_GET()->recursion_critical = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9123 | /* The two references in interned are not counted by refcnt. |
| 9124 | The deallocator will take care of this */ |
Martin v. Löwis | 5d7428b | 2007-07-21 18:47:48 +0000 | [diff] [blame] | 9125 | Py_Refcnt(s) -= 2; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9126 | PyUnicode_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL; |
| 9127 | } |
| 9128 | |
| 9129 | void |
| 9130 | PyUnicode_InternImmortal(PyObject **p) |
| 9131 | { |
| 9132 | PyUnicode_InternInPlace(p); |
| 9133 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
| 9134 | PyUnicode_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL; |
| 9135 | Py_INCREF(*p); |
| 9136 | } |
| 9137 | } |
| 9138 | |
| 9139 | PyObject * |
| 9140 | PyUnicode_InternFromString(const char *cp) |
| 9141 | { |
| 9142 | PyObject *s = PyUnicode_FromString(cp); |
| 9143 | if (s == NULL) |
| 9144 | return NULL; |
| 9145 | PyUnicode_InternInPlace(&s); |
| 9146 | return s; |
| 9147 | } |
| 9148 | |
| 9149 | void _Py_ReleaseInternedUnicodeStrings(void) |
| 9150 | { |
| 9151 | PyObject *keys; |
| 9152 | PyUnicodeObject *s; |
| 9153 | Py_ssize_t i, n; |
| 9154 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
| 9155 | |
| 9156 | if (interned == NULL || !PyDict_Check(interned)) |
| 9157 | return; |
| 9158 | keys = PyDict_Keys(interned); |
| 9159 | if (keys == NULL || !PyList_Check(keys)) { |
| 9160 | PyErr_Clear(); |
| 9161 | return; |
| 9162 | } |
| 9163 | |
| 9164 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 9165 | detector, interned unicode strings are not forcibly deallocated; |
| 9166 | rather, we give them their stolen references back, and then clear |
| 9167 | and DECREF the interned dict. */ |
| 9168 | |
| 9169 | n = PyList_GET_SIZE(keys); |
| 9170 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
| 9171 | n); |
| 9172 | for (i = 0; i < n; i++) { |
| 9173 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
| 9174 | switch (s->state) { |
| 9175 | case SSTATE_NOT_INTERNED: |
| 9176 | /* XXX Shouldn't happen */ |
| 9177 | break; |
| 9178 | case SSTATE_INTERNED_IMMORTAL: |
Martin v. Löwis | 5d7428b | 2007-07-21 18:47:48 +0000 | [diff] [blame] | 9179 | Py_Refcnt(s) += 1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9180 | immortal_size += s->length; |
| 9181 | break; |
| 9182 | case SSTATE_INTERNED_MORTAL: |
Martin v. Löwis | 5d7428b | 2007-07-21 18:47:48 +0000 | [diff] [blame] | 9183 | Py_Refcnt(s) += 2; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9184 | mortal_size += s->length; |
| 9185 | break; |
| 9186 | default: |
| 9187 | Py_FatalError("Inconsistent interned string state."); |
| 9188 | } |
| 9189 | s->state = SSTATE_NOT_INTERNED; |
| 9190 | } |
| 9191 | fprintf(stderr, "total size of all interned strings: " |
| 9192 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 9193 | "mortal/immortal\n", mortal_size, immortal_size); |
| 9194 | Py_DECREF(keys); |
| 9195 | PyDict_Clear(interned); |
| 9196 | Py_DECREF(interned); |
| 9197 | interned = NULL; |
| 9198 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9199 | |
| 9200 | |
| 9201 | /********************* Unicode Iterator **************************/ |
| 9202 | |
| 9203 | typedef struct { |
| 9204 | PyObject_HEAD |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9205 | Py_ssize_t it_index; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9206 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
| 9207 | } unicodeiterobject; |
| 9208 | |
| 9209 | static void |
| 9210 | unicodeiter_dealloc(unicodeiterobject *it) |
| 9211 | { |
| 9212 | _PyObject_GC_UNTRACK(it); |
| 9213 | Py_XDECREF(it->it_seq); |
| 9214 | PyObject_GC_Del(it); |
| 9215 | } |
| 9216 | |
| 9217 | static int |
| 9218 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 9219 | { |
| 9220 | Py_VISIT(it->it_seq); |
| 9221 | return 0; |
| 9222 | } |
| 9223 | |
| 9224 | static PyObject * |
| 9225 | unicodeiter_next(unicodeiterobject *it) |
| 9226 | { |
| 9227 | PyUnicodeObject *seq; |
| 9228 | PyObject *item; |
| 9229 | |
| 9230 | assert(it != NULL); |
| 9231 | seq = it->it_seq; |
| 9232 | if (seq == NULL) |
| 9233 | return NULL; |
| 9234 | assert(PyUnicode_Check(seq)); |
| 9235 | |
| 9236 | if (it->it_index < PyUnicode_GET_SIZE(seq)) { |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9237 | item = PyUnicode_FromUnicode( |
| 9238 | PyUnicode_AS_UNICODE(seq)+it->it_index, 1); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9239 | if (item != NULL) |
| 9240 | ++it->it_index; |
| 9241 | return item; |
| 9242 | } |
| 9243 | |
| 9244 | Py_DECREF(seq); |
| 9245 | it->it_seq = NULL; |
| 9246 | return NULL; |
| 9247 | } |
| 9248 | |
| 9249 | static PyObject * |
| 9250 | unicodeiter_len(unicodeiterobject *it) |
| 9251 | { |
| 9252 | Py_ssize_t len = 0; |
| 9253 | if (it->it_seq) |
| 9254 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
| 9255 | return PyInt_FromSsize_t(len); |
| 9256 | } |
| 9257 | |
| 9258 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 9259 | |
| 9260 | static PyMethodDef unicodeiter_methods[] = { |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9261 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
| 9262 | length_hint_doc}, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9263 | {NULL, NULL} /* sentinel */ |
| 9264 | }; |
| 9265 | |
| 9266 | PyTypeObject PyUnicodeIter_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 9267 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9268 | "unicodeiterator", /* tp_name */ |
| 9269 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 9270 | 0, /* tp_itemsize */ |
| 9271 | /* methods */ |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9272 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9273 | 0, /* tp_print */ |
| 9274 | 0, /* tp_getattr */ |
| 9275 | 0, /* tp_setattr */ |
| 9276 | 0, /* tp_compare */ |
| 9277 | 0, /* tp_repr */ |
| 9278 | 0, /* tp_as_number */ |
| 9279 | 0, /* tp_as_sequence */ |
| 9280 | 0, /* tp_as_mapping */ |
| 9281 | 0, /* tp_hash */ |
| 9282 | 0, /* tp_call */ |
| 9283 | 0, /* tp_str */ |
| 9284 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9285 | 0, /* tp_setattro */ |
| 9286 | 0, /* tp_as_buffer */ |
| 9287 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 9288 | 0, /* tp_doc */ |
| 9289 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 9290 | 0, /* tp_clear */ |
| 9291 | 0, /* tp_richcompare */ |
| 9292 | 0, /* tp_weaklistoffset */ |
| 9293 | PyObject_SelfIter, /* tp_iter */ |
| 9294 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 9295 | unicodeiter_methods, /* tp_methods */ |
| 9296 | 0, |
| 9297 | }; |
| 9298 | |
| 9299 | static PyObject * |
| 9300 | unicode_iter(PyObject *seq) |
| 9301 | { |
| 9302 | unicodeiterobject *it; |
| 9303 | |
| 9304 | if (!PyUnicode_Check(seq)) { |
| 9305 | PyErr_BadInternalCall(); |
| 9306 | return NULL; |
| 9307 | } |
| 9308 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 9309 | if (it == NULL) |
| 9310 | return NULL; |
| 9311 | it->it_index = 0; |
| 9312 | Py_INCREF(seq); |
| 9313 | it->it_seq = (PyUnicodeObject *)seq; |
| 9314 | _PyObject_GC_TRACK(it); |
| 9315 | return (PyObject *)it; |
| 9316 | } |
| 9317 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9318 | size_t |
| 9319 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 9320 | { |
| 9321 | int res = 0; |
| 9322 | while(*u++) |
| 9323 | res++; |
| 9324 | return res; |
| 9325 | } |
| 9326 | |
| 9327 | Py_UNICODE* |
| 9328 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9329 | { |
| 9330 | Py_UNICODE *u = s1; |
| 9331 | while ((*u++ = *s2++)); |
| 9332 | return s1; |
| 9333 | } |
| 9334 | |
| 9335 | Py_UNICODE* |
| 9336 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 9337 | { |
| 9338 | Py_UNICODE *u = s1; |
| 9339 | while ((*u++ = *s2++)) |
| 9340 | if (n-- == 0) |
| 9341 | break; |
| 9342 | return s1; |
| 9343 | } |
| 9344 | |
| 9345 | int |
| 9346 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9347 | { |
| 9348 | while (*s1 && *s2 && *s1 == *s2) |
| 9349 | s1++, s2++; |
| 9350 | if (*s1 && *s2) |
| 9351 | return (*s1 < *s2) ? -1 : +1; |
| 9352 | if (*s1) |
| 9353 | return 1; |
| 9354 | if (*s2) |
| 9355 | return -1; |
| 9356 | return 0; |
| 9357 | } |
| 9358 | |
| 9359 | Py_UNICODE* |
| 9360 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 9361 | { |
| 9362 | const Py_UNICODE *p; |
| 9363 | for (p = s; *p; p++) |
| 9364 | if (*p == c) |
| 9365 | return (Py_UNICODE*)p; |
| 9366 | return NULL; |
| 9367 | } |
| 9368 | |
| 9369 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9370 | #ifdef __cplusplus |
| 9371 | } |
| 9372 | #endif |
| 9373 | |
| 9374 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9375 | /* |
| 9376 | Local variables: |
| 9377 | c-basic-offset: 4 |
| 9378 | indent-tabs-mode: nil |
| 9379 | End: |
| 9380 | */ |