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 |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 104 | count of a string is: s->ob_refcnt + (s->state ? 2 : 0) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 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 |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 575 | * (we call PyObject_Str()/PyObject_Repr() for these objects |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 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 |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 582 | * PyObject_Str()/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); |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 686 | str = PyObject_Str(obj); |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 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 |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 990 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 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 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1001 | PyErr_Format(PyExc_TypeError, |
| 1002 | "Can't convert '%.100s' object to str implicitly", |
| 1003 | Py_Type(obj)->tp_name); |
| 1004 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
| 1008 | const char *encoding, |
| 1009 | const char *errors) |
| 1010 | { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1011 | const char *s = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1012 | Py_ssize_t len; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1013 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1014 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1015 | if (obj == NULL) { |
| 1016 | PyErr_BadInternalCall(); |
| 1017 | return NULL; |
| 1018 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1019 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1020 | if (PyUnicode_Check(obj)) { |
| 1021 | PyErr_SetString(PyExc_TypeError, |
| 1022 | "decoding Unicode is not supported"); |
| 1023 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1024 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1025 | |
| 1026 | /* Coerce object */ |
| 1027 | if (PyString_Check(obj)) { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1028 | s = PyString_AS_STRING(obj); |
| 1029 | len = PyString_GET_SIZE(obj); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1030 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1031 | else if (PyObject_AsCharBuffer(obj, &s, &len)) { |
| 1032 | /* Overwrite the error message with something more useful in |
| 1033 | case of a TypeError. */ |
| 1034 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1035 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1036 | "coercing to Unicode: need string or buffer, " |
| 1037 | "%.80s found", |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1038 | Py_Type(obj)->tp_name); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1039 | goto onError; |
| 1040 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1041 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1042 | /* Convert to Unicode */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1043 | if (len == 0) { |
| 1044 | Py_INCREF(unicode_empty); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1045 | v = (PyObject *)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1046 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1047 | else |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1048 | v = PyUnicode_Decode(s, len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 1049 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1050 | return v; |
| 1051 | |
| 1052 | onError: |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1053 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | PyObject *PyUnicode_Decode(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1057 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1058 | const char *encoding, |
| 1059 | const char *errors) |
| 1060 | { |
| 1061 | PyObject *buffer = NULL, *unicode; |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1062 | Py_buffer info; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1063 | char lower[20]; /* Enough for any encoding name we recognize */ |
| 1064 | char *l; |
| 1065 | const char *e; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1066 | |
| 1067 | if (encoding == NULL) |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1068 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1069 | |
| 1070 | /* Convert encoding to lower case and replace '_' with '-' in order to |
| 1071 | catch e.g. UTF_8 */ |
| 1072 | e = encoding; |
| 1073 | l = lower; |
| 1074 | while (*e && l < &lower[(sizeof lower) - 2]) { |
| 1075 | if (ISUPPER(*e)) { |
| 1076 | *l++ = TOLOWER(*e++); |
| 1077 | } |
| 1078 | else if (*e == '_') { |
| 1079 | *l++ = '-'; |
| 1080 | e++; |
| 1081 | } |
| 1082 | else { |
| 1083 | *l++ = *e++; |
| 1084 | } |
| 1085 | } |
| 1086 | *l = '\0'; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1087 | |
| 1088 | /* Shortcuts for common default encodings */ |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1089 | if (strcmp(lower, "utf-8") == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1090 | return PyUnicode_DecodeUTF8(s, size, errors); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1091 | else if ((strcmp(lower, "latin-1") == 0) || |
| 1092 | (strcmp(lower, "iso-8859-1") == 0)) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1093 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1094 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1095 | else if (strcmp(lower, "mbcs") == 0) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1096 | return PyUnicode_DecodeMBCS(s, size, errors); |
| 1097 | #endif |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1098 | else if (strcmp(lower, "ascii") == 0) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1099 | return PyUnicode_DecodeASCII(s, size, errors); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1100 | else if (strcmp(lower, "utf-16") == 0) |
| 1101 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 1102 | else if (strcmp(lower, "utf-32") == 0) |
| 1103 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1104 | |
| 1105 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1106 | buffer = NULL; |
| 1107 | if (PyBuffer_FillInfo(&info, (void *)s, size, 1, PyBUF_SIMPLE) < 0) |
| 1108 | goto onError; |
| 1109 | buffer = PyMemoryView_FromMemory(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1110 | if (buffer == NULL) |
| 1111 | goto onError; |
| 1112 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 1113 | if (unicode == NULL) |
| 1114 | goto onError; |
| 1115 | if (!PyUnicode_Check(unicode)) { |
| 1116 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 1117 | "decoder did not return an unicode object (type=%.400s)", |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1118 | Py_Type(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1119 | Py_DECREF(unicode); |
| 1120 | goto onError; |
| 1121 | } |
| 1122 | Py_DECREF(buffer); |
| 1123 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1124 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1125 | onError: |
| 1126 | Py_XDECREF(buffer); |
| 1127 | return NULL; |
| 1128 | } |
| 1129 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1130 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 1131 | const char *encoding, |
| 1132 | const char *errors) |
| 1133 | { |
| 1134 | PyObject *v; |
| 1135 | |
| 1136 | if (!PyUnicode_Check(unicode)) { |
| 1137 | PyErr_BadArgument(); |
| 1138 | goto onError; |
| 1139 | } |
| 1140 | |
| 1141 | if (encoding == NULL) |
| 1142 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1143 | |
| 1144 | /* Decode via the codec registry */ |
| 1145 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1146 | if (v == NULL) |
| 1147 | goto onError; |
| 1148 | return v; |
| 1149 | |
| 1150 | onError: |
| 1151 | return NULL; |
| 1152 | } |
| 1153 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1154 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1155 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1156 | const char *encoding, |
| 1157 | const char *errors) |
| 1158 | { |
| 1159 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1160 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1161 | unicode = PyUnicode_FromUnicode(s, size); |
| 1162 | if (unicode == NULL) |
| 1163 | return NULL; |
| 1164 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 1165 | Py_DECREF(unicode); |
| 1166 | return v; |
| 1167 | } |
| 1168 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1169 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 1170 | const char *encoding, |
| 1171 | const char *errors) |
| 1172 | { |
| 1173 | PyObject *v; |
| 1174 | |
| 1175 | if (!PyUnicode_Check(unicode)) { |
| 1176 | PyErr_BadArgument(); |
| 1177 | goto onError; |
| 1178 | } |
| 1179 | |
| 1180 | if (encoding == NULL) |
| 1181 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1182 | |
| 1183 | /* Encode via the codec registry */ |
| 1184 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1185 | if (v == NULL) |
| 1186 | goto onError; |
| 1187 | return v; |
| 1188 | |
| 1189 | onError: |
| 1190 | return NULL; |
| 1191 | } |
| 1192 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1193 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 1194 | const char *encoding, |
| 1195 | const char *errors) |
| 1196 | { |
| 1197 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1198 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1199 | if (!PyUnicode_Check(unicode)) { |
| 1200 | PyErr_BadArgument(); |
| 1201 | goto onError; |
| 1202 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1203 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1204 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1205 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1206 | |
| 1207 | /* Shortcuts for common default encodings */ |
| 1208 | if (errors == NULL) { |
| 1209 | if (strcmp(encoding, "utf-8") == 0) |
Jeremy Hylton | 9cea41c | 2001-05-29 17:13:15 +0000 | [diff] [blame] | 1210 | return PyUnicode_AsUTF8String(unicode); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1211 | else if (strcmp(encoding, "latin-1") == 0) |
| 1212 | return PyUnicode_AsLatin1String(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1213 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 1214 | else if (strcmp(encoding, "mbcs") == 0) |
| 1215 | return PyUnicode_AsMBCSString(unicode); |
| 1216 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1217 | else if (strcmp(encoding, "ascii") == 0) |
| 1218 | return PyUnicode_AsASCIIString(unicode); |
| 1219 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1220 | |
| 1221 | /* Encode via the codec registry */ |
| 1222 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1223 | if (v == NULL) |
| 1224 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1225 | assert(PyString_Check(v)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1226 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1227 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1228 | onError: |
| 1229 | return NULL; |
| 1230 | } |
| 1231 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1232 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
| 1233 | const char *errors) |
| 1234 | { |
| 1235 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1236 | if (v) |
| 1237 | return v; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1238 | if (errors != NULL) |
| 1239 | Py_FatalError("non-NULL encoding in _PyUnicode_AsDefaultEncodedString"); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1240 | v = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Guido van Rossum | 0661009 | 2007-08-16 21:02:22 +0000 | [diff] [blame] | 1241 | PyUnicode_GET_SIZE(unicode), |
| 1242 | NULL); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1243 | if (!v) |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1244 | return NULL; |
Guido van Rossum | e7a0d39 | 2007-07-12 07:53:00 +0000 | [diff] [blame] | 1245 | ((PyUnicodeObject *)unicode)->defenc = v; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1246 | return v; |
| 1247 | } |
| 1248 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1249 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1250 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1251 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1252 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 1253 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1254 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1255 | PyObject* |
| 1256 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 1257 | { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1258 | /* During the early bootstrapping process, Py_FileSystemDefaultEncoding |
| 1259 | can be undefined. If it is case, decode using UTF-8. The following assumes |
| 1260 | that Py_FileSystemDefaultEncoding is set to a built-in encoding during the |
| 1261 | bootstrapping process where the codecs aren't ready yet. |
| 1262 | */ |
| 1263 | if (Py_FileSystemDefaultEncoding) { |
| 1264 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1265 | if (strcmp(Py_FileSystemDefaultEncoding, "mbcs") == 0) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1266 | return PyUnicode_DecodeMBCS(s, size, "replace"); |
| 1267 | } |
| 1268 | #elif defined(__APPLE__) |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1269 | if (strcmp(Py_FileSystemDefaultEncoding, "utf-8") == 0) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1270 | return PyUnicode_DecodeUTF8(s, size, "replace"); |
| 1271 | } |
| 1272 | #endif |
| 1273 | return PyUnicode_Decode(s, size, |
| 1274 | Py_FileSystemDefaultEncoding, |
| 1275 | "replace"); |
| 1276 | } |
| 1277 | else { |
| 1278 | return PyUnicode_DecodeUTF8(s, size, "replace"); |
| 1279 | } |
| 1280 | } |
| 1281 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1282 | char* |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1283 | PyUnicode_AsStringAndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1284 | { |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1285 | PyObject *str8; |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 1286 | if (!PyUnicode_Check(unicode)) { |
| 1287 | PyErr_BadArgument(); |
| 1288 | return NULL; |
| 1289 | } |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1290 | str8 = _PyUnicode_AsDefaultEncodedString(unicode, NULL); |
| 1291 | if (str8 == NULL) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1292 | return NULL; |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1293 | if (psize != NULL) |
| 1294 | *psize = PyString_GET_SIZE(str8); |
| 1295 | return PyString_AS_STRING(str8); |
| 1296 | } |
| 1297 | |
| 1298 | char* |
| 1299 | PyUnicode_AsString(PyObject *unicode) |
| 1300 | { |
| 1301 | return PyUnicode_AsStringAndSize(unicode, NULL); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1304 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 1305 | { |
| 1306 | if (!PyUnicode_Check(unicode)) { |
| 1307 | PyErr_BadArgument(); |
| 1308 | goto onError; |
| 1309 | } |
| 1310 | return PyUnicode_AS_UNICODE(unicode); |
| 1311 | |
| 1312 | onError: |
| 1313 | return NULL; |
| 1314 | } |
| 1315 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1316 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1317 | { |
| 1318 | if (!PyUnicode_Check(unicode)) { |
| 1319 | PyErr_BadArgument(); |
| 1320 | goto onError; |
| 1321 | } |
| 1322 | return PyUnicode_GET_SIZE(unicode); |
| 1323 | |
| 1324 | onError: |
| 1325 | return -1; |
| 1326 | } |
| 1327 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 1328 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1329 | { |
| 1330 | return unicode_default_encoding; |
| 1331 | } |
| 1332 | |
| 1333 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 1334 | { |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1335 | if (strcmp(encoding, unicode_default_encoding) != 0) { |
| 1336 | PyErr_Format(PyExc_ValueError, |
| 1337 | "Can only set default encoding to %s", |
| 1338 | unicode_default_encoding); |
| 1339 | return -1; |
| 1340 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1341 | return 0; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1344 | /* error handling callback helper: |
| 1345 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 1346 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1347 | and adjust various state variables. |
| 1348 | return 0 on success, -1 on error |
| 1349 | */ |
| 1350 | |
| 1351 | static |
| 1352 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
| 1353 | const char *encoding, const char *reason, |
Walter Dörwald | a651d3d | 2007-08-30 15:29:21 +0000 | [diff] [blame] | 1354 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 1355 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1356 | PyObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1357 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1358 | 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] | 1359 | |
| 1360 | PyObject *restuple = NULL; |
| 1361 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1362 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1363 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1364 | Py_ssize_t requiredsize; |
| 1365 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1366 | Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1367 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1368 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1369 | int res = -1; |
| 1370 | |
| 1371 | if (*errorHandler == NULL) { |
| 1372 | *errorHandler = PyCodec_LookupError(errors); |
| 1373 | if (*errorHandler == NULL) |
| 1374 | goto onError; |
| 1375 | } |
| 1376 | |
| 1377 | if (*exceptionObject == NULL) { |
| 1378 | *exceptionObject = PyUnicodeDecodeError_Create( |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1379 | encoding, *input, *inend-*input, *startinpos, *endinpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1380 | if (*exceptionObject == NULL) |
| 1381 | goto onError; |
| 1382 | } |
| 1383 | else { |
| 1384 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 1385 | goto onError; |
| 1386 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
| 1387 | goto onError; |
| 1388 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 1389 | goto onError; |
| 1390 | } |
| 1391 | |
| 1392 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 1393 | if (restuple == NULL) |
| 1394 | goto onError; |
| 1395 | if (!PyTuple_Check(restuple)) { |
| 1396 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 1397 | goto onError; |
| 1398 | } |
| 1399 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
| 1400 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1401 | |
| 1402 | /* Copy back the bytes variables, which might have been modified by the |
| 1403 | callback */ |
| 1404 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 1405 | if (!inputobj) |
| 1406 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1407 | if (!PyString_Check(inputobj)) { |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1408 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
| 1409 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1410 | *input = PyString_AS_STRING(inputobj); |
| 1411 | insize = PyString_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1412 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 1413 | /* we can DECREF safely, as the exception has another reference, |
| 1414 | so the object won't go away. */ |
| 1415 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1416 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1417 | if (newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1418 | newpos = insize+newpos; |
| 1419 | if (newpos<0 || newpos>insize) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 1420 | 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] | 1421 | goto onError; |
| 1422 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1423 | |
| 1424 | /* need more space? (at least enough for what we |
| 1425 | have+the replacement+the rest of the string (starting |
| 1426 | at the new input position), so we won't have to check space |
| 1427 | when there are no errors in the rest of the string) */ |
| 1428 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 1429 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 1430 | requiredsize = *outpos + repsize + insize-newpos; |
| 1431 | if (requiredsize > outsize) { |
| 1432 | if (requiredsize<2*outsize) |
| 1433 | requiredsize = 2*outsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1434 | if (PyUnicode_Resize(output, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1435 | goto onError; |
| 1436 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
| 1437 | } |
| 1438 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1439 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1440 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 1441 | *outptr += repsize; |
| 1442 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1443 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1444 | /* we made it! */ |
| 1445 | res = 0; |
| 1446 | |
| 1447 | onError: |
| 1448 | Py_XDECREF(restuple); |
| 1449 | return res; |
| 1450 | } |
| 1451 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1452 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 1453 | |
| 1454 | /* see RFC2152 for details */ |
| 1455 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1456 | static |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1457 | char utf7_special[128] = { |
| 1458 | /* indicate whether a UTF-7 character is special i.e. cannot be directly |
| 1459 | encoded: |
| 1460 | 0 - not special |
| 1461 | 1 - special |
| 1462 | 2 - whitespace (optional) |
| 1463 | 3 - RFC2152 Set O (optional) */ |
| 1464 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, |
| 1465 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1466 | 2, 3, 3, 3, 3, 3, 3, 0, 0, 0, 3, 1, 0, 0, 0, 1, |
| 1467 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, |
| 1468 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1469 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, |
| 1470 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1471 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 1, 1, |
| 1472 | |
| 1473 | }; |
| 1474 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1475 | /* Note: The comparison (c) <= 0 is a trick to work-around gcc |
| 1476 | warnings about the comparison always being false; since |
| 1477 | utf7_special[0] is 1, we can safely make that one comparison |
| 1478 | true */ |
| 1479 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1480 | #define SPECIAL(c, encodeO, encodeWS) \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1481 | ((c) > 127 || (c) <= 0 || utf7_special[(c)] == 1 || \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1482 | (encodeWS && (utf7_special[(c)] == 2)) || \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1483 | (encodeO && (utf7_special[(c)] == 3))) |
| 1484 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1485 | #define B64(n) \ |
| 1486 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 1487 | #define B64CHAR(c) \ |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1488 | (ISALNUM(c) || (c) == '+' || (c) == '/') |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1489 | #define UB64(c) \ |
| 1490 | ((c) == '+' ? 62 : (c) == '/' ? 63 : (c) >= 'a' ? \ |
| 1491 | (c) - 71 : (c) >= 'A' ? (c) - 65 : (c) + 4 ) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1492 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1493 | #define ENCODE(out, ch, bits) \ |
| 1494 | while (bits >= 6) { \ |
| 1495 | *out++ = B64(ch >> (bits-6)); \ |
| 1496 | bits -= 6; \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1499 | #define DECODE(out, ch, bits, surrogate) \ |
| 1500 | while (bits >= 16) { \ |
| 1501 | Py_UNICODE outCh = (Py_UNICODE) ((ch >> (bits-16)) & 0xffff); \ |
| 1502 | bits -= 16; \ |
| 1503 | if (surrogate) { \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1504 | /* We have already generated an error for the high surrogate \ |
| 1505 | 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] | 1506 | surrogate = 0; \ |
| 1507 | } else if (0xDC00 <= outCh && outCh <= 0xDFFF) { \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1508 | /* This is a surrogate pair. Unfortunately we can't represent \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1509 | it in a 16-bit character */ \ |
| 1510 | surrogate = 1; \ |
| 1511 | errmsg = "code pairs are not supported"; \ |
| 1512 | goto utf7Error; \ |
| 1513 | } else { \ |
| 1514 | *out++ = outCh; \ |
| 1515 | } \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1516 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1517 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1518 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1519 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1520 | const char *errors) |
| 1521 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1522 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 1523 | } |
| 1524 | |
| 1525 | PyObject *PyUnicode_DecodeUTF7Stateful(const char *s, |
| 1526 | Py_ssize_t size, |
| 1527 | const char *errors, |
| 1528 | Py_ssize_t *consumed) |
| 1529 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1530 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1531 | Py_ssize_t startinpos; |
| 1532 | Py_ssize_t endinpos; |
| 1533 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1534 | const char *e; |
| 1535 | PyUnicodeObject *unicode; |
| 1536 | Py_UNICODE *p; |
| 1537 | const char *errmsg = ""; |
| 1538 | int inShift = 0; |
| 1539 | unsigned int bitsleft = 0; |
| 1540 | unsigned long charsleft = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1541 | int surrogate = 0; |
| 1542 | PyObject *errorHandler = NULL; |
| 1543 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1544 | |
| 1545 | unicode = _PyUnicode_New(size); |
| 1546 | if (!unicode) |
| 1547 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1548 | if (size == 0) { |
| 1549 | if (consumed) |
| 1550 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1551 | return (PyObject *)unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1552 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1553 | |
| 1554 | p = unicode->str; |
| 1555 | e = s + size; |
| 1556 | |
| 1557 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1558 | Py_UNICODE ch; |
| 1559 | restart: |
| 1560 | ch = *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1561 | |
| 1562 | if (inShift) { |
| 1563 | if ((ch == '-') || !B64CHAR(ch)) { |
| 1564 | inShift = 0; |
| 1565 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1566 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1567 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1568 | if (bitsleft >= 6) { |
| 1569 | /* The shift sequence has a partial character in it. If |
| 1570 | bitsleft < 6 then we could just classify it as padding |
| 1571 | but that is not the case here */ |
| 1572 | |
| 1573 | errmsg = "partial character in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1574 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1575 | } |
| 1576 | /* According to RFC2152 the remaining bits should be zero. We |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1577 | choose to signal an error/insert a replacement character |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1578 | here so indicate the potential of a misencoded character. */ |
| 1579 | |
| 1580 | /* On x86, a << b == a << (b%32) so make sure that bitsleft != 0 */ |
| 1581 | if (bitsleft && charsleft << (sizeof(charsleft) * 8 - bitsleft)) { |
| 1582 | errmsg = "non-zero padding bits in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1583 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1584 | } |
| 1585 | |
| 1586 | if (ch == '-') { |
| 1587 | if ((s < e) && (*(s) == '-')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1588 | *p++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1589 | inShift = 1; |
| 1590 | } |
| 1591 | } else if (SPECIAL(ch,0,0)) { |
| 1592 | errmsg = "unexpected special character"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1593 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1594 | } else { |
| 1595 | *p++ = ch; |
| 1596 | } |
| 1597 | } else { |
| 1598 | charsleft = (charsleft << 6) | UB64(ch); |
| 1599 | bitsleft += 6; |
| 1600 | s++; |
| 1601 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1602 | } |
| 1603 | } |
| 1604 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1605 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1606 | s++; |
| 1607 | if (s < e && *s == '-') { |
| 1608 | s++; |
| 1609 | *p++ = '+'; |
| 1610 | } else |
| 1611 | { |
| 1612 | inShift = 1; |
| 1613 | bitsleft = 0; |
| 1614 | } |
| 1615 | } |
| 1616 | else if (SPECIAL(ch,0,0)) { |
Walter Dörwald | 2b65c75 | 2007-08-30 15:35:26 +0000 | [diff] [blame] | 1617 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1618 | errmsg = "unexpected special character"; |
| 1619 | s++; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1620 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1621 | } |
| 1622 | else { |
| 1623 | *p++ = ch; |
| 1624 | s++; |
| 1625 | } |
| 1626 | continue; |
| 1627 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1628 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1629 | endinpos = s-starts; |
| 1630 | if (unicode_decode_call_errorhandler( |
| 1631 | errors, &errorHandler, |
| 1632 | "utf7", errmsg, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1633 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1634 | (PyObject **)&unicode, &outpos, &p)) |
| 1635 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1636 | } |
| 1637 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1638 | if (inShift && !consumed) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1639 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1640 | endinpos = size; |
| 1641 | if (unicode_decode_call_errorhandler( |
| 1642 | errors, &errorHandler, |
| 1643 | "utf7", "unterminated shift sequence", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1644 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1645 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1646 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1647 | if (s < e) |
| 1648 | goto restart; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1649 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1650 | if (consumed) { |
| 1651 | if(inShift) |
| 1652 | *consumed = startinpos; |
| 1653 | else |
| 1654 | *consumed = s-starts; |
| 1655 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1656 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1657 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1658 | goto onError; |
| 1659 | |
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 | return (PyObject *)unicode; |
| 1663 | |
| 1664 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1665 | Py_XDECREF(errorHandler); |
| 1666 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1667 | Py_DECREF(unicode); |
| 1668 | return NULL; |
| 1669 | } |
| 1670 | |
| 1671 | |
| 1672 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1673 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1674 | int encodeSetO, |
| 1675 | int encodeWhiteSpace, |
| 1676 | const char *errors) |
| 1677 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1678 | PyObject *v, *result; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1679 | /* It might be possible to tighten this worst case */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1680 | Py_ssize_t cbAllocated = 5 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1681 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1682 | Py_ssize_t i = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1683 | unsigned int bitsleft = 0; |
| 1684 | unsigned long charsleft = 0; |
| 1685 | char * out; |
| 1686 | char * start; |
| 1687 | |
| 1688 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1689 | return PyString_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1690 | |
Walter Dörwald | 51ab414 | 2007-05-05 14:43:36 +0000 | [diff] [blame] | 1691 | v = PyBytes_FromStringAndSize(NULL, cbAllocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1692 | if (v == NULL) |
| 1693 | return NULL; |
| 1694 | |
Walter Dörwald | 51ab414 | 2007-05-05 14:43:36 +0000 | [diff] [blame] | 1695 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1696 | for (;i < size; ++i) { |
| 1697 | Py_UNICODE ch = s[i]; |
| 1698 | |
| 1699 | if (!inShift) { |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1700 | if (ch == '+') { |
| 1701 | *out++ = '+'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1702 | *out++ = '-'; |
| 1703 | } else if (SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1704 | charsleft = ch; |
| 1705 | bitsleft = 16; |
| 1706 | *out++ = '+'; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1707 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1708 | inShift = bitsleft > 0; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1709 | } else { |
| 1710 | *out++ = (char) ch; |
| 1711 | } |
| 1712 | } else { |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1713 | if (!SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1714 | *out++ = B64(charsleft << (6-bitsleft)); |
| 1715 | charsleft = 0; |
| 1716 | bitsleft = 0; |
| 1717 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 1718 | so no '-' is required, except if the character is itself a '-' */ |
| 1719 | if (B64CHAR(ch) || ch == '-') { |
| 1720 | *out++ = '-'; |
| 1721 | } |
| 1722 | inShift = 0; |
| 1723 | *out++ = (char) ch; |
| 1724 | } else { |
| 1725 | bitsleft += 16; |
| 1726 | charsleft = (charsleft << 16) | ch; |
| 1727 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
| 1728 | |
| 1729 | /* If the next character is special then we dont' need to terminate |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1730 | 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] | 1731 | or '-' then the shift sequence will be terminated implicitly and we |
| 1732 | don't have to insert a '-'. */ |
| 1733 | |
| 1734 | if (bitsleft == 0) { |
| 1735 | if (i + 1 < size) { |
| 1736 | Py_UNICODE ch2 = s[i+1]; |
| 1737 | |
| 1738 | if (SPECIAL(ch2, encodeSetO, encodeWhiteSpace)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1739 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1740 | } else if (B64CHAR(ch2) || ch2 == '-') { |
| 1741 | *out++ = '-'; |
| 1742 | inShift = 0; |
| 1743 | } else { |
| 1744 | inShift = 0; |
| 1745 | } |
| 1746 | |
| 1747 | } |
| 1748 | else { |
| 1749 | *out++ = '-'; |
| 1750 | inShift = 0; |
| 1751 | } |
| 1752 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1753 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1754 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1755 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1756 | if (bitsleft) { |
| 1757 | *out++= B64(charsleft << (6-bitsleft) ); |
| 1758 | *out++ = '-'; |
| 1759 | } |
| 1760 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1761 | result = PyString_FromStringAndSize(PyBytes_AS_STRING(v), out - start); |
| 1762 | Py_DECREF(v); |
| 1763 | return result; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1764 | } |
| 1765 | |
| 1766 | #undef SPECIAL |
| 1767 | #undef B64 |
| 1768 | #undef B64CHAR |
| 1769 | #undef UB64 |
| 1770 | #undef ENCODE |
| 1771 | #undef DECODE |
| 1772 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1773 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 1774 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1775 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1776 | char utf8_code_length[256] = { |
| 1777 | /* Map UTF-8 encoded prefix byte to sequence length. zero means |
| 1778 | illegal prefix. see RFC 2279 for details */ |
| 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 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1785 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1786 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1787 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1788 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1789 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1790 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1791 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1792 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1793 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 1794 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 |
| 1795 | }; |
| 1796 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1797 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1798 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1799 | const char *errors) |
| 1800 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1801 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 1802 | } |
| 1803 | |
| 1804 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1805 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1806 | const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1807 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1808 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1809 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1810 | int n; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1811 | Py_ssize_t startinpos; |
| 1812 | Py_ssize_t endinpos; |
| 1813 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1814 | const char *e; |
| 1815 | PyUnicodeObject *unicode; |
| 1816 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1817 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1818 | PyObject *errorHandler = NULL; |
| 1819 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1820 | |
| 1821 | /* Note: size will always be longer than the resulting Unicode |
| 1822 | character count */ |
| 1823 | unicode = _PyUnicode_New(size); |
| 1824 | if (!unicode) |
| 1825 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1826 | if (size == 0) { |
| 1827 | if (consumed) |
| 1828 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1829 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1830 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1831 | |
| 1832 | /* Unpack UTF-8 encoded data */ |
| 1833 | p = unicode->str; |
| 1834 | e = s + size; |
| 1835 | |
| 1836 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1837 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1838 | |
| 1839 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1840 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1841 | s++; |
| 1842 | continue; |
| 1843 | } |
| 1844 | |
| 1845 | n = utf8_code_length[ch]; |
| 1846 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1847 | if (s + n > e) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1848 | if (consumed) |
| 1849 | break; |
| 1850 | else { |
| 1851 | errmsg = "unexpected end of data"; |
| 1852 | startinpos = s-starts; |
| 1853 | endinpos = size; |
| 1854 | goto utf8Error; |
| 1855 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1856 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1857 | |
| 1858 | switch (n) { |
| 1859 | |
| 1860 | case 0: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1861 | errmsg = "unexpected code byte"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1862 | startinpos = s-starts; |
| 1863 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1864 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1865 | |
| 1866 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1867 | errmsg = "internal error"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1868 | startinpos = s-starts; |
| 1869 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1870 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1871 | |
| 1872 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1873 | if ((s[1] & 0xc0) != 0x80) { |
| 1874 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1875 | startinpos = s-starts; |
| 1876 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1877 | goto utf8Error; |
| 1878 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1879 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1880 | if (ch < 0x80) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1881 | startinpos = s-starts; |
| 1882 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1883 | errmsg = "illegal encoding"; |
| 1884 | goto utf8Error; |
| 1885 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1886 | else |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1887 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1888 | break; |
| 1889 | |
| 1890 | case 3: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1891 | if ((s[1] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1892 | (s[2] & 0xc0) != 0x80) { |
| 1893 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1894 | startinpos = s-starts; |
| 1895 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1896 | goto utf8Error; |
| 1897 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1898 | 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] | 1899 | if (ch < 0x0800) { |
| 1900 | /* Note: UTF-8 encodings of surrogates are considered |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1901 | legal UTF-8 sequences; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1902 | |
| 1903 | XXX For wide builds (UCS-4) we should probably try |
| 1904 | to recombine the surrogates into a single code |
| 1905 | unit. |
| 1906 | */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1907 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1908 | startinpos = s-starts; |
| 1909 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1910 | goto utf8Error; |
| 1911 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1912 | else |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1913 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1914 | break; |
| 1915 | |
| 1916 | case 4: |
| 1917 | if ((s[1] & 0xc0) != 0x80 || |
| 1918 | (s[2] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1919 | (s[3] & 0xc0) != 0x80) { |
| 1920 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1921 | startinpos = s-starts; |
| 1922 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1923 | goto utf8Error; |
| 1924 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1925 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 1926 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 1927 | /* validate and convert to UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1928 | if ((ch < 0x10000) /* minimum value allowed for 4 |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1929 | byte encoding */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1930 | || (ch > 0x10ffff)) /* maximum value allowed for |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1931 | UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1932 | { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1933 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1934 | startinpos = s-starts; |
| 1935 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1936 | goto utf8Error; |
| 1937 | } |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1938 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1939 | *p++ = (Py_UNICODE)ch; |
| 1940 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1941 | /* compute and append the two surrogates: */ |
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 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 1944 | ch -= 0x10000; |
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 | /* high surrogate = top 10 bits added to D800 */ |
| 1947 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1948 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1949 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 1950 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1951 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1952 | break; |
| 1953 | |
| 1954 | default: |
| 1955 | /* Other sizes are only needed for UCS-4 */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1956 | errmsg = "unsupported Unicode code range"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1957 | startinpos = s-starts; |
| 1958 | endinpos = startinpos+n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1959 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1960 | } |
| 1961 | s += n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1962 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1963 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1964 | utf8Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1965 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1966 | if (unicode_decode_call_errorhandler( |
| 1967 | errors, &errorHandler, |
| 1968 | "utf8", errmsg, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1969 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1970 | (PyObject **)&unicode, &outpos, &p)) |
| 1971 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1972 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1973 | if (consumed) |
| 1974 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1975 | |
| 1976 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1977 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1978 | goto onError; |
| 1979 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1980 | Py_XDECREF(errorHandler); |
| 1981 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1982 | return (PyObject *)unicode; |
| 1983 | |
| 1984 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1985 | Py_XDECREF(errorHandler); |
| 1986 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1987 | Py_DECREF(unicode); |
| 1988 | return NULL; |
| 1989 | } |
| 1990 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1991 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 1992 | and allocate exactly as much space needed at the end. Else allocate the |
| 1993 | maximum possible needed (4 result bytes per Unicode character), and return |
| 1994 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1995 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1996 | PyObject * |
| 1997 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1998 | Py_ssize_t size, |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1999 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2000 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2001 | #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] | 2002 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2003 | Py_ssize_t i; /* index into s of next input byte */ |
| 2004 | PyObject *result; /* result string object */ |
| 2005 | char *p; /* next free byte in output buffer */ |
| 2006 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 2007 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2008 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2009 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2010 | assert(s != NULL); |
| 2011 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2012 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2013 | if (size <= MAX_SHORT_UNICHARS) { |
| 2014 | /* Write into the stack buffer; nallocated can't overflow. |
| 2015 | * At the end, we'll allocate exactly as much heap space as it |
| 2016 | * turns out we need. |
| 2017 | */ |
| 2018 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2019 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2020 | p = stackbuf; |
| 2021 | } |
| 2022 | else { |
| 2023 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 2024 | nallocated = size * 4; |
| 2025 | if (nallocated / 4 != size) /* overflow! */ |
| 2026 | return PyErr_NoMemory(); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2027 | result = PyString_FromStringAndSize(NULL, nallocated); |
| 2028 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2029 | return NULL; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2030 | p = PyString_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2031 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2032 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2033 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2034 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2035 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2036 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2037 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2038 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2039 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2040 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2041 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 2042 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 2043 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2044 | } |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2045 | else { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2046 | /* Encode UCS2 Unicode ordinals */ |
| 2047 | if (ch < 0x10000) { |
| 2048 | /* Special case: check for high surrogate */ |
| 2049 | if (0xD800 <= ch && ch <= 0xDBFF && i != size) { |
| 2050 | Py_UCS4 ch2 = s[i]; |
| 2051 | /* Check for low surrogate and combine the two to |
| 2052 | form a UCS4 value */ |
| 2053 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2054 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2055 | i++; |
| 2056 | goto encodeUCS4; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2057 | } |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2058 | /* Fall through: handles isolated high surrogates */ |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2059 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2060 | *p++ = (char)(0xe0 | (ch >> 12)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2061 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2062 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2063 | continue; |
| 2064 | } |
| 2065 | encodeUCS4: |
| 2066 | /* Encode UCS4 Unicode ordinals */ |
| 2067 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2068 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 2069 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2070 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2071 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2072 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 2073 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2074 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2075 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2076 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2077 | assert(nneeded <= nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2078 | result = PyString_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2079 | } |
| 2080 | else { |
| 2081 | /* Cut back to size actually needed. */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2082 | nneeded = p - PyString_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2083 | assert(nneeded <= nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2084 | _PyString_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2085 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2086 | return result; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2087 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2088 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2089 | } |
| 2090 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2091 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 2092 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2093 | if (!PyUnicode_Check(unicode)) { |
| 2094 | PyErr_BadArgument(); |
| 2095 | return NULL; |
| 2096 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 2097 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 2098 | PyUnicode_GET_SIZE(unicode), |
| 2099 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2100 | } |
| 2101 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2102 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 2103 | |
| 2104 | PyObject * |
| 2105 | PyUnicode_DecodeUTF32(const char *s, |
| 2106 | Py_ssize_t size, |
| 2107 | const char *errors, |
| 2108 | int *byteorder) |
| 2109 | { |
| 2110 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 2111 | } |
| 2112 | |
| 2113 | PyObject * |
| 2114 | PyUnicode_DecodeUTF32Stateful(const char *s, |
| 2115 | Py_ssize_t size, |
| 2116 | const char *errors, |
| 2117 | int *byteorder, |
| 2118 | Py_ssize_t *consumed) |
| 2119 | { |
| 2120 | const char *starts = s; |
| 2121 | Py_ssize_t startinpos; |
| 2122 | Py_ssize_t endinpos; |
| 2123 | Py_ssize_t outpos; |
| 2124 | PyUnicodeObject *unicode; |
| 2125 | Py_UNICODE *p; |
| 2126 | #ifndef Py_UNICODE_WIDE |
| 2127 | int i, pairs; |
| 2128 | #else |
| 2129 | const int pairs = 0; |
| 2130 | #endif |
| 2131 | const unsigned char *q, *e; |
| 2132 | int bo = 0; /* assume native ordering by default */ |
| 2133 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2134 | /* Offsets from q for retrieving bytes in the right order. */ |
| 2135 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2136 | int iorder[] = {0, 1, 2, 3}; |
| 2137 | #else |
| 2138 | int iorder[] = {3, 2, 1, 0}; |
| 2139 | #endif |
| 2140 | PyObject *errorHandler = NULL; |
| 2141 | PyObject *exc = NULL; |
Guido van Rossum | 8d991ed | 2007-08-17 15:41:00 +0000 | [diff] [blame] | 2142 | /* On narrow builds we split characters outside the BMP into two |
| 2143 | codepoints => count how much extra space we need. */ |
| 2144 | #ifndef Py_UNICODE_WIDE |
| 2145 | for (i = pairs = 0; i < size/4; i++) |
| 2146 | if (((Py_UCS4 *)s)[i] >= 0x10000) |
| 2147 | pairs++; |
| 2148 | #endif |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2149 | |
| 2150 | /* This might be one to much, because of a BOM */ |
| 2151 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 2152 | if (!unicode) |
| 2153 | return NULL; |
| 2154 | if (size == 0) |
| 2155 | return (PyObject *)unicode; |
| 2156 | |
| 2157 | /* Unpack UTF-32 encoded data */ |
| 2158 | p = unicode->str; |
| 2159 | q = (unsigned char *)s; |
| 2160 | e = q + size; |
| 2161 | |
| 2162 | if (byteorder) |
| 2163 | bo = *byteorder; |
| 2164 | |
| 2165 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2166 | byte order setting accordingly. In native mode, the leading BOM |
| 2167 | mark is skipped, in all other modes, it is copied to the output |
| 2168 | stream as-is (giving a ZWNBSP character). */ |
| 2169 | if (bo == 0) { |
| 2170 | if (size >= 4) { |
| 2171 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2172 | (q[iorder[1]] << 8) | q[iorder[0]]; |
| 2173 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2174 | if (bom == 0x0000FEFF) { |
| 2175 | q += 4; |
| 2176 | bo = -1; |
| 2177 | } |
| 2178 | else if (bom == 0xFFFE0000) { |
| 2179 | q += 4; |
| 2180 | bo = 1; |
| 2181 | } |
| 2182 | #else |
| 2183 | if (bom == 0x0000FEFF) { |
| 2184 | q += 4; |
| 2185 | bo = 1; |
| 2186 | } |
| 2187 | else if (bom == 0xFFFE0000) { |
| 2188 | q += 4; |
| 2189 | bo = -1; |
| 2190 | } |
| 2191 | #endif |
| 2192 | } |
| 2193 | } |
| 2194 | |
| 2195 | if (bo == -1) { |
| 2196 | /* force LE */ |
| 2197 | iorder[0] = 0; |
| 2198 | iorder[1] = 1; |
| 2199 | iorder[2] = 2; |
| 2200 | iorder[3] = 3; |
| 2201 | } |
| 2202 | else if (bo == 1) { |
| 2203 | /* force BE */ |
| 2204 | iorder[0] = 3; |
| 2205 | iorder[1] = 2; |
| 2206 | iorder[2] = 1; |
| 2207 | iorder[3] = 0; |
| 2208 | } |
| 2209 | |
| 2210 | while (q < e) { |
| 2211 | Py_UCS4 ch; |
| 2212 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 2213 | if (e-q<4) { |
| 2214 | if (consumed) |
| 2215 | break; |
| 2216 | errmsg = "truncated data"; |
| 2217 | startinpos = ((const char *)q)-starts; |
| 2218 | endinpos = ((const char *)e)-starts; |
| 2219 | goto utf32Error; |
| 2220 | /* The remaining input chars are ignored if the callback |
| 2221 | chooses to skip the input */ |
| 2222 | } |
| 2223 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2224 | (q[iorder[1]] << 8) | q[iorder[0]]; |
| 2225 | |
| 2226 | if (ch >= 0x110000) |
| 2227 | { |
| 2228 | errmsg = "codepoint not in range(0x110000)"; |
| 2229 | startinpos = ((const char *)q)-starts; |
| 2230 | endinpos = startinpos+4; |
| 2231 | goto utf32Error; |
| 2232 | } |
| 2233 | #ifndef Py_UNICODE_WIDE |
| 2234 | if (ch >= 0x10000) |
| 2235 | { |
| 2236 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 2237 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2238 | } |
| 2239 | else |
| 2240 | #endif |
| 2241 | *p++ = ch; |
| 2242 | q += 4; |
| 2243 | continue; |
| 2244 | utf32Error: |
| 2245 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2246 | if (unicode_decode_call_errorhandler( |
| 2247 | errors, &errorHandler, |
| 2248 | "utf32", errmsg, |
| 2249 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 2250 | (PyObject **)&unicode, &outpos, &p)) |
| 2251 | goto onError; |
| 2252 | } |
| 2253 | |
| 2254 | if (byteorder) |
| 2255 | *byteorder = bo; |
| 2256 | |
| 2257 | if (consumed) |
| 2258 | *consumed = (const char *)q-starts; |
| 2259 | |
| 2260 | /* Adjust length */ |
| 2261 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
| 2262 | goto onError; |
| 2263 | |
| 2264 | Py_XDECREF(errorHandler); |
| 2265 | Py_XDECREF(exc); |
| 2266 | return (PyObject *)unicode; |
| 2267 | |
| 2268 | onError: |
| 2269 | Py_DECREF(unicode); |
| 2270 | Py_XDECREF(errorHandler); |
| 2271 | Py_XDECREF(exc); |
| 2272 | return NULL; |
| 2273 | } |
| 2274 | |
| 2275 | PyObject * |
| 2276 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 2277 | Py_ssize_t size, |
| 2278 | const char *errors, |
| 2279 | int byteorder) |
| 2280 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2281 | PyObject *v, *result; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2282 | unsigned char *p; |
| 2283 | #ifndef Py_UNICODE_WIDE |
| 2284 | int i, pairs; |
| 2285 | #else |
| 2286 | const int pairs = 0; |
| 2287 | #endif |
| 2288 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2289 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2290 | int iorder[] = {0, 1, 2, 3}; |
| 2291 | #else |
| 2292 | int iorder[] = {3, 2, 1, 0}; |
| 2293 | #endif |
| 2294 | |
| 2295 | #define STORECHAR(CH) \ |
| 2296 | do { \ |
| 2297 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 2298 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 2299 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 2300 | p[iorder[0]] = (CH) & 0xff; \ |
| 2301 | p += 4; \ |
| 2302 | } while(0) |
| 2303 | |
| 2304 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 2305 | so we need less space. */ |
| 2306 | #ifndef Py_UNICODE_WIDE |
| 2307 | for (i = pairs = 0; i < size-1; i++) |
| 2308 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 2309 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 2310 | pairs++; |
| 2311 | #endif |
| 2312 | v = PyBytes_FromStringAndSize(NULL, |
| 2313 | 4 * (size - pairs + (byteorder == 0))); |
| 2314 | if (v == NULL) |
| 2315 | return NULL; |
| 2316 | |
| 2317 | p = (unsigned char *)PyBytes_AS_STRING(v); |
| 2318 | if (byteorder == 0) |
| 2319 | STORECHAR(0xFEFF); |
| 2320 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2321 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2322 | |
| 2323 | if (byteorder == -1) { |
| 2324 | /* force LE */ |
| 2325 | iorder[0] = 0; |
| 2326 | iorder[1] = 1; |
| 2327 | iorder[2] = 2; |
| 2328 | iorder[3] = 3; |
| 2329 | } |
| 2330 | else if (byteorder == 1) { |
| 2331 | /* force BE */ |
| 2332 | iorder[0] = 3; |
| 2333 | iorder[1] = 2; |
| 2334 | iorder[2] = 1; |
| 2335 | iorder[3] = 0; |
| 2336 | } |
| 2337 | |
| 2338 | while (size-- > 0) { |
| 2339 | Py_UCS4 ch = *s++; |
| 2340 | #ifndef Py_UNICODE_WIDE |
| 2341 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 2342 | Py_UCS4 ch2 = *s; |
| 2343 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 2344 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 2345 | s++; |
| 2346 | size--; |
| 2347 | } |
| 2348 | } |
| 2349 | #endif |
| 2350 | STORECHAR(ch); |
| 2351 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2352 | |
| 2353 | done: |
| 2354 | result = PyString_FromStringAndSize(PyBytes_AS_STRING(v), Py_Size(v)); |
| 2355 | Py_DECREF(v); |
| 2356 | return result; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2357 | #undef STORECHAR |
| 2358 | } |
| 2359 | |
| 2360 | PyObject *PyUnicode_AsUTF32String(PyObject *unicode) |
| 2361 | { |
| 2362 | if (!PyUnicode_Check(unicode)) { |
| 2363 | PyErr_BadArgument(); |
| 2364 | return NULL; |
| 2365 | } |
| 2366 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
| 2367 | PyUnicode_GET_SIZE(unicode), |
| 2368 | NULL, |
| 2369 | 0); |
| 2370 | } |
| 2371 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2372 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 2373 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2374 | PyObject * |
| 2375 | PyUnicode_DecodeUTF16(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2376 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2377 | const char *errors, |
| 2378 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2379 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2380 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 2381 | } |
| 2382 | |
| 2383 | PyObject * |
| 2384 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2385 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2386 | const char *errors, |
| 2387 | int *byteorder, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2388 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2389 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2390 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2391 | Py_ssize_t startinpos; |
| 2392 | Py_ssize_t endinpos; |
| 2393 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2394 | PyUnicodeObject *unicode; |
| 2395 | Py_UNICODE *p; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2396 | const unsigned char *q, *e; |
| 2397 | int bo = 0; /* assume native ordering by default */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2398 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2399 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 2400 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2401 | int ihi = 1, ilo = 0; |
| 2402 | #else |
| 2403 | int ihi = 0, ilo = 1; |
| 2404 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2405 | PyObject *errorHandler = NULL; |
| 2406 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2407 | |
| 2408 | /* Note: size will always be longer than the resulting Unicode |
| 2409 | character count */ |
| 2410 | unicode = _PyUnicode_New(size); |
| 2411 | if (!unicode) |
| 2412 | return NULL; |
| 2413 | if (size == 0) |
| 2414 | return (PyObject *)unicode; |
| 2415 | |
| 2416 | /* Unpack UTF-16 encoded data */ |
| 2417 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2418 | q = (unsigned char *)s; |
| 2419 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2420 | |
| 2421 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2422 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2423 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2424 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2425 | byte order setting accordingly. In native mode, the leading BOM |
| 2426 | mark is skipped, in all other modes, it is copied to the output |
| 2427 | stream as-is (giving a ZWNBSP character). */ |
| 2428 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2429 | if (size >= 2) { |
| 2430 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2431 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2432 | if (bom == 0xFEFF) { |
| 2433 | q += 2; |
| 2434 | bo = -1; |
| 2435 | } |
| 2436 | else if (bom == 0xFFFE) { |
| 2437 | q += 2; |
| 2438 | bo = 1; |
| 2439 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2440 | #else |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2441 | if (bom == 0xFEFF) { |
| 2442 | q += 2; |
| 2443 | bo = 1; |
| 2444 | } |
| 2445 | else if (bom == 0xFFFE) { |
| 2446 | q += 2; |
| 2447 | bo = -1; |
| 2448 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2449 | #endif |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2450 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2451 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2452 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2453 | if (bo == -1) { |
| 2454 | /* force LE */ |
| 2455 | ihi = 1; |
| 2456 | ilo = 0; |
| 2457 | } |
| 2458 | else if (bo == 1) { |
| 2459 | /* force BE */ |
| 2460 | ihi = 0; |
| 2461 | ilo = 1; |
| 2462 | } |
| 2463 | |
| 2464 | while (q < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2465 | Py_UNICODE ch; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2466 | /* remaining bytes at the end? (size should be even) */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2467 | if (e-q<2) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2468 | if (consumed) |
| 2469 | break; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2470 | errmsg = "truncated data"; |
| 2471 | startinpos = ((const char *)q)-starts; |
| 2472 | endinpos = ((const char *)e)-starts; |
| 2473 | goto utf16Error; |
| 2474 | /* The remaining input chars are ignored if the callback |
| 2475 | chooses to skip the input */ |
| 2476 | } |
| 2477 | ch = (q[ihi] << 8) | q[ilo]; |
| 2478 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2479 | q += 2; |
| 2480 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2481 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 2482 | *p++ = ch; |
| 2483 | continue; |
| 2484 | } |
| 2485 | |
| 2486 | /* UTF-16 code pair: */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2487 | if (q >= e) { |
| 2488 | errmsg = "unexpected end of data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2489 | startinpos = (((const char *)q)-2)-starts; |
| 2490 | endinpos = ((const char *)e)-starts; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2491 | goto utf16Error; |
| 2492 | } |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 2493 | if (0xD800 <= ch && ch <= 0xDBFF) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2494 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 2495 | q += 2; |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 2496 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2497 | #ifndef Py_UNICODE_WIDE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2498 | *p++ = ch; |
| 2499 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2500 | #else |
| 2501 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2502 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2503 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2504 | } |
| 2505 | else { |
| 2506 | errmsg = "illegal UTF-16 surrogate"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2507 | startinpos = (((const char *)q)-4)-starts; |
| 2508 | endinpos = startinpos+2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2509 | goto utf16Error; |
| 2510 | } |
| 2511 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2512 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2513 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2514 | startinpos = (((const char *)q)-2)-starts; |
| 2515 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2516 | /* Fall through to report the error */ |
| 2517 | |
| 2518 | utf16Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2519 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2520 | if (unicode_decode_call_errorhandler( |
| 2521 | errors, &errorHandler, |
| 2522 | "utf16", errmsg, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2523 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2524 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2525 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2526 | } |
| 2527 | |
| 2528 | if (byteorder) |
| 2529 | *byteorder = bo; |
| 2530 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2531 | if (consumed) |
| 2532 | *consumed = (const char *)q-starts; |
| 2533 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2534 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2535 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2536 | goto onError; |
| 2537 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2538 | Py_XDECREF(errorHandler); |
| 2539 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2540 | return (PyObject *)unicode; |
| 2541 | |
| 2542 | onError: |
| 2543 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2544 | Py_XDECREF(errorHandler); |
| 2545 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2546 | return NULL; |
| 2547 | } |
| 2548 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2549 | PyObject * |
| 2550 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2551 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2552 | const char *errors, |
| 2553 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2554 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2555 | PyObject *v, *result; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2556 | unsigned char *p; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2557 | #ifdef Py_UNICODE_WIDE |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2558 | int i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2559 | #else |
| 2560 | const int pairs = 0; |
| 2561 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2562 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2563 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2564 | int ihi = 1, ilo = 0; |
| 2565 | #else |
| 2566 | int ihi = 0, ilo = 1; |
| 2567 | #endif |
| 2568 | |
| 2569 | #define STORECHAR(CH) \ |
| 2570 | do { \ |
| 2571 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 2572 | p[ilo] = (CH) & 0xff; \ |
| 2573 | p += 2; \ |
| 2574 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2575 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2576 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2577 | for (i = pairs = 0; i < size; i++) |
| 2578 | if (s[i] >= 0x10000) |
| 2579 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2580 | #endif |
Walter Dörwald | 3cc3452 | 2007-05-04 10:48:27 +0000 | [diff] [blame] | 2581 | v = PyBytes_FromStringAndSize(NULL, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2582 | 2 * (size + pairs + (byteorder == 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2583 | if (v == NULL) |
| 2584 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2585 | |
Walter Dörwald | 3cc3452 | 2007-05-04 10:48:27 +0000 | [diff] [blame] | 2586 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2587 | if (byteorder == 0) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2588 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2589 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2590 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2591 | |
| 2592 | if (byteorder == -1) { |
| 2593 | /* force LE */ |
| 2594 | ihi = 1; |
| 2595 | ilo = 0; |
| 2596 | } |
| 2597 | else if (byteorder == 1) { |
| 2598 | /* force BE */ |
| 2599 | ihi = 0; |
| 2600 | ilo = 1; |
| 2601 | } |
| 2602 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2603 | while (size-- > 0) { |
| 2604 | Py_UNICODE ch = *s++; |
| 2605 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2606 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2607 | if (ch >= 0x10000) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2608 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2609 | ch = 0xD800 | ((ch-0x10000) >> 10); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2610 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2611 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2612 | STORECHAR(ch); |
| 2613 | if (ch2) |
| 2614 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2615 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2616 | |
| 2617 | done: |
| 2618 | result = PyString_FromStringAndSize(PyBytes_AS_STRING(v), Py_Size(v)); |
| 2619 | Py_DECREF(v); |
| 2620 | return result; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2621 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2622 | } |
| 2623 | |
| 2624 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 2625 | { |
| 2626 | if (!PyUnicode_Check(unicode)) { |
| 2627 | PyErr_BadArgument(); |
| 2628 | return NULL; |
| 2629 | } |
| 2630 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
| 2631 | PyUnicode_GET_SIZE(unicode), |
| 2632 | NULL, |
| 2633 | 0); |
| 2634 | } |
| 2635 | |
| 2636 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 2637 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2638 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 2639 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2640 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2641 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2642 | const char *errors) |
| 2643 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2644 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2645 | Py_ssize_t startinpos; |
| 2646 | Py_ssize_t endinpos; |
| 2647 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2648 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2649 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2650 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2651 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2652 | char* message; |
| 2653 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2654 | PyObject *errorHandler = NULL; |
| 2655 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2656 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2657 | /* Escaped strings will always be longer than the resulting |
| 2658 | 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] | 2659 | length after conversion to the true value. |
| 2660 | (but if the error callback returns a long replacement string |
| 2661 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2662 | v = _PyUnicode_New(size); |
| 2663 | if (v == NULL) |
| 2664 | goto onError; |
| 2665 | if (size == 0) |
| 2666 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2667 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2668 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2669 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2670 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2671 | while (s < end) { |
| 2672 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2673 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2674 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2675 | |
| 2676 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 2677 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2678 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2679 | continue; |
| 2680 | } |
| 2681 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2682 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2683 | /* \ - Escapes */ |
| 2684 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2685 | c = *s++; |
| 2686 | if (s > end) |
| 2687 | c = '\0'; /* Invalid after \ */ |
| 2688 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2689 | |
| 2690 | /* \x escapes */ |
| 2691 | case '\n': break; |
| 2692 | case '\\': *p++ = '\\'; break; |
| 2693 | case '\'': *p++ = '\''; break; |
| 2694 | case '\"': *p++ = '\"'; break; |
| 2695 | case 'b': *p++ = '\b'; break; |
| 2696 | case 'f': *p++ = '\014'; break; /* FF */ |
| 2697 | case 't': *p++ = '\t'; break; |
| 2698 | case 'n': *p++ = '\n'; break; |
| 2699 | case 'r': *p++ = '\r'; break; |
| 2700 | case 'v': *p++ = '\013'; break; /* VT */ |
| 2701 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 2702 | |
| 2703 | /* \OOO (octal) escapes */ |
| 2704 | case '0': case '1': case '2': case '3': |
| 2705 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2706 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2707 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2708 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2709 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2710 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2711 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2712 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2713 | break; |
| 2714 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2715 | /* hex escapes */ |
| 2716 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2717 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2718 | digits = 2; |
| 2719 | message = "truncated \\xXX escape"; |
| 2720 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2721 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2722 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2723 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2724 | digits = 4; |
| 2725 | message = "truncated \\uXXXX escape"; |
| 2726 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2727 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2728 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2729 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2730 | digits = 8; |
| 2731 | message = "truncated \\UXXXXXXXX escape"; |
| 2732 | hexescape: |
| 2733 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2734 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2735 | if (s+digits>end) { |
| 2736 | endinpos = size; |
| 2737 | if (unicode_decode_call_errorhandler( |
| 2738 | errors, &errorHandler, |
| 2739 | "unicodeescape", "end of string in escape sequence", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2740 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2741 | (PyObject **)&v, &outpos, &p)) |
| 2742 | goto onError; |
| 2743 | goto nextByte; |
| 2744 | } |
| 2745 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2746 | c = (unsigned char) s[i]; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2747 | if (!ISXDIGIT(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2748 | endinpos = (s+i+1)-starts; |
| 2749 | if (unicode_decode_call_errorhandler( |
| 2750 | errors, &errorHandler, |
| 2751 | "unicodeescape", message, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2752 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2753 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2754 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2755 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2756 | } |
| 2757 | chr = (chr<<4) & ~0xF; |
| 2758 | if (c >= '0' && c <= '9') |
| 2759 | chr += c - '0'; |
| 2760 | else if (c >= 'a' && c <= 'f') |
| 2761 | chr += 10 + c - 'a'; |
| 2762 | else |
| 2763 | chr += 10 + c - 'A'; |
| 2764 | } |
| 2765 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 2766 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2767 | /* _decoding_error will have already written into the |
| 2768 | target buffer. */ |
| 2769 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2770 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2771 | /* when we get here, chr is a 32-bit unicode character */ |
| 2772 | if (chr <= 0xffff) |
| 2773 | /* UCS-2 character */ |
| 2774 | *p++ = (Py_UNICODE) chr; |
| 2775 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2776 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2777 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2778 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2779 | *p++ = chr; |
| 2780 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2781 | chr -= 0x10000L; |
| 2782 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2783 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2784 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2785 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2786 | endinpos = s-starts; |
| 2787 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2788 | if (unicode_decode_call_errorhandler( |
| 2789 | errors, &errorHandler, |
| 2790 | "unicodeescape", "illegal Unicode character", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2791 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2792 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2793 | goto onError; |
| 2794 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2795 | break; |
| 2796 | |
| 2797 | /* \N{name} */ |
| 2798 | case 'N': |
| 2799 | message = "malformed \\N character escape"; |
| 2800 | if (ucnhash_CAPI == NULL) { |
| 2801 | /* load the unicode data module */ |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2802 | PyObject *m, *api; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2803 | m = PyImport_ImportModule("unicodedata"); |
| 2804 | if (m == NULL) |
| 2805 | goto ucnhashError; |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2806 | api = PyObject_GetAttrString(m, "ucnhash_CAPI"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2807 | Py_DECREF(m); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2808 | if (api == NULL) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2809 | goto ucnhashError; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2810 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCObject_AsVoidPtr(api); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2811 | Py_DECREF(api); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2812 | if (ucnhash_CAPI == NULL) |
| 2813 | goto ucnhashError; |
| 2814 | } |
| 2815 | if (*s == '{') { |
| 2816 | const char *start = s+1; |
| 2817 | /* look for the closing brace */ |
| 2818 | while (*s != '}' && s < end) |
| 2819 | s++; |
| 2820 | if (s > start && s < end && *s == '}') { |
| 2821 | /* found a name. look it up in the unicode database */ |
| 2822 | message = "unknown Unicode character name"; |
| 2823 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 2824 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2825 | goto store; |
| 2826 | } |
| 2827 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2828 | endinpos = s-starts; |
| 2829 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2830 | if (unicode_decode_call_errorhandler( |
| 2831 | errors, &errorHandler, |
| 2832 | "unicodeescape", message, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2833 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2834 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2835 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2836 | break; |
| 2837 | |
| 2838 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2839 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2840 | message = "\\ at end of string"; |
| 2841 | s--; |
| 2842 | endinpos = s-starts; |
| 2843 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2844 | if (unicode_decode_call_errorhandler( |
| 2845 | errors, &errorHandler, |
| 2846 | "unicodeescape", message, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2847 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2848 | (PyObject **)&v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2849 | goto onError; |
| 2850 | } |
| 2851 | else { |
| 2852 | *p++ = '\\'; |
| 2853 | *p++ = (unsigned char)s[-1]; |
| 2854 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2855 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2856 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2857 | nextByte: |
| 2858 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2859 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2860 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2861 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 2862 | Py_XDECREF(errorHandler); |
| 2863 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2864 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2865 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2866 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2867 | PyErr_SetString( |
| 2868 | PyExc_UnicodeError, |
| 2869 | "\\N escapes not supported (can't load unicodedata module)" |
| 2870 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2871 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2872 | Py_XDECREF(errorHandler); |
| 2873 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 2874 | return NULL; |
| 2875 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2876 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2877 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2878 | Py_XDECREF(errorHandler); |
| 2879 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2880 | return NULL; |
| 2881 | } |
| 2882 | |
| 2883 | /* Return a Unicode-Escape string version of the Unicode object. |
| 2884 | |
| 2885 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 2886 | appropriate. |
| 2887 | |
| 2888 | */ |
| 2889 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2890 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
| 2891 | Py_ssize_t size, |
| 2892 | Py_UNICODE ch) |
| 2893 | { |
| 2894 | /* like wcschr, but doesn't stop at NULL characters */ |
| 2895 | |
| 2896 | while (size-- > 0) { |
| 2897 | if (*s == ch) |
| 2898 | return s; |
| 2899 | s++; |
| 2900 | } |
| 2901 | |
| 2902 | return NULL; |
| 2903 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 2904 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2905 | static const char *hexdigits = "0123456789abcdef"; |
| 2906 | |
| 2907 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 2908 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2909 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2910 | PyObject *repr, *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2911 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2912 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2913 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 2914 | better to choose a different scheme. Perhaps scan the |
| 2915 | first N-chars of the string and allocate based on that size. |
| 2916 | */ |
| 2917 | /* Initial allocation is based on the longest-possible unichr |
| 2918 | escape. |
| 2919 | |
| 2920 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 2921 | unichr, so in this case it's the longest unichr escape. In |
| 2922 | narrow (UTF-16) builds this is five chars per source unichr |
| 2923 | since there are two unichrs in the surrogate pair, so in narrow |
| 2924 | (UTF-16) builds it's not the longest unichr escape. |
| 2925 | |
| 2926 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 2927 | so in the narrow (UTF-16) build case it's the longest unichr |
| 2928 | escape. |
| 2929 | */ |
| 2930 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2931 | repr = PyBytes_FromStringAndSize(NULL, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2932 | #ifdef Py_UNICODE_WIDE |
| 2933 | + 10*size |
| 2934 | #else |
| 2935 | + 6*size |
| 2936 | #endif |
| 2937 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2938 | if (repr == NULL) |
| 2939 | return NULL; |
| 2940 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2941 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2942 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2943 | while (size-- > 0) { |
| 2944 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2945 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2946 | /* Escape backslashes */ |
| 2947 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2948 | *p++ = '\\'; |
| 2949 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2950 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2951 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2952 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 2953 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2954 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 2955 | else if (ch >= 0x10000) { |
| 2956 | *p++ = '\\'; |
| 2957 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2958 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 2959 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 2960 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 2961 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 2962 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 2963 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 2964 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 2965 | *p++ = hexdigits[ch & 0x0000000F]; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2966 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2967 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2968 | #else |
| 2969 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2970 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 2971 | Py_UNICODE ch2; |
| 2972 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2973 | |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2974 | ch2 = *s++; |
| 2975 | size--; |
| 2976 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 2977 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 2978 | *p++ = '\\'; |
| 2979 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2980 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 2981 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 2982 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 2983 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 2984 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 2985 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 2986 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 2987 | *p++ = hexdigits[ucs & 0x0000000F]; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2988 | continue; |
| 2989 | } |
| 2990 | /* Fall through: isolated surrogates are copied as-is */ |
| 2991 | s--; |
| 2992 | size++; |
| 2993 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2994 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2995 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2996 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2997 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2998 | *p++ = '\\'; |
| 2999 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3000 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 3001 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 3002 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 3003 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3004 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3005 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3006 | /* Map special whitespace to '\t', \n', '\r' */ |
| 3007 | else if (ch == '\t') { |
| 3008 | *p++ = '\\'; |
| 3009 | *p++ = 't'; |
| 3010 | } |
| 3011 | else if (ch == '\n') { |
| 3012 | *p++ = '\\'; |
| 3013 | *p++ = 'n'; |
| 3014 | } |
| 3015 | else if (ch == '\r') { |
| 3016 | *p++ = '\\'; |
| 3017 | *p++ = 'r'; |
| 3018 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3019 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3020 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 3021 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3022 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3023 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3024 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 3025 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3026 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3027 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3028 | /* Copy everything else as-is */ |
| 3029 | else |
| 3030 | *p++ = (char) ch; |
| 3031 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3032 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3033 | result = PyString_FromStringAndSize(PyBytes_AS_STRING(repr), |
| 3034 | p - PyBytes_AS_STRING(repr)); |
| 3035 | Py_DECREF(repr); |
| 3036 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3037 | } |
| 3038 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3039 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
| 3040 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3041 | PyObject *s, *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3042 | if (!PyUnicode_Check(unicode)) { |
| 3043 | PyErr_BadArgument(); |
| 3044 | return NULL; |
| 3045 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3046 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3047 | PyUnicode_GET_SIZE(unicode)); |
| 3048 | |
| 3049 | if (!s) |
| 3050 | return NULL; |
| 3051 | result = PyString_FromStringAndSize(PyBytes_AS_STRING(s), |
| 3052 | PyBytes_GET_SIZE(s)); |
| 3053 | Py_DECREF(s); |
| 3054 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3055 | } |
| 3056 | |
| 3057 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 3058 | |
| 3059 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3060 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3061 | const char *errors) |
| 3062 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3063 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3064 | Py_ssize_t startinpos; |
| 3065 | Py_ssize_t endinpos; |
| 3066 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3067 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3068 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3069 | const char *end; |
| 3070 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3071 | PyObject *errorHandler = NULL; |
| 3072 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3073 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3074 | /* Escaped strings will always be longer than the resulting |
| 3075 | 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] | 3076 | length after conversion to the true value. (But decoding error |
| 3077 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3078 | v = _PyUnicode_New(size); |
| 3079 | if (v == NULL) |
| 3080 | goto onError; |
| 3081 | if (size == 0) |
| 3082 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3083 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3084 | end = s + size; |
| 3085 | while (s < end) { |
| 3086 | unsigned char c; |
Martin v. Löwis | 047c05e | 2002-03-21 08:55:28 +0000 | [diff] [blame] | 3087 | Py_UCS4 x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3088 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3089 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3090 | |
| 3091 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3092 | if (*s != '\\') { |
| 3093 | *p++ = (unsigned char)*s++; |
| 3094 | continue; |
| 3095 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3096 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3097 | |
| 3098 | /* \u-escapes are only interpreted iff the number of leading |
| 3099 | backslashes if odd */ |
| 3100 | bs = s; |
| 3101 | for (;s < end;) { |
| 3102 | if (*s != '\\') |
| 3103 | break; |
| 3104 | *p++ = (unsigned char)*s++; |
| 3105 | } |
| 3106 | if (((s - bs) & 1) == 0 || |
| 3107 | s >= end || |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3108 | (*s != 'u' && *s != 'U')) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3109 | continue; |
| 3110 | } |
| 3111 | p--; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3112 | count = *s=='u' ? 4 : 8; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3113 | s++; |
| 3114 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3115 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3116 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3117 | for (x = 0, i = 0; i < count; ++i, ++s) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3118 | c = (unsigned char)*s; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3119 | if (!ISXDIGIT(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3120 | endinpos = s-starts; |
| 3121 | if (unicode_decode_call_errorhandler( |
| 3122 | errors, &errorHandler, |
| 3123 | "rawunicodeescape", "truncated \\uXXXX", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3124 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3125 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3126 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3127 | goto nextByte; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3128 | } |
| 3129 | x = (x<<4) & ~0xF; |
| 3130 | if (c >= '0' && c <= '9') |
| 3131 | x += c - '0'; |
| 3132 | else if (c >= 'a' && c <= 'f') |
| 3133 | x += 10 + c - 'a'; |
| 3134 | else |
| 3135 | x += 10 + c - 'A'; |
| 3136 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3137 | #ifndef Py_UNICODE_WIDE |
| 3138 | if (x > 0x10000) { |
| 3139 | if (unicode_decode_call_errorhandler( |
| 3140 | errors, &errorHandler, |
| 3141 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3142 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3143 | (PyObject **)&v, &outpos, &p)) |
| 3144 | goto onError; |
| 3145 | } |
| 3146 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3147 | *p++ = x; |
| 3148 | nextByte: |
| 3149 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3150 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3151 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3152 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3153 | Py_XDECREF(errorHandler); |
| 3154 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3155 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3156 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3157 | onError: |
| 3158 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3159 | Py_XDECREF(errorHandler); |
| 3160 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3161 | return NULL; |
| 3162 | } |
| 3163 | |
| 3164 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3165 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3166 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3167 | PyObject *repr, *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3168 | char *p; |
| 3169 | char *q; |
| 3170 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3171 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3172 | repr = PyBytes_FromStringAndSize(NULL, 10 * size); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3173 | #else |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3174 | repr = PyBytes_FromStringAndSize(NULL, 6 * size); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3175 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3176 | if (repr == NULL) |
| 3177 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3178 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3179 | goto done; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3180 | |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3181 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3182 | while (size-- > 0) { |
| 3183 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3184 | #ifdef Py_UNICODE_WIDE |
| 3185 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 3186 | if (ch >= 0x10000) { |
| 3187 | *p++ = '\\'; |
| 3188 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 3189 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 3190 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 3191 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 3192 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 3193 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 3194 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 3195 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 3196 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3197 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3198 | else |
| 3199 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3200 | /* Map 16-bit characters to '\uxxxx' */ |
| 3201 | if (ch >= 256) { |
| 3202 | *p++ = '\\'; |
| 3203 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 3204 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 3205 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 3206 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 3207 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3208 | } |
| 3209 | /* Copy everything else as-is */ |
| 3210 | else |
| 3211 | *p++ = (char) ch; |
| 3212 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3213 | size = p - q; |
| 3214 | |
| 3215 | done: |
| 3216 | result = PyString_FromStringAndSize(PyBytes_AS_STRING(repr), size); |
| 3217 | Py_DECREF(repr); |
| 3218 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3219 | } |
| 3220 | |
| 3221 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 3222 | { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3223 | PyObject *s, *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3224 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3225 | PyErr_BadArgument(); |
| 3226 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3227 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3228 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3229 | PyUnicode_GET_SIZE(unicode)); |
| 3230 | |
| 3231 | if (!s) |
| 3232 | return NULL; |
| 3233 | result = PyString_FromStringAndSize(PyBytes_AS_STRING(s), |
| 3234 | PyBytes_GET_SIZE(s)); |
| 3235 | Py_DECREF(s); |
| 3236 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3237 | } |
| 3238 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3239 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 3240 | |
| 3241 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3242 | Py_ssize_t size, |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3243 | const char *errors) |
| 3244 | { |
| 3245 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3246 | Py_ssize_t startinpos; |
| 3247 | Py_ssize_t endinpos; |
| 3248 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3249 | PyUnicodeObject *v; |
| 3250 | Py_UNICODE *p; |
| 3251 | const char *end; |
| 3252 | const char *reason; |
| 3253 | PyObject *errorHandler = NULL; |
| 3254 | PyObject *exc = NULL; |
| 3255 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 3256 | #ifdef Py_UNICODE_WIDE |
| 3257 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 3258 | #endif |
| 3259 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3260 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3261 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 3262 | if (v == NULL) |
| 3263 | goto onError; |
| 3264 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
| 3265 | return (PyObject *)v; |
| 3266 | p = PyUnicode_AS_UNICODE(v); |
| 3267 | end = s + size; |
| 3268 | |
| 3269 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3270 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3271 | /* We have to sanity check the raw data, otherwise doom looms for |
| 3272 | some malformed UCS-4 data. */ |
| 3273 | if ( |
| 3274 | #ifdef Py_UNICODE_WIDE |
| 3275 | *p > unimax || *p < 0 || |
| 3276 | #endif |
| 3277 | end-s < Py_UNICODE_SIZE |
| 3278 | ) |
| 3279 | { |
| 3280 | startinpos = s - starts; |
| 3281 | if (end-s < Py_UNICODE_SIZE) { |
| 3282 | endinpos = end-starts; |
| 3283 | reason = "truncated input"; |
| 3284 | } |
| 3285 | else { |
| 3286 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 3287 | reason = "illegal code point (> 0x10FFFF)"; |
| 3288 | } |
| 3289 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 3290 | if (unicode_decode_call_errorhandler( |
| 3291 | errors, &errorHandler, |
| 3292 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3293 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3294 | (PyObject **)&v, &outpos, &p)) { |
| 3295 | goto onError; |
| 3296 | } |
| 3297 | } |
| 3298 | else { |
| 3299 | p++; |
| 3300 | s += Py_UNICODE_SIZE; |
| 3301 | } |
| 3302 | } |
| 3303 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3304 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3305 | goto onError; |
| 3306 | Py_XDECREF(errorHandler); |
| 3307 | Py_XDECREF(exc); |
| 3308 | return (PyObject *)v; |
| 3309 | |
| 3310 | onError: |
| 3311 | Py_XDECREF(v); |
| 3312 | Py_XDECREF(errorHandler); |
| 3313 | Py_XDECREF(exc); |
| 3314 | return NULL; |
| 3315 | } |
| 3316 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3317 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 3318 | |
| 3319 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3320 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3321 | const char *errors) |
| 3322 | { |
| 3323 | PyUnicodeObject *v; |
| 3324 | Py_UNICODE *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3325 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3326 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3327 | if (size == 1) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3328 | Py_UNICODE r = *(unsigned char*)s; |
| 3329 | return PyUnicode_FromUnicode(&r, 1); |
| 3330 | } |
| 3331 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3332 | v = _PyUnicode_New(size); |
| 3333 | if (v == NULL) |
| 3334 | goto onError; |
| 3335 | if (size == 0) |
| 3336 | return (PyObject *)v; |
| 3337 | p = PyUnicode_AS_UNICODE(v); |
| 3338 | while (size-- > 0) |
| 3339 | *p++ = (unsigned char)*s++; |
| 3340 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3341 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3342 | onError: |
| 3343 | Py_XDECREF(v); |
| 3344 | return NULL; |
| 3345 | } |
| 3346 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3347 | /* create or adjust a UnicodeEncodeError */ |
| 3348 | static void make_encode_exception(PyObject **exceptionObject, |
| 3349 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3350 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3351 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3352 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3353 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3354 | if (*exceptionObject == NULL) { |
| 3355 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 3356 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3357 | } |
| 3358 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3359 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 3360 | goto onError; |
| 3361 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 3362 | goto onError; |
| 3363 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 3364 | goto onError; |
| 3365 | return; |
| 3366 | onError: |
| 3367 | Py_DECREF(*exceptionObject); |
| 3368 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3369 | } |
| 3370 | } |
| 3371 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3372 | /* raises a UnicodeEncodeError */ |
| 3373 | static void raise_encode_exception(PyObject **exceptionObject, |
| 3374 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3375 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3376 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3377 | const char *reason) |
| 3378 | { |
| 3379 | make_encode_exception(exceptionObject, |
| 3380 | encoding, unicode, size, startpos, endpos, reason); |
| 3381 | if (*exceptionObject != NULL) |
| 3382 | PyCodec_StrictErrors(*exceptionObject); |
| 3383 | } |
| 3384 | |
| 3385 | /* error handling callback helper: |
| 3386 | build arguments, call the callback and check the arguments, |
| 3387 | put the result into newpos and return the replacement string, which |
| 3388 | has to be freed by the caller */ |
| 3389 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
| 3390 | PyObject **errorHandler, |
| 3391 | const char *encoding, const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3392 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 3393 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3394 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3395 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3396 | 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] | 3397 | |
| 3398 | PyObject *restuple; |
| 3399 | PyObject *resunicode; |
| 3400 | |
| 3401 | if (*errorHandler == NULL) { |
| 3402 | *errorHandler = PyCodec_LookupError(errors); |
| 3403 | if (*errorHandler == NULL) |
| 3404 | return NULL; |
| 3405 | } |
| 3406 | |
| 3407 | make_encode_exception(exceptionObject, |
| 3408 | encoding, unicode, size, startpos, endpos, reason); |
| 3409 | if (*exceptionObject == NULL) |
| 3410 | return NULL; |
| 3411 | |
| 3412 | restuple = PyObject_CallFunctionObjArgs( |
| 3413 | *errorHandler, *exceptionObject, NULL); |
| 3414 | if (restuple == NULL) |
| 3415 | return NULL; |
| 3416 | if (!PyTuple_Check(restuple)) { |
| 3417 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 3418 | Py_DECREF(restuple); |
| 3419 | return NULL; |
| 3420 | } |
| 3421 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
| 3422 | &resunicode, newpos)) { |
| 3423 | Py_DECREF(restuple); |
| 3424 | return NULL; |
| 3425 | } |
| 3426 | if (*newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3427 | *newpos = size+*newpos; |
| 3428 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 3429 | 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] | 3430 | Py_DECREF(restuple); |
| 3431 | return NULL; |
| 3432 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3433 | Py_INCREF(resunicode); |
| 3434 | Py_DECREF(restuple); |
| 3435 | return resunicode; |
| 3436 | } |
| 3437 | |
| 3438 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3439 | Py_ssize_t size, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3440 | const char *errors, |
| 3441 | int limit) |
| 3442 | { |
| 3443 | /* output object */ |
| 3444 | PyObject *res; |
| 3445 | /* pointers to the beginning and end+1 of input */ |
| 3446 | const Py_UNICODE *startp = p; |
| 3447 | const Py_UNICODE *endp = p + size; |
| 3448 | /* pointer to the beginning of the unencodable characters */ |
| 3449 | /* const Py_UNICODE *badp = NULL; */ |
| 3450 | /* pointer into the output */ |
| 3451 | char *str; |
| 3452 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3453 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3454 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 3455 | 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] | 3456 | PyObject *errorHandler = NULL; |
| 3457 | PyObject *exc = NULL; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3458 | PyObject *result = NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3459 | /* the following variable is used for caching string comparisons |
| 3460 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 3461 | int known_errorHandler = -1; |
| 3462 | |
| 3463 | /* allocate enough for a simple encoding without |
| 3464 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3465 | if (size == 0) |
| 3466 | return PyString_FromStringAndSize(NULL, 0); |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3467 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3468 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3469 | return NULL; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3470 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3471 | ressize = size; |
| 3472 | |
| 3473 | while (p<endp) { |
| 3474 | Py_UNICODE c = *p; |
| 3475 | |
| 3476 | /* can we encode this? */ |
| 3477 | if (c<limit) { |
| 3478 | /* no overflow check, because we know that the space is enough */ |
| 3479 | *str++ = (char)c; |
| 3480 | ++p; |
| 3481 | } |
| 3482 | else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3483 | Py_ssize_t unicodepos = p-startp; |
| 3484 | Py_ssize_t requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3485 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3486 | Py_ssize_t repsize; |
| 3487 | Py_ssize_t newpos; |
| 3488 | Py_ssize_t respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3489 | Py_UNICODE *uni2; |
| 3490 | /* startpos for collecting unencodable chars */ |
| 3491 | const Py_UNICODE *collstart = p; |
| 3492 | const Py_UNICODE *collend = p; |
| 3493 | /* find all unecodable characters */ |
| 3494 | while ((collend < endp) && ((*collend)>=limit)) |
| 3495 | ++collend; |
| 3496 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 3497 | if (known_errorHandler==-1) { |
| 3498 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3499 | known_errorHandler = 1; |
| 3500 | else if (!strcmp(errors, "replace")) |
| 3501 | known_errorHandler = 2; |
| 3502 | else if (!strcmp(errors, "ignore")) |
| 3503 | known_errorHandler = 3; |
| 3504 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3505 | known_errorHandler = 4; |
| 3506 | else |
| 3507 | known_errorHandler = 0; |
| 3508 | } |
| 3509 | switch (known_errorHandler) { |
| 3510 | case 1: /* strict */ |
| 3511 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 3512 | goto onError; |
| 3513 | case 2: /* replace */ |
| 3514 | while (collstart++<collend) |
| 3515 | *str++ = '?'; /* fall through */ |
| 3516 | case 3: /* ignore */ |
| 3517 | p = collend; |
| 3518 | break; |
| 3519 | case 4: /* xmlcharrefreplace */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3520 | respos = str - PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3521 | /* determine replacement size (temporarily (mis)uses p) */ |
| 3522 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 3523 | if (*p<10) |
| 3524 | repsize += 2+1+1; |
| 3525 | else if (*p<100) |
| 3526 | repsize += 2+2+1; |
| 3527 | else if (*p<1000) |
| 3528 | repsize += 2+3+1; |
| 3529 | else if (*p<10000) |
| 3530 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 3531 | #ifndef Py_UNICODE_WIDE |
| 3532 | else |
| 3533 | repsize += 2+5+1; |
| 3534 | #else |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3535 | else if (*p<100000) |
| 3536 | repsize += 2+5+1; |
| 3537 | else if (*p<1000000) |
| 3538 | repsize += 2+6+1; |
| 3539 | else |
| 3540 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3541 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3542 | } |
| 3543 | requiredsize = respos+repsize+(endp-collend); |
| 3544 | if (requiredsize > ressize) { |
| 3545 | if (requiredsize<2*ressize) |
| 3546 | requiredsize = 2*ressize; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3547 | if (PyBytes_Resize(res, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3548 | goto onError; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3549 | str = PyBytes_AS_STRING(res) + respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3550 | ressize = requiredsize; |
| 3551 | } |
| 3552 | /* generate replacement (temporarily (mis)uses p) */ |
| 3553 | for (p = collstart; p < collend; ++p) { |
| 3554 | str += sprintf(str, "&#%d;", (int)*p); |
| 3555 | } |
| 3556 | p = collend; |
| 3557 | break; |
| 3558 | default: |
| 3559 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 3560 | encoding, reason, startp, size, &exc, |
| 3561 | collstart-startp, collend-startp, &newpos); |
| 3562 | if (repunicode == NULL) |
| 3563 | goto onError; |
| 3564 | /* need more space? (at least enough for what we |
| 3565 | have+the replacement+the rest of the string, so |
| 3566 | we won't have to check space for encodable characters) */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3567 | respos = str - PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3568 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3569 | requiredsize = respos+repsize+(endp-collend); |
| 3570 | if (requiredsize > ressize) { |
| 3571 | if (requiredsize<2*ressize) |
| 3572 | requiredsize = 2*ressize; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3573 | if (PyBytes_Resize(res, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3574 | Py_DECREF(repunicode); |
| 3575 | goto onError; |
| 3576 | } |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3577 | str = PyBytes_AS_STRING(res) + respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3578 | ressize = requiredsize; |
| 3579 | } |
| 3580 | /* check if there is anything unencodable in the replacement |
| 3581 | and copy it to the output */ |
| 3582 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 3583 | c = *uni2; |
| 3584 | if (c >= limit) { |
| 3585 | raise_encode_exception(&exc, encoding, startp, size, |
| 3586 | unicodepos, unicodepos+1, reason); |
| 3587 | Py_DECREF(repunicode); |
| 3588 | goto onError; |
| 3589 | } |
| 3590 | *str = (char)c; |
| 3591 | } |
| 3592 | p = startp + newpos; |
| 3593 | Py_DECREF(repunicode); |
| 3594 | } |
| 3595 | } |
| 3596 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3597 | result = PyString_FromStringAndSize(PyBytes_AS_STRING(res), |
| 3598 | str - PyBytes_AS_STRING(res)); |
| 3599 | onError: |
| 3600 | Py_DECREF(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3601 | Py_XDECREF(errorHandler); |
| 3602 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3603 | return result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3604 | } |
| 3605 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3606 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3607 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3608 | const char *errors) |
| 3609 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3610 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3611 | } |
| 3612 | |
| 3613 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 3614 | { |
| 3615 | if (!PyUnicode_Check(unicode)) { |
| 3616 | PyErr_BadArgument(); |
| 3617 | return NULL; |
| 3618 | } |
| 3619 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 3620 | PyUnicode_GET_SIZE(unicode), |
| 3621 | NULL); |
| 3622 | } |
| 3623 | |
| 3624 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 3625 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3626 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3627 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3628 | const char *errors) |
| 3629 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3630 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3631 | PyUnicodeObject *v; |
| 3632 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3633 | Py_ssize_t startinpos; |
| 3634 | Py_ssize_t endinpos; |
| 3635 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3636 | const char *e; |
| 3637 | PyObject *errorHandler = NULL; |
| 3638 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3639 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3640 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3641 | if (size == 1 && *(unsigned char*)s < 128) { |
| 3642 | Py_UNICODE r = *(unsigned char*)s; |
| 3643 | return PyUnicode_FromUnicode(&r, 1); |
| 3644 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3645 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3646 | v = _PyUnicode_New(size); |
| 3647 | if (v == NULL) |
| 3648 | goto onError; |
| 3649 | if (size == 0) |
| 3650 | return (PyObject *)v; |
| 3651 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3652 | e = s + size; |
| 3653 | while (s < e) { |
| 3654 | register unsigned char c = (unsigned char)*s; |
| 3655 | if (c < 128) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3656 | *p++ = c; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3657 | ++s; |
| 3658 | } |
| 3659 | else { |
| 3660 | startinpos = s-starts; |
| 3661 | endinpos = startinpos + 1; |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 3662 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3663 | if (unicode_decode_call_errorhandler( |
| 3664 | errors, &errorHandler, |
| 3665 | "ascii", "ordinal not in range(128)", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3666 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3667 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3668 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3669 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3670 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3671 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3672 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3673 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3674 | Py_XDECREF(errorHandler); |
| 3675 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3676 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3677 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3678 | onError: |
| 3679 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3680 | Py_XDECREF(errorHandler); |
| 3681 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3682 | return NULL; |
| 3683 | } |
| 3684 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3685 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3686 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3687 | const char *errors) |
| 3688 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3689 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3690 | } |
| 3691 | |
| 3692 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 3693 | { |
| 3694 | if (!PyUnicode_Check(unicode)) { |
| 3695 | PyErr_BadArgument(); |
| 3696 | return NULL; |
| 3697 | } |
| 3698 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 3699 | PyUnicode_GET_SIZE(unicode), |
| 3700 | NULL); |
| 3701 | } |
| 3702 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3703 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3704 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3705 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3706 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3707 | #if SIZEOF_INT < SIZEOF_SSIZE_T |
| 3708 | #define NEED_RETRY |
| 3709 | #endif |
| 3710 | |
| 3711 | /* XXX This code is limited to "true" double-byte encodings, as |
| 3712 | a) it assumes an incomplete character consists of a single byte, and |
| 3713 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
| 3714 | encodings, see IsDBCSLeadByteEx documentation. */ |
| 3715 | |
| 3716 | static int is_dbcs_lead_byte(const char *s, int offset) |
| 3717 | { |
| 3718 | const char *curr = s + offset; |
| 3719 | |
| 3720 | if (IsDBCSLeadByte(*curr)) { |
| 3721 | const char *prev = CharPrev(s, curr); |
| 3722 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
| 3723 | } |
| 3724 | return 0; |
| 3725 | } |
| 3726 | |
| 3727 | /* |
| 3728 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 3729 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 3730 | */ |
| 3731 | static int decode_mbcs(PyUnicodeObject **v, |
| 3732 | const char *s, /* MBCS string */ |
| 3733 | int size, /* sizeof MBCS string */ |
| 3734 | int final) |
| 3735 | { |
| 3736 | Py_UNICODE *p; |
| 3737 | Py_ssize_t n = 0; |
| 3738 | int usize = 0; |
| 3739 | |
| 3740 | assert(size >= 0); |
| 3741 | |
| 3742 | /* Skip trailing lead-byte unless 'final' is set */ |
| 3743 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
| 3744 | --size; |
| 3745 | |
| 3746 | /* First get the size of the result */ |
| 3747 | if (size > 0) { |
| 3748 | usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); |
| 3749 | if (usize == 0) { |
| 3750 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3751 | return -1; |
| 3752 | } |
| 3753 | } |
| 3754 | |
| 3755 | if (*v == NULL) { |
| 3756 | /* Create unicode object */ |
| 3757 | *v = _PyUnicode_New(usize); |
| 3758 | if (*v == NULL) |
| 3759 | return -1; |
| 3760 | } |
| 3761 | else { |
| 3762 | /* Extend unicode object */ |
| 3763 | n = PyUnicode_GET_SIZE(*v); |
| 3764 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 3765 | return -1; |
| 3766 | } |
| 3767 | |
| 3768 | /* Do the conversion */ |
| 3769 | if (size > 0) { |
| 3770 | p = PyUnicode_AS_UNICODE(*v) + n; |
| 3771 | if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { |
| 3772 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3773 | return -1; |
| 3774 | } |
| 3775 | } |
| 3776 | |
| 3777 | return size; |
| 3778 | } |
| 3779 | |
| 3780 | PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
| 3781 | Py_ssize_t size, |
| 3782 | const char *errors, |
| 3783 | Py_ssize_t *consumed) |
| 3784 | { |
| 3785 | PyUnicodeObject *v = NULL; |
| 3786 | int done; |
| 3787 | |
| 3788 | if (consumed) |
| 3789 | *consumed = 0; |
| 3790 | |
| 3791 | #ifdef NEED_RETRY |
| 3792 | retry: |
| 3793 | if (size > INT_MAX) |
| 3794 | done = decode_mbcs(&v, s, INT_MAX, 0); |
| 3795 | else |
| 3796 | #endif |
| 3797 | done = decode_mbcs(&v, s, (int)size, !consumed); |
| 3798 | |
| 3799 | if (done < 0) { |
| 3800 | Py_XDECREF(v); |
| 3801 | return NULL; |
| 3802 | } |
| 3803 | |
| 3804 | if (consumed) |
| 3805 | *consumed += done; |
| 3806 | |
| 3807 | #ifdef NEED_RETRY |
| 3808 | if (size > INT_MAX) { |
| 3809 | s += done; |
| 3810 | size -= done; |
| 3811 | goto retry; |
| 3812 | } |
| 3813 | #endif |
| 3814 | |
| 3815 | return (PyObject *)v; |
| 3816 | } |
| 3817 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3818 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3819 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3820 | const char *errors) |
| 3821 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3822 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 3823 | } |
| 3824 | |
| 3825 | /* |
| 3826 | * Convert unicode into string object (MBCS). |
| 3827 | * Returns 0 if succeed, -1 otherwise. |
| 3828 | */ |
| 3829 | static int encode_mbcs(PyObject **repr, |
| 3830 | const Py_UNICODE *p, /* unicode */ |
| 3831 | int size) /* size of unicode */ |
| 3832 | { |
| 3833 | int mbcssize = 0; |
| 3834 | Py_ssize_t n = 0; |
| 3835 | |
| 3836 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3837 | |
| 3838 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3839 | if (size > 0) { |
| 3840 | mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); |
| 3841 | if (mbcssize == 0) { |
| 3842 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3843 | return -1; |
| 3844 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3845 | } |
| 3846 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3847 | if (*repr == NULL) { |
| 3848 | /* Create string object */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3849 | *repr = PyString_FromStringAndSize(NULL, mbcssize); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3850 | if (*repr == NULL) |
| 3851 | return -1; |
| 3852 | } |
| 3853 | else { |
| 3854 | /* Extend string object */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3855 | n = PyString_Size(*repr); |
| 3856 | if (_PyString_Resize(repr, n + mbcssize) < 0) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3857 | return -1; |
| 3858 | } |
| 3859 | |
| 3860 | /* Do the conversion */ |
| 3861 | if (size > 0) { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3862 | char *s = PyString_AS_STRING(*repr) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3863 | if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { |
| 3864 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3865 | return -1; |
| 3866 | } |
| 3867 | } |
| 3868 | |
| 3869 | return 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3870 | } |
| 3871 | |
| 3872 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3873 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3874 | const char *errors) |
| 3875 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3876 | PyObject *repr = NULL; |
| 3877 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 3878 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3879 | #ifdef NEED_RETRY |
| 3880 | retry: |
| 3881 | if (size > INT_MAX) |
| 3882 | ret = encode_mbcs(&repr, p, INT_MAX); |
| 3883 | else |
| 3884 | #endif |
| 3885 | ret = encode_mbcs(&repr, p, (int)size); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3886 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3887 | if (ret < 0) { |
| 3888 | Py_XDECREF(repr); |
| 3889 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3890 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3891 | |
| 3892 | #ifdef NEED_RETRY |
| 3893 | if (size > INT_MAX) { |
| 3894 | p += INT_MAX; |
| 3895 | size -= INT_MAX; |
| 3896 | goto retry; |
| 3897 | } |
| 3898 | #endif |
| 3899 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3900 | return repr; |
| 3901 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3902 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3903 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 3904 | { |
| 3905 | if (!PyUnicode_Check(unicode)) { |
| 3906 | PyErr_BadArgument(); |
| 3907 | return NULL; |
| 3908 | } |
| 3909 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 3910 | PyUnicode_GET_SIZE(unicode), |
| 3911 | NULL); |
| 3912 | } |
| 3913 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3914 | #undef NEED_RETRY |
| 3915 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3916 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3917 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3918 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 3919 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3920 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3921 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3922 | PyObject *mapping, |
| 3923 | const char *errors) |
| 3924 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3925 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3926 | Py_ssize_t startinpos; |
| 3927 | Py_ssize_t endinpos; |
| 3928 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3929 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3930 | PyUnicodeObject *v; |
| 3931 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3932 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3933 | PyObject *errorHandler = NULL; |
| 3934 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3935 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3936 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3937 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3938 | /* Default to Latin-1 */ |
| 3939 | if (mapping == NULL) |
| 3940 | return PyUnicode_DecodeLatin1(s, size, errors); |
| 3941 | |
| 3942 | v = _PyUnicode_New(size); |
| 3943 | if (v == NULL) |
| 3944 | goto onError; |
| 3945 | if (size == 0) |
| 3946 | return (PyObject *)v; |
| 3947 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3948 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3949 | if (PyUnicode_CheckExact(mapping)) { |
| 3950 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 3951 | maplen = PyUnicode_GET_SIZE(mapping); |
| 3952 | while (s < e) { |
| 3953 | unsigned char ch = *s; |
| 3954 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3955 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3956 | if (ch < maplen) |
| 3957 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3958 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3959 | if (x == 0xfffe) { |
| 3960 | /* undefined mapping */ |
| 3961 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3962 | startinpos = s-starts; |
| 3963 | endinpos = startinpos+1; |
| 3964 | if (unicode_decode_call_errorhandler( |
| 3965 | errors, &errorHandler, |
| 3966 | "charmap", "character maps to <undefined>", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3967 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3968 | (PyObject **)&v, &outpos, &p)) { |
| 3969 | goto onError; |
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 | continue; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 3972 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3973 | *p++ = x; |
| 3974 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3975 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3976 | } |
| 3977 | else { |
| 3978 | while (s < e) { |
| 3979 | unsigned char ch = *s; |
| 3980 | PyObject *w, *x; |
| 3981 | |
| 3982 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 3983 | w = PyInt_FromLong((long)ch); |
| 3984 | if (w == NULL) |
| 3985 | goto onError; |
| 3986 | x = PyObject_GetItem(mapping, w); |
| 3987 | Py_DECREF(w); |
| 3988 | if (x == NULL) { |
| 3989 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 3990 | /* No mapping found means: mapping is undefined. */ |
| 3991 | PyErr_Clear(); |
| 3992 | x = Py_None; |
| 3993 | Py_INCREF(x); |
| 3994 | } else |
| 3995 | goto onError; |
| 3996 | } |
| 3997 | |
| 3998 | /* Apply mapping */ |
| 3999 | if (PyInt_Check(x)) { |
| 4000 | long value = PyInt_AS_LONG(x); |
| 4001 | if (value < 0 || value > 65535) { |
| 4002 | PyErr_SetString(PyExc_TypeError, |
| 4003 | "character mapping must be in range(65536)"); |
| 4004 | Py_DECREF(x); |
| 4005 | goto onError; |
| 4006 | } |
| 4007 | *p++ = (Py_UNICODE)value; |
| 4008 | } |
| 4009 | else if (x == Py_None) { |
| 4010 | /* undefined mapping */ |
| 4011 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4012 | startinpos = s-starts; |
| 4013 | endinpos = startinpos+1; |
| 4014 | if (unicode_decode_call_errorhandler( |
| 4015 | errors, &errorHandler, |
| 4016 | "charmap", "character maps to <undefined>", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4017 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4018 | (PyObject **)&v, &outpos, &p)) { |
| 4019 | Py_DECREF(x); |
| 4020 | goto onError; |
| 4021 | } |
Walter Dörwald | d4fff17 | 2005-11-28 22:15:56 +0000 | [diff] [blame] | 4022 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4023 | continue; |
| 4024 | } |
| 4025 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4026 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4027 | |
| 4028 | if (targetsize == 1) |
| 4029 | /* 1-1 mapping */ |
| 4030 | *p++ = *PyUnicode_AS_UNICODE(x); |
| 4031 | |
| 4032 | else if (targetsize > 1) { |
| 4033 | /* 1-n mapping */ |
| 4034 | if (targetsize > extrachars) { |
| 4035 | /* resize first */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4036 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 4037 | Py_ssize_t needed = (targetsize - extrachars) + \ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4038 | (targetsize << 2); |
| 4039 | extrachars += needed; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4040 | /* XXX overflow detection missing */ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4041 | if (_PyUnicode_Resize(&v, |
| 4042 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 4043 | Py_DECREF(x); |
| 4044 | goto onError; |
| 4045 | } |
| 4046 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 4047 | } |
| 4048 | Py_UNICODE_COPY(p, |
| 4049 | PyUnicode_AS_UNICODE(x), |
| 4050 | targetsize); |
| 4051 | p += targetsize; |
| 4052 | extrachars -= targetsize; |
| 4053 | } |
| 4054 | /* 1-0 mapping: skip the character */ |
| 4055 | } |
| 4056 | else { |
| 4057 | /* wrong return value */ |
| 4058 | PyErr_SetString(PyExc_TypeError, |
| 4059 | "character mapping must return integer, None or unicode"); |
| 4060 | Py_DECREF(x); |
| 4061 | goto onError; |
| 4062 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4063 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4064 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4065 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4066 | } |
| 4067 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4068 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4069 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4070 | Py_XDECREF(errorHandler); |
| 4071 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4072 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4073 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4074 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4075 | Py_XDECREF(errorHandler); |
| 4076 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4077 | Py_XDECREF(v); |
| 4078 | return NULL; |
| 4079 | } |
| 4080 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4081 | /* Charmap encoding: the lookup table */ |
| 4082 | |
| 4083 | struct encoding_map{ |
| 4084 | PyObject_HEAD |
| 4085 | unsigned char level1[32]; |
| 4086 | int count2, count3; |
| 4087 | unsigned char level23[1]; |
| 4088 | }; |
| 4089 | |
| 4090 | static PyObject* |
| 4091 | encoding_map_size(PyObject *obj, PyObject* args) |
| 4092 | { |
| 4093 | struct encoding_map *map = (struct encoding_map*)obj; |
| 4094 | return PyInt_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
| 4095 | 128*map->count3); |
| 4096 | } |
| 4097 | |
| 4098 | static PyMethodDef encoding_map_methods[] = { |
| 4099 | {"size", encoding_map_size, METH_NOARGS, |
| 4100 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 4101 | { 0 } |
| 4102 | }; |
| 4103 | |
| 4104 | static void |
| 4105 | encoding_map_dealloc(PyObject* o) |
| 4106 | { |
| 4107 | PyObject_FREE(o); |
| 4108 | } |
| 4109 | |
| 4110 | static PyTypeObject EncodingMapType = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 4111 | PyVarObject_HEAD_INIT(NULL, 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4112 | "EncodingMap", /*tp_name*/ |
| 4113 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 4114 | 0, /*tp_itemsize*/ |
| 4115 | /* methods */ |
| 4116 | encoding_map_dealloc, /*tp_dealloc*/ |
| 4117 | 0, /*tp_print*/ |
| 4118 | 0, /*tp_getattr*/ |
| 4119 | 0, /*tp_setattr*/ |
| 4120 | 0, /*tp_compare*/ |
| 4121 | 0, /*tp_repr*/ |
| 4122 | 0, /*tp_as_number*/ |
| 4123 | 0, /*tp_as_sequence*/ |
| 4124 | 0, /*tp_as_mapping*/ |
| 4125 | 0, /*tp_hash*/ |
| 4126 | 0, /*tp_call*/ |
| 4127 | 0, /*tp_str*/ |
| 4128 | 0, /*tp_getattro*/ |
| 4129 | 0, /*tp_setattro*/ |
| 4130 | 0, /*tp_as_buffer*/ |
| 4131 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 4132 | 0, /*tp_doc*/ |
| 4133 | 0, /*tp_traverse*/ |
| 4134 | 0, /*tp_clear*/ |
| 4135 | 0, /*tp_richcompare*/ |
| 4136 | 0, /*tp_weaklistoffset*/ |
| 4137 | 0, /*tp_iter*/ |
| 4138 | 0, /*tp_iternext*/ |
| 4139 | encoding_map_methods, /*tp_methods*/ |
| 4140 | 0, /*tp_members*/ |
| 4141 | 0, /*tp_getset*/ |
| 4142 | 0, /*tp_base*/ |
| 4143 | 0, /*tp_dict*/ |
| 4144 | 0, /*tp_descr_get*/ |
| 4145 | 0, /*tp_descr_set*/ |
| 4146 | 0, /*tp_dictoffset*/ |
| 4147 | 0, /*tp_init*/ |
| 4148 | 0, /*tp_alloc*/ |
| 4149 | 0, /*tp_new*/ |
| 4150 | 0, /*tp_free*/ |
| 4151 | 0, /*tp_is_gc*/ |
| 4152 | }; |
| 4153 | |
| 4154 | PyObject* |
| 4155 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 4156 | { |
| 4157 | Py_UNICODE *decode; |
| 4158 | PyObject *result; |
| 4159 | struct encoding_map *mresult; |
| 4160 | int i; |
| 4161 | int need_dict = 0; |
| 4162 | unsigned char level1[32]; |
| 4163 | unsigned char level2[512]; |
| 4164 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 4165 | int count2 = 0, count3 = 0; |
| 4166 | |
| 4167 | if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { |
| 4168 | PyErr_BadArgument(); |
| 4169 | return NULL; |
| 4170 | } |
| 4171 | decode = PyUnicode_AS_UNICODE(string); |
| 4172 | memset(level1, 0xFF, sizeof level1); |
| 4173 | memset(level2, 0xFF, sizeof level2); |
| 4174 | |
| 4175 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 4176 | or if there are non-BMP characters, we need to use |
| 4177 | a mapping dictionary. */ |
| 4178 | if (decode[0] != 0) |
| 4179 | need_dict = 1; |
| 4180 | for (i = 1; i < 256; i++) { |
| 4181 | int l1, l2; |
| 4182 | if (decode[i] == 0 |
| 4183 | #ifdef Py_UNICODE_WIDE |
| 4184 | || decode[i] > 0xFFFF |
| 4185 | #endif |
| 4186 | ) { |
| 4187 | need_dict = 1; |
| 4188 | break; |
| 4189 | } |
| 4190 | if (decode[i] == 0xFFFE) |
| 4191 | /* unmapped character */ |
| 4192 | continue; |
| 4193 | l1 = decode[i] >> 11; |
| 4194 | l2 = decode[i] >> 7; |
| 4195 | if (level1[l1] == 0xFF) |
| 4196 | level1[l1] = count2++; |
| 4197 | if (level2[l2] == 0xFF) |
| 4198 | level2[l2] = count3++; |
| 4199 | } |
| 4200 | |
| 4201 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 4202 | need_dict = 1; |
| 4203 | |
| 4204 | if (need_dict) { |
| 4205 | PyObject *result = PyDict_New(); |
| 4206 | PyObject *key, *value; |
| 4207 | if (!result) |
| 4208 | return NULL; |
| 4209 | for (i = 0; i < 256; i++) { |
| 4210 | key = value = NULL; |
| 4211 | key = PyInt_FromLong(decode[i]); |
| 4212 | value = PyInt_FromLong(i); |
| 4213 | if (!key || !value) |
| 4214 | goto failed1; |
| 4215 | if (PyDict_SetItem(result, key, value) == -1) |
| 4216 | goto failed1; |
| 4217 | Py_DECREF(key); |
| 4218 | Py_DECREF(value); |
| 4219 | } |
| 4220 | return result; |
| 4221 | failed1: |
| 4222 | Py_XDECREF(key); |
| 4223 | Py_XDECREF(value); |
| 4224 | Py_DECREF(result); |
| 4225 | return NULL; |
| 4226 | } |
| 4227 | |
| 4228 | /* Create a three-level trie */ |
| 4229 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 4230 | 16*count2 + 128*count3 - 1); |
| 4231 | if (!result) |
| 4232 | return PyErr_NoMemory(); |
| 4233 | PyObject_Init(result, &EncodingMapType); |
| 4234 | mresult = (struct encoding_map*)result; |
| 4235 | mresult->count2 = count2; |
| 4236 | mresult->count3 = count3; |
| 4237 | mlevel1 = mresult->level1; |
| 4238 | mlevel2 = mresult->level23; |
| 4239 | mlevel3 = mresult->level23 + 16*count2; |
| 4240 | memcpy(mlevel1, level1, 32); |
| 4241 | memset(mlevel2, 0xFF, 16*count2); |
| 4242 | memset(mlevel3, 0, 128*count3); |
| 4243 | count3 = 0; |
| 4244 | for (i = 1; i < 256; i++) { |
| 4245 | int o1, o2, o3, i2, i3; |
| 4246 | if (decode[i] == 0xFFFE) |
| 4247 | /* unmapped character */ |
| 4248 | continue; |
| 4249 | o1 = decode[i]>>11; |
| 4250 | o2 = (decode[i]>>7) & 0xF; |
| 4251 | i2 = 16*mlevel1[o1] + o2; |
| 4252 | if (mlevel2[i2] == 0xFF) |
| 4253 | mlevel2[i2] = count3++; |
| 4254 | o3 = decode[i] & 0x7F; |
| 4255 | i3 = 128*mlevel2[i2] + o3; |
| 4256 | mlevel3[i3] = i; |
| 4257 | } |
| 4258 | return result; |
| 4259 | } |
| 4260 | |
| 4261 | static int |
| 4262 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 4263 | { |
| 4264 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 4265 | int l1 = c>>11; |
| 4266 | int l2 = (c>>7) & 0xF; |
| 4267 | int l3 = c & 0x7F; |
| 4268 | int i; |
| 4269 | |
| 4270 | #ifdef Py_UNICODE_WIDE |
| 4271 | if (c > 0xFFFF) { |
| 4272 | return -1; |
| 4273 | } |
| 4274 | #endif |
| 4275 | if (c == 0) |
| 4276 | return 0; |
| 4277 | /* level 1*/ |
| 4278 | i = map->level1[l1]; |
| 4279 | if (i == 0xFF) { |
| 4280 | return -1; |
| 4281 | } |
| 4282 | /* level 2*/ |
| 4283 | i = map->level23[16*i+l2]; |
| 4284 | if (i == 0xFF) { |
| 4285 | return -1; |
| 4286 | } |
| 4287 | /* level 3 */ |
| 4288 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 4289 | if (i == 0) { |
| 4290 | return -1; |
| 4291 | } |
| 4292 | return i; |
| 4293 | } |
| 4294 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4295 | /* Lookup the character ch in the mapping. If the character |
| 4296 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 4297 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4298 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4299 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4300 | PyObject *w = PyInt_FromLong((long)c); |
| 4301 | PyObject *x; |
| 4302 | |
| 4303 | if (w == NULL) |
| 4304 | return NULL; |
| 4305 | x = PyObject_GetItem(mapping, w); |
| 4306 | Py_DECREF(w); |
| 4307 | if (x == NULL) { |
| 4308 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4309 | /* No mapping found means: mapping is undefined. */ |
| 4310 | PyErr_Clear(); |
| 4311 | x = Py_None; |
| 4312 | Py_INCREF(x); |
| 4313 | return x; |
| 4314 | } else |
| 4315 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4316 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 4317 | else if (x == Py_None) |
| 4318 | return x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4319 | else if (PyInt_Check(x)) { |
| 4320 | long value = PyInt_AS_LONG(x); |
| 4321 | if (value < 0 || value > 255) { |
| 4322 | PyErr_SetString(PyExc_TypeError, |
| 4323 | "character mapping must be in range(256)"); |
| 4324 | Py_DECREF(x); |
| 4325 | return NULL; |
| 4326 | } |
| 4327 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4328 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4329 | else if (PyString_Check(x)) |
| 4330 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4331 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4332 | /* wrong return value */ |
Walter Dörwald | 580ceed | 2007-05-09 10:39:19 +0000 | [diff] [blame] | 4333 | PyErr_Format(PyExc_TypeError, |
| 4334 | "character mapping must return integer, None or str8, not %.400s", |
| 4335 | x->ob_type->tp_name); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4336 | Py_DECREF(x); |
| 4337 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4338 | } |
| 4339 | } |
| 4340 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4341 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4342 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4343 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4344 | Py_ssize_t outsize = PyString_GET_SIZE(*outobj); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4345 | /* exponentially overallocate to minimize reallocations */ |
| 4346 | if (requiredsize < 2*outsize) |
| 4347 | requiredsize = 2*outsize; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4348 | if (_PyString_Resize(outobj, requiredsize)) |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4349 | return -1; |
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, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +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; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4368 | Py_ssize_t outsize = PyString_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; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 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; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4378 | outstart = PyString_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 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4397 | outstart = PyString_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 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4409 | outstart = PyString_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, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +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 */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4560 | res = PyString_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 */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +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, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +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 */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4585 | if (respos<PyString_GET_SIZE(res)) |
| 4586 | _PyString_Resize(&res, respos); |
| 4587 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4588 | Py_XDECREF(exc); |
| 4589 | Py_XDECREF(errorHandler); |
| 4590 | return res; |
| 4591 | |
| 4592 | onError: |
| 4593 | Py_XDECREF(res); |
| 4594 | Py_XDECREF(exc); |
| 4595 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4596 | return NULL; |
| 4597 | } |
| 4598 | |
| 4599 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
| 4600 | PyObject *mapping) |
| 4601 | { |
| 4602 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
| 4603 | PyErr_BadArgument(); |
| 4604 | return NULL; |
| 4605 | } |
| 4606 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
| 4607 | PyUnicode_GET_SIZE(unicode), |
| 4608 | mapping, |
| 4609 | NULL); |
| 4610 | } |
| 4611 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4612 | /* create or adjust a UnicodeTranslateError */ |
| 4613 | static void make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4614 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4615 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4616 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4617 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4618 | if (*exceptionObject == NULL) { |
| 4619 | *exceptionObject = PyUnicodeTranslateError_Create( |
| 4620 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4621 | } |
| 4622 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4623 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 4624 | goto onError; |
| 4625 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 4626 | goto onError; |
| 4627 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 4628 | goto onError; |
| 4629 | return; |
| 4630 | onError: |
| 4631 | Py_DECREF(*exceptionObject); |
| 4632 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4633 | } |
| 4634 | } |
| 4635 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4636 | /* raises a UnicodeTranslateError */ |
| 4637 | static void raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4638 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4639 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4640 | const char *reason) |
| 4641 | { |
| 4642 | make_translate_exception(exceptionObject, |
| 4643 | unicode, size, startpos, endpos, reason); |
| 4644 | if (*exceptionObject != NULL) |
| 4645 | PyCodec_StrictErrors(*exceptionObject); |
| 4646 | } |
| 4647 | |
| 4648 | /* error handling callback helper: |
| 4649 | build arguments, call the callback and check the arguments, |
| 4650 | put the result into newpos and return the replacement string, which |
| 4651 | has to be freed by the caller */ |
| 4652 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
| 4653 | PyObject **errorHandler, |
| 4654 | const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4655 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 4656 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4657 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4658 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4659 | 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] | 4660 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4661 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4662 | PyObject *restuple; |
| 4663 | PyObject *resunicode; |
| 4664 | |
| 4665 | if (*errorHandler == NULL) { |
| 4666 | *errorHandler = PyCodec_LookupError(errors); |
| 4667 | if (*errorHandler == NULL) |
| 4668 | return NULL; |
| 4669 | } |
| 4670 | |
| 4671 | make_translate_exception(exceptionObject, |
| 4672 | unicode, size, startpos, endpos, reason); |
| 4673 | if (*exceptionObject == NULL) |
| 4674 | return NULL; |
| 4675 | |
| 4676 | restuple = PyObject_CallFunctionObjArgs( |
| 4677 | *errorHandler, *exceptionObject, NULL); |
| 4678 | if (restuple == NULL) |
| 4679 | return NULL; |
| 4680 | if (!PyTuple_Check(restuple)) { |
| 4681 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 4682 | Py_DECREF(restuple); |
| 4683 | return NULL; |
| 4684 | } |
| 4685 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4686 | &resunicode, &i_newpos)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4687 | Py_DECREF(restuple); |
| 4688 | return NULL; |
| 4689 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4690 | if (i_newpos<0) |
| 4691 | *newpos = size+i_newpos; |
| 4692 | else |
| 4693 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4694 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 4695 | 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] | 4696 | Py_DECREF(restuple); |
| 4697 | return NULL; |
| 4698 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4699 | Py_INCREF(resunicode); |
| 4700 | Py_DECREF(restuple); |
| 4701 | return resunicode; |
| 4702 | } |
| 4703 | |
| 4704 | /* Lookup the character ch in the mapping and put the result in result, |
| 4705 | which must be decrefed by the caller. |
| 4706 | Return 0 on success, -1 on error */ |
| 4707 | static |
| 4708 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 4709 | { |
| 4710 | PyObject *w = PyInt_FromLong((long)c); |
| 4711 | PyObject *x; |
| 4712 | |
| 4713 | if (w == NULL) |
| 4714 | return -1; |
| 4715 | x = PyObject_GetItem(mapping, w); |
| 4716 | Py_DECREF(w); |
| 4717 | if (x == NULL) { |
| 4718 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4719 | /* No mapping found means: use 1:1 mapping. */ |
| 4720 | PyErr_Clear(); |
| 4721 | *result = NULL; |
| 4722 | return 0; |
| 4723 | } else |
| 4724 | return -1; |
| 4725 | } |
| 4726 | else if (x == Py_None) { |
| 4727 | *result = x; |
| 4728 | return 0; |
| 4729 | } |
| 4730 | else if (PyInt_Check(x)) { |
| 4731 | long value = PyInt_AS_LONG(x); |
| 4732 | long max = PyUnicode_GetMax(); |
| 4733 | if (value < 0 || value > max) { |
| 4734 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 4735 | "character mapping must be in range(0x%x)", max+1); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4736 | Py_DECREF(x); |
| 4737 | return -1; |
| 4738 | } |
| 4739 | *result = x; |
| 4740 | return 0; |
| 4741 | } |
| 4742 | else if (PyUnicode_Check(x)) { |
| 4743 | *result = x; |
| 4744 | return 0; |
| 4745 | } |
| 4746 | else { |
| 4747 | /* wrong return value */ |
| 4748 | PyErr_SetString(PyExc_TypeError, |
| 4749 | "character mapping must return integer, None or unicode"); |
Walter Dörwald | 150523e | 2003-08-15 16:52:19 +0000 | [diff] [blame] | 4750 | Py_DECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4751 | return -1; |
| 4752 | } |
| 4753 | } |
| 4754 | /* ensure that *outobj is at least requiredsize characters long, |
| 4755 | if not reallocate and adjust various state variables. |
| 4756 | Return 0 on success, -1 on error */ |
| 4757 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4758 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4759 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4760 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4761 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4762 | if (requiredsize > oldsize) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4763 | /* remember old output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4764 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4765 | /* exponentially overallocate to minimize reallocations */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4766 | if (requiredsize < 2 * oldsize) |
| 4767 | requiredsize = 2 * oldsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 4768 | if (_PyUnicode_Resize(outobj, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4769 | return -1; |
| 4770 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4771 | } |
| 4772 | return 0; |
| 4773 | } |
| 4774 | /* lookup the character, put the result in the output string and adjust |
| 4775 | various state variables. Return a new reference to the object that |
| 4776 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 4777 | undefined (in which case no character was written). |
| 4778 | The called must decref result. |
| 4779 | Return 0 on success, -1 on error. */ |
| 4780 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4781 | 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] | 4782 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4783 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4784 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4785 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4786 | return -1; |
| 4787 | if (*res==NULL) { |
| 4788 | /* not found => default to 1:1 mapping */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4789 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4790 | } |
| 4791 | else if (*res==Py_None) |
| 4792 | ; |
| 4793 | else if (PyInt_Check(*res)) { |
| 4794 | /* no overflow check, because we know that the space is enough */ |
| 4795 | *(*outp)++ = (Py_UNICODE)PyInt_AS_LONG(*res); |
| 4796 | } |
| 4797 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4798 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4799 | if (repsize==1) { |
| 4800 | /* no overflow check, because we know that the space is enough */ |
| 4801 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 4802 | } |
| 4803 | else if (repsize!=0) { |
| 4804 | /* more than one character */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4805 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
Walter Dörwald | cd736e7 | 2004-02-05 17:36:00 +0000 | [diff] [blame] | 4806 | (insize - (curinp-startinp)) + |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4807 | repsize - 1; |
| 4808 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4809 | return -1; |
| 4810 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 4811 | *outp += repsize; |
| 4812 | } |
| 4813 | } |
| 4814 | else |
| 4815 | return -1; |
| 4816 | return 0; |
| 4817 | } |
| 4818 | |
| 4819 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4820 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4821 | PyObject *mapping, |
| 4822 | const char *errors) |
| 4823 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4824 | /* output object */ |
| 4825 | PyObject *res = NULL; |
| 4826 | /* pointers to the beginning and end+1 of input */ |
| 4827 | const Py_UNICODE *startp = p; |
| 4828 | const Py_UNICODE *endp = p + size; |
| 4829 | /* pointer into the output */ |
| 4830 | Py_UNICODE *str; |
| 4831 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4832 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4833 | char *reason = "character maps to <undefined>"; |
| 4834 | PyObject *errorHandler = NULL; |
| 4835 | PyObject *exc = NULL; |
| 4836 | /* the following variable is used for caching string comparisons |
| 4837 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 4838 | * 3=ignore, 4=xmlcharrefreplace */ |
| 4839 | int known_errorHandler = -1; |
| 4840 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4841 | if (mapping == NULL) { |
| 4842 | PyErr_BadArgument(); |
| 4843 | return NULL; |
| 4844 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4845 | |
| 4846 | /* allocate enough for a simple 1:1 translation without |
| 4847 | replacements, if we need more, we'll resize */ |
| 4848 | res = PyUnicode_FromUnicode(NULL, size); |
| 4849 | if (res == NULL) |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4850 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4851 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4852 | return res; |
| 4853 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4854 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4855 | while (p<endp) { |
| 4856 | /* try to encode it */ |
| 4857 | PyObject *x = NULL; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4858 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4859 | Py_XDECREF(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4860 | goto onError; |
| 4861 | } |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 4862 | Py_XDECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4863 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 4864 | ++p; |
| 4865 | else { /* untranslatable character */ |
| 4866 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4867 | Py_ssize_t repsize; |
| 4868 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4869 | Py_UNICODE *uni2; |
| 4870 | /* startpos for collecting untranslatable chars */ |
| 4871 | const Py_UNICODE *collstart = p; |
| 4872 | const Py_UNICODE *collend = p+1; |
| 4873 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4874 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4875 | /* find all untranslatable characters */ |
| 4876 | while (collend < endp) { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4877 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4878 | goto onError; |
| 4879 | Py_XDECREF(x); |
| 4880 | if (x!=Py_None) |
| 4881 | break; |
| 4882 | ++collend; |
| 4883 | } |
| 4884 | /* cache callback name lookup |
| 4885 | * (if not done yet, i.e. it's the first error) */ |
| 4886 | if (known_errorHandler==-1) { |
| 4887 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4888 | known_errorHandler = 1; |
| 4889 | else if (!strcmp(errors, "replace")) |
| 4890 | known_errorHandler = 2; |
| 4891 | else if (!strcmp(errors, "ignore")) |
| 4892 | known_errorHandler = 3; |
| 4893 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4894 | known_errorHandler = 4; |
| 4895 | else |
| 4896 | known_errorHandler = 0; |
| 4897 | } |
| 4898 | switch (known_errorHandler) { |
| 4899 | case 1: /* strict */ |
| 4900 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
| 4901 | goto onError; |
| 4902 | case 2: /* replace */ |
| 4903 | /* No need to check for space, this is a 1:1 replacement */ |
| 4904 | for (coll = collstart; coll<collend; ++coll) |
| 4905 | *str++ = '?'; |
| 4906 | /* fall through */ |
| 4907 | case 3: /* ignore */ |
| 4908 | p = collend; |
| 4909 | break; |
| 4910 | case 4: /* xmlcharrefreplace */ |
| 4911 | /* generate replacement (temporarily (mis)uses p) */ |
| 4912 | for (p = collstart; p < collend; ++p) { |
| 4913 | char buffer[2+29+1+1]; |
| 4914 | char *cp; |
| 4915 | sprintf(buffer, "&#%d;", (int)*p); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4916 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4917 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 4918 | goto onError; |
| 4919 | for (cp = buffer; *cp; ++cp) |
| 4920 | *str++ = *cp; |
| 4921 | } |
| 4922 | p = collend; |
| 4923 | break; |
| 4924 | default: |
| 4925 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 4926 | reason, startp, size, &exc, |
| 4927 | collstart-startp, collend-startp, &newpos); |
| 4928 | if (repunicode == NULL) |
| 4929 | goto onError; |
| 4930 | /* generate replacement */ |
| 4931 | repsize = PyUnicode_GET_SIZE(repunicode); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4932 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4933 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 4934 | Py_DECREF(repunicode); |
| 4935 | goto onError; |
| 4936 | } |
| 4937 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 4938 | *str++ = *uni2; |
| 4939 | p = startp + newpos; |
| 4940 | Py_DECREF(repunicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4941 | } |
| 4942 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4943 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4944 | /* Resize if we allocated to much */ |
| 4945 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4946 | if (respos<PyUnicode_GET_SIZE(res)) { |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 4947 | if (_PyUnicode_Resize(&res, respos) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 4948 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4949 | } |
| 4950 | Py_XDECREF(exc); |
| 4951 | Py_XDECREF(errorHandler); |
| 4952 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4953 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4954 | onError: |
| 4955 | Py_XDECREF(res); |
| 4956 | Py_XDECREF(exc); |
| 4957 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4958 | return NULL; |
| 4959 | } |
| 4960 | |
| 4961 | PyObject *PyUnicode_Translate(PyObject *str, |
| 4962 | PyObject *mapping, |
| 4963 | const char *errors) |
| 4964 | { |
| 4965 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4966 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4967 | str = PyUnicode_FromObject(str); |
| 4968 | if (str == NULL) |
| 4969 | goto onError; |
| 4970 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
| 4971 | PyUnicode_GET_SIZE(str), |
| 4972 | mapping, |
| 4973 | errors); |
| 4974 | Py_DECREF(str); |
| 4975 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4976 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4977 | onError: |
| 4978 | Py_XDECREF(str); |
| 4979 | return NULL; |
| 4980 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4981 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4982 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 4983 | |
| 4984 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4985 | Py_ssize_t length, |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4986 | char *output, |
| 4987 | const char *errors) |
| 4988 | { |
| 4989 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4990 | PyObject *errorHandler = NULL; |
| 4991 | PyObject *exc = NULL; |
| 4992 | const char *encoding = "decimal"; |
| 4993 | const char *reason = "invalid decimal Unicode string"; |
| 4994 | /* the following variable is used for caching string comparisons |
| 4995 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 4996 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4997 | |
| 4998 | if (output == NULL) { |
| 4999 | PyErr_BadArgument(); |
| 5000 | return -1; |
| 5001 | } |
| 5002 | |
| 5003 | p = s; |
| 5004 | end = s + length; |
| 5005 | while (p < end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5006 | register Py_UNICODE ch = *p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5007 | int decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5008 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5009 | Py_ssize_t repsize; |
| 5010 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5011 | Py_UNICODE *uni2; |
| 5012 | Py_UNICODE *collstart; |
| 5013 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5014 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5015 | if (Py_UNICODE_ISSPACE(ch)) { |
| 5016 | *output++ = ' '; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5017 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5018 | continue; |
| 5019 | } |
| 5020 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5021 | if (decimal >= 0) { |
| 5022 | *output++ = '0' + decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5023 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5024 | continue; |
| 5025 | } |
Guido van Rossum | ba47704 | 2000-04-06 18:18:10 +0000 | [diff] [blame] | 5026 | if (0 < ch && ch < 256) { |
Guido van Rossum | 42c29aa | 2000-05-03 23:58:29 +0000 | [diff] [blame] | 5027 | *output++ = (char)ch; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5028 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5029 | continue; |
| 5030 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5031 | /* All other characters are considered unencodable */ |
| 5032 | collstart = p; |
| 5033 | collend = p+1; |
| 5034 | while (collend < end) { |
| 5035 | if ((0 < *collend && *collend < 256) || |
| 5036 | !Py_UNICODE_ISSPACE(*collend) || |
| 5037 | Py_UNICODE_TODECIMAL(*collend)) |
| 5038 | break; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5039 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5040 | /* cache callback name lookup |
| 5041 | * (if not done yet, i.e. it's the first error) */ |
| 5042 | if (known_errorHandler==-1) { |
| 5043 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5044 | known_errorHandler = 1; |
| 5045 | else if (!strcmp(errors, "replace")) |
| 5046 | known_errorHandler = 2; |
| 5047 | else if (!strcmp(errors, "ignore")) |
| 5048 | known_errorHandler = 3; |
| 5049 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5050 | known_errorHandler = 4; |
| 5051 | else |
| 5052 | known_errorHandler = 0; |
| 5053 | } |
| 5054 | switch (known_errorHandler) { |
| 5055 | case 1: /* strict */ |
| 5056 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 5057 | goto onError; |
| 5058 | case 2: /* replace */ |
| 5059 | for (p = collstart; p < collend; ++p) |
| 5060 | *output++ = '?'; |
| 5061 | /* fall through */ |
| 5062 | case 3: /* ignore */ |
| 5063 | p = collend; |
| 5064 | break; |
| 5065 | case 4: /* xmlcharrefreplace */ |
| 5066 | /* generate replacement (temporarily (mis)uses p) */ |
| 5067 | for (p = collstart; p < collend; ++p) |
| 5068 | output += sprintf(output, "&#%d;", (int)*p); |
| 5069 | p = collend; |
| 5070 | break; |
| 5071 | default: |
| 5072 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 5073 | encoding, reason, s, length, &exc, |
| 5074 | collstart-s, collend-s, &newpos); |
| 5075 | if (repunicode == NULL) |
| 5076 | goto onError; |
| 5077 | /* generate replacement */ |
| 5078 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5079 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 5080 | Py_UNICODE ch = *uni2; |
| 5081 | if (Py_UNICODE_ISSPACE(ch)) |
| 5082 | *output++ = ' '; |
| 5083 | else { |
| 5084 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5085 | if (decimal >= 0) |
| 5086 | *output++ = '0' + decimal; |
| 5087 | else if (0 < ch && ch < 256) |
| 5088 | *output++ = (char)ch; |
| 5089 | else { |
| 5090 | Py_DECREF(repunicode); |
| 5091 | raise_encode_exception(&exc, encoding, |
| 5092 | s, length, collstart-s, collend-s, reason); |
| 5093 | goto onError; |
| 5094 | } |
| 5095 | } |
| 5096 | } |
| 5097 | p = s + newpos; |
| 5098 | Py_DECREF(repunicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5099 | } |
| 5100 | } |
| 5101 | /* 0-terminate the output string */ |
| 5102 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5103 | Py_XDECREF(exc); |
| 5104 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5105 | return 0; |
| 5106 | |
| 5107 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5108 | Py_XDECREF(exc); |
| 5109 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5110 | return -1; |
| 5111 | } |
| 5112 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5113 | /* --- Helpers ------------------------------------------------------------ */ |
| 5114 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 5115 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5116 | #include "stringlib/fastsearch.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5117 | #include "stringlib/count.h" |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 5118 | /* Include _ParseTupleFinds from find.h */ |
| 5119 | #define FROM_UNICODE |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5120 | #include "stringlib/find.h" |
| 5121 | #include "stringlib/partition.h" |
| 5122 | |
| 5123 | /* helper macro to fixup start/end slice values */ |
| 5124 | #define FIX_START_END(obj) \ |
| 5125 | if (start < 0) \ |
| 5126 | start += (obj)->length; \ |
| 5127 | if (start < 0) \ |
| 5128 | start = 0; \ |
| 5129 | if (end > (obj)->length) \ |
| 5130 | end = (obj)->length; \ |
| 5131 | if (end < 0) \ |
| 5132 | end += (obj)->length; \ |
| 5133 | if (end < 0) \ |
| 5134 | end = 0; |
| 5135 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5136 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5137 | PyObject *substr, |
| 5138 | Py_ssize_t start, |
| 5139 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5140 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5141 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5142 | PyUnicodeObject* str_obj; |
| 5143 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5144 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5145 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 5146 | if (!str_obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5147 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5148 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 5149 | if (!sub_obj) { |
| 5150 | Py_DECREF(str_obj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5151 | return -1; |
| 5152 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5153 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5154 | FIX_START_END(str_obj); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5155 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5156 | result = stringlib_count( |
| 5157 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length |
| 5158 | ); |
| 5159 | |
| 5160 | Py_DECREF(sub_obj); |
| 5161 | Py_DECREF(str_obj); |
| 5162 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5163 | return result; |
| 5164 | } |
| 5165 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5166 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5167 | PyObject *sub, |
| 5168 | Py_ssize_t start, |
| 5169 | Py_ssize_t end, |
| 5170 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5171 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5172 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5173 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5174 | str = PyUnicode_FromObject(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5175 | if (!str) |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 5176 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5177 | sub = PyUnicode_FromObject(sub); |
| 5178 | if (!sub) { |
Marc-André Lemburg | 4164439 | 2002-05-29 13:46:29 +0000 | [diff] [blame] | 5179 | Py_DECREF(str); |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 5180 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5181 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5182 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5183 | if (direction > 0) |
| 5184 | result = stringlib_find_slice( |
| 5185 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5186 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5187 | start, end |
| 5188 | ); |
| 5189 | else |
| 5190 | result = stringlib_rfind_slice( |
| 5191 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5192 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5193 | start, end |
| 5194 | ); |
| 5195 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5196 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5197 | Py_DECREF(sub); |
| 5198 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5199 | return result; |
| 5200 | } |
| 5201 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5202 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5203 | int tailmatch(PyUnicodeObject *self, |
| 5204 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5205 | Py_ssize_t start, |
| 5206 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5207 | int direction) |
| 5208 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5209 | if (substring->length == 0) |
| 5210 | return 1; |
| 5211 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5212 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5213 | |
| 5214 | end -= substring->length; |
| 5215 | if (end < start) |
| 5216 | return 0; |
| 5217 | |
| 5218 | if (direction > 0) { |
| 5219 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 5220 | return 1; |
| 5221 | } else { |
| 5222 | if (Py_UNICODE_MATCH(self, start, substring)) |
| 5223 | return 1; |
| 5224 | } |
| 5225 | |
| 5226 | return 0; |
| 5227 | } |
| 5228 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5229 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5230 | PyObject *substr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5231 | Py_ssize_t start, |
| 5232 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5233 | int direction) |
| 5234 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5235 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5236 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5237 | str = PyUnicode_FromObject(str); |
| 5238 | if (str == NULL) |
| 5239 | return -1; |
| 5240 | substr = PyUnicode_FromObject(substr); |
| 5241 | if (substr == NULL) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5242 | Py_DECREF(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5243 | return -1; |
| 5244 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5245 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5246 | result = tailmatch((PyUnicodeObject *)str, |
| 5247 | (PyUnicodeObject *)substr, |
| 5248 | start, end, direction); |
| 5249 | Py_DECREF(str); |
| 5250 | Py_DECREF(substr); |
| 5251 | return result; |
| 5252 | } |
| 5253 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5254 | /* Apply fixfct filter to the Unicode object self and return a |
| 5255 | reference to the modified object */ |
| 5256 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5257 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5258 | PyObject *fixup(PyUnicodeObject *self, |
| 5259 | int (*fixfct)(PyUnicodeObject *s)) |
| 5260 | { |
| 5261 | |
| 5262 | PyUnicodeObject *u; |
| 5263 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5264 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5265 | if (u == NULL) |
| 5266 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5267 | |
| 5268 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5269 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5270 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5271 | /* fixfct should return TRUE if it modified the buffer. If |
| 5272 | FALSE, return a reference to the original buffer instead |
| 5273 | (to save space, not time) */ |
| 5274 | Py_INCREF(self); |
| 5275 | Py_DECREF(u); |
| 5276 | return (PyObject*) self; |
| 5277 | } |
| 5278 | return (PyObject*) u; |
| 5279 | } |
| 5280 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5281 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5282 | int fixupper(PyUnicodeObject *self) |
| 5283 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5284 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5285 | Py_UNICODE *s = self->str; |
| 5286 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5287 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5288 | while (len-- > 0) { |
| 5289 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5290 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5291 | ch = Py_UNICODE_TOUPPER(*s); |
| 5292 | if (ch != *s) { |
| 5293 | status = 1; |
| 5294 | *s = ch; |
| 5295 | } |
| 5296 | s++; |
| 5297 | } |
| 5298 | |
| 5299 | return status; |
| 5300 | } |
| 5301 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5302 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5303 | int fixlower(PyUnicodeObject *self) |
| 5304 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5305 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5306 | Py_UNICODE *s = self->str; |
| 5307 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5308 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5309 | while (len-- > 0) { |
| 5310 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5311 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5312 | ch = Py_UNICODE_TOLOWER(*s); |
| 5313 | if (ch != *s) { |
| 5314 | status = 1; |
| 5315 | *s = ch; |
| 5316 | } |
| 5317 | s++; |
| 5318 | } |
| 5319 | |
| 5320 | return status; |
| 5321 | } |
| 5322 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5323 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5324 | int fixswapcase(PyUnicodeObject *self) |
| 5325 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5326 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5327 | Py_UNICODE *s = self->str; |
| 5328 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5329 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5330 | while (len-- > 0) { |
| 5331 | if (Py_UNICODE_ISUPPER(*s)) { |
| 5332 | *s = Py_UNICODE_TOLOWER(*s); |
| 5333 | status = 1; |
| 5334 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 5335 | *s = Py_UNICODE_TOUPPER(*s); |
| 5336 | status = 1; |
| 5337 | } |
| 5338 | s++; |
| 5339 | } |
| 5340 | |
| 5341 | return status; |
| 5342 | } |
| 5343 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5344 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5345 | int fixcapitalize(PyUnicodeObject *self) |
| 5346 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5347 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5348 | Py_UNICODE *s = self->str; |
| 5349 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5350 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5351 | if (len == 0) |
| 5352 | return 0; |
| 5353 | if (Py_UNICODE_ISLOWER(*s)) { |
| 5354 | *s = Py_UNICODE_TOUPPER(*s); |
| 5355 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5356 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5357 | s++; |
| 5358 | while (--len > 0) { |
| 5359 | if (Py_UNICODE_ISUPPER(*s)) { |
| 5360 | *s = Py_UNICODE_TOLOWER(*s); |
| 5361 | status = 1; |
| 5362 | } |
| 5363 | s++; |
| 5364 | } |
| 5365 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5366 | } |
| 5367 | |
| 5368 | static |
| 5369 | int fixtitle(PyUnicodeObject *self) |
| 5370 | { |
| 5371 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5372 | register Py_UNICODE *e; |
| 5373 | int previous_is_cased; |
| 5374 | |
| 5375 | /* Shortcut for single character strings */ |
| 5376 | if (PyUnicode_GET_SIZE(self) == 1) { |
| 5377 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 5378 | if (*p != ch) { |
| 5379 | *p = ch; |
| 5380 | return 1; |
| 5381 | } |
| 5382 | else |
| 5383 | return 0; |
| 5384 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5385 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5386 | e = p + PyUnicode_GET_SIZE(self); |
| 5387 | previous_is_cased = 0; |
| 5388 | for (; p < e; p++) { |
| 5389 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5390 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5391 | if (previous_is_cased) |
| 5392 | *p = Py_UNICODE_TOLOWER(ch); |
| 5393 | else |
| 5394 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5395 | |
| 5396 | if (Py_UNICODE_ISLOWER(ch) || |
| 5397 | Py_UNICODE_ISUPPER(ch) || |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5398 | Py_UNICODE_ISTITLE(ch)) |
| 5399 | previous_is_cased = 1; |
| 5400 | else |
| 5401 | previous_is_cased = 0; |
| 5402 | } |
| 5403 | return 1; |
| 5404 | } |
| 5405 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5406 | PyObject * |
| 5407 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5408 | { |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5409 | PyObject *internal_separator = NULL; |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 5410 | const Py_UNICODE blank = ' '; |
| 5411 | const Py_UNICODE *sep = ␣ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5412 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5413 | PyUnicodeObject *res = NULL; /* the result */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5414 | Py_ssize_t res_alloc = 100; /* # allocated bytes for string in res */ |
| 5415 | Py_ssize_t res_used; /* # used bytes */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5416 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 5417 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5418 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5419 | PyObject *item; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5420 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5421 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5422 | fseq = PySequence_Fast(seq, ""); |
| 5423 | if (fseq == NULL) { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5424 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5425 | } |
| 5426 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5427 | /* Grrrr. A codec may be invoked to convert str objects to |
| 5428 | * Unicode, and so it's possible to call back into Python code |
| 5429 | * during PyUnicode_FromObject(), and so it's possible for a sick |
| 5430 | * codec to change the size of fseq (if seq is a list). Therefore |
| 5431 | * we have to keep refetching the size -- can't assume seqlen |
| 5432 | * is invariant. |
| 5433 | */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5434 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 5435 | /* If empty sequence, return u"". */ |
| 5436 | if (seqlen == 0) { |
| 5437 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 5438 | goto Done; |
| 5439 | } |
| 5440 | /* If singleton sequence with an exact Unicode, return that. */ |
| 5441 | if (seqlen == 1) { |
| 5442 | item = PySequence_Fast_GET_ITEM(fseq, 0); |
| 5443 | if (PyUnicode_CheckExact(item)) { |
| 5444 | Py_INCREF(item); |
| 5445 | res = (PyUnicodeObject *)item; |
| 5446 | goto Done; |
| 5447 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5448 | } |
| 5449 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5450 | /* At least two items to join, or one that isn't exact Unicode. */ |
| 5451 | if (seqlen > 1) { |
| 5452 | /* Set up sep and seplen -- they're needed. */ |
| 5453 | if (separator == NULL) { |
| 5454 | sep = ␣ |
| 5455 | seplen = 1; |
| 5456 | } |
| 5457 | else { |
| 5458 | internal_separator = PyUnicode_FromObject(separator); |
| 5459 | if (internal_separator == NULL) |
| 5460 | goto onError; |
| 5461 | sep = PyUnicode_AS_UNICODE(internal_separator); |
| 5462 | seplen = PyUnicode_GET_SIZE(internal_separator); |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5463 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 5464 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5465 | } |
| 5466 | } |
| 5467 | |
| 5468 | /* Get space. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5469 | res = _PyUnicode_New(res_alloc); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5470 | if (res == NULL) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5471 | goto onError; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5472 | res_p = PyUnicode_AS_UNICODE(res); |
| 5473 | res_used = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5474 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5475 | for (i = 0; i < seqlen; ++i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5476 | Py_ssize_t itemlen; |
| 5477 | Py_ssize_t new_res_used; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5478 | |
| 5479 | item = PySequence_Fast_GET_ITEM(fseq, i); |
| 5480 | /* Convert item to Unicode. */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5481 | if (!PyUnicode_Check(item)) { |
| 5482 | PyErr_Format(PyExc_TypeError, |
| 5483 | "sequence item %zd: expected str instance," |
| 5484 | " %.80s found", |
| 5485 | i, Py_Type(item)->tp_name); |
| 5486 | goto onError; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5487 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5488 | item = PyUnicode_FromObject(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5489 | if (item == NULL) |
| 5490 | goto onError; |
| 5491 | /* We own a reference to item from here on. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5492 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5493 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 5494 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 5495 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5496 | /* 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] | 5497 | itemlen = PyUnicode_GET_SIZE(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5498 | new_res_used = res_used + itemlen; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5499 | if (new_res_used < 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5500 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5501 | if (i < seqlen - 1) { |
| 5502 | new_res_used += seplen; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5503 | if (new_res_used < 0) |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5504 | goto Overflow; |
| 5505 | } |
| 5506 | if (new_res_used > res_alloc) { |
| 5507 | /* double allocated size until it's big enough */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5508 | do { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5509 | res_alloc += res_alloc; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5510 | if (res_alloc <= 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5511 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5512 | } while (new_res_used > res_alloc); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5513 | if (_PyUnicode_Resize(&res, res_alloc) < 0) { |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 5514 | Py_DECREF(item); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5515 | goto onError; |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 5516 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5517 | res_p = PyUnicode_AS_UNICODE(res) + res_used; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5518 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5519 | |
| 5520 | /* Copy item, and maybe the separator. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5521 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5522 | res_p += itemlen; |
| 5523 | if (i < seqlen - 1) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5524 | Py_UNICODE_COPY(res_p, sep, seplen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5525 | res_p += seplen; |
| 5526 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5527 | Py_DECREF(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5528 | res_used = new_res_used; |
| 5529 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5530 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5531 | /* Shrink res to match the used area; this probably can't fail, |
| 5532 | * but it's cheap to check. |
| 5533 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5534 | if (_PyUnicode_Resize(&res, res_used) < 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5535 | goto onError; |
| 5536 | |
| 5537 | Done: |
| 5538 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5539 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5540 | return (PyObject *)res; |
| 5541 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5542 | Overflow: |
| 5543 | PyErr_SetString(PyExc_OverflowError, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5544 | "join() result is too long for a Python string"); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5545 | Py_DECREF(item); |
| 5546 | /* fall through */ |
| 5547 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5548 | onError: |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5549 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5550 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5551 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5552 | return NULL; |
| 5553 | } |
| 5554 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5555 | static |
| 5556 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5557 | Py_ssize_t left, |
| 5558 | Py_ssize_t right, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5559 | Py_UNICODE fill) |
| 5560 | { |
| 5561 | PyUnicodeObject *u; |
| 5562 | |
| 5563 | if (left < 0) |
| 5564 | left = 0; |
| 5565 | if (right < 0) |
| 5566 | right = 0; |
| 5567 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5568 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5569 | Py_INCREF(self); |
| 5570 | return self; |
| 5571 | } |
| 5572 | |
| 5573 | u = _PyUnicode_New(left + self->length + right); |
| 5574 | if (u) { |
| 5575 | if (left) |
| 5576 | Py_UNICODE_FILL(u->str, fill, left); |
| 5577 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 5578 | if (right) |
| 5579 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 5580 | } |
| 5581 | |
| 5582 | return u; |
| 5583 | } |
| 5584 | |
| 5585 | #define SPLIT_APPEND(data, left, right) \ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5586 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5587 | if (!str) \ |
| 5588 | goto onError; \ |
| 5589 | if (PyList_Append(list, str)) { \ |
| 5590 | Py_DECREF(str); \ |
| 5591 | goto onError; \ |
| 5592 | } \ |
| 5593 | else \ |
| 5594 | Py_DECREF(str); |
| 5595 | |
| 5596 | static |
| 5597 | PyObject *split_whitespace(PyUnicodeObject *self, |
| 5598 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5599 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5600 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5601 | register Py_ssize_t i; |
| 5602 | register Py_ssize_t j; |
| 5603 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5604 | PyObject *str; |
| 5605 | |
| 5606 | for (i = j = 0; i < len; ) { |
| 5607 | /* find a token */ |
| 5608 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 5609 | i++; |
| 5610 | j = i; |
| 5611 | while (i < len && !Py_UNICODE_ISSPACE(self->str[i])) |
| 5612 | i++; |
| 5613 | if (j < i) { |
| 5614 | if (maxcount-- <= 0) |
| 5615 | break; |
| 5616 | SPLIT_APPEND(self->str, j, i); |
| 5617 | while (i < len && Py_UNICODE_ISSPACE(self->str[i])) |
| 5618 | i++; |
| 5619 | j = i; |
| 5620 | } |
| 5621 | } |
| 5622 | if (j < len) { |
| 5623 | SPLIT_APPEND(self->str, j, len); |
| 5624 | } |
| 5625 | return list; |
| 5626 | |
| 5627 | onError: |
| 5628 | Py_DECREF(list); |
| 5629 | return NULL; |
| 5630 | } |
| 5631 | |
| 5632 | PyObject *PyUnicode_Splitlines(PyObject *string, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5633 | int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5634 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5635 | register Py_ssize_t i; |
| 5636 | register Py_ssize_t j; |
| 5637 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5638 | PyObject *list; |
| 5639 | PyObject *str; |
| 5640 | Py_UNICODE *data; |
| 5641 | |
| 5642 | string = PyUnicode_FromObject(string); |
| 5643 | if (string == NULL) |
| 5644 | return NULL; |
| 5645 | data = PyUnicode_AS_UNICODE(string); |
| 5646 | len = PyUnicode_GET_SIZE(string); |
| 5647 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5648 | list = PyList_New(0); |
| 5649 | if (!list) |
| 5650 | goto onError; |
| 5651 | |
| 5652 | for (i = j = 0; i < len; ) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5653 | Py_ssize_t eol; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5654 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5655 | /* Find a line and append it */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5656 | while (i < len && !BLOOM_LINEBREAK(data[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5657 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5658 | |
| 5659 | /* Skip the line break reading CRLF as one line break */ |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5660 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5661 | if (i < len) { |
| 5662 | if (data[i] == '\r' && i + 1 < len && |
| 5663 | data[i+1] == '\n') |
| 5664 | i += 2; |
| 5665 | else |
| 5666 | i++; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5667 | if (keepends) |
| 5668 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5669 | } |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5670 | SPLIT_APPEND(data, j, eol); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5671 | j = i; |
| 5672 | } |
| 5673 | if (j < len) { |
| 5674 | SPLIT_APPEND(data, j, len); |
| 5675 | } |
| 5676 | |
| 5677 | Py_DECREF(string); |
| 5678 | return list; |
| 5679 | |
| 5680 | onError: |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5681 | Py_XDECREF(list); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5682 | Py_DECREF(string); |
| 5683 | return NULL; |
| 5684 | } |
| 5685 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5686 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5687 | PyObject *split_char(PyUnicodeObject *self, |
| 5688 | PyObject *list, |
| 5689 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5690 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5691 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5692 | register Py_ssize_t i; |
| 5693 | register Py_ssize_t j; |
| 5694 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5695 | PyObject *str; |
| 5696 | |
| 5697 | for (i = j = 0; i < len; ) { |
| 5698 | if (self->str[i] == ch) { |
| 5699 | if (maxcount-- <= 0) |
| 5700 | break; |
| 5701 | SPLIT_APPEND(self->str, j, i); |
| 5702 | i = j = i + 1; |
| 5703 | } else |
| 5704 | i++; |
| 5705 | } |
| 5706 | if (j <= len) { |
| 5707 | SPLIT_APPEND(self->str, j, len); |
| 5708 | } |
| 5709 | return list; |
| 5710 | |
| 5711 | onError: |
| 5712 | Py_DECREF(list); |
| 5713 | return NULL; |
| 5714 | } |
| 5715 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5716 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5717 | PyObject *split_substring(PyUnicodeObject *self, |
| 5718 | PyObject *list, |
| 5719 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5720 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5721 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5722 | register Py_ssize_t i; |
| 5723 | register Py_ssize_t j; |
| 5724 | Py_ssize_t len = self->length; |
| 5725 | Py_ssize_t sublen = substring->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5726 | PyObject *str; |
| 5727 | |
Guido van Rossum | cda4f9a | 2000-12-19 02:23:19 +0000 | [diff] [blame] | 5728 | for (i = j = 0; i <= len - sublen; ) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5729 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 5730 | if (maxcount-- <= 0) |
| 5731 | break; |
| 5732 | SPLIT_APPEND(self->str, j, i); |
| 5733 | i = j = i + sublen; |
| 5734 | } else |
| 5735 | i++; |
| 5736 | } |
| 5737 | if (j <= len) { |
| 5738 | SPLIT_APPEND(self->str, j, len); |
| 5739 | } |
| 5740 | return list; |
| 5741 | |
| 5742 | onError: |
| 5743 | Py_DECREF(list); |
| 5744 | return NULL; |
| 5745 | } |
| 5746 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5747 | static |
| 5748 | PyObject *rsplit_whitespace(PyUnicodeObject *self, |
| 5749 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5750 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5751 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5752 | register Py_ssize_t i; |
| 5753 | register Py_ssize_t j; |
| 5754 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5755 | PyObject *str; |
| 5756 | |
| 5757 | for (i = j = len - 1; i >= 0; ) { |
| 5758 | /* find a token */ |
| 5759 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 5760 | i--; |
| 5761 | j = i; |
| 5762 | while (i >= 0 && !Py_UNICODE_ISSPACE(self->str[i])) |
| 5763 | i--; |
| 5764 | if (j > i) { |
| 5765 | if (maxcount-- <= 0) |
| 5766 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5767 | SPLIT_APPEND(self->str, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5768 | while (i >= 0 && Py_UNICODE_ISSPACE(self->str[i])) |
| 5769 | i--; |
| 5770 | j = i; |
| 5771 | } |
| 5772 | } |
| 5773 | if (j >= 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5774 | SPLIT_APPEND(self->str, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5775 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5776 | if (PyList_Reverse(list) < 0) |
| 5777 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5778 | return list; |
| 5779 | |
| 5780 | onError: |
| 5781 | Py_DECREF(list); |
| 5782 | return NULL; |
| 5783 | } |
| 5784 | |
| 5785 | static |
| 5786 | PyObject *rsplit_char(PyUnicodeObject *self, |
| 5787 | PyObject *list, |
| 5788 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5789 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5790 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5791 | register Py_ssize_t i; |
| 5792 | register Py_ssize_t j; |
| 5793 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5794 | PyObject *str; |
| 5795 | |
| 5796 | for (i = j = len - 1; i >= 0; ) { |
| 5797 | if (self->str[i] == ch) { |
| 5798 | if (maxcount-- <= 0) |
| 5799 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5800 | SPLIT_APPEND(self->str, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5801 | j = i = i - 1; |
| 5802 | } else |
| 5803 | i--; |
| 5804 | } |
Hye-Shik Chang | 7fc4cf5 | 2003-12-23 09:10:16 +0000 | [diff] [blame] | 5805 | if (j >= -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5806 | SPLIT_APPEND(self->str, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5807 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5808 | if (PyList_Reverse(list) < 0) |
| 5809 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5810 | return list; |
| 5811 | |
| 5812 | onError: |
| 5813 | Py_DECREF(list); |
| 5814 | return NULL; |
| 5815 | } |
| 5816 | |
| 5817 | static |
| 5818 | PyObject *rsplit_substring(PyUnicodeObject *self, |
| 5819 | PyObject *list, |
| 5820 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5821 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5822 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5823 | register Py_ssize_t i; |
| 5824 | register Py_ssize_t j; |
| 5825 | Py_ssize_t len = self->length; |
| 5826 | Py_ssize_t sublen = substring->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5827 | PyObject *str; |
| 5828 | |
| 5829 | for (i = len - sublen, j = len; i >= 0; ) { |
| 5830 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 5831 | if (maxcount-- <= 0) |
| 5832 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5833 | SPLIT_APPEND(self->str, i + sublen, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5834 | j = i; |
| 5835 | i -= sublen; |
| 5836 | } else |
| 5837 | i--; |
| 5838 | } |
| 5839 | if (j >= 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5840 | SPLIT_APPEND(self->str, 0, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5841 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5842 | if (PyList_Reverse(list) < 0) |
| 5843 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5844 | return list; |
| 5845 | |
| 5846 | onError: |
| 5847 | Py_DECREF(list); |
| 5848 | return NULL; |
| 5849 | } |
| 5850 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5851 | #undef SPLIT_APPEND |
| 5852 | |
| 5853 | static |
| 5854 | PyObject *split(PyUnicodeObject *self, |
| 5855 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5856 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5857 | { |
| 5858 | PyObject *list; |
| 5859 | |
| 5860 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5861 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5862 | |
| 5863 | list = PyList_New(0); |
| 5864 | if (!list) |
| 5865 | return NULL; |
| 5866 | |
| 5867 | if (substring == NULL) |
| 5868 | return split_whitespace(self,list,maxcount); |
| 5869 | |
| 5870 | else if (substring->length == 1) |
| 5871 | return split_char(self,list,substring->str[0],maxcount); |
| 5872 | |
| 5873 | else if (substring->length == 0) { |
| 5874 | Py_DECREF(list); |
| 5875 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 5876 | return NULL; |
| 5877 | } |
| 5878 | else |
| 5879 | return split_substring(self,list,substring,maxcount); |
| 5880 | } |
| 5881 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5882 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5883 | PyObject *rsplit(PyUnicodeObject *self, |
| 5884 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5885 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5886 | { |
| 5887 | PyObject *list; |
| 5888 | |
| 5889 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5890 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5891 | |
| 5892 | list = PyList_New(0); |
| 5893 | if (!list) |
| 5894 | return NULL; |
| 5895 | |
| 5896 | if (substring == NULL) |
| 5897 | return rsplit_whitespace(self,list,maxcount); |
| 5898 | |
| 5899 | else if (substring->length == 1) |
| 5900 | return rsplit_char(self,list,substring->str[0],maxcount); |
| 5901 | |
| 5902 | else if (substring->length == 0) { |
| 5903 | Py_DECREF(list); |
| 5904 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 5905 | return NULL; |
| 5906 | } |
| 5907 | else |
| 5908 | return rsplit_substring(self,list,substring,maxcount); |
| 5909 | } |
| 5910 | |
| 5911 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5912 | PyObject *replace(PyUnicodeObject *self, |
| 5913 | PyUnicodeObject *str1, |
| 5914 | PyUnicodeObject *str2, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5915 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5916 | { |
| 5917 | PyUnicodeObject *u; |
| 5918 | |
| 5919 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5920 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5921 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5922 | if (str1->length == str2->length) { |
| 5923 | /* same length */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5924 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5925 | if (str1->length == 1) { |
| 5926 | /* replace characters */ |
| 5927 | Py_UNICODE u1, u2; |
| 5928 | if (!findchar(self->str, self->length, str1->str[0])) |
| 5929 | goto nothing; |
| 5930 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 5931 | if (!u) |
| 5932 | return NULL; |
| 5933 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5934 | u1 = str1->str[0]; |
| 5935 | u2 = str2->str[0]; |
| 5936 | for (i = 0; i < u->length; i++) |
| 5937 | if (u->str[i] == u1) { |
| 5938 | if (--maxcount < 0) |
| 5939 | break; |
| 5940 | u->str[i] = u2; |
| 5941 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5942 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5943 | i = fastsearch( |
| 5944 | self->str, self->length, str1->str, str1->length, FAST_SEARCH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5945 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5946 | if (i < 0) |
| 5947 | goto nothing; |
| 5948 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 5949 | if (!u) |
| 5950 | return NULL; |
| 5951 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5952 | while (i <= self->length - str1->length) |
| 5953 | if (Py_UNICODE_MATCH(self, i, str1)) { |
| 5954 | if (--maxcount < 0) |
| 5955 | break; |
| 5956 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 5957 | i += str1->length; |
| 5958 | } else |
| 5959 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5960 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5961 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5962 | |
| 5963 | Py_ssize_t n, i, j, e; |
| 5964 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5965 | Py_UNICODE *p; |
| 5966 | |
| 5967 | /* replace strings */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5968 | n = stringlib_count(self->str, self->length, str1->str, str1->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5969 | if (n > maxcount) |
| 5970 | n = maxcount; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5971 | if (n == 0) |
| 5972 | goto nothing; |
| 5973 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 5974 | delta = (str2->length - str1->length); |
| 5975 | if (delta == 0) { |
| 5976 | new_size = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5977 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5978 | product = n * (str2->length - str1->length); |
| 5979 | if ((product / (str2->length - str1->length)) != n) { |
| 5980 | PyErr_SetString(PyExc_OverflowError, |
| 5981 | "replace string is too long"); |
| 5982 | return NULL; |
| 5983 | } |
| 5984 | new_size = self->length + product; |
| 5985 | if (new_size < 0) { |
| 5986 | PyErr_SetString(PyExc_OverflowError, |
| 5987 | "replace string is too long"); |
| 5988 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5989 | } |
| 5990 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5991 | u = _PyUnicode_New(new_size); |
| 5992 | if (!u) |
| 5993 | return NULL; |
| 5994 | i = 0; |
| 5995 | p = u->str; |
| 5996 | e = self->length - str1->length; |
| 5997 | if (str1->length > 0) { |
| 5998 | while (n-- > 0) { |
| 5999 | /* look for next match */ |
| 6000 | j = i; |
| 6001 | while (j <= e) { |
| 6002 | if (Py_UNICODE_MATCH(self, j, str1)) |
| 6003 | break; |
| 6004 | j++; |
| 6005 | } |
| 6006 | if (j > i) { |
| 6007 | if (j > e) |
| 6008 | break; |
| 6009 | /* copy unchanged part [i:j] */ |
| 6010 | Py_UNICODE_COPY(p, self->str+i, j-i); |
| 6011 | p += j - i; |
| 6012 | } |
| 6013 | /* copy substitution string */ |
| 6014 | if (str2->length > 0) { |
| 6015 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6016 | p += str2->length; |
| 6017 | } |
| 6018 | i = j + str1->length; |
| 6019 | } |
| 6020 | if (i < self->length) |
| 6021 | /* copy tail [i:] */ |
| 6022 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6023 | } else { |
| 6024 | /* interleave */ |
| 6025 | while (n > 0) { |
| 6026 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6027 | p += str2->length; |
| 6028 | if (--n <= 0) |
| 6029 | break; |
| 6030 | *p++ = self->str[i++]; |
| 6031 | } |
| 6032 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6033 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6034 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6035 | return (PyObject *) u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6036 | |
| 6037 | nothing: |
| 6038 | /* nothing to replace; return original string (when possible) */ |
| 6039 | if (PyUnicode_CheckExact(self)) { |
| 6040 | Py_INCREF(self); |
| 6041 | return (PyObject *) self; |
| 6042 | } |
| 6043 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6044 | } |
| 6045 | |
| 6046 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 6047 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6048 | PyDoc_STRVAR(title__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6049 | "S.title() -> unicode\n\ |
| 6050 | \n\ |
| 6051 | 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] | 6052 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6053 | |
| 6054 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6055 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6056 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6057 | return fixup(self, fixtitle); |
| 6058 | } |
| 6059 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6060 | PyDoc_STRVAR(capitalize__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6061 | "S.capitalize() -> unicode\n\ |
| 6062 | \n\ |
| 6063 | 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] | 6064 | have upper case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6065 | |
| 6066 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6067 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6068 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6069 | return fixup(self, fixcapitalize); |
| 6070 | } |
| 6071 | |
| 6072 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6073 | PyDoc_STRVAR(capwords__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6074 | "S.capwords() -> unicode\n\ |
| 6075 | \n\ |
| 6076 | 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] | 6077 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6078 | |
| 6079 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6080 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6081 | { |
| 6082 | PyObject *list; |
| 6083 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6084 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6085 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6086 | /* Split into words */ |
| 6087 | list = split(self, NULL, -1); |
| 6088 | if (!list) |
| 6089 | return NULL; |
| 6090 | |
| 6091 | /* Capitalize each word */ |
| 6092 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 6093 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
| 6094 | fixcapitalize); |
| 6095 | if (item == NULL) |
| 6096 | goto onError; |
| 6097 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 6098 | PyList_SET_ITEM(list, i, item); |
| 6099 | } |
| 6100 | |
| 6101 | /* Join the words to form a new string */ |
| 6102 | item = PyUnicode_Join(NULL, list); |
| 6103 | |
| 6104 | onError: |
| 6105 | Py_DECREF(list); |
| 6106 | return (PyObject *)item; |
| 6107 | } |
| 6108 | #endif |
| 6109 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6110 | /* Argument converter. Coerces to a single unicode character */ |
| 6111 | |
| 6112 | static int |
| 6113 | convert_uc(PyObject *obj, void *addr) |
| 6114 | { |
| 6115 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 6116 | PyObject *uniobj; |
| 6117 | Py_UNICODE *unistr; |
| 6118 | |
| 6119 | uniobj = PyUnicode_FromObject(obj); |
| 6120 | if (uniobj == NULL) { |
| 6121 | PyErr_SetString(PyExc_TypeError, |
| 6122 | "The fill character cannot be converted to Unicode"); |
| 6123 | return 0; |
| 6124 | } |
| 6125 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 6126 | PyErr_SetString(PyExc_TypeError, |
| 6127 | "The fill character must be exactly one character long"); |
| 6128 | Py_DECREF(uniobj); |
| 6129 | return 0; |
| 6130 | } |
| 6131 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 6132 | *fillcharloc = unistr[0]; |
| 6133 | Py_DECREF(uniobj); |
| 6134 | return 1; |
| 6135 | } |
| 6136 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6137 | PyDoc_STRVAR(center__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6138 | "S.center(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6139 | \n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6140 | Return S centered in a Unicode string of length width. Padding is\n\ |
| 6141 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6142 | |
| 6143 | static PyObject * |
| 6144 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 6145 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6146 | Py_ssize_t marg, left; |
| 6147 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6148 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6149 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 6150 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6151 | return NULL; |
| 6152 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6153 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6154 | Py_INCREF(self); |
| 6155 | return (PyObject*) self; |
| 6156 | } |
| 6157 | |
| 6158 | marg = width - self->length; |
| 6159 | left = marg / 2 + (marg & width & 1); |
| 6160 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6161 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6162 | } |
| 6163 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6164 | #if 0 |
| 6165 | |
| 6166 | /* This code should go into some future Unicode collation support |
| 6167 | module. The basic comparison should compare ordinals on a naive |
Trent Mick | 20abf57 | 2000-08-12 22:14:34 +0000 | [diff] [blame] | 6168 | basis (this is what Java does and thus JPython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6169 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6170 | /* speedy UTF-16 code point order comparison */ |
| 6171 | /* gleaned from: */ |
| 6172 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 6173 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6174 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6175 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6176 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6177 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 6178 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6179 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6180 | }; |
| 6181 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6182 | static int |
| 6183 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6184 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6185 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6186 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6187 | Py_UNICODE *s1 = str1->str; |
| 6188 | Py_UNICODE *s2 = str2->str; |
| 6189 | |
| 6190 | len1 = str1->length; |
| 6191 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6192 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6193 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6194 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6195 | |
| 6196 | c1 = *s1++; |
| 6197 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6198 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6199 | if (c1 > (1<<11) * 26) |
| 6200 | c1 += utf16Fixup[c1>>11]; |
| 6201 | if (c2 > (1<<11) * 26) |
| 6202 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6203 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6204 | |
| 6205 | if (c1 != c2) |
| 6206 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6207 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6208 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6209 | } |
| 6210 | |
| 6211 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6212 | } |
| 6213 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6214 | #else |
| 6215 | |
| 6216 | static int |
| 6217 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6218 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6219 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6220 | |
| 6221 | Py_UNICODE *s1 = str1->str; |
| 6222 | Py_UNICODE *s2 = str2->str; |
| 6223 | |
| 6224 | len1 = str1->length; |
| 6225 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6226 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6227 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6228 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6229 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6230 | c1 = *s1++; |
| 6231 | c2 = *s2++; |
| 6232 | |
| 6233 | if (c1 != c2) |
| 6234 | return (c1 < c2) ? -1 : 1; |
| 6235 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6236 | len1--; len2--; |
| 6237 | } |
| 6238 | |
| 6239 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6240 | } |
| 6241 | |
| 6242 | #endif |
| 6243 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6244 | int PyUnicode_Compare(PyObject *left, |
| 6245 | PyObject *right) |
| 6246 | { |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6247 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) |
| 6248 | return unicode_compare((PyUnicodeObject *)left, |
| 6249 | (PyUnicodeObject *)right); |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6250 | PyErr_Format(PyExc_TypeError, |
| 6251 | "Can't compare %.100s and %.100s", |
| 6252 | left->ob_type->tp_name, |
| 6253 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6254 | return -1; |
| 6255 | } |
| 6256 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6257 | int |
| 6258 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 6259 | { |
| 6260 | int i; |
| 6261 | Py_UNICODE *id; |
| 6262 | assert(PyUnicode_Check(uni)); |
| 6263 | id = PyUnicode_AS_UNICODE(uni); |
| 6264 | /* Compare Unicode string and source character set string */ |
| 6265 | for (i = 0; id[i] && str[i]; i++) |
| 6266 | if (id[i] != str[i]) |
| 6267 | return ((int)id[i] < (int)str[i]) ? -1 : 1; |
| 6268 | if (id[i]) |
| 6269 | return 1; /* uni is longer */ |
| 6270 | if (str[i]) |
| 6271 | return -1; /* str is longer */ |
| 6272 | return 0; |
| 6273 | } |
| 6274 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6275 | PyObject *PyUnicode_RichCompare(PyObject *left, |
| 6276 | PyObject *right, |
| 6277 | int op) |
| 6278 | { |
| 6279 | int result; |
| 6280 | |
| 6281 | result = PyUnicode_Compare(left, right); |
| 6282 | if (result == -1 && PyErr_Occurred()) |
| 6283 | goto onError; |
| 6284 | |
| 6285 | /* Convert the return value to a Boolean */ |
| 6286 | switch (op) { |
| 6287 | case Py_EQ: |
| 6288 | result = (result == 0); |
| 6289 | break; |
| 6290 | case Py_NE: |
| 6291 | result = (result != 0); |
| 6292 | break; |
| 6293 | case Py_LE: |
| 6294 | result = (result <= 0); |
| 6295 | break; |
| 6296 | case Py_GE: |
| 6297 | result = (result >= 0); |
| 6298 | break; |
| 6299 | case Py_LT: |
| 6300 | result = (result == -1); |
| 6301 | break; |
| 6302 | case Py_GT: |
| 6303 | result = (result == 1); |
| 6304 | break; |
| 6305 | } |
| 6306 | return PyBool_FromLong(result); |
| 6307 | |
| 6308 | onError: |
| 6309 | |
| 6310 | /* Standard case |
| 6311 | |
| 6312 | Type errors mean that PyUnicode_FromObject() could not convert |
| 6313 | one of the arguments (usually the right hand side) to Unicode, |
| 6314 | ie. we can't handle the comparison request. However, it is |
| 6315 | possible that the other object knows a comparison method, which |
| 6316 | is why we return Py_NotImplemented to give the other object a |
| 6317 | chance. |
| 6318 | |
| 6319 | */ |
| 6320 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 6321 | PyErr_Clear(); |
| 6322 | Py_INCREF(Py_NotImplemented); |
| 6323 | return Py_NotImplemented; |
| 6324 | } |
| 6325 | if (op != Py_EQ && op != Py_NE) |
| 6326 | return NULL; |
| 6327 | |
| 6328 | /* Equality comparison. |
| 6329 | |
| 6330 | This is a special case: we silence any PyExc_UnicodeDecodeError |
| 6331 | and instead turn it into a PyErr_UnicodeWarning. |
| 6332 | |
| 6333 | */ |
| 6334 | if (!PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) |
| 6335 | return NULL; |
| 6336 | PyErr_Clear(); |
Skip Montanaro | 46fc337 | 2007-08-12 11:44:53 +0000 | [diff] [blame] | 6337 | if (PyErr_WarnEx(PyExc_UnicodeWarning, |
| 6338 | (op == Py_EQ) ? |
| 6339 | "Unicode equal comparison " |
| 6340 | "failed to convert both arguments to Unicode - " |
| 6341 | "interpreting them as being unequal" |
| 6342 | : |
| 6343 | "Unicode unequal comparison " |
| 6344 | "failed to convert both arguments to Unicode - " |
| 6345 | "interpreting them as being unequal", |
| 6346 | 1) < 0) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6347 | return NULL; |
| 6348 | result = (op == Py_NE); |
| 6349 | return PyBool_FromLong(result); |
| 6350 | } |
| 6351 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6352 | int PyUnicode_Contains(PyObject *container, |
| 6353 | PyObject *element) |
| 6354 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6355 | PyObject *str, *sub; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6356 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6357 | |
| 6358 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6359 | sub = PyUnicode_FromObject(element); |
| 6360 | if (!sub) { |
Walter Dörwald | 26e0f51 | 2007-06-12 16:51:31 +0000 | [diff] [blame] | 6361 | PyErr_Format(PyExc_TypeError, |
| 6362 | "'in <string>' requires string as left operand, not %s", |
| 6363 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6364 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6365 | } |
| 6366 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6367 | str = PyUnicode_FromObject(container); |
| 6368 | if (!str) { |
| 6369 | Py_DECREF(sub); |
| 6370 | return -1; |
| 6371 | } |
| 6372 | |
| 6373 | result = stringlib_contains_obj(str, sub); |
| 6374 | |
| 6375 | Py_DECREF(str); |
| 6376 | Py_DECREF(sub); |
| 6377 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6378 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6379 | } |
| 6380 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6381 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 6382 | |
| 6383 | PyObject *PyUnicode_Concat(PyObject *left, |
| 6384 | PyObject *right) |
| 6385 | { |
| 6386 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 6387 | |
| 6388 | /* Coerce the two arguments */ |
| 6389 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 6390 | if (u == NULL) |
| 6391 | goto onError; |
| 6392 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 6393 | if (v == NULL) |
| 6394 | goto onError; |
| 6395 | |
| 6396 | /* Shortcuts */ |
| 6397 | if (v == unicode_empty) { |
| 6398 | Py_DECREF(v); |
| 6399 | return (PyObject *)u; |
| 6400 | } |
| 6401 | if (u == unicode_empty) { |
| 6402 | Py_DECREF(u); |
| 6403 | return (PyObject *)v; |
| 6404 | } |
| 6405 | |
| 6406 | /* Concat the two Unicode strings */ |
| 6407 | w = _PyUnicode_New(u->length + v->length); |
| 6408 | if (w == NULL) |
| 6409 | goto onError; |
| 6410 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 6411 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 6412 | |
| 6413 | Py_DECREF(u); |
| 6414 | Py_DECREF(v); |
| 6415 | return (PyObject *)w; |
| 6416 | |
| 6417 | onError: |
| 6418 | Py_XDECREF(u); |
| 6419 | Py_XDECREF(v); |
| 6420 | return NULL; |
| 6421 | } |
| 6422 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 6423 | void |
| 6424 | PyUnicode_Append(PyObject **pleft, PyObject *right) |
| 6425 | { |
| 6426 | PyObject *new; |
| 6427 | if (*pleft == NULL) |
| 6428 | return; |
| 6429 | if (right == NULL || !PyUnicode_Check(*pleft)) { |
| 6430 | Py_DECREF(*pleft); |
| 6431 | *pleft = NULL; |
| 6432 | return; |
| 6433 | } |
| 6434 | new = PyUnicode_Concat(*pleft, right); |
| 6435 | Py_DECREF(*pleft); |
| 6436 | *pleft = new; |
| 6437 | } |
| 6438 | |
| 6439 | void |
| 6440 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 6441 | { |
| 6442 | PyUnicode_Append(pleft, right); |
| 6443 | Py_XDECREF(right); |
| 6444 | } |
| 6445 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6446 | PyDoc_STRVAR(count__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6447 | "S.count(sub[, start[, end]]) -> int\n\ |
| 6448 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6449 | Return the number of non-overlapping occurrences of substring sub in\n\ |
| 6450 | 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] | 6451 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6452 | |
| 6453 | static PyObject * |
| 6454 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 6455 | { |
| 6456 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6457 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6458 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6459 | PyObject *result; |
| 6460 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6461 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
| 6462 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6463 | return NULL; |
| 6464 | |
| 6465 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6466 | (PyObject *)substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6467 | if (substring == NULL) |
| 6468 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6469 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6470 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6471 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6472 | result = PyInt_FromSsize_t( |
| 6473 | stringlib_count(self->str + start, end - start, |
| 6474 | substring->str, substring->length) |
| 6475 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6476 | |
| 6477 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6478 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6479 | return result; |
| 6480 | } |
| 6481 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6482 | PyDoc_STRVAR(encode__doc__, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6483 | "S.encode([encoding[,errors]]) -> string or unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6484 | \n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6485 | Encodes S using the codec registered for encoding. encoding defaults\n\ |
| 6486 | 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] | 6487 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6488 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 6489 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 6490 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6491 | |
| 6492 | static PyObject * |
| 6493 | unicode_encode(PyUnicodeObject *self, PyObject *args) |
| 6494 | { |
| 6495 | char *encoding = NULL; |
| 6496 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6497 | PyObject *v; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 6498 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6499 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 6500 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6501 | v = PyUnicode_AsEncodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6502 | if (v == NULL) |
| 6503 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6504 | if (!PyString_Check(v)) { |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6505 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 6506 | "encoder did not return a bytes object " |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6507 | "(type=%.400s)", |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 6508 | Py_Type(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6509 | Py_DECREF(v); |
| 6510 | return NULL; |
| 6511 | } |
| 6512 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6513 | |
| 6514 | onError: |
| 6515 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6516 | } |
| 6517 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6518 | PyDoc_STRVAR(expandtabs__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6519 | "S.expandtabs([tabsize]) -> unicode\n\ |
| 6520 | \n\ |
| 6521 | 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] | 6522 | 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] | 6523 | |
| 6524 | static PyObject* |
| 6525 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 6526 | { |
| 6527 | Py_UNICODE *e; |
| 6528 | Py_UNICODE *p; |
| 6529 | Py_UNICODE *q; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6530 | Py_ssize_t i, j, old_j; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6531 | PyUnicodeObject *u; |
| 6532 | int tabsize = 8; |
| 6533 | |
| 6534 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
| 6535 | return NULL; |
| 6536 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6537 | /* First pass: determine size of output string */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6538 | i = j = old_j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6539 | e = self->str + self->length; |
| 6540 | for (p = self->str; p < e; p++) |
| 6541 | if (*p == '\t') { |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6542 | if (tabsize > 0) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6543 | j += tabsize - (j % tabsize); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6544 | if (old_j > j) { |
| 6545 | PyErr_SetString(PyExc_OverflowError, |
| 6546 | "new string is too long"); |
| 6547 | return NULL; |
| 6548 | } |
| 6549 | old_j = j; |
| 6550 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6551 | } |
| 6552 | else { |
| 6553 | j++; |
| 6554 | if (*p == '\n' || *p == '\r') { |
| 6555 | i += j; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6556 | old_j = j = 0; |
| 6557 | if (i < 0) { |
| 6558 | PyErr_SetString(PyExc_OverflowError, |
| 6559 | "new string is too long"); |
| 6560 | return NULL; |
| 6561 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6562 | } |
| 6563 | } |
| 6564 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6565 | if ((i + j) < 0) { |
| 6566 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 6567 | return NULL; |
| 6568 | } |
| 6569 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6570 | /* Second pass: create output string and fill it */ |
| 6571 | u = _PyUnicode_New(i + j); |
| 6572 | if (!u) |
| 6573 | return NULL; |
| 6574 | |
| 6575 | j = 0; |
| 6576 | q = u->str; |
| 6577 | |
| 6578 | for (p = self->str; p < e; p++) |
| 6579 | if (*p == '\t') { |
| 6580 | if (tabsize > 0) { |
| 6581 | i = tabsize - (j % tabsize); |
| 6582 | j += i; |
| 6583 | while (i--) |
| 6584 | *q++ = ' '; |
| 6585 | } |
| 6586 | } |
| 6587 | else { |
| 6588 | j++; |
| 6589 | *q++ = *p; |
| 6590 | if (*p == '\n' || *p == '\r') |
| 6591 | j = 0; |
| 6592 | } |
| 6593 | |
| 6594 | return (PyObject*) u; |
| 6595 | } |
| 6596 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6597 | PyDoc_STRVAR(find__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6598 | "S.find(sub [,start [,end]]) -> int\n\ |
| 6599 | \n\ |
| 6600 | 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] | 6601 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6602 | arguments start and end are interpreted as in slice notation.\n\ |
| 6603 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6604 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6605 | |
| 6606 | static PyObject * |
| 6607 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 6608 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6609 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 6610 | Py_ssize_t start; |
| 6611 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6612 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6613 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 6614 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6615 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6616 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6617 | result = stringlib_find_slice( |
| 6618 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6619 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6620 | start, end |
| 6621 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6622 | |
| 6623 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6624 | |
| 6625 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6626 | } |
| 6627 | |
| 6628 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6629 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6630 | { |
| 6631 | if (index < 0 || index >= self->length) { |
| 6632 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 6633 | return NULL; |
| 6634 | } |
| 6635 | |
| 6636 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 6637 | } |
| 6638 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 6639 | /* Believe it or not, this produces the same value for ASCII strings |
| 6640 | as string_hash(). */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6641 | static long |
Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 6642 | unicode_hash(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6643 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 6644 | Py_ssize_t len; |
| 6645 | Py_UNICODE *p; |
| 6646 | long x; |
| 6647 | |
| 6648 | if (self->hash != -1) |
| 6649 | return self->hash; |
| 6650 | len = Py_Size(self); |
| 6651 | p = self->str; |
| 6652 | x = *p << 7; |
| 6653 | while (--len >= 0) |
| 6654 | x = (1000003*x) ^ *p++; |
| 6655 | x ^= Py_Size(self); |
| 6656 | if (x == -1) |
| 6657 | x = -2; |
| 6658 | self->hash = x; |
| 6659 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6660 | } |
| 6661 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6662 | PyDoc_STRVAR(index__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6663 | "S.index(sub [,start [,end]]) -> int\n\ |
| 6664 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6665 | 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] | 6666 | |
| 6667 | static PyObject * |
| 6668 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 6669 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6670 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6671 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 6672 | Py_ssize_t start; |
| 6673 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6674 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 6675 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6676 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6677 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6678 | result = stringlib_find_slice( |
| 6679 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6680 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6681 | start, end |
| 6682 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6683 | |
| 6684 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6685 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6686 | if (result < 0) { |
| 6687 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 6688 | return NULL; |
| 6689 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6690 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6691 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6692 | } |
| 6693 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6694 | PyDoc_STRVAR(islower__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6695 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6696 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6697 | 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] | 6698 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6699 | |
| 6700 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6701 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6702 | { |
| 6703 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6704 | register const Py_UNICODE *e; |
| 6705 | int cased; |
| 6706 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6707 | /* Shortcut for single character strings */ |
| 6708 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6709 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6710 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6711 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6712 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6713 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6714 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6715 | e = p + PyUnicode_GET_SIZE(self); |
| 6716 | cased = 0; |
| 6717 | for (; p < e; p++) { |
| 6718 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6719 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6720 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6721 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6722 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 6723 | cased = 1; |
| 6724 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6725 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6726 | } |
| 6727 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6728 | PyDoc_STRVAR(isupper__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6729 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6730 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6731 | 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] | 6732 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6733 | |
| 6734 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6735 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6736 | { |
| 6737 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6738 | register const Py_UNICODE *e; |
| 6739 | int cased; |
| 6740 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6741 | /* Shortcut for single character strings */ |
| 6742 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6743 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6744 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6745 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6746 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6747 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6748 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6749 | e = p + PyUnicode_GET_SIZE(self); |
| 6750 | cased = 0; |
| 6751 | for (; p < e; p++) { |
| 6752 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6753 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6754 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6755 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6756 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 6757 | cased = 1; |
| 6758 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6759 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6760 | } |
| 6761 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6762 | PyDoc_STRVAR(istitle__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6763 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6764 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6765 | Return True if S is a titlecased string and there is at least one\n\ |
| 6766 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 6767 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 6768 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6769 | |
| 6770 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6771 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6772 | { |
| 6773 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6774 | register const Py_UNICODE *e; |
| 6775 | int cased, previous_is_cased; |
| 6776 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6777 | /* Shortcut for single character strings */ |
| 6778 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6779 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 6780 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6781 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6782 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6783 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6784 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6785 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6786 | e = p + PyUnicode_GET_SIZE(self); |
| 6787 | cased = 0; |
| 6788 | previous_is_cased = 0; |
| 6789 | for (; p < e; p++) { |
| 6790 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6791 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6792 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 6793 | if (previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6794 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6795 | previous_is_cased = 1; |
| 6796 | cased = 1; |
| 6797 | } |
| 6798 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 6799 | if (!previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6800 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6801 | previous_is_cased = 1; |
| 6802 | cased = 1; |
| 6803 | } |
| 6804 | else |
| 6805 | previous_is_cased = 0; |
| 6806 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6807 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6808 | } |
| 6809 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6810 | PyDoc_STRVAR(isspace__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6811 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6812 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6813 | Return True if all characters in S are whitespace\n\ |
| 6814 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6815 | |
| 6816 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6817 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6818 | { |
| 6819 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6820 | register const Py_UNICODE *e; |
| 6821 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6822 | /* Shortcut for single character strings */ |
| 6823 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6824 | Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6825 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6826 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6827 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6828 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6829 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6830 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6831 | e = p + PyUnicode_GET_SIZE(self); |
| 6832 | for (; p < e; p++) { |
| 6833 | if (!Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6834 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6835 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6836 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6837 | } |
| 6838 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6839 | PyDoc_STRVAR(isalpha__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6840 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6841 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6842 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6843 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6844 | |
| 6845 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6846 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6847 | { |
| 6848 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6849 | register const Py_UNICODE *e; |
| 6850 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6851 | /* Shortcut for single character strings */ |
| 6852 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6853 | Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6854 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6855 | |
| 6856 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6857 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6858 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6859 | |
| 6860 | e = p + PyUnicode_GET_SIZE(self); |
| 6861 | for (; p < e; p++) { |
| 6862 | if (!Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6863 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6864 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6865 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6866 | } |
| 6867 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6868 | PyDoc_STRVAR(isalnum__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6869 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6870 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6871 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6872 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6873 | |
| 6874 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6875 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6876 | { |
| 6877 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6878 | register const Py_UNICODE *e; |
| 6879 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6880 | /* Shortcut for single character strings */ |
| 6881 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6882 | Py_UNICODE_ISALNUM(*p)) |
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 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6886 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6887 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6888 | |
| 6889 | e = p + PyUnicode_GET_SIZE(self); |
| 6890 | for (; p < e; p++) { |
| 6891 | if (!Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6892 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6893 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6894 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6895 | } |
| 6896 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6897 | PyDoc_STRVAR(isdecimal__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6898 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6899 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6900 | 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] | 6901 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6902 | |
| 6903 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6904 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6905 | { |
| 6906 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6907 | register const Py_UNICODE *e; |
| 6908 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6909 | /* Shortcut for single character strings */ |
| 6910 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6911 | Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6912 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6913 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6914 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6915 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6916 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6917 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6918 | e = p + PyUnicode_GET_SIZE(self); |
| 6919 | for (; p < e; p++) { |
| 6920 | if (!Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6921 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6922 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6923 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6924 | } |
| 6925 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6926 | PyDoc_STRVAR(isdigit__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6927 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6928 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6929 | Return True if all characters in S are digits\n\ |
| 6930 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6931 | |
| 6932 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6933 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6934 | { |
| 6935 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6936 | register const Py_UNICODE *e; |
| 6937 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6938 | /* Shortcut for single character strings */ |
| 6939 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6940 | Py_UNICODE_ISDIGIT(*p)) |
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 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6943 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6944 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6945 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6946 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6947 | e = p + PyUnicode_GET_SIZE(self); |
| 6948 | for (; p < e; p++) { |
| 6949 | if (!Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6950 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6951 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6952 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6953 | } |
| 6954 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6955 | PyDoc_STRVAR(isnumeric__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6956 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6957 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6958 | 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] | 6959 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6960 | |
| 6961 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6962 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6963 | { |
| 6964 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6965 | register const Py_UNICODE *e; |
| 6966 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6967 | /* Shortcut for single character strings */ |
| 6968 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6969 | Py_UNICODE_ISNUMERIC(*p)) |
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 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6972 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6973 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6974 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6975 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6976 | e = p + PyUnicode_GET_SIZE(self); |
| 6977 | for (; p < e; p++) { |
| 6978 | if (!Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6979 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6980 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6981 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6982 | } |
| 6983 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 6984 | int |
| 6985 | PyUnicode_IsIdentifier(PyObject *self) |
| 6986 | { |
| 6987 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE((PyUnicodeObject*)self); |
| 6988 | register const Py_UNICODE *e; |
| 6989 | |
| 6990 | /* Special case for empty strings */ |
| 6991 | if (PyUnicode_GET_SIZE(self) == 0) |
| 6992 | return 0; |
| 6993 | |
| 6994 | /* PEP 3131 says that the first character must be in |
| 6995 | XID_Start and subsequent characters in XID_Continue, |
| 6996 | and for the ASCII range, the 2.x rules apply (i.e |
| 6997 | start with letters and underscore, continue with |
| 6998 | letters, digits, underscore). However, given the current |
| 6999 | definition of XID_Start and XID_Continue, it is sufficient |
| 7000 | to check just for these, except that _ must be allowed |
| 7001 | as starting an identifier. */ |
| 7002 | if (!_PyUnicode_IsXidStart(*p) && *p != 0x5F /* LOW LINE */) |
| 7003 | return 0; |
| 7004 | |
| 7005 | e = p + PyUnicode_GET_SIZE(self); |
| 7006 | for (p++; p < e; p++) { |
| 7007 | if (!_PyUnicode_IsXidContinue(*p)) |
| 7008 | return 0; |
| 7009 | } |
| 7010 | return 1; |
| 7011 | } |
| 7012 | |
| 7013 | PyDoc_STRVAR(isidentifier__doc__, |
| 7014 | "S.isidentifier() -> bool\n\ |
| 7015 | \n\ |
| 7016 | Return True if S is a valid identifier according\n\ |
| 7017 | to the language definition."); |
| 7018 | |
| 7019 | static PyObject* |
| 7020 | unicode_isidentifier(PyObject *self) |
| 7021 | { |
| 7022 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 7023 | } |
| 7024 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7025 | PyDoc_STRVAR(join__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7026 | "S.join(sequence) -> unicode\n\ |
| 7027 | \n\ |
| 7028 | 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] | 7029 | sequence. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7030 | |
| 7031 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7032 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7033 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7034 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7035 | } |
| 7036 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7037 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7038 | unicode_length(PyUnicodeObject *self) |
| 7039 | { |
| 7040 | return self->length; |
| 7041 | } |
| 7042 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7043 | PyDoc_STRVAR(ljust__doc__, |
Hye-Shik Chang | 974ed7c | 2004-06-02 16:49:17 +0000 | [diff] [blame] | 7044 | "S.ljust(width[, fillchar]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7045 | \n\ |
| 7046 | 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] | 7047 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7048 | |
| 7049 | static PyObject * |
| 7050 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 7051 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7052 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7053 | Py_UNICODE fillchar = ' '; |
| 7054 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7055 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7056 | return NULL; |
| 7057 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7058 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7059 | Py_INCREF(self); |
| 7060 | return (PyObject*) self; |
| 7061 | } |
| 7062 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7063 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7064 | } |
| 7065 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7066 | PyDoc_STRVAR(lower__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7067 | "S.lower() -> unicode\n\ |
| 7068 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7069 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7070 | |
| 7071 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7072 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7073 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7074 | return fixup(self, fixlower); |
| 7075 | } |
| 7076 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7077 | #define LEFTSTRIP 0 |
| 7078 | #define RIGHTSTRIP 1 |
| 7079 | #define BOTHSTRIP 2 |
| 7080 | |
| 7081 | /* Arrays indexed by above */ |
| 7082 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 7083 | |
| 7084 | #define STRIPNAME(i) (stripformat[i]+3) |
| 7085 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7086 | /* externally visible for str.strip(unicode) */ |
| 7087 | PyObject * |
| 7088 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 7089 | { |
| 7090 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7091 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7092 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7093 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 7094 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7095 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7096 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
| 7097 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7098 | i = 0; |
| 7099 | if (striptype != RIGHTSTRIP) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7100 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 7101 | i++; |
| 7102 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7103 | } |
| 7104 | |
| 7105 | j = len; |
| 7106 | if (striptype != LEFTSTRIP) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7107 | do { |
| 7108 | j--; |
| 7109 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 7110 | j++; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7111 | } |
| 7112 | |
| 7113 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7114 | Py_INCREF(self); |
| 7115 | return (PyObject*)self; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7116 | } |
| 7117 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7118 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7119 | } |
| 7120 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7121 | |
| 7122 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7123 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7124 | { |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7125 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7126 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7127 | |
| 7128 | i = 0; |
| 7129 | if (striptype != RIGHTSTRIP) { |
| 7130 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 7131 | i++; |
| 7132 | } |
| 7133 | } |
| 7134 | |
| 7135 | j = len; |
| 7136 | if (striptype != LEFTSTRIP) { |
| 7137 | do { |
| 7138 | j--; |
| 7139 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 7140 | j++; |
| 7141 | } |
| 7142 | |
| 7143 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 7144 | Py_INCREF(self); |
| 7145 | return (PyObject*)self; |
| 7146 | } |
| 7147 | else |
| 7148 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7149 | } |
| 7150 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7151 | |
| 7152 | static PyObject * |
| 7153 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 7154 | { |
| 7155 | PyObject *sep = NULL; |
| 7156 | |
| 7157 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 7158 | return NULL; |
| 7159 | |
| 7160 | if (sep != NULL && sep != Py_None) { |
| 7161 | if (PyUnicode_Check(sep)) |
| 7162 | return _PyUnicode_XStrip(self, striptype, sep); |
| 7163 | else if (PyString_Check(sep)) { |
| 7164 | PyObject *res; |
| 7165 | sep = PyUnicode_FromObject(sep); |
| 7166 | if (sep==NULL) |
| 7167 | return NULL; |
| 7168 | res = _PyUnicode_XStrip(self, striptype, sep); |
| 7169 | Py_DECREF(sep); |
| 7170 | return res; |
| 7171 | } |
| 7172 | else { |
| 7173 | PyErr_Format(PyExc_TypeError, |
| 7174 | "%s arg must be None, unicode or str", |
| 7175 | STRIPNAME(striptype)); |
| 7176 | return NULL; |
| 7177 | } |
| 7178 | } |
| 7179 | |
| 7180 | return do_strip(self, striptype); |
| 7181 | } |
| 7182 | |
| 7183 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7184 | PyDoc_STRVAR(strip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7185 | "S.strip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7186 | \n\ |
| 7187 | Return a copy of the string S with leading and trailing\n\ |
| 7188 | whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7189 | If chars is given and not None, remove characters in chars instead.\n\ |
| 7190 | 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] | 7191 | |
| 7192 | static PyObject * |
| 7193 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 7194 | { |
| 7195 | if (PyTuple_GET_SIZE(args) == 0) |
| 7196 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 7197 | else |
| 7198 | return do_argstrip(self, BOTHSTRIP, args); |
| 7199 | } |
| 7200 | |
| 7201 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7202 | PyDoc_STRVAR(lstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7203 | "S.lstrip([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 whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7206 | If chars is given and not None, remove characters in chars instead.\n\ |
| 7207 | 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] | 7208 | |
| 7209 | static PyObject * |
| 7210 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 7211 | { |
| 7212 | if (PyTuple_GET_SIZE(args) == 0) |
| 7213 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 7214 | else |
| 7215 | return do_argstrip(self, LEFTSTRIP, args); |
| 7216 | } |
| 7217 | |
| 7218 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7219 | PyDoc_STRVAR(rstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7220 | "S.rstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7221 | \n\ |
| 7222 | Return a copy of the string S with trailing whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7223 | If chars is given and not None, remove characters in chars instead.\n\ |
| 7224 | 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] | 7225 | |
| 7226 | static PyObject * |
| 7227 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 7228 | { |
| 7229 | if (PyTuple_GET_SIZE(args) == 0) |
| 7230 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 7231 | else |
| 7232 | return do_argstrip(self, RIGHTSTRIP, args); |
| 7233 | } |
| 7234 | |
| 7235 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7236 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7237 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7238 | { |
| 7239 | PyUnicodeObject *u; |
| 7240 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7241 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7242 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7243 | |
| 7244 | if (len < 0) |
| 7245 | len = 0; |
| 7246 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7247 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7248 | /* no repeat, return original string */ |
| 7249 | Py_INCREF(str); |
| 7250 | return (PyObject*) str; |
| 7251 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7252 | |
| 7253 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 7254 | * needed doesn't overflow size_t |
| 7255 | */ |
| 7256 | nchars = len * str->length; |
| 7257 | if (len && nchars / len != str->length) { |
| 7258 | PyErr_SetString(PyExc_OverflowError, |
| 7259 | "repeated string is too long"); |
| 7260 | return NULL; |
| 7261 | } |
| 7262 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 7263 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 7264 | PyErr_SetString(PyExc_OverflowError, |
| 7265 | "repeated string is too long"); |
| 7266 | return NULL; |
| 7267 | } |
| 7268 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7269 | if (!u) |
| 7270 | return NULL; |
| 7271 | |
| 7272 | p = u->str; |
| 7273 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7274 | if (str->length == 1 && len > 0) { |
| 7275 | Py_UNICODE_FILL(p, str->str[0], len); |
| 7276 | } else { |
| 7277 | Py_ssize_t done = 0; /* number of characters copied this far */ |
| 7278 | if (done < nchars) { |
| 7279 | Py_UNICODE_COPY(p, str->str, str->length); |
| 7280 | done = str->length; |
| 7281 | } |
| 7282 | while (done < nchars) { |
| 7283 | int n = (done <= nchars-done) ? done : nchars-done; |
| 7284 | Py_UNICODE_COPY(p+done, p, n); |
| 7285 | done += n; |
| 7286 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7287 | } |
| 7288 | |
| 7289 | return (PyObject*) u; |
| 7290 | } |
| 7291 | |
| 7292 | PyObject *PyUnicode_Replace(PyObject *obj, |
| 7293 | PyObject *subobj, |
| 7294 | PyObject *replobj, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7295 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7296 | { |
| 7297 | PyObject *self; |
| 7298 | PyObject *str1; |
| 7299 | PyObject *str2; |
| 7300 | PyObject *result; |
| 7301 | |
| 7302 | self = PyUnicode_FromObject(obj); |
| 7303 | if (self == NULL) |
| 7304 | return NULL; |
| 7305 | str1 = PyUnicode_FromObject(subobj); |
| 7306 | if (str1 == NULL) { |
| 7307 | Py_DECREF(self); |
| 7308 | return NULL; |
| 7309 | } |
| 7310 | str2 = PyUnicode_FromObject(replobj); |
| 7311 | if (str2 == NULL) { |
| 7312 | Py_DECREF(self); |
| 7313 | Py_DECREF(str1); |
| 7314 | return NULL; |
| 7315 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7316 | result = replace((PyUnicodeObject *)self, |
| 7317 | (PyUnicodeObject *)str1, |
| 7318 | (PyUnicodeObject *)str2, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7319 | maxcount); |
| 7320 | Py_DECREF(self); |
| 7321 | Py_DECREF(str1); |
| 7322 | Py_DECREF(str2); |
| 7323 | return result; |
| 7324 | } |
| 7325 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7326 | PyDoc_STRVAR(replace__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7327 | "S.replace (old, new[, maxsplit]) -> unicode\n\ |
| 7328 | \n\ |
| 7329 | Return a copy of S with all occurrences of substring\n\ |
| 7330 | 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] | 7331 | given, only the first maxsplit occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7332 | |
| 7333 | static PyObject* |
| 7334 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 7335 | { |
| 7336 | PyUnicodeObject *str1; |
| 7337 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7338 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7339 | PyObject *result; |
| 7340 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7341 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7342 | return NULL; |
| 7343 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 7344 | if (str1 == NULL) |
| 7345 | return NULL; |
| 7346 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7347 | if (str2 == NULL) { |
| 7348 | Py_DECREF(str1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7349 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7350 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7351 | |
| 7352 | result = replace(self, str1, str2, maxcount); |
| 7353 | |
| 7354 | Py_DECREF(str1); |
| 7355 | Py_DECREF(str2); |
| 7356 | return result; |
| 7357 | } |
| 7358 | |
| 7359 | static |
| 7360 | PyObject *unicode_repr(PyObject *unicode) |
| 7361 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7362 | PyObject *repr; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7363 | Py_UNICODE *p; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7364 | Py_UNICODE *s = PyUnicode_AS_UNICODE(unicode); |
| 7365 | Py_ssize_t size = PyUnicode_GET_SIZE(unicode); |
| 7366 | |
| 7367 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 7368 | better to choose a different scheme. Perhaps scan the |
| 7369 | first N-chars of the string and allocate based on that size. |
| 7370 | */ |
| 7371 | /* Initial allocation is based on the longest-possible unichr |
| 7372 | escape. |
| 7373 | |
| 7374 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 7375 | unichr, so in this case it's the longest unichr escape. In |
| 7376 | narrow (UTF-16) builds this is five chars per source unichr |
| 7377 | since there are two unichrs in the surrogate pair, so in narrow |
| 7378 | (UTF-16) builds it's not the longest unichr escape. |
| 7379 | |
| 7380 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 7381 | so in the narrow (UTF-16) build case it's the longest unichr |
| 7382 | escape. |
| 7383 | */ |
| 7384 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7385 | repr = PyUnicode_FromUnicode(NULL, |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7386 | 2 /* quotes */ |
| 7387 | #ifdef Py_UNICODE_WIDE |
| 7388 | + 10*size |
| 7389 | #else |
| 7390 | + 6*size |
| 7391 | #endif |
| 7392 | + 1); |
| 7393 | if (repr == NULL) |
| 7394 | return NULL; |
| 7395 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7396 | p = PyUnicode_AS_UNICODE(repr); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7397 | |
| 7398 | /* Add quote */ |
| 7399 | *p++ = (findchar(s, size, '\'') && |
| 7400 | !findchar(s, size, '"')) ? '"' : '\''; |
| 7401 | while (size-- > 0) { |
| 7402 | Py_UNICODE ch = *s++; |
| 7403 | |
| 7404 | /* Escape quotes and backslashes */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7405 | if ((ch == PyUnicode_AS_UNICODE(repr)[0]) || (ch == '\\')) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7406 | *p++ = '\\'; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7407 | *p++ = ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7408 | continue; |
| 7409 | } |
| 7410 | |
| 7411 | #ifdef Py_UNICODE_WIDE |
| 7412 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 7413 | else if (ch >= 0x10000) { |
| 7414 | *p++ = '\\'; |
| 7415 | *p++ = 'U'; |
| 7416 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 7417 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 7418 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 7419 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 7420 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 7421 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 7422 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 7423 | *p++ = hexdigits[ch & 0x0000000F]; |
| 7424 | continue; |
| 7425 | } |
| 7426 | #else |
| 7427 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 7428 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 7429 | Py_UNICODE ch2; |
| 7430 | Py_UCS4 ucs; |
| 7431 | |
| 7432 | ch2 = *s++; |
| 7433 | size--; |
| 7434 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 7435 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 7436 | *p++ = '\\'; |
| 7437 | *p++ = 'U'; |
| 7438 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 7439 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 7440 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 7441 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 7442 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 7443 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 7444 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 7445 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 7446 | continue; |
| 7447 | } |
| 7448 | /* Fall through: isolated surrogates are copied as-is */ |
| 7449 | s--; |
| 7450 | size++; |
| 7451 | } |
| 7452 | #endif |
| 7453 | |
| 7454 | /* Map 16-bit characters to '\uxxxx' */ |
| 7455 | if (ch >= 256) { |
| 7456 | *p++ = '\\'; |
| 7457 | *p++ = 'u'; |
| 7458 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 7459 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 7460 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 7461 | *p++ = hexdigits[ch & 0x000F]; |
| 7462 | } |
| 7463 | |
| 7464 | /* Map special whitespace to '\t', \n', '\r' */ |
| 7465 | else if (ch == '\t') { |
| 7466 | *p++ = '\\'; |
| 7467 | *p++ = 't'; |
| 7468 | } |
| 7469 | else if (ch == '\n') { |
| 7470 | *p++ = '\\'; |
| 7471 | *p++ = 'n'; |
| 7472 | } |
| 7473 | else if (ch == '\r') { |
| 7474 | *p++ = '\\'; |
| 7475 | *p++ = 'r'; |
| 7476 | } |
| 7477 | |
| 7478 | /* Map non-printable US ASCII to '\xhh' */ |
| 7479 | else if (ch < ' ' || ch >= 0x7F) { |
| 7480 | *p++ = '\\'; |
| 7481 | *p++ = 'x'; |
| 7482 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 7483 | *p++ = hexdigits[ch & 0x000F]; |
| 7484 | } |
| 7485 | |
| 7486 | /* Copy everything else as-is */ |
| 7487 | else |
| 7488 | *p++ = (char) ch; |
| 7489 | } |
| 7490 | /* Add quote */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7491 | *p++ = PyUnicode_AS_UNICODE(repr)[0]; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7492 | |
| 7493 | *p = '\0'; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7494 | _PyUnicode_Resize(&repr, p - PyUnicode_AS_UNICODE(repr)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7495 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7496 | } |
| 7497 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7498 | PyDoc_STRVAR(rfind__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7499 | "S.rfind(sub [,start [,end]]) -> int\n\ |
| 7500 | \n\ |
| 7501 | 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] | 7502 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7503 | arguments start and end are interpreted as in slice notation.\n\ |
| 7504 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7505 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7506 | |
| 7507 | static PyObject * |
| 7508 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 7509 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7510 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7511 | Py_ssize_t start; |
| 7512 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7513 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7514 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7515 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
| 7516 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7517 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7518 | result = stringlib_rfind_slice( |
| 7519 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7520 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7521 | start, end |
| 7522 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7523 | |
| 7524 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7525 | |
| 7526 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7527 | } |
| 7528 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7529 | PyDoc_STRVAR(rindex__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7530 | "S.rindex(sub [,start [,end]]) -> int\n\ |
| 7531 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7532 | 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] | 7533 | |
| 7534 | static PyObject * |
| 7535 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 7536 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7537 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7538 | Py_ssize_t start; |
| 7539 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7540 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7541 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7542 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
| 7543 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7544 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7545 | result = stringlib_rfind_slice( |
| 7546 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7547 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7548 | start, end |
| 7549 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7550 | |
| 7551 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7552 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7553 | if (result < 0) { |
| 7554 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 7555 | return NULL; |
| 7556 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7557 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7558 | } |
| 7559 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7560 | PyDoc_STRVAR(rjust__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7561 | "S.rjust(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7562 | \n\ |
| 7563 | 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] | 7564 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7565 | |
| 7566 | static PyObject * |
| 7567 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 7568 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7569 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7570 | Py_UNICODE fillchar = ' '; |
| 7571 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7572 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7573 | return NULL; |
| 7574 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7575 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7576 | Py_INCREF(self); |
| 7577 | return (PyObject*) self; |
| 7578 | } |
| 7579 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7580 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7581 | } |
| 7582 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7583 | PyObject *PyUnicode_Split(PyObject *s, |
| 7584 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7585 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7586 | { |
| 7587 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7588 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7589 | s = PyUnicode_FromObject(s); |
| 7590 | if (s == NULL) |
| 7591 | return NULL; |
| 7592 | if (sep != NULL) { |
| 7593 | sep = PyUnicode_FromObject(sep); |
| 7594 | if (sep == NULL) { |
| 7595 | Py_DECREF(s); |
| 7596 | return NULL; |
| 7597 | } |
| 7598 | } |
| 7599 | |
| 7600 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 7601 | |
| 7602 | Py_DECREF(s); |
| 7603 | Py_XDECREF(sep); |
| 7604 | return result; |
| 7605 | } |
| 7606 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7607 | PyDoc_STRVAR(split__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7608 | "S.split([sep [,maxsplit]]) -> list of strings\n\ |
| 7609 | \n\ |
| 7610 | Return a list of the words in S, using sep as the\n\ |
| 7611 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Thomas Heller | ca0d2cb | 2004-09-15 11:41:32 +0000 | [diff] [blame] | 7612 | 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] | 7613 | any whitespace string is a separator."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7614 | |
| 7615 | static PyObject* |
| 7616 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 7617 | { |
| 7618 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7619 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7620 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7621 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7622 | return NULL; |
| 7623 | |
| 7624 | if (substring == Py_None) |
| 7625 | return split(self, NULL, maxcount); |
| 7626 | else if (PyUnicode_Check(substring)) |
| 7627 | return split(self, (PyUnicodeObject *)substring, maxcount); |
| 7628 | else |
| 7629 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
| 7630 | } |
| 7631 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7632 | PyObject * |
| 7633 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 7634 | { |
| 7635 | PyObject* str_obj; |
| 7636 | PyObject* sep_obj; |
| 7637 | PyObject* out; |
| 7638 | |
| 7639 | str_obj = PyUnicode_FromObject(str_in); |
| 7640 | if (!str_obj) |
| 7641 | return NULL; |
| 7642 | sep_obj = PyUnicode_FromObject(sep_in); |
| 7643 | if (!sep_obj) { |
| 7644 | Py_DECREF(str_obj); |
| 7645 | return NULL; |
| 7646 | } |
| 7647 | |
| 7648 | out = stringlib_partition( |
| 7649 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 7650 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 7651 | ); |
| 7652 | |
| 7653 | Py_DECREF(sep_obj); |
| 7654 | Py_DECREF(str_obj); |
| 7655 | |
| 7656 | return out; |
| 7657 | } |
| 7658 | |
| 7659 | |
| 7660 | PyObject * |
| 7661 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 7662 | { |
| 7663 | PyObject* str_obj; |
| 7664 | PyObject* sep_obj; |
| 7665 | PyObject* out; |
| 7666 | |
| 7667 | str_obj = PyUnicode_FromObject(str_in); |
| 7668 | if (!str_obj) |
| 7669 | return NULL; |
| 7670 | sep_obj = PyUnicode_FromObject(sep_in); |
| 7671 | if (!sep_obj) { |
| 7672 | Py_DECREF(str_obj); |
| 7673 | return NULL; |
| 7674 | } |
| 7675 | |
| 7676 | out = stringlib_rpartition( |
| 7677 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 7678 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 7679 | ); |
| 7680 | |
| 7681 | Py_DECREF(sep_obj); |
| 7682 | Py_DECREF(str_obj); |
| 7683 | |
| 7684 | return out; |
| 7685 | } |
| 7686 | |
| 7687 | PyDoc_STRVAR(partition__doc__, |
| 7688 | "S.partition(sep) -> (head, sep, tail)\n\ |
| 7689 | \n\ |
| 7690 | Searches for the separator sep in S, and returns the part before it,\n\ |
| 7691 | the separator itself, and the part after it. If the separator is not\n\ |
| 7692 | found, returns S and two empty strings."); |
| 7693 | |
| 7694 | static PyObject* |
| 7695 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
| 7696 | { |
| 7697 | return PyUnicode_Partition((PyObject *)self, separator); |
| 7698 | } |
| 7699 | |
| 7700 | PyDoc_STRVAR(rpartition__doc__, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7701 | "S.rpartition(sep) -> (tail, sep, head)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7702 | \n\ |
| 7703 | Searches for the separator sep in S, starting at the end of S, and returns\n\ |
| 7704 | 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] | 7705 | separator is not found, returns two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7706 | |
| 7707 | static PyObject* |
| 7708 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 7709 | { |
| 7710 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 7711 | } |
| 7712 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7713 | PyObject *PyUnicode_RSplit(PyObject *s, |
| 7714 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7715 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7716 | { |
| 7717 | PyObject *result; |
| 7718 | |
| 7719 | s = PyUnicode_FromObject(s); |
| 7720 | if (s == NULL) |
| 7721 | return NULL; |
| 7722 | if (sep != NULL) { |
| 7723 | sep = PyUnicode_FromObject(sep); |
| 7724 | if (sep == NULL) { |
| 7725 | Py_DECREF(s); |
| 7726 | return NULL; |
| 7727 | } |
| 7728 | } |
| 7729 | |
| 7730 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 7731 | |
| 7732 | Py_DECREF(s); |
| 7733 | Py_XDECREF(sep); |
| 7734 | return result; |
| 7735 | } |
| 7736 | |
| 7737 | PyDoc_STRVAR(rsplit__doc__, |
| 7738 | "S.rsplit([sep [,maxsplit]]) -> list of strings\n\ |
| 7739 | \n\ |
| 7740 | Return a list of the words in S, using sep as the\n\ |
| 7741 | delimiter string, starting at the end of the string and\n\ |
| 7742 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 7743 | splits are done. If sep is not specified, any whitespace string\n\ |
| 7744 | is a separator."); |
| 7745 | |
| 7746 | static PyObject* |
| 7747 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 7748 | { |
| 7749 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7750 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7751 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7752 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7753 | return NULL; |
| 7754 | |
| 7755 | if (substring == Py_None) |
| 7756 | return rsplit(self, NULL, maxcount); |
| 7757 | else if (PyUnicode_Check(substring)) |
| 7758 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
| 7759 | else |
| 7760 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
| 7761 | } |
| 7762 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7763 | PyDoc_STRVAR(splitlines__doc__, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7764 | "S.splitlines([keepends]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7765 | \n\ |
| 7766 | 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] | 7767 | 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] | 7768 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7769 | |
| 7770 | static PyObject* |
| 7771 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 7772 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7773 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7774 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7775 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7776 | return NULL; |
| 7777 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7778 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7779 | } |
| 7780 | |
| 7781 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 7782 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7783 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 7784 | if (PyUnicode_CheckExact(self)) { |
| 7785 | Py_INCREF(self); |
| 7786 | return self; |
| 7787 | } else |
| 7788 | /* Subtype -- return genuine unicode string with the same value. */ |
| 7789 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(self), |
| 7790 | PyUnicode_GET_SIZE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7791 | } |
| 7792 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7793 | PyDoc_STRVAR(swapcase__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7794 | "S.swapcase() -> unicode\n\ |
| 7795 | \n\ |
| 7796 | 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] | 7797 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7798 | |
| 7799 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7800 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7801 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7802 | return fixup(self, fixswapcase); |
| 7803 | } |
| 7804 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7805 | PyDoc_STRVAR(translate__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7806 | "S.translate(table) -> unicode\n\ |
| 7807 | \n\ |
| 7808 | Return a copy of the string S, where all characters have been mapped\n\ |
| 7809 | 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] | 7810 | Unicode ordinals to Unicode ordinals, Unicode strings or None.\n\ |
| 7811 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 7812 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7813 | |
| 7814 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7815 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7816 | { |
Georg Brandl | 94c2c75 | 2007-10-23 06:52:59 +0000 | [diff] [blame] | 7817 | PyObject *newtable = NULL; |
| 7818 | Py_ssize_t i = 0; |
| 7819 | PyObject *key, *value, *result; |
| 7820 | |
| 7821 | if (!PyDict_Check(table)) { |
| 7822 | PyErr_SetString(PyExc_TypeError, "translate argument must be a dict"); |
| 7823 | return NULL; |
| 7824 | } |
| 7825 | /* fixup the table -- allow size-1 string keys instead of only int keys */ |
| 7826 | newtable = PyDict_Copy(table); |
| 7827 | if (!newtable) return NULL; |
| 7828 | while (PyDict_Next(table, &i, &key, &value)) { |
| 7829 | if (PyUnicode_Check(key)) { |
| 7830 | /* convert string keys to integer keys */ |
| 7831 | PyObject *newkey; |
| 7832 | int res; |
| 7833 | if (PyUnicode_GET_SIZE(key) != 1) { |
| 7834 | PyErr_SetString(PyExc_ValueError, "string items in translate " |
| 7835 | "table must be 1 element long"); |
| 7836 | goto err; |
| 7837 | } |
| 7838 | newkey = PyInt_FromLong(PyUnicode_AS_UNICODE(key)[0]); |
| 7839 | if (!newkey) |
| 7840 | goto err; |
| 7841 | res = PyDict_SetItem(newtable, newkey, value); |
| 7842 | Py_DECREF(newkey); |
| 7843 | if (res < 0) |
| 7844 | goto err; |
| 7845 | } else if (PyInt_Check(key)) { |
| 7846 | /* just keep integer keys */ |
| 7847 | if (PyDict_SetItem(newtable, key, value) < 0) |
| 7848 | goto err; |
| 7849 | } else { |
| 7850 | PyErr_SetString(PyExc_TypeError, "items in translate table must be " |
| 7851 | "strings or integers"); |
| 7852 | goto err; |
| 7853 | } |
| 7854 | } |
| 7855 | |
| 7856 | result = PyUnicode_TranslateCharmap(self->str, |
| 7857 | self->length, |
| 7858 | newtable, |
| 7859 | "ignore"); |
| 7860 | Py_DECREF(newtable); |
| 7861 | return result; |
| 7862 | err: |
| 7863 | Py_DECREF(newtable); |
| 7864 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7865 | } |
| 7866 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7867 | PyDoc_STRVAR(upper__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7868 | "S.upper() -> unicode\n\ |
| 7869 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7870 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7871 | |
| 7872 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7873 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7874 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7875 | return fixup(self, fixupper); |
| 7876 | } |
| 7877 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7878 | PyDoc_STRVAR(zfill__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7879 | "S.zfill(width) -> unicode\n\ |
| 7880 | \n\ |
| 7881 | 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] | 7882 | of the specified width. The string x is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7883 | |
| 7884 | static PyObject * |
| 7885 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 7886 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7887 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7888 | PyUnicodeObject *u; |
| 7889 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7890 | Py_ssize_t width; |
| 7891 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7892 | return NULL; |
| 7893 | |
| 7894 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 7895 | if (PyUnicode_CheckExact(self)) { |
| 7896 | Py_INCREF(self); |
| 7897 | return (PyObject*) self; |
| 7898 | } |
| 7899 | else |
| 7900 | return PyUnicode_FromUnicode( |
| 7901 | PyUnicode_AS_UNICODE(self), |
| 7902 | PyUnicode_GET_SIZE(self) |
| 7903 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7904 | } |
| 7905 | |
| 7906 | fill = width - self->length; |
| 7907 | |
| 7908 | u = pad(self, fill, 0, '0'); |
| 7909 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 7910 | if (u == NULL) |
| 7911 | return NULL; |
| 7912 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7913 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 7914 | /* move sign to beginning of string */ |
| 7915 | u->str[0] = u->str[fill]; |
| 7916 | u->str[fill] = '0'; |
| 7917 | } |
| 7918 | |
| 7919 | return (PyObject*) u; |
| 7920 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7921 | |
| 7922 | #if 0 |
| 7923 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7924 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7925 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7926 | return PyInt_FromLong(unicode_freelist_size); |
| 7927 | } |
| 7928 | #endif |
| 7929 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7930 | PyDoc_STRVAR(startswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7931 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7932 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 7933 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 7934 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7935 | With optional end, stop comparing S at that position.\n\ |
| 7936 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7937 | |
| 7938 | static PyObject * |
| 7939 | unicode_startswith(PyUnicodeObject *self, |
| 7940 | PyObject *args) |
| 7941 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7942 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7943 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7944 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7945 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7946 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7947 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7948 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 7949 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7950 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7951 | if (PyTuple_Check(subobj)) { |
| 7952 | Py_ssize_t i; |
| 7953 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 7954 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 7955 | PyTuple_GET_ITEM(subobj, i)); |
| 7956 | if (substring == NULL) |
| 7957 | return NULL; |
| 7958 | result = tailmatch(self, substring, start, end, -1); |
| 7959 | Py_DECREF(substring); |
| 7960 | if (result) { |
| 7961 | Py_RETURN_TRUE; |
| 7962 | } |
| 7963 | } |
| 7964 | /* nothing matched */ |
| 7965 | Py_RETURN_FALSE; |
| 7966 | } |
| 7967 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7968 | if (substring == NULL) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7969 | return NULL; |
| 7970 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7971 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7972 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7973 | } |
| 7974 | |
| 7975 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7976 | PyDoc_STRVAR(endswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7977 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7978 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 7979 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 7980 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7981 | With optional end, stop comparing S at that position.\n\ |
| 7982 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7983 | |
| 7984 | static PyObject * |
| 7985 | unicode_endswith(PyUnicodeObject *self, |
| 7986 | PyObject *args) |
| 7987 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7988 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7989 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7990 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7991 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7992 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7993 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7994 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
| 7995 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7996 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7997 | if (PyTuple_Check(subobj)) { |
| 7998 | Py_ssize_t i; |
| 7999 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 8000 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 8001 | PyTuple_GET_ITEM(subobj, i)); |
| 8002 | if (substring == NULL) |
| 8003 | return NULL; |
| 8004 | result = tailmatch(self, substring, start, end, +1); |
| 8005 | Py_DECREF(substring); |
| 8006 | if (result) { |
| 8007 | Py_RETURN_TRUE; |
| 8008 | } |
| 8009 | } |
| 8010 | Py_RETURN_FALSE; |
| 8011 | } |
| 8012 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8013 | if (substring == NULL) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8014 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8015 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8016 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8017 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8018 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8019 | } |
| 8020 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8021 | #include "stringlib/string_format.h" |
| 8022 | |
| 8023 | PyDoc_STRVAR(format__doc__, |
| 8024 | "S.format(*args, **kwargs) -> unicode\n\ |
| 8025 | \n\ |
| 8026 | "); |
| 8027 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8028 | PyDoc_STRVAR(p_format__doc__, |
| 8029 | "S.__format__(format_spec) -> unicode\n\ |
| 8030 | \n\ |
| 8031 | "); |
| 8032 | |
| 8033 | static PyObject * |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8034 | unicode_getnewargs(PyUnicodeObject *v) |
| 8035 | { |
| 8036 | return Py_BuildValue("(u#)", v->str, v->length); |
| 8037 | } |
| 8038 | |
| 8039 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8040 | static PyMethodDef unicode_methods[] = { |
| 8041 | |
| 8042 | /* Order is according to common usage: often used methods should |
| 8043 | appear first, since lookup is done sequentially. */ |
| 8044 | |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8045 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS, encode__doc__}, |
| 8046 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 8047 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8048 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8049 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 8050 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 8051 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 8052 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 8053 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 8054 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 8055 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8056 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8057 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 8058 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 8059 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8060 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8061 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 8062 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 8063 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8064 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8065 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8066 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8067 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8068 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 8069 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 8070 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 8071 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 8072 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 8073 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 8074 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 8075 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 8076 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 8077 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 8078 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 8079 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 8080 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 8081 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 8082 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8083 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 8084 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
| 8085 | {"__format__", (PyCFunction) unicode_unicode__format__, METH_VARARGS, p_format__doc__}, |
Eric Smith | f6db409 | 2007-08-27 23:52:26 +0000 | [diff] [blame] | 8086 | {"_formatter_field_name_split", (PyCFunction) formatter_field_name_split, METH_NOARGS}, |
| 8087 | {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8088 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8089 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8090 | #endif |
| 8091 | |
| 8092 | #if 0 |
| 8093 | /* This one is just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8094 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8095 | #endif |
| 8096 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8097 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8098 | {NULL, NULL} |
| 8099 | }; |
| 8100 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8101 | static PyObject * |
| 8102 | unicode_mod(PyObject *v, PyObject *w) |
| 8103 | { |
| 8104 | if (!PyUnicode_Check(v)) { |
| 8105 | Py_INCREF(Py_NotImplemented); |
| 8106 | return Py_NotImplemented; |
| 8107 | } |
| 8108 | return PyUnicode_Format(v, w); |
| 8109 | } |
| 8110 | |
| 8111 | static PyNumberMethods unicode_as_number = { |
| 8112 | 0, /*nb_add*/ |
| 8113 | 0, /*nb_subtract*/ |
| 8114 | 0, /*nb_multiply*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8115 | unicode_mod, /*nb_remainder*/ |
| 8116 | }; |
| 8117 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8118 | static PySequenceMethods unicode_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8119 | (lenfunc) unicode_length, /* sq_length */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8120 | PyUnicode_Concat, /* sq_concat */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8121 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 8122 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
Thomas Wouters | d2cf20e | 2007-08-30 22:57:53 +0000 | [diff] [blame] | 8123 | 0, /* sq_slice */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8124 | 0, /* sq_ass_item */ |
| 8125 | 0, /* sq_ass_slice */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8126 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8127 | }; |
| 8128 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8129 | static PyObject* |
| 8130 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 8131 | { |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 8132 | if (PyIndex_Check(item)) { |
| 8133 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8134 | if (i == -1 && PyErr_Occurred()) |
| 8135 | return NULL; |
| 8136 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8137 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8138 | return unicode_getitem(self, i); |
| 8139 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8140 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8141 | Py_UNICODE* source_buf; |
| 8142 | Py_UNICODE* result_buf; |
| 8143 | PyObject* result; |
| 8144 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8145 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8146 | &start, &stop, &step, &slicelength) < 0) { |
| 8147 | return NULL; |
| 8148 | } |
| 8149 | |
| 8150 | if (slicelength <= 0) { |
| 8151 | return PyUnicode_FromUnicode(NULL, 0); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 8152 | } else if (start == 0 && step == 1 && slicelength == self->length && |
| 8153 | PyUnicode_CheckExact(self)) { |
| 8154 | Py_INCREF(self); |
| 8155 | return (PyObject *)self; |
| 8156 | } else if (step == 1) { |
| 8157 | return PyUnicode_FromUnicode(self->str + start, slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8158 | } else { |
| 8159 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8160 | result_buf = (Py_UNICODE *)PyMem_MALLOC(slicelength* |
| 8161 | sizeof(Py_UNICODE)); |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8162 | |
| 8163 | if (result_buf == NULL) |
| 8164 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8165 | |
| 8166 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 8167 | result_buf[i] = source_buf[cur]; |
| 8168 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8169 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8170 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
| 8171 | PyMem_FREE(result_buf); |
| 8172 | return result; |
| 8173 | } |
| 8174 | } else { |
| 8175 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 8176 | return NULL; |
| 8177 | } |
| 8178 | } |
| 8179 | |
| 8180 | static PyMappingMethods unicode_as_mapping = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8181 | (lenfunc)unicode_length, /* mp_length */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8182 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 8183 | (objobjargproc)0, /* mp_ass_subscript */ |
| 8184 | }; |
| 8185 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8186 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8187 | /* Helpers for PyUnicode_Format() */ |
| 8188 | |
| 8189 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8190 | 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] | 8191 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8192 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8193 | if (argidx < arglen) { |
| 8194 | (*p_argidx)++; |
| 8195 | if (arglen < 0) |
| 8196 | return args; |
| 8197 | else |
| 8198 | return PyTuple_GetItem(args, argidx); |
| 8199 | } |
| 8200 | PyErr_SetString(PyExc_TypeError, |
| 8201 | "not enough arguments for format string"); |
| 8202 | return NULL; |
| 8203 | } |
| 8204 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8205 | static Py_ssize_t |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8206 | strtounicode(Py_UNICODE *buffer, const char *charbuffer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8207 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8208 | register Py_ssize_t i; |
| 8209 | Py_ssize_t len = strlen(charbuffer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8210 | for (i = len - 1; i >= 0; i--) |
| 8211 | buffer[i] = (Py_UNICODE) charbuffer[i]; |
| 8212 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8213 | return len; |
| 8214 | } |
| 8215 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8216 | static int |
| 8217 | doubletounicode(Py_UNICODE *buffer, size_t len, const char *format, double x) |
| 8218 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8219 | Py_ssize_t result; |
| 8220 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8221 | PyOS_ascii_formatd((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8222 | result = strtounicode(buffer, (char *)buffer); |
| 8223 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8224 | } |
| 8225 | |
| 8226 | static int |
| 8227 | longtounicode(Py_UNICODE *buffer, size_t len, const char *format, long x) |
| 8228 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8229 | Py_ssize_t result; |
| 8230 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8231 | PyOS_snprintf((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8232 | result = strtounicode(buffer, (char *)buffer); |
| 8233 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8234 | } |
| 8235 | |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 8236 | /* XXX To save some code duplication, formatfloat/long/int could have been |
| 8237 | shared with stringobject.c, converting from 8-bit to Unicode after the |
| 8238 | formatting is done. */ |
| 8239 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8240 | static int |
| 8241 | formatfloat(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8242 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8243 | int flags, |
| 8244 | int prec, |
| 8245 | int type, |
| 8246 | PyObject *v) |
| 8247 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8248 | /* fmt = '%#.' + `prec` + `type` |
| 8249 | 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] | 8250 | char fmt[20]; |
| 8251 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8252 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8253 | x = PyFloat_AsDouble(v); |
| 8254 | if (x == -1.0 && PyErr_Occurred()) |
| 8255 | return -1; |
| 8256 | if (prec < 0) |
| 8257 | prec = 6; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8258 | if (type == 'f' && (fabs(x) / 1e25) >= 1e25) |
| 8259 | type = 'g'; |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8260 | /* Worst case length calc to ensure no buffer overrun: |
| 8261 | |
| 8262 | 'g' formats: |
| 8263 | fmt = %#.<prec>g |
| 8264 | buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
| 8265 | for any double rep.) |
| 8266 | len = 1 + prec + 1 + 2 + 5 = 9 + prec |
| 8267 | |
| 8268 | 'f' formats: |
| 8269 | buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
| 8270 | len = 1 + 50 + 1 + prec = 52 + prec |
| 8271 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8272 | If prec=0 the effective precision is 1 (the leading digit is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8273 | always given), therefore increase the length by one. |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8274 | |
| 8275 | */ |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 8276 | if (((type == 'g' || type == 'G') && |
| 8277 | buflen <= (size_t)10 + (size_t)prec) || |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8278 | (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8279 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8280 | "formatted float is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8281 | return -1; |
| 8282 | } |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8283 | PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
| 8284 | (flags&F_ALT) ? "#" : "", |
| 8285 | prec, type); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8286 | return doubletounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8287 | } |
| 8288 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8289 | static PyObject* |
| 8290 | formatlong(PyObject *val, int flags, int prec, int type) |
| 8291 | { |
| 8292 | char *buf; |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8293 | int len; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8294 | PyObject *str; /* temporary string object. */ |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8295 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8296 | |
| 8297 | str = _PyString_FormatLong(val, flags, prec, type, &buf, &len); |
| 8298 | if (!str) |
| 8299 | return NULL; |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8300 | result = PyUnicode_FromStringAndSize(buf, len); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8301 | Py_DECREF(str); |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8302 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8303 | } |
| 8304 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8305 | static int |
| 8306 | formatint(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8307 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8308 | int flags, |
| 8309 | int prec, |
| 8310 | int type, |
| 8311 | PyObject *v) |
| 8312 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8313 | /* fmt = '%#.' + `prec` + 'l' + `type` |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8314 | * worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
| 8315 | * + 1 + 1 |
| 8316 | * = 24 |
| 8317 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8318 | char fmt[64]; /* plenty big enough! */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8319 | char *sign; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8320 | long x; |
| 8321 | |
| 8322 | x = PyInt_AsLong(v); |
| 8323 | if (x == -1 && PyErr_Occurred()) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8324 | return -1; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8325 | if (x < 0 && type == 'u') { |
| 8326 | type = 'd'; |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 8327 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8328 | if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) |
| 8329 | sign = "-"; |
| 8330 | else |
| 8331 | sign = ""; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8332 | if (prec < 0) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8333 | prec = 1; |
| 8334 | |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8335 | /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) |
| 8336 | * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8337 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8338 | if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8339 | PyErr_SetString(PyExc_OverflowError, |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8340 | "formatted integer is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8341 | return -1; |
| 8342 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8343 | |
| 8344 | if ((flags & F_ALT) && |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 8345 | (type == 'x' || type == 'X' || type == 'o')) { |
| 8346 | /* When converting under %#o, %#x or %#X, there are a number |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8347 | * of issues that cause pain: |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 8348 | * - for %#o, we want a different base marker than C |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8349 | * - when 0 is being converted, the C standard leaves off |
| 8350 | * the '0x' or '0X', which is inconsistent with other |
| 8351 | * %#x/%#X conversions and inconsistent with Python's |
| 8352 | * hex() function |
| 8353 | * - there are platforms that violate the standard and |
| 8354 | * convert 0 with the '0x' or '0X' |
| 8355 | * (Metrowerks, Compaq Tru64) |
| 8356 | * - there are platforms that give '0x' when converting |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8357 | * under %#X, but convert 0 in accordance with the |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8358 | * standard (OS/2 EMX) |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8359 | * |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8360 | * We can achieve the desired consistency by inserting our |
| 8361 | * own '0x' or '0X' prefix, and substituting %x/%X in place |
| 8362 | * of %#x/%#X. |
| 8363 | * |
| 8364 | * Note that this is the same approach as used in |
| 8365 | * formatint() in stringobject.c |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 8366 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8367 | PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", |
| 8368 | sign, type, prec, type); |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 8369 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8370 | else { |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8371 | PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", |
| 8372 | sign, (flags&F_ALT) ? "#" : "", |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8373 | prec, type); |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 8374 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8375 | if (sign[0]) |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8376 | return longtounicode(buf, buflen, fmt, -x); |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8377 | else |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8378 | return longtounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8379 | } |
| 8380 | |
| 8381 | static int |
| 8382 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8383 | size_t buflen, |
| 8384 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8385 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8386 | /* presume that the buffer is at least 2 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8387 | if (PyUnicode_Check(v)) { |
| 8388 | if (PyUnicode_GET_SIZE(v) != 1) |
| 8389 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8390 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8391 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8392 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8393 | else if (PyString_Check(v)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8394 | if (PyString_GET_SIZE(v) != 1) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8395 | goto onError; |
| 8396 | buf[0] = (Py_UNICODE)PyString_AS_STRING(v)[0]; |
| 8397 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8398 | |
| 8399 | else { |
| 8400 | /* Integer input truncated to a character */ |
| 8401 | long x; |
| 8402 | x = PyInt_AsLong(v); |
| 8403 | if (x == -1 && PyErr_Occurred()) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8404 | goto onError; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8405 | #ifdef Py_UNICODE_WIDE |
| 8406 | if (x < 0 || x > 0x10ffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 8407 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8408 | "%c arg not in range(0x110000) " |
| 8409 | "(wide Python build)"); |
| 8410 | return -1; |
| 8411 | } |
| 8412 | #else |
| 8413 | if (x < 0 || x > 0xffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 8414 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8415 | "%c arg not in range(0x10000) " |
| 8416 | "(narrow Python build)"); |
| 8417 | return -1; |
| 8418 | } |
| 8419 | #endif |
| 8420 | buf[0] = (Py_UNICODE) x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8421 | } |
| 8422 | buf[1] = '\0'; |
| 8423 | return 1; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8424 | |
| 8425 | onError: |
| 8426 | PyErr_SetString(PyExc_TypeError, |
| 8427 | "%c requires int or char"); |
| 8428 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8429 | } |
| 8430 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8431 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 8432 | |
| 8433 | FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
| 8434 | chars are formatted. XXX This is a magic number. Each formatting |
| 8435 | routine does bounds checking to ensure no overflow, but a better |
| 8436 | solution may be to malloc a buffer of appropriate size for each |
| 8437 | format. For now, the current solution is sufficient. |
| 8438 | */ |
| 8439 | #define FORMATBUFLEN (size_t)120 |
| 8440 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8441 | PyObject *PyUnicode_Format(PyObject *format, |
| 8442 | PyObject *args) |
| 8443 | { |
| 8444 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8445 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8446 | int args_owned = 0; |
| 8447 | PyUnicodeObject *result = NULL; |
| 8448 | PyObject *dict = NULL; |
| 8449 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8450 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8451 | if (format == NULL || args == NULL) { |
| 8452 | PyErr_BadInternalCall(); |
| 8453 | return NULL; |
| 8454 | } |
| 8455 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8456 | if (uformat == NULL) |
| 8457 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8458 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 8459 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 8460 | |
| 8461 | reslen = rescnt = fmtcnt + 100; |
| 8462 | result = _PyUnicode_New(reslen); |
| 8463 | if (result == NULL) |
| 8464 | goto onError; |
| 8465 | res = PyUnicode_AS_UNICODE(result); |
| 8466 | |
| 8467 | if (PyTuple_Check(args)) { |
| 8468 | arglen = PyTuple_Size(args); |
| 8469 | argidx = 0; |
| 8470 | } |
| 8471 | else { |
| 8472 | arglen = -1; |
| 8473 | argidx = -2; |
| 8474 | } |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 8475 | if (Py_Type(args)->tp_as_mapping && !PyTuple_Check(args) && |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 8476 | !PyString_Check(args) && !PyUnicode_Check(args)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8477 | dict = args; |
| 8478 | |
| 8479 | while (--fmtcnt >= 0) { |
| 8480 | if (*fmt != '%') { |
| 8481 | if (--rescnt < 0) { |
| 8482 | rescnt = fmtcnt + 100; |
| 8483 | reslen += rescnt; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8484 | if (_PyUnicode_Resize(&result, reslen) < 0) |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 8485 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8486 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 8487 | --rescnt; |
| 8488 | } |
| 8489 | *res++ = *fmt++; |
| 8490 | } |
| 8491 | else { |
| 8492 | /* Got a format specifier */ |
| 8493 | int flags = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8494 | Py_ssize_t width = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8495 | int prec = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8496 | Py_UNICODE c = '\0'; |
| 8497 | Py_UNICODE fill; |
| 8498 | PyObject *v = NULL; |
| 8499 | PyObject *temp = NULL; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8500 | Py_UNICODE *pbuf; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8501 | Py_UNICODE sign; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8502 | Py_ssize_t len; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8503 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8504 | |
| 8505 | fmt++; |
| 8506 | if (*fmt == '(') { |
| 8507 | Py_UNICODE *keystart; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8508 | Py_ssize_t keylen; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8509 | PyObject *key; |
| 8510 | int pcount = 1; |
| 8511 | |
| 8512 | if (dict == NULL) { |
| 8513 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8514 | "format requires a mapping"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8515 | goto onError; |
| 8516 | } |
| 8517 | ++fmt; |
| 8518 | --fmtcnt; |
| 8519 | keystart = fmt; |
| 8520 | /* Skip over balanced parentheses */ |
| 8521 | while (pcount > 0 && --fmtcnt >= 0) { |
| 8522 | if (*fmt == ')') |
| 8523 | --pcount; |
| 8524 | else if (*fmt == '(') |
| 8525 | ++pcount; |
| 8526 | fmt++; |
| 8527 | } |
| 8528 | keylen = fmt - keystart - 1; |
| 8529 | if (fmtcnt < 0 || pcount > 0) { |
| 8530 | PyErr_SetString(PyExc_ValueError, |
| 8531 | "incomplete format key"); |
| 8532 | goto onError; |
| 8533 | } |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 8534 | #if 0 |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8535 | /* keys are converted to strings using UTF-8 and |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8536 | then looked up since Python uses strings to hold |
| 8537 | variables names etc. in its namespaces and we |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8538 | wouldn't want to break common idioms. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8539 | key = PyUnicode_EncodeUTF8(keystart, |
| 8540 | keylen, |
| 8541 | NULL); |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 8542 | #else |
| 8543 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 8544 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8545 | if (key == NULL) |
| 8546 | goto onError; |
| 8547 | if (args_owned) { |
| 8548 | Py_DECREF(args); |
| 8549 | args_owned = 0; |
| 8550 | } |
| 8551 | args = PyObject_GetItem(dict, key); |
| 8552 | Py_DECREF(key); |
| 8553 | if (args == NULL) { |
| 8554 | goto onError; |
| 8555 | } |
| 8556 | args_owned = 1; |
| 8557 | arglen = -1; |
| 8558 | argidx = -2; |
| 8559 | } |
| 8560 | while (--fmtcnt >= 0) { |
| 8561 | switch (c = *fmt++) { |
| 8562 | case '-': flags |= F_LJUST; continue; |
| 8563 | case '+': flags |= F_SIGN; continue; |
| 8564 | case ' ': flags |= F_BLANK; continue; |
| 8565 | case '#': flags |= F_ALT; continue; |
| 8566 | case '0': flags |= F_ZERO; continue; |
| 8567 | } |
| 8568 | break; |
| 8569 | } |
| 8570 | if (c == '*') { |
| 8571 | v = getnextarg(args, arglen, &argidx); |
| 8572 | if (v == NULL) |
| 8573 | goto onError; |
| 8574 | if (!PyInt_Check(v)) { |
| 8575 | PyErr_SetString(PyExc_TypeError, |
| 8576 | "* wants int"); |
| 8577 | goto onError; |
| 8578 | } |
| 8579 | width = PyInt_AsLong(v); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 8580 | if (width == -1 && PyErr_Occurred()) |
| 8581 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8582 | if (width < 0) { |
| 8583 | flags |= F_LJUST; |
| 8584 | width = -width; |
| 8585 | } |
| 8586 | if (--fmtcnt >= 0) |
| 8587 | c = *fmt++; |
| 8588 | } |
| 8589 | else if (c >= '0' && c <= '9') { |
| 8590 | width = c - '0'; |
| 8591 | while (--fmtcnt >= 0) { |
| 8592 | c = *fmt++; |
| 8593 | if (c < '0' || c > '9') |
| 8594 | break; |
| 8595 | if ((width*10) / 10 != width) { |
| 8596 | PyErr_SetString(PyExc_ValueError, |
| 8597 | "width too big"); |
| 8598 | goto onError; |
| 8599 | } |
| 8600 | width = width*10 + (c - '0'); |
| 8601 | } |
| 8602 | } |
| 8603 | if (c == '.') { |
| 8604 | prec = 0; |
| 8605 | if (--fmtcnt >= 0) |
| 8606 | c = *fmt++; |
| 8607 | if (c == '*') { |
| 8608 | v = getnextarg(args, arglen, &argidx); |
| 8609 | if (v == NULL) |
| 8610 | goto onError; |
| 8611 | if (!PyInt_Check(v)) { |
| 8612 | PyErr_SetString(PyExc_TypeError, |
| 8613 | "* wants int"); |
| 8614 | goto onError; |
| 8615 | } |
| 8616 | prec = PyInt_AsLong(v); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 8617 | if (prec == -1 && PyErr_Occurred()) |
| 8618 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8619 | if (prec < 0) |
| 8620 | prec = 0; |
| 8621 | if (--fmtcnt >= 0) |
| 8622 | c = *fmt++; |
| 8623 | } |
| 8624 | else if (c >= '0' && c <= '9') { |
| 8625 | prec = c - '0'; |
| 8626 | while (--fmtcnt >= 0) { |
| 8627 | c = Py_CHARMASK(*fmt++); |
| 8628 | if (c < '0' || c > '9') |
| 8629 | break; |
| 8630 | if ((prec*10) / 10 != prec) { |
| 8631 | PyErr_SetString(PyExc_ValueError, |
| 8632 | "prec too big"); |
| 8633 | goto onError; |
| 8634 | } |
| 8635 | prec = prec*10 + (c - '0'); |
| 8636 | } |
| 8637 | } |
| 8638 | } /* prec */ |
| 8639 | if (fmtcnt >= 0) { |
| 8640 | if (c == 'h' || c == 'l' || c == 'L') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8641 | if (--fmtcnt >= 0) |
| 8642 | c = *fmt++; |
| 8643 | } |
| 8644 | } |
| 8645 | if (fmtcnt < 0) { |
| 8646 | PyErr_SetString(PyExc_ValueError, |
| 8647 | "incomplete format"); |
| 8648 | goto onError; |
| 8649 | } |
| 8650 | if (c != '%') { |
| 8651 | v = getnextarg(args, arglen, &argidx); |
| 8652 | if (v == NULL) |
| 8653 | goto onError; |
| 8654 | } |
| 8655 | sign = 0; |
| 8656 | fill = ' '; |
| 8657 | switch (c) { |
| 8658 | |
| 8659 | case '%': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8660 | pbuf = formatbuf; |
| 8661 | /* presume that buffer length is at least 1 */ |
| 8662 | pbuf[0] = '%'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8663 | len = 1; |
| 8664 | break; |
| 8665 | |
| 8666 | case 's': |
| 8667 | case 'r': |
| 8668 | if (PyUnicode_Check(v) && c == 's') { |
| 8669 | temp = v; |
| 8670 | Py_INCREF(temp); |
| 8671 | } |
| 8672 | else { |
| 8673 | PyObject *unicode; |
| 8674 | if (c == 's') |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 8675 | temp = PyObject_Str(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8676 | else |
| 8677 | temp = PyObject_Repr(v); |
| 8678 | if (temp == NULL) |
| 8679 | goto onError; |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8680 | if (PyUnicode_Check(temp)) |
| 8681 | /* nothing to do */; |
| 8682 | else if (PyString_Check(temp)) { |
| 8683 | /* convert to string to Unicode */ |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8684 | unicode = PyUnicode_Decode(PyString_AS_STRING(temp), |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8685 | PyString_GET_SIZE(temp), |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8686 | NULL, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8687 | "strict"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8688 | Py_DECREF(temp); |
| 8689 | temp = unicode; |
| 8690 | if (temp == NULL) |
| 8691 | goto onError; |
| 8692 | } |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8693 | else { |
| 8694 | Py_DECREF(temp); |
| 8695 | PyErr_SetString(PyExc_TypeError, |
| 8696 | "%s argument has non-string str()"); |
| 8697 | goto onError; |
| 8698 | } |
| 8699 | } |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8700 | pbuf = PyUnicode_AS_UNICODE(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8701 | len = PyUnicode_GET_SIZE(temp); |
| 8702 | if (prec >= 0 && len > prec) |
| 8703 | len = prec; |
| 8704 | break; |
| 8705 | |
| 8706 | case 'i': |
| 8707 | case 'd': |
| 8708 | case 'u': |
| 8709 | case 'o': |
| 8710 | case 'x': |
| 8711 | case 'X': |
| 8712 | if (c == 'i') |
| 8713 | c = 'd'; |
Tim Peters | a3a3a03 | 2000-11-30 05:22:44 +0000 | [diff] [blame] | 8714 | if (PyLong_Check(v)) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8715 | temp = formatlong(v, flags, prec, c); |
| 8716 | if (!temp) |
| 8717 | goto onError; |
| 8718 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 8719 | len = PyUnicode_GET_SIZE(temp); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8720 | sign = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8721 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8722 | else { |
| 8723 | pbuf = formatbuf; |
| 8724 | len = formatint(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 8725 | flags, prec, c, v); |
| 8726 | if (len < 0) |
| 8727 | goto onError; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8728 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8729 | } |
| 8730 | if (flags & F_ZERO) |
| 8731 | fill = '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8732 | break; |
| 8733 | |
| 8734 | case 'e': |
| 8735 | case 'E': |
| 8736 | case 'f': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 8737 | case 'F': |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8738 | case 'g': |
| 8739 | case 'G': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 8740 | if (c == 'F') |
| 8741 | c = 'f'; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8742 | pbuf = formatbuf; |
| 8743 | len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 8744 | flags, prec, c, v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8745 | if (len < 0) |
| 8746 | goto onError; |
| 8747 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8748 | if (flags & F_ZERO) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8749 | fill = '0'; |
| 8750 | break; |
| 8751 | |
| 8752 | case 'c': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8753 | pbuf = formatbuf; |
| 8754 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8755 | if (len < 0) |
| 8756 | goto onError; |
| 8757 | break; |
| 8758 | |
| 8759 | default: |
| 8760 | PyErr_Format(PyExc_ValueError, |
Andrew M. Kuchling | 6ca8917 | 2000-12-15 13:07:46 +0000 | [diff] [blame] | 8761 | "unsupported format character '%c' (0x%x) " |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 8762 | "at index %zd", |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8763 | (31<=c && c<=126) ? (char)c : '?', |
Marc-André Lemburg | 24e53b6 | 2002-09-24 09:32:14 +0000 | [diff] [blame] | 8764 | (int)c, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 8765 | (Py_ssize_t)(fmt - 1 - |
| 8766 | PyUnicode_AS_UNICODE(uformat))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8767 | goto onError; |
| 8768 | } |
| 8769 | if (sign) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8770 | if (*pbuf == '-' || *pbuf == '+') { |
| 8771 | sign = *pbuf++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8772 | len--; |
| 8773 | } |
| 8774 | else if (flags & F_SIGN) |
| 8775 | sign = '+'; |
| 8776 | else if (flags & F_BLANK) |
| 8777 | sign = ' '; |
| 8778 | else |
| 8779 | sign = 0; |
| 8780 | } |
| 8781 | if (width < len) |
| 8782 | width = len; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8783 | if (rescnt - (sign != 0) < width) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8784 | reslen -= rescnt; |
| 8785 | rescnt = width + fmtcnt + 100; |
| 8786 | reslen += rescnt; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8787 | if (reslen < 0) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 8788 | Py_XDECREF(temp); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8789 | PyErr_NoMemory(); |
| 8790 | goto onError; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8791 | } |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8792 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 8793 | Py_XDECREF(temp); |
| 8794 | goto onError; |
| 8795 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8796 | res = PyUnicode_AS_UNICODE(result) |
| 8797 | + reslen - rescnt; |
| 8798 | } |
| 8799 | if (sign) { |
| 8800 | if (fill != ' ') |
| 8801 | *res++ = sign; |
| 8802 | rescnt--; |
| 8803 | if (width > len) |
| 8804 | width--; |
| 8805 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 8806 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8807 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8808 | assert(pbuf[1] == c); |
| 8809 | if (fill != ' ') { |
| 8810 | *res++ = *pbuf++; |
| 8811 | *res++ = *pbuf++; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8812 | } |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8813 | rescnt -= 2; |
| 8814 | width -= 2; |
| 8815 | if (width < 0) |
| 8816 | width = 0; |
| 8817 | len -= 2; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8818 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8819 | if (width > len && !(flags & F_LJUST)) { |
| 8820 | do { |
| 8821 | --rescnt; |
| 8822 | *res++ = fill; |
| 8823 | } while (--width > len); |
| 8824 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8825 | if (fill == ' ') { |
| 8826 | if (sign) |
| 8827 | *res++ = sign; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 8828 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8829 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8830 | assert(pbuf[1] == c); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8831 | *res++ = *pbuf++; |
| 8832 | *res++ = *pbuf++; |
| 8833 | } |
| 8834 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8835 | Py_UNICODE_COPY(res, pbuf, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8836 | res += len; |
| 8837 | rescnt -= len; |
| 8838 | while (--width >= len) { |
| 8839 | --rescnt; |
| 8840 | *res++ = ' '; |
| 8841 | } |
| 8842 | if (dict && (argidx < arglen) && c != '%') { |
| 8843 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 8844 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8845 | Py_XDECREF(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8846 | goto onError; |
| 8847 | } |
| 8848 | Py_XDECREF(temp); |
| 8849 | } /* '%' */ |
| 8850 | } /* until end */ |
| 8851 | if (argidx < arglen && !dict) { |
| 8852 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 8853 | "not all arguments converted during string formatting"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8854 | goto onError; |
| 8855 | } |
| 8856 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8857 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
| 8858 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8859 | if (args_owned) { |
| 8860 | Py_DECREF(args); |
| 8861 | } |
| 8862 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8863 | return (PyObject *)result; |
| 8864 | |
| 8865 | onError: |
| 8866 | Py_XDECREF(result); |
| 8867 | Py_DECREF(uformat); |
| 8868 | if (args_owned) { |
| 8869 | Py_DECREF(args); |
| 8870 | } |
| 8871 | return NULL; |
| 8872 | } |
| 8873 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 8874 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8875 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 8876 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8877 | static PyObject * |
| 8878 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 8879 | { |
| 8880 | PyObject *x = NULL; |
Guido van Rossum | 55b4a7b | 2007-07-11 09:28:11 +0000 | [diff] [blame] | 8881 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8882 | char *encoding = NULL; |
| 8883 | char *errors = NULL; |
| 8884 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8885 | if (type != &PyUnicode_Type) |
| 8886 | return unicode_subtype_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8887 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:unicode", |
| 8888 | kwlist, &x, &encoding, &errors)) |
| 8889 | return NULL; |
| 8890 | if (x == NULL) |
| 8891 | return (PyObject *)_PyUnicode_New(0); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 8892 | if (encoding == NULL && errors == NULL) |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 8893 | return PyObject_Str(x); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 8894 | else |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8895 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
| 8896 | } |
| 8897 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8898 | static PyObject * |
| 8899 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 8900 | { |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8901 | PyUnicodeObject *tmp, *pnew; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8902 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8903 | |
| 8904 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 8905 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 8906 | if (tmp == NULL) |
| 8907 | return NULL; |
| 8908 | assert(PyUnicode_Check(tmp)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8909 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 8910 | if (pnew == NULL) { |
| 8911 | Py_DECREF(tmp); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8912 | return NULL; |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 8913 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8914 | pnew->str = PyMem_NEW(Py_UNICODE, n+1); |
| 8915 | if (pnew->str == NULL) { |
| 8916 | _Py_ForgetReference((PyObject *)pnew); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 8917 | PyObject_Del(pnew); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 8918 | Py_DECREF(tmp); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 8919 | return PyErr_NoMemory(); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8920 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8921 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 8922 | pnew->length = n; |
| 8923 | pnew->hash = tmp->hash; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8924 | Py_DECREF(tmp); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8925 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8926 | } |
| 8927 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8928 | PyDoc_STRVAR(unicode_doc, |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 8929 | "str(string [, encoding[, errors]]) -> object\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8930 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 8931 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 8932 | encoding defaults to the current default string encoding.\n\ |
| 8933 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8934 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 8935 | static PyObject *unicode_iter(PyObject *seq); |
| 8936 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8937 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 8938 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | 84fc66d | 2007-05-03 17:18:26 +0000 | [diff] [blame] | 8939 | "str", /* tp_name */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8940 | sizeof(PyUnicodeObject), /* tp_size */ |
| 8941 | 0, /* tp_itemsize */ |
| 8942 | /* Slots */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 8943 | (destructor)unicode_dealloc, /* tp_dealloc */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8944 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8945 | 0, /* tp_getattr */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8946 | 0, /* tp_setattr */ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 8947 | 0, /* tp_compare */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8948 | unicode_repr, /* tp_repr */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8949 | &unicode_as_number, /* tp_as_number */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8950 | &unicode_as_sequence, /* tp_as_sequence */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8951 | &unicode_as_mapping, /* tp_as_mapping */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8952 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 8953 | 0, /* tp_call*/ |
| 8954 | (reprfunc) unicode_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8955 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 8956 | 0, /* tp_setattro */ |
Alexandre Vassalotti | 70a2371 | 2007-10-14 02:05:51 +0000 | [diff] [blame] | 8957 | 0, /* tp_as_buffer */ |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 8958 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 8959 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8960 | unicode_doc, /* tp_doc */ |
| 8961 | 0, /* tp_traverse */ |
| 8962 | 0, /* tp_clear */ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 8963 | PyUnicode_RichCompare, /* tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8964 | 0, /* tp_weaklistoffset */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 8965 | unicode_iter, /* tp_iter */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8966 | 0, /* tp_iternext */ |
| 8967 | unicode_methods, /* tp_methods */ |
| 8968 | 0, /* tp_members */ |
| 8969 | 0, /* tp_getset */ |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 8970 | &PyBaseObject_Type, /* tp_base */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8971 | 0, /* tp_dict */ |
| 8972 | 0, /* tp_descr_get */ |
| 8973 | 0, /* tp_descr_set */ |
| 8974 | 0, /* tp_dictoffset */ |
| 8975 | 0, /* tp_init */ |
| 8976 | 0, /* tp_alloc */ |
| 8977 | unicode_new, /* tp_new */ |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 8978 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8979 | }; |
| 8980 | |
| 8981 | /* Initialize the Unicode implementation */ |
| 8982 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 8983 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8984 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8985 | int i; |
| 8986 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8987 | /* XXX - move this array to unicodectype.c ? */ |
| 8988 | Py_UNICODE linebreak[] = { |
| 8989 | 0x000A, /* LINE FEED */ |
| 8990 | 0x000D, /* CARRIAGE RETURN */ |
| 8991 | 0x001C, /* FILE SEPARATOR */ |
| 8992 | 0x001D, /* GROUP SEPARATOR */ |
| 8993 | 0x001E, /* RECORD SEPARATOR */ |
| 8994 | 0x0085, /* NEXT LINE */ |
| 8995 | 0x2028, /* LINE SEPARATOR */ |
| 8996 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 8997 | }; |
| 8998 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8999 | /* Init the implementation */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 9000 | unicode_freelist = NULL; |
| 9001 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9002 | unicode_empty = _PyUnicode_New(0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9003 | if (!unicode_empty) |
| 9004 | return; |
| 9005 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9006 | for (i = 0; i < 256; i++) |
| 9007 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 9008 | if (PyType_Ready(&PyUnicode_Type) < 0) |
| 9009 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9010 | |
| 9011 | /* initialize the linebreak bloom filter */ |
| 9012 | bloom_linebreak = make_bloom_mask( |
| 9013 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 9014 | ); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9015 | |
| 9016 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9017 | } |
| 9018 | |
| 9019 | /* Finalize the Unicode implementation */ |
| 9020 | |
| 9021 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9022 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9023 | { |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 9024 | PyUnicodeObject *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9025 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9026 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 9027 | Py_XDECREF(unicode_empty); |
| 9028 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 9029 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9030 | for (i = 0; i < 256; i++) { |
| 9031 | if (unicode_latin1[i]) { |
| 9032 | Py_DECREF(unicode_latin1[i]); |
| 9033 | unicode_latin1[i] = NULL; |
| 9034 | } |
| 9035 | } |
| 9036 | |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 9037 | for (u = unicode_freelist; u != NULL;) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9038 | PyUnicodeObject *v = u; |
| 9039 | u = *(PyUnicodeObject **)u; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 9040 | if (v->str) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 9041 | PyMem_DEL(v->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 9042 | Py_XDECREF(v->defenc); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 9043 | PyObject_Del(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9044 | } |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 9045 | unicode_freelist = NULL; |
| 9046 | unicode_freelist_size = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9047 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9048 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9049 | void |
| 9050 | PyUnicode_InternInPlace(PyObject **p) |
| 9051 | { |
| 9052 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 9053 | PyObject *t; |
| 9054 | if (s == NULL || !PyUnicode_Check(s)) |
| 9055 | Py_FatalError( |
| 9056 | "PyUnicode_InternInPlace: unicode strings only please!"); |
| 9057 | /* If it's a subclass, we don't really know what putting |
| 9058 | it in the interned dict might do. */ |
| 9059 | if (!PyUnicode_CheckExact(s)) |
| 9060 | return; |
| 9061 | if (PyUnicode_CHECK_INTERNED(s)) |
| 9062 | return; |
| 9063 | if (interned == NULL) { |
| 9064 | interned = PyDict_New(); |
| 9065 | if (interned == NULL) { |
| 9066 | PyErr_Clear(); /* Don't leave an exception */ |
| 9067 | return; |
| 9068 | } |
| 9069 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9070 | /* It might be that the GetItem call fails even |
| 9071 | though the key is present in the dictionary, |
| 9072 | namely when this happens during a stack overflow. */ |
| 9073 | Py_ALLOW_RECURSION |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9074 | t = PyDict_GetItem(interned, (PyObject *)s); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9075 | Py_END_ALLOW_RECURSION |
| 9076 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9077 | if (t) { |
| 9078 | Py_INCREF(t); |
| 9079 | Py_DECREF(*p); |
| 9080 | *p = t; |
| 9081 | return; |
| 9082 | } |
| 9083 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9084 | PyThreadState_GET()->recursion_critical = 1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9085 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 9086 | PyErr_Clear(); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9087 | PyThreadState_GET()->recursion_critical = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9088 | return; |
| 9089 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9090 | PyThreadState_GET()->recursion_critical = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9091 | /* The two references in interned are not counted by refcnt. |
| 9092 | The deallocator will take care of this */ |
Martin v. Löwis | 5d7428b | 2007-07-21 18:47:48 +0000 | [diff] [blame] | 9093 | Py_Refcnt(s) -= 2; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9094 | PyUnicode_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL; |
| 9095 | } |
| 9096 | |
| 9097 | void |
| 9098 | PyUnicode_InternImmortal(PyObject **p) |
| 9099 | { |
| 9100 | PyUnicode_InternInPlace(p); |
| 9101 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
| 9102 | PyUnicode_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL; |
| 9103 | Py_INCREF(*p); |
| 9104 | } |
| 9105 | } |
| 9106 | |
| 9107 | PyObject * |
| 9108 | PyUnicode_InternFromString(const char *cp) |
| 9109 | { |
| 9110 | PyObject *s = PyUnicode_FromString(cp); |
| 9111 | if (s == NULL) |
| 9112 | return NULL; |
| 9113 | PyUnicode_InternInPlace(&s); |
| 9114 | return s; |
| 9115 | } |
| 9116 | |
| 9117 | void _Py_ReleaseInternedUnicodeStrings(void) |
| 9118 | { |
| 9119 | PyObject *keys; |
| 9120 | PyUnicodeObject *s; |
| 9121 | Py_ssize_t i, n; |
| 9122 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
| 9123 | |
| 9124 | if (interned == NULL || !PyDict_Check(interned)) |
| 9125 | return; |
| 9126 | keys = PyDict_Keys(interned); |
| 9127 | if (keys == NULL || !PyList_Check(keys)) { |
| 9128 | PyErr_Clear(); |
| 9129 | return; |
| 9130 | } |
| 9131 | |
| 9132 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 9133 | detector, interned unicode strings are not forcibly deallocated; |
| 9134 | rather, we give them their stolen references back, and then clear |
| 9135 | and DECREF the interned dict. */ |
| 9136 | |
| 9137 | n = PyList_GET_SIZE(keys); |
| 9138 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
| 9139 | n); |
| 9140 | for (i = 0; i < n; i++) { |
| 9141 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
| 9142 | switch (s->state) { |
| 9143 | case SSTATE_NOT_INTERNED: |
| 9144 | /* XXX Shouldn't happen */ |
| 9145 | break; |
| 9146 | case SSTATE_INTERNED_IMMORTAL: |
Martin v. Löwis | 5d7428b | 2007-07-21 18:47:48 +0000 | [diff] [blame] | 9147 | Py_Refcnt(s) += 1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9148 | immortal_size += s->length; |
| 9149 | break; |
| 9150 | case SSTATE_INTERNED_MORTAL: |
Martin v. Löwis | 5d7428b | 2007-07-21 18:47:48 +0000 | [diff] [blame] | 9151 | Py_Refcnt(s) += 2; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9152 | mortal_size += s->length; |
| 9153 | break; |
| 9154 | default: |
| 9155 | Py_FatalError("Inconsistent interned string state."); |
| 9156 | } |
| 9157 | s->state = SSTATE_NOT_INTERNED; |
| 9158 | } |
| 9159 | fprintf(stderr, "total size of all interned strings: " |
| 9160 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 9161 | "mortal/immortal\n", mortal_size, immortal_size); |
| 9162 | Py_DECREF(keys); |
| 9163 | PyDict_Clear(interned); |
| 9164 | Py_DECREF(interned); |
| 9165 | interned = NULL; |
| 9166 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9167 | |
| 9168 | |
| 9169 | /********************* Unicode Iterator **************************/ |
| 9170 | |
| 9171 | typedef struct { |
| 9172 | PyObject_HEAD |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9173 | Py_ssize_t it_index; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9174 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
| 9175 | } unicodeiterobject; |
| 9176 | |
| 9177 | static void |
| 9178 | unicodeiter_dealloc(unicodeiterobject *it) |
| 9179 | { |
| 9180 | _PyObject_GC_UNTRACK(it); |
| 9181 | Py_XDECREF(it->it_seq); |
| 9182 | PyObject_GC_Del(it); |
| 9183 | } |
| 9184 | |
| 9185 | static int |
| 9186 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 9187 | { |
| 9188 | Py_VISIT(it->it_seq); |
| 9189 | return 0; |
| 9190 | } |
| 9191 | |
| 9192 | static PyObject * |
| 9193 | unicodeiter_next(unicodeiterobject *it) |
| 9194 | { |
| 9195 | PyUnicodeObject *seq; |
| 9196 | PyObject *item; |
| 9197 | |
| 9198 | assert(it != NULL); |
| 9199 | seq = it->it_seq; |
| 9200 | if (seq == NULL) |
| 9201 | return NULL; |
| 9202 | assert(PyUnicode_Check(seq)); |
| 9203 | |
| 9204 | if (it->it_index < PyUnicode_GET_SIZE(seq)) { |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9205 | item = PyUnicode_FromUnicode( |
| 9206 | PyUnicode_AS_UNICODE(seq)+it->it_index, 1); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9207 | if (item != NULL) |
| 9208 | ++it->it_index; |
| 9209 | return item; |
| 9210 | } |
| 9211 | |
| 9212 | Py_DECREF(seq); |
| 9213 | it->it_seq = NULL; |
| 9214 | return NULL; |
| 9215 | } |
| 9216 | |
| 9217 | static PyObject * |
| 9218 | unicodeiter_len(unicodeiterobject *it) |
| 9219 | { |
| 9220 | Py_ssize_t len = 0; |
| 9221 | if (it->it_seq) |
| 9222 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
| 9223 | return PyInt_FromSsize_t(len); |
| 9224 | } |
| 9225 | |
| 9226 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 9227 | |
| 9228 | static PyMethodDef unicodeiter_methods[] = { |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9229 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
| 9230 | length_hint_doc}, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9231 | {NULL, NULL} /* sentinel */ |
| 9232 | }; |
| 9233 | |
| 9234 | PyTypeObject PyUnicodeIter_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 9235 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 9236 | "unicode_iterator", /* tp_name */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9237 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 9238 | 0, /* tp_itemsize */ |
| 9239 | /* methods */ |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9240 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9241 | 0, /* tp_print */ |
| 9242 | 0, /* tp_getattr */ |
| 9243 | 0, /* tp_setattr */ |
| 9244 | 0, /* tp_compare */ |
| 9245 | 0, /* tp_repr */ |
| 9246 | 0, /* tp_as_number */ |
| 9247 | 0, /* tp_as_sequence */ |
| 9248 | 0, /* tp_as_mapping */ |
| 9249 | 0, /* tp_hash */ |
| 9250 | 0, /* tp_call */ |
| 9251 | 0, /* tp_str */ |
| 9252 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9253 | 0, /* tp_setattro */ |
| 9254 | 0, /* tp_as_buffer */ |
| 9255 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 9256 | 0, /* tp_doc */ |
| 9257 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 9258 | 0, /* tp_clear */ |
| 9259 | 0, /* tp_richcompare */ |
| 9260 | 0, /* tp_weaklistoffset */ |
| 9261 | PyObject_SelfIter, /* tp_iter */ |
| 9262 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 9263 | unicodeiter_methods, /* tp_methods */ |
| 9264 | 0, |
| 9265 | }; |
| 9266 | |
| 9267 | static PyObject * |
| 9268 | unicode_iter(PyObject *seq) |
| 9269 | { |
| 9270 | unicodeiterobject *it; |
| 9271 | |
| 9272 | if (!PyUnicode_Check(seq)) { |
| 9273 | PyErr_BadInternalCall(); |
| 9274 | return NULL; |
| 9275 | } |
| 9276 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 9277 | if (it == NULL) |
| 9278 | return NULL; |
| 9279 | it->it_index = 0; |
| 9280 | Py_INCREF(seq); |
| 9281 | it->it_seq = (PyUnicodeObject *)seq; |
| 9282 | _PyObject_GC_TRACK(it); |
| 9283 | return (PyObject *)it; |
| 9284 | } |
| 9285 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9286 | size_t |
| 9287 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 9288 | { |
| 9289 | int res = 0; |
| 9290 | while(*u++) |
| 9291 | res++; |
| 9292 | return res; |
| 9293 | } |
| 9294 | |
| 9295 | Py_UNICODE* |
| 9296 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9297 | { |
| 9298 | Py_UNICODE *u = s1; |
| 9299 | while ((*u++ = *s2++)); |
| 9300 | return s1; |
| 9301 | } |
| 9302 | |
| 9303 | Py_UNICODE* |
| 9304 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 9305 | { |
| 9306 | Py_UNICODE *u = s1; |
| 9307 | while ((*u++ = *s2++)) |
| 9308 | if (n-- == 0) |
| 9309 | break; |
| 9310 | return s1; |
| 9311 | } |
| 9312 | |
| 9313 | int |
| 9314 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9315 | { |
| 9316 | while (*s1 && *s2 && *s1 == *s2) |
| 9317 | s1++, s2++; |
| 9318 | if (*s1 && *s2) |
| 9319 | return (*s1 < *s2) ? -1 : +1; |
| 9320 | if (*s1) |
| 9321 | return 1; |
| 9322 | if (*s2) |
| 9323 | return -1; |
| 9324 | return 0; |
| 9325 | } |
| 9326 | |
| 9327 | Py_UNICODE* |
| 9328 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 9329 | { |
| 9330 | const Py_UNICODE *p; |
| 9331 | for (p = s; *p; p++) |
| 9332 | if (*p == c) |
| 9333 | return (Py_UNICODE*)p; |
| 9334 | return NULL; |
| 9335 | } |
| 9336 | |
| 9337 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9338 | #ifdef __cplusplus |
| 9339 | } |
| 9340 | #endif |
| 9341 | |
| 9342 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9343 | /* |
| 9344 | Local variables: |
| 9345 | c-basic-offset: 4 |
| 9346 | indent-tabs-mode: nil |
| 9347 | End: |
| 9348 | */ |