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 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 57 | #define PyUnicode_MAXFREELIST 1024 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 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 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 65 | At worst this will result in PyUnicode_MAXFREELIST * |
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 */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 109 | static PyUnicodeObject *free_list; |
| 110 | static int numfree; |
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 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 128 | /* Fast detection of the most frequent whitespace characters */ |
| 129 | const unsigned char _Py_ascii_whitespace[] = { |
| 130 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 131 | // case 0x0009: /* HORIZONTAL TABULATION */ |
| 132 | // case 0x000A: /* LINE FEED */ |
| 133 | // case 0x000B: /* VERTICAL TABULATION */ |
| 134 | // case 0x000C: /* FORM FEED */ |
| 135 | // case 0x000D: /* CARRIAGE RETURN */ |
| 136 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 137 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 138 | // case 0x001C: /* FILE SEPARATOR */ |
| 139 | // case 0x001D: /* GROUP SEPARATOR */ |
| 140 | // case 0x001E: /* RECORD SEPARATOR */ |
| 141 | // case 0x001F: /* UNIT SEPARATOR */ |
| 142 | 0, 0, 0, 0, 1, 1, 1, 1, |
| 143 | // case 0x0020: /* SPACE */ |
| 144 | 1, 0, 0, 0, 0, 0, 0, 0, |
| 145 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 146 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 147 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 148 | |
| 149 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 150 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 151 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 152 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 153 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 154 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 155 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 156 | 0, 0, 0, 0, 0, 0, 0, 0 |
| 157 | }; |
| 158 | |
| 159 | /* Same for linebreaks */ |
| 160 | static unsigned char ascii_linebreak[] = { |
| 161 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 162 | // 0x000A, /* LINE FEED */ |
| 163 | // 0x000D, /* CARRIAGE RETURN */ |
| 164 | 0, 0, 1, 0, 0, 1, 0, 0, |
| 165 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 166 | // 0x001C, /* FILE SEPARATOR */ |
| 167 | // 0x001D, /* GROUP SEPARATOR */ |
| 168 | // 0x001E, /* RECORD SEPARATOR */ |
| 169 | 0, 0, 0, 0, 1, 1, 1, 0, |
| 170 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 171 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 172 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 173 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 174 | |
| 175 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 176 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 177 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 178 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 179 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 180 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 181 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 182 | 0, 0, 0, 0, 0, 0, 0, 0 |
| 183 | }; |
| 184 | |
| 185 | |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 186 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 187 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 188 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 189 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 190 | return 0x10FFFF; |
| 191 | #else |
| 192 | /* This is actually an illegal character, so it should |
| 193 | not be passed to unichr. */ |
| 194 | return 0xFFFF; |
| 195 | #endif |
| 196 | } |
| 197 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 198 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 199 | |
| 200 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 201 | to keep things simple, we use a single bitmask, using the least 5 |
| 202 | bits from each unicode characters as the bit index. */ |
| 203 | |
| 204 | /* the linebreak mask is set up by Unicode_Init below */ |
| 205 | |
| 206 | #define BLOOM_MASK unsigned long |
| 207 | |
| 208 | static BLOOM_MASK bloom_linebreak; |
| 209 | |
| 210 | #define BLOOM(mask, ch) ((mask & (1 << ((ch) & 0x1F)))) |
| 211 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 212 | #define BLOOM_LINEBREAK(ch) \ |
| 213 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 214 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 215 | |
| 216 | Py_LOCAL_INLINE(BLOOM_MASK) make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len) |
| 217 | { |
| 218 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 219 | |
| 220 | long mask; |
| 221 | Py_ssize_t i; |
| 222 | |
| 223 | mask = 0; |
| 224 | for (i = 0; i < len; i++) |
| 225 | mask |= (1 << (ptr[i] & 0x1F)); |
| 226 | |
| 227 | return mask; |
| 228 | } |
| 229 | |
| 230 | Py_LOCAL_INLINE(int) unicode_member(Py_UNICODE chr, Py_UNICODE* set, Py_ssize_t setlen) |
| 231 | { |
| 232 | Py_ssize_t i; |
| 233 | |
| 234 | for (i = 0; i < setlen; i++) |
| 235 | if (set[i] == chr) |
| 236 | return 1; |
| 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | #define BLOOM_MEMBER(mask, chr, set, setlen)\ |
| 242 | BLOOM(mask, chr) && unicode_member(chr, set, setlen) |
| 243 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 244 | /* --- Unicode Object ----------------------------------------------------- */ |
| 245 | |
| 246 | static |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 247 | int unicode_resize(register PyUnicodeObject *unicode, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 248 | Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 249 | { |
| 250 | void *oldstr; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 251 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 252 | /* Shortcut if there's nothing much to do. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 253 | if (unicode->length == length) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 254 | goto reset; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 255 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 256 | /* Resizing shared object (unicode_empty or single character |
| 257 | objects) in-place is not allowed. Use PyUnicode_Resize() |
| 258 | instead ! */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 259 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 260 | if (unicode == unicode_empty || |
| 261 | (unicode->length == 1 && |
| 262 | unicode->str[0] < 256U && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 263 | unicode_latin1[unicode->str[0]] == unicode)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 264 | PyErr_SetString(PyExc_SystemError, |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 265 | "can't resize shared unicode objects"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 266 | return -1; |
| 267 | } |
| 268 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 269 | /* We allocate one more byte to make sure the string is Ux0000 terminated. |
| 270 | The overallocation is also used by fastsearch, which assumes that it's |
| 271 | safe to look at str[length] (without making any assumptions about what |
| 272 | it contains). */ |
| 273 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 274 | oldstr = unicode->str; |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 275 | unicode->str = PyObject_REALLOC(unicode->str, |
| 276 | sizeof(Py_UNICODE) * (length + 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 277 | if (!unicode->str) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 278 | unicode->str = (Py_UNICODE *)oldstr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 279 | PyErr_NoMemory(); |
| 280 | return -1; |
| 281 | } |
| 282 | unicode->str[length] = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 283 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 284 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 285 | reset: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 286 | /* Reset the object caches */ |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 287 | if (unicode->defenc) { |
| 288 | Py_DECREF(unicode->defenc); |
| 289 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 290 | } |
| 291 | unicode->hash = -1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 292 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | /* 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] | 297 | Ux0000 terminated; some code (e.g. new_identifier) |
| 298 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 299 | |
| 300 | XXX This allocator could further be enhanced by assuring that the |
| 301 | free list never reduces its size below 1. |
| 302 | |
| 303 | */ |
| 304 | |
| 305 | static |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 306 | PyUnicodeObject *_PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 307 | { |
| 308 | register PyUnicodeObject *unicode; |
| 309 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 310 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 311 | if (length == 0 && unicode_empty != NULL) { |
| 312 | Py_INCREF(unicode_empty); |
| 313 | return unicode_empty; |
| 314 | } |
| 315 | |
| 316 | /* Unicode freelist & memory allocation */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 317 | if (free_list) { |
| 318 | unicode = free_list; |
| 319 | free_list = *(PyUnicodeObject **)unicode; |
| 320 | numfree--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 321 | if (unicode->str) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 322 | /* Keep-Alive optimization: we only upsize the buffer, |
| 323 | never downsize it. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 324 | if ((unicode->length < length) && |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 325 | unicode_resize(unicode, length) < 0) { |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 326 | PyObject_DEL(unicode->str); |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 327 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 328 | } |
| 329 | } |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 330 | else { |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 331 | size_t new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 332 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 333 | } |
| 334 | PyObject_INIT(unicode, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 335 | } |
| 336 | else { |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 337 | size_t new_size; |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 338 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 339 | if (unicode == NULL) |
| 340 | return NULL; |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 341 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 342 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 345 | if (!unicode->str) { |
| 346 | PyErr_NoMemory(); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 347 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 348 | } |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 349 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 350 | * the caller fails before initializing str -- unicode_resize() |
| 351 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 352 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 353 | * We don't want unicode_resize to read uninitialized memory in |
| 354 | * that case. |
| 355 | */ |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 356 | unicode->str[0] = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 357 | unicode->str[length] = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 358 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 359 | unicode->hash = -1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 360 | unicode->state = 0; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 361 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 362 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 363 | |
| 364 | onError: |
| 365 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 366 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 367 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | static |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 371 | void unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 372 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 373 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
| 374 | case SSTATE_NOT_INTERNED: |
| 375 | break; |
| 376 | |
| 377 | case SSTATE_INTERNED_MORTAL: |
| 378 | /* revive dead object temporarily for DelItem */ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 379 | Py_REFCNT(unicode) = 3; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 380 | if (PyDict_DelItem(interned, (PyObject *)unicode) != 0) |
| 381 | Py_FatalError( |
| 382 | "deletion of interned unicode string failed"); |
| 383 | break; |
| 384 | |
| 385 | case SSTATE_INTERNED_IMMORTAL: |
| 386 | Py_FatalError("Immortal interned unicode string died."); |
| 387 | |
| 388 | default: |
| 389 | Py_FatalError("Inconsistent interned unicode string state."); |
| 390 | } |
| 391 | |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 392 | if (PyUnicode_CheckExact(unicode) && |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 393 | numfree < PyUnicode_MAXFREELIST) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 394 | /* Keep-Alive optimization */ |
| 395 | if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 396 | PyObject_DEL(unicode->str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 397 | unicode->str = NULL; |
| 398 | unicode->length = 0; |
| 399 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 400 | if (unicode->defenc) { |
| 401 | Py_DECREF(unicode->defenc); |
| 402 | unicode->defenc = NULL; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 403 | } |
| 404 | /* Add to free list */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 405 | *(PyUnicodeObject **)unicode = free_list; |
| 406 | free_list = unicode; |
| 407 | numfree++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 408 | } |
| 409 | else { |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 410 | PyObject_DEL(unicode->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 411 | Py_XDECREF(unicode->defenc); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 412 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 416 | int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 417 | { |
| 418 | register PyUnicodeObject *v; |
| 419 | |
| 420 | /* Argument checks */ |
| 421 | if (unicode == NULL) { |
| 422 | PyErr_BadInternalCall(); |
| 423 | return -1; |
| 424 | } |
| 425 | v = (PyUnicodeObject *)*unicode; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 426 | 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] | 427 | PyErr_BadInternalCall(); |
| 428 | return -1; |
| 429 | } |
| 430 | |
| 431 | /* Resizing unicode_empty and single character objects is not |
| 432 | possible since these are being shared. We simply return a fresh |
| 433 | copy with the same Unicode content. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 434 | if (v->length != length && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 435 | (v == unicode_empty || v->length == 1)) { |
| 436 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 437 | if (w == NULL) |
| 438 | return -1; |
| 439 | Py_UNICODE_COPY(w->str, v->str, |
| 440 | length < v->length ? length : v->length); |
Raymond Hettinger | c8df578 | 2003-03-09 07:30:43 +0000 | [diff] [blame] | 441 | Py_DECREF(*unicode); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 442 | *unicode = (PyObject *)w; |
| 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 447 | objects, since we can modify them in-place. */ |
| 448 | return unicode_resize(v, length); |
| 449 | } |
| 450 | |
| 451 | /* Internal API for use in unicodeobject.c only ! */ |
| 452 | #define _PyUnicode_Resize(unicodevar, length) \ |
| 453 | PyUnicode_Resize(((PyObject **)(unicodevar)), length) |
| 454 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 455 | PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 456 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 457 | { |
| 458 | PyUnicodeObject *unicode; |
| 459 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 460 | /* If the Unicode data is known at construction time, we can apply |
| 461 | some optimizations which share commonly used objects. */ |
| 462 | if (u != NULL) { |
| 463 | |
| 464 | /* Optimization for empty strings */ |
| 465 | if (size == 0 && unicode_empty != NULL) { |
| 466 | Py_INCREF(unicode_empty); |
| 467 | return (PyObject *)unicode_empty; |
| 468 | } |
| 469 | |
| 470 | /* Single character Unicode objects in the Latin-1 range are |
| 471 | shared when using this constructor */ |
| 472 | if (size == 1 && *u < 256) { |
| 473 | unicode = unicode_latin1[*u]; |
| 474 | if (!unicode) { |
| 475 | unicode = _PyUnicode_New(1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 476 | if (!unicode) |
| 477 | return NULL; |
Marc-André Lemburg | 8879a33 | 2001-06-07 12:26:56 +0000 | [diff] [blame] | 478 | unicode->str[0] = *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 479 | unicode_latin1[*u] = unicode; |
| 480 | } |
| 481 | Py_INCREF(unicode); |
| 482 | return (PyObject *)unicode; |
| 483 | } |
| 484 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 485 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 486 | unicode = _PyUnicode_New(size); |
| 487 | if (!unicode) |
| 488 | return NULL; |
| 489 | |
| 490 | /* Copy the Unicode data into the new object */ |
| 491 | if (u != NULL) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 492 | Py_UNICODE_COPY(unicode->str, u, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 493 | |
| 494 | return (PyObject *)unicode; |
| 495 | } |
| 496 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 497 | PyObject *PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 498 | { |
| 499 | PyUnicodeObject *unicode; |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 500 | /* 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] | 501 | some optimizations which share commonly used objects. |
| 502 | Also, this means the input must be UTF-8, so fall back to the |
| 503 | UTF-8 decoder at the end. */ |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 504 | if (u != NULL) { |
| 505 | |
| 506 | /* Optimization for empty strings */ |
| 507 | if (size == 0 && unicode_empty != NULL) { |
| 508 | Py_INCREF(unicode_empty); |
| 509 | return (PyObject *)unicode_empty; |
| 510 | } |
| 511 | |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 512 | /* Single characters are shared when using this constructor. |
| 513 | Restrict to ASCII, since the input must be UTF-8. */ |
| 514 | if (size == 1 && Py_CHARMASK(*u) < 128) { |
Guido van Rossum | 00058aa | 2007-07-19 18:21:28 +0000 | [diff] [blame] | 515 | unicode = unicode_latin1[Py_CHARMASK(*u)]; |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 516 | if (!unicode) { |
| 517 | unicode = _PyUnicode_New(1); |
| 518 | if (!unicode) |
| 519 | return NULL; |
Guido van Rossum | 00058aa | 2007-07-19 18:21:28 +0000 | [diff] [blame] | 520 | unicode->str[0] = Py_CHARMASK(*u); |
| 521 | unicode_latin1[Py_CHARMASK(*u)] = unicode; |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 522 | } |
| 523 | Py_INCREF(unicode); |
| 524 | return (PyObject *)unicode; |
| 525 | } |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 526 | |
| 527 | return PyUnicode_DecodeUTF8(u, size, NULL); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Walter Dörwald | 5550731 | 2007-05-18 13:12:10 +0000 | [diff] [blame] | 530 | unicode = _PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 531 | if (!unicode) |
| 532 | return NULL; |
| 533 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 534 | return (PyObject *)unicode; |
| 535 | } |
| 536 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 537 | PyObject *PyUnicode_FromString(const char *u) |
| 538 | { |
| 539 | size_t size = strlen(u); |
| 540 | if (size > PY_SSIZE_T_MAX) { |
| 541 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 542 | return NULL; |
| 543 | } |
| 544 | |
| 545 | return PyUnicode_FromStringAndSize(u, size); |
| 546 | } |
| 547 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 548 | #ifdef HAVE_WCHAR_H |
| 549 | |
| 550 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 551 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 552 | { |
| 553 | PyUnicodeObject *unicode; |
| 554 | |
| 555 | if (w == NULL) { |
| 556 | PyErr_BadInternalCall(); |
| 557 | return NULL; |
| 558 | } |
| 559 | |
| 560 | unicode = _PyUnicode_New(size); |
| 561 | if (!unicode) |
| 562 | return NULL; |
| 563 | |
| 564 | /* Copy the wchar_t data into the new object */ |
| 565 | #ifdef HAVE_USABLE_WCHAR_T |
| 566 | memcpy(unicode->str, w, size * sizeof(wchar_t)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 567 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 568 | { |
| 569 | register Py_UNICODE *u; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 570 | register Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 571 | u = PyUnicode_AS_UNICODE(unicode); |
Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 572 | for (i = size; i > 0; i--) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 573 | *u++ = *w++; |
| 574 | } |
| 575 | #endif |
| 576 | |
| 577 | return (PyObject *)unicode; |
| 578 | } |
| 579 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 580 | static void |
| 581 | makefmt(char *fmt, int longflag, int size_tflag, int zeropad, int width, int precision, char c) |
| 582 | { |
| 583 | *fmt++ = '%'; |
| 584 | if (width) { |
| 585 | if (zeropad) |
| 586 | *fmt++ = '0'; |
| 587 | fmt += sprintf(fmt, "%d", width); |
| 588 | } |
| 589 | if (precision) |
| 590 | fmt += sprintf(fmt, ".%d", precision); |
| 591 | if (longflag) |
| 592 | *fmt++ = 'l'; |
| 593 | else if (size_tflag) { |
| 594 | char *f = PY_FORMAT_SIZE_T; |
| 595 | while (*f) |
| 596 | *fmt++ = *f++; |
| 597 | } |
| 598 | *fmt++ = c; |
| 599 | *fmt = '\0'; |
| 600 | } |
| 601 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 602 | #define appendstring(string) {for (copy = string;*copy;) *s++ = *copy++;} |
| 603 | |
| 604 | PyObject * |
| 605 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 606 | { |
| 607 | va_list count; |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 608 | Py_ssize_t callcount = 0; |
| 609 | PyObject **callresults = NULL; |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 610 | PyObject **callresult = NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 611 | Py_ssize_t n = 0; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 612 | int width = 0; |
| 613 | int precision = 0; |
| 614 | int zeropad; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 615 | const char* f; |
| 616 | Py_UNICODE *s; |
| 617 | PyObject *string; |
| 618 | /* used by sprintf */ |
| 619 | char buffer[21]; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 620 | /* use abuffer instead of buffer, if we need more space |
| 621 | * (which can happen if there's a format specifier with width). */ |
| 622 | char *abuffer = NULL; |
| 623 | char *realbuffer; |
| 624 | Py_ssize_t abuffersize = 0; |
| 625 | char fmt[60]; /* should be enough for %0width.precisionld */ |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 626 | const char *copy; |
| 627 | |
| 628 | #ifdef VA_LIST_IS_ARRAY |
| 629 | Py_MEMCPY(count, vargs, sizeof(va_list)); |
| 630 | #else |
| 631 | #ifdef __va_copy |
| 632 | __va_copy(count, vargs); |
| 633 | #else |
| 634 | count = vargs; |
| 635 | #endif |
| 636 | #endif |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 637 | /* step 1: count the number of %S/%R format specifications |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 638 | * (we call PyObject_Str()/PyObject_Repr() for these objects |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 639 | * once during step 3 and put the result in an array) */ |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 640 | for (f = format; *f; f++) { |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 641 | if (*f == '%' && (*(f+1)=='S' || *(f+1)=='R')) |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 642 | ++callcount; |
| 643 | } |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 644 | /* step 2: allocate memory for the results of |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 645 | * PyObject_Str()/PyObject_Repr() calls */ |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 646 | if (callcount) { |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 647 | callresults = PyObject_Malloc(sizeof(PyObject *)*callcount); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 648 | if (!callresults) { |
| 649 | PyErr_NoMemory(); |
| 650 | return NULL; |
| 651 | } |
| 652 | callresult = callresults; |
| 653 | } |
| 654 | /* step 3: figure out how large a buffer we need */ |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 655 | for (f = format; *f; f++) { |
| 656 | if (*f == '%') { |
| 657 | const char* p = f; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 658 | width = 0; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 659 | while (ISDIGIT(*f)) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 660 | width = (width*10) + *f++ - '0'; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 661 | while (*++f && *f != '%' && !ISALPHA(*f)) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 662 | ; |
| 663 | |
| 664 | /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
| 665 | * they don't affect the amount of space we reserve. |
| 666 | */ |
| 667 | if ((*f == 'l' || *f == 'z') && |
| 668 | (f[1] == 'd' || f[1] == 'u')) |
Eric Smith | ddd2582 | 2007-08-27 11:33:42 +0000 | [diff] [blame] | 669 | ++f; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 670 | |
| 671 | switch (*f) { |
| 672 | case 'c': |
| 673 | (void)va_arg(count, int); |
| 674 | /* fall through... */ |
| 675 | case '%': |
| 676 | n++; |
| 677 | break; |
| 678 | case 'd': case 'u': case 'i': case 'x': |
| 679 | (void) va_arg(count, int); |
| 680 | /* 20 bytes is enough to hold a 64-bit |
| 681 | integer. Decimal takes the most space. |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 682 | This isn't enough for octal. |
| 683 | If a width is specified we need more |
| 684 | (which we allocate later). */ |
| 685 | if (width < 20) |
| 686 | width = 20; |
| 687 | n += width; |
| 688 | if (abuffersize < width) |
| 689 | abuffersize = width; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 690 | break; |
| 691 | case 's': |
Martin v. Löwis | 90d1fcd | 2007-08-31 11:01:23 +0000 | [diff] [blame] | 692 | { |
| 693 | /* UTF-8 */ |
| 694 | unsigned char*s; |
| 695 | s = va_arg(count, unsigned char*); |
| 696 | while (*s) { |
| 697 | if (*s < 128) { |
| 698 | n++; s++; |
| 699 | } else if (*s < 0xc0) { |
| 700 | /* invalid UTF-8 */ |
| 701 | n++; s++; |
| 702 | } else if (*s < 0xc0) { |
| 703 | n++; |
| 704 | s++; if(!*s)break; |
| 705 | s++; |
| 706 | } else if (*s < 0xe0) { |
| 707 | n++; |
| 708 | s++; if(!*s)break; |
| 709 | s++; if(!*s)break; |
| 710 | s++; |
| 711 | } else { |
| 712 | #ifdef Py_UNICODE_WIDE |
| 713 | n++; |
| 714 | #else |
| 715 | n+=2; |
| 716 | #endif |
| 717 | s++; if(!*s)break; |
| 718 | s++; if(!*s)break; |
| 719 | s++; if(!*s)break; |
| 720 | s++; |
| 721 | } |
| 722 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 723 | break; |
Martin v. Löwis | 90d1fcd | 2007-08-31 11:01:23 +0000 | [diff] [blame] | 724 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 725 | case 'U': |
| 726 | { |
| 727 | PyObject *obj = va_arg(count, PyObject *); |
| 728 | assert(obj && PyUnicode_Check(obj)); |
| 729 | n += PyUnicode_GET_SIZE(obj); |
| 730 | break; |
| 731 | } |
Walter Dörwald | d7fb764 | 2007-06-11 16:36:59 +0000 | [diff] [blame] | 732 | case 'V': |
| 733 | { |
| 734 | PyObject *obj = va_arg(count, PyObject *); |
| 735 | const char *str = va_arg(count, const char *); |
| 736 | assert(obj || str); |
| 737 | assert(!obj || PyUnicode_Check(obj)); |
| 738 | if (obj) |
| 739 | n += PyUnicode_GET_SIZE(obj); |
| 740 | else |
| 741 | n += strlen(str); |
| 742 | break; |
| 743 | } |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 744 | case 'S': |
| 745 | { |
| 746 | PyObject *obj = va_arg(count, PyObject *); |
| 747 | PyObject *str; |
| 748 | assert(obj); |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 749 | str = PyObject_Str(obj); |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 750 | if (!str) |
| 751 | goto fail; |
| 752 | n += PyUnicode_GET_SIZE(str); |
| 753 | /* Remember the str and switch to the next slot */ |
| 754 | *callresult++ = str; |
| 755 | break; |
| 756 | } |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 757 | case 'R': |
| 758 | { |
| 759 | PyObject *obj = va_arg(count, PyObject *); |
| 760 | PyObject *repr; |
| 761 | assert(obj); |
| 762 | repr = PyObject_Repr(obj); |
| 763 | if (!repr) |
| 764 | goto fail; |
| 765 | n += PyUnicode_GET_SIZE(repr); |
| 766 | /* Remember the repr and switch to the next slot */ |
| 767 | *callresult++ = repr; |
| 768 | break; |
| 769 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 770 | case 'p': |
| 771 | (void) va_arg(count, int); |
| 772 | /* maximum 64-bit pointer representation: |
| 773 | * 0xffffffffffffffff |
| 774 | * so 19 characters is enough. |
| 775 | * XXX I count 18 -- what's the extra for? |
| 776 | */ |
| 777 | n += 19; |
| 778 | break; |
| 779 | default: |
| 780 | /* if we stumble upon an unknown |
| 781 | formatting code, copy the rest of |
| 782 | the format string to the output |
| 783 | string. (we cannot just skip the |
| 784 | code, since there's no way to know |
| 785 | what's in the argument list) */ |
| 786 | n += strlen(p); |
| 787 | goto expand; |
| 788 | } |
| 789 | } else |
| 790 | n++; |
| 791 | } |
| 792 | expand: |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 793 | if (abuffersize > 20) { |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 794 | abuffer = PyObject_Malloc(abuffersize); |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 795 | if (!abuffer) { |
| 796 | PyErr_NoMemory(); |
| 797 | goto fail; |
| 798 | } |
| 799 | realbuffer = abuffer; |
| 800 | } |
| 801 | else |
| 802 | realbuffer = buffer; |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 803 | /* step 4: fill the buffer */ |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 804 | /* 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] | 805 | we don't have to resize the string. |
| 806 | There can be no errors beyond this point. */ |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 807 | string = PyUnicode_FromUnicode(NULL, n); |
| 808 | if (!string) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 809 | goto fail; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 810 | |
| 811 | s = PyUnicode_AS_UNICODE(string); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 812 | callresult = callresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 813 | |
| 814 | for (f = format; *f; f++) { |
| 815 | if (*f == '%') { |
| 816 | const char* p = f++; |
| 817 | int longflag = 0; |
| 818 | int size_tflag = 0; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 819 | zeropad = (*f == '0'); |
| 820 | /* parse the width.precision part */ |
| 821 | width = 0; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 822 | while (ISDIGIT(*f)) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 823 | width = (width*10) + *f++ - '0'; |
| 824 | precision = 0; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 825 | if (*f == '.') { |
| 826 | f++; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 827 | while (ISDIGIT(*f)) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 828 | precision = (precision*10) + *f++ - '0'; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 829 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 830 | /* handle the long flag, but only for %ld and %lu. |
| 831 | others can be added when necessary. */ |
| 832 | if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) { |
| 833 | longflag = 1; |
| 834 | ++f; |
| 835 | } |
| 836 | /* handle the size_t flag. */ |
| 837 | if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
| 838 | size_tflag = 1; |
| 839 | ++f; |
| 840 | } |
| 841 | |
| 842 | switch (*f) { |
| 843 | case 'c': |
| 844 | *s++ = va_arg(vargs, int); |
| 845 | break; |
| 846 | case 'd': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 847 | makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'd'); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 848 | if (longflag) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 849 | sprintf(realbuffer, fmt, va_arg(vargs, long)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 850 | else if (size_tflag) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 851 | sprintf(realbuffer, fmt, va_arg(vargs, Py_ssize_t)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 852 | else |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 853 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 854 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 855 | break; |
| 856 | case 'u': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 857 | makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'u'); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 858 | if (longflag) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 859 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned long)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 860 | else if (size_tflag) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 861 | sprintf(realbuffer, fmt, va_arg(vargs, size_t)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 862 | else |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 863 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned int)); |
| 864 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 865 | break; |
| 866 | case 'i': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 867 | makefmt(fmt, 0, 0, zeropad, width, precision, 'i'); |
| 868 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 869 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 870 | break; |
| 871 | case 'x': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 872 | makefmt(fmt, 0, 0, zeropad, width, precision, 'x'); |
| 873 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 874 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 875 | break; |
| 876 | case 's': |
Martin v. Löwis | 90d1fcd | 2007-08-31 11:01:23 +0000 | [diff] [blame] | 877 | { |
| 878 | /* Parameter must be UTF-8 encoded. |
| 879 | In case of encoding errors, use |
| 880 | the replacement character. */ |
| 881 | PyObject *u; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 882 | p = va_arg(vargs, char*); |
Martin v. Löwis | 90d1fcd | 2007-08-31 11:01:23 +0000 | [diff] [blame] | 883 | u = PyUnicode_DecodeUTF8(p, strlen(p), |
| 884 | "replace"); |
| 885 | if (!u) |
| 886 | goto fail; |
| 887 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(u), |
| 888 | PyUnicode_GET_SIZE(u)); |
| 889 | s += PyUnicode_GET_SIZE(u); |
| 890 | Py_DECREF(u); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 891 | break; |
Martin v. Löwis | 90d1fcd | 2007-08-31 11:01:23 +0000 | [diff] [blame] | 892 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 893 | case 'U': |
| 894 | { |
| 895 | PyObject *obj = va_arg(vargs, PyObject *); |
Walter Dörwald | 5c2fab6 | 2007-05-24 19:51:02 +0000 | [diff] [blame] | 896 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 897 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 898 | s += size; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 899 | break; |
| 900 | } |
Walter Dörwald | d7fb764 | 2007-06-11 16:36:59 +0000 | [diff] [blame] | 901 | case 'V': |
| 902 | { |
| 903 | PyObject *obj = va_arg(vargs, PyObject *); |
| 904 | const char *str = va_arg(vargs, const char *); |
| 905 | if (obj) { |
| 906 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 907 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 908 | s += size; |
| 909 | } else { |
| 910 | appendstring(str); |
| 911 | } |
| 912 | break; |
| 913 | } |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 914 | case 'S': |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 915 | case 'R': |
| 916 | { |
Guido van Rossum | 755114a | 2007-06-13 01:04:27 +0000 | [diff] [blame] | 917 | Py_UNICODE *ucopy; |
| 918 | Py_ssize_t usize; |
| 919 | Py_ssize_t upos; |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 920 | /* unused, since we already have the result */ |
| 921 | (void) va_arg(vargs, PyObject *); |
Guido van Rossum | 755114a | 2007-06-13 01:04:27 +0000 | [diff] [blame] | 922 | ucopy = PyUnicode_AS_UNICODE(*callresult); |
| 923 | usize = PyUnicode_GET_SIZE(*callresult); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 924 | for (upos = 0; upos<usize;) |
| 925 | *s++ = ucopy[upos++]; |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 926 | /* We're done with the unicode()/repr() => forget it */ |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 927 | Py_DECREF(*callresult); |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 928 | /* switch to next unicode()/repr() result */ |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 929 | ++callresult; |
| 930 | break; |
| 931 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 932 | case 'p': |
| 933 | sprintf(buffer, "%p", va_arg(vargs, void*)); |
| 934 | /* %p is ill-defined: ensure leading 0x. */ |
| 935 | if (buffer[1] == 'X') |
| 936 | buffer[1] = 'x'; |
| 937 | else if (buffer[1] != 'x') { |
| 938 | memmove(buffer+2, buffer, strlen(buffer)+1); |
| 939 | buffer[0] = '0'; |
| 940 | buffer[1] = 'x'; |
| 941 | } |
| 942 | appendstring(buffer); |
| 943 | break; |
| 944 | case '%': |
| 945 | *s++ = '%'; |
| 946 | break; |
| 947 | default: |
| 948 | appendstring(p); |
| 949 | goto end; |
| 950 | } |
| 951 | } else |
| 952 | *s++ = *f; |
| 953 | } |
| 954 | |
| 955 | end: |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 956 | if (callresults) |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 957 | PyObject_Free(callresults); |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 958 | if (abuffer) |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 959 | PyObject_Free(abuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 960 | _PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string)); |
| 961 | return string; |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 962 | fail: |
| 963 | if (callresults) { |
| 964 | PyObject **callresult2 = callresults; |
Guido van Rossum | 307fa8c | 2007-07-16 20:46:27 +0000 | [diff] [blame] | 965 | while (callresult2 < callresult) { |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 966 | Py_DECREF(*callresult2); |
| 967 | ++callresult2; |
| 968 | } |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 969 | PyObject_Free(callresults); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 970 | } |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 971 | if (abuffer) |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 972 | PyObject_Free(abuffer); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 973 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | #undef appendstring |
| 977 | |
| 978 | PyObject * |
| 979 | PyUnicode_FromFormat(const char *format, ...) |
| 980 | { |
| 981 | PyObject* ret; |
| 982 | va_list vargs; |
| 983 | |
| 984 | #ifdef HAVE_STDARG_PROTOTYPES |
| 985 | va_start(vargs, format); |
| 986 | #else |
| 987 | va_start(vargs); |
| 988 | #endif |
| 989 | ret = PyUnicode_FromFormatV(format, vargs); |
| 990 | va_end(vargs); |
| 991 | return ret; |
| 992 | } |
| 993 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 994 | Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
| 995 | wchar_t *w, |
| 996 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 997 | { |
| 998 | if (unicode == NULL) { |
| 999 | PyErr_BadInternalCall(); |
| 1000 | return -1; |
| 1001 | } |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1002 | |
| 1003 | /* If possible, try to copy the 0-termination as well */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1004 | if (size > PyUnicode_GET_SIZE(unicode)) |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1005 | size = PyUnicode_GET_SIZE(unicode) + 1; |
| 1006 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1007 | #ifdef HAVE_USABLE_WCHAR_T |
| 1008 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 1009 | #else |
| 1010 | { |
| 1011 | register Py_UNICODE *u; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1012 | register Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1013 | u = PyUnicode_AS_UNICODE(unicode); |
Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 1014 | for (i = size; i > 0; i--) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1015 | *w++ = *u++; |
| 1016 | } |
| 1017 | #endif |
| 1018 | |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1019 | if (size > PyUnicode_GET_SIZE(unicode)) |
| 1020 | return PyUnicode_GET_SIZE(unicode); |
| 1021 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1022 | return size; |
| 1023 | } |
| 1024 | |
| 1025 | #endif |
| 1026 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1027 | PyObject *PyUnicode_FromOrdinal(int ordinal) |
| 1028 | { |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1029 | Py_UNICODE s[2]; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1030 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1031 | if (ordinal < 0 || ordinal > 0x10ffff) { |
| 1032 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1033 | "chr() arg not in range(0x110000)"); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1034 | return NULL; |
| 1035 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1036 | |
| 1037 | #ifndef Py_UNICODE_WIDE |
| 1038 | if (ordinal > 0xffff) { |
| 1039 | ordinal -= 0x10000; |
| 1040 | s[0] = 0xD800 | (ordinal >> 10); |
| 1041 | s[1] = 0xDC00 | (ordinal & 0x3FF); |
| 1042 | return PyUnicode_FromUnicode(s, 2); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1043 | } |
| 1044 | #endif |
| 1045 | |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 1046 | s[0] = (Py_UNICODE)ordinal; |
| 1047 | return PyUnicode_FromUnicode(s, 1); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1050 | PyObject *PyUnicode_FromObject(register PyObject *obj) |
| 1051 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1052 | /* XXX Perhaps we should make this API an alias of |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 1053 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1054 | if (PyUnicode_CheckExact(obj)) { |
| 1055 | Py_INCREF(obj); |
| 1056 | return obj; |
| 1057 | } |
| 1058 | if (PyUnicode_Check(obj)) { |
| 1059 | /* For a Unicode subtype that's not a Unicode object, |
| 1060 | return a true Unicode object with the same data. */ |
| 1061 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 1062 | PyUnicode_GET_SIZE(obj)); |
| 1063 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1064 | PyErr_Format(PyExc_TypeError, |
| 1065 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1066 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1067 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
| 1071 | const char *encoding, |
| 1072 | const char *errors) |
| 1073 | { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1074 | const char *s = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1075 | Py_ssize_t len; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1076 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1077 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1078 | if (obj == NULL) { |
| 1079 | PyErr_BadInternalCall(); |
| 1080 | return NULL; |
| 1081 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1082 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1083 | if (PyUnicode_Check(obj)) { |
| 1084 | PyErr_SetString(PyExc_TypeError, |
| 1085 | "decoding Unicode is not supported"); |
| 1086 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1087 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1088 | |
| 1089 | /* Coerce object */ |
| 1090 | if (PyString_Check(obj)) { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1091 | s = PyString_AS_STRING(obj); |
| 1092 | len = PyString_GET_SIZE(obj); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1093 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1094 | else if (PyObject_AsCharBuffer(obj, &s, &len)) { |
| 1095 | /* Overwrite the error message with something more useful in |
| 1096 | case of a TypeError. */ |
| 1097 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1098 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1099 | "coercing to Unicode: need string or buffer, " |
| 1100 | "%.80s found", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1101 | Py_TYPE(obj)->tp_name); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1102 | goto onError; |
| 1103 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1104 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1105 | /* Convert to Unicode */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1106 | if (len == 0) { |
| 1107 | Py_INCREF(unicode_empty); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1108 | v = (PyObject *)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1109 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1110 | else |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1111 | v = PyUnicode_Decode(s, len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 1112 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1113 | return v; |
| 1114 | |
| 1115 | onError: |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1116 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | PyObject *PyUnicode_Decode(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1120 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1121 | const char *encoding, |
| 1122 | const char *errors) |
| 1123 | { |
| 1124 | PyObject *buffer = NULL, *unicode; |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1125 | Py_buffer info; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1126 | char lower[20]; /* Enough for any encoding name we recognize */ |
| 1127 | char *l; |
| 1128 | const char *e; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1129 | |
| 1130 | if (encoding == NULL) |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1131 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1132 | |
| 1133 | /* Convert encoding to lower case and replace '_' with '-' in order to |
| 1134 | catch e.g. UTF_8 */ |
| 1135 | e = encoding; |
| 1136 | l = lower; |
| 1137 | while (*e && l < &lower[(sizeof lower) - 2]) { |
| 1138 | if (ISUPPER(*e)) { |
| 1139 | *l++ = TOLOWER(*e++); |
| 1140 | } |
| 1141 | else if (*e == '_') { |
| 1142 | *l++ = '-'; |
| 1143 | e++; |
| 1144 | } |
| 1145 | else { |
| 1146 | *l++ = *e++; |
| 1147 | } |
| 1148 | } |
| 1149 | *l = '\0'; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1150 | |
| 1151 | /* Shortcuts for common default encodings */ |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1152 | if (strcmp(lower, "utf-8") == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1153 | return PyUnicode_DecodeUTF8(s, size, errors); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1154 | else if ((strcmp(lower, "latin-1") == 0) || |
| 1155 | (strcmp(lower, "iso-8859-1") == 0)) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1156 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1157 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1158 | else if (strcmp(lower, "mbcs") == 0) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1159 | return PyUnicode_DecodeMBCS(s, size, errors); |
| 1160 | #endif |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1161 | else if (strcmp(lower, "ascii") == 0) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1162 | return PyUnicode_DecodeASCII(s, size, errors); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1163 | else if (strcmp(lower, "utf-16") == 0) |
| 1164 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 1165 | else if (strcmp(lower, "utf-32") == 0) |
| 1166 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1167 | |
| 1168 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1169 | buffer = NULL; |
| 1170 | if (PyBuffer_FillInfo(&info, (void *)s, size, 1, PyBUF_SIMPLE) < 0) |
| 1171 | goto onError; |
| 1172 | buffer = PyMemoryView_FromMemory(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1173 | if (buffer == NULL) |
| 1174 | goto onError; |
| 1175 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 1176 | if (unicode == NULL) |
| 1177 | goto onError; |
| 1178 | if (!PyUnicode_Check(unicode)) { |
| 1179 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 1180 | "decoder did not return an unicode object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1181 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1182 | Py_DECREF(unicode); |
| 1183 | goto onError; |
| 1184 | } |
| 1185 | Py_DECREF(buffer); |
| 1186 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1187 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1188 | onError: |
| 1189 | Py_XDECREF(buffer); |
| 1190 | return NULL; |
| 1191 | } |
| 1192 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1193 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 1194 | const char *encoding, |
| 1195 | const char *errors) |
| 1196 | { |
| 1197 | PyObject *v; |
| 1198 | |
| 1199 | if (!PyUnicode_Check(unicode)) { |
| 1200 | PyErr_BadArgument(); |
| 1201 | goto onError; |
| 1202 | } |
| 1203 | |
| 1204 | if (encoding == NULL) |
| 1205 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1206 | |
| 1207 | /* Decode via the codec registry */ |
| 1208 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1209 | if (v == NULL) |
| 1210 | goto onError; |
| 1211 | return v; |
| 1212 | |
| 1213 | onError: |
| 1214 | return NULL; |
| 1215 | } |
| 1216 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1217 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1218 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1219 | const char *encoding, |
| 1220 | const char *errors) |
| 1221 | { |
| 1222 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1223 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1224 | unicode = PyUnicode_FromUnicode(s, size); |
| 1225 | if (unicode == NULL) |
| 1226 | return NULL; |
| 1227 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 1228 | Py_DECREF(unicode); |
| 1229 | return v; |
| 1230 | } |
| 1231 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1232 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 1233 | const char *encoding, |
| 1234 | const char *errors) |
| 1235 | { |
| 1236 | PyObject *v; |
| 1237 | |
| 1238 | if (!PyUnicode_Check(unicode)) { |
| 1239 | PyErr_BadArgument(); |
| 1240 | goto onError; |
| 1241 | } |
| 1242 | |
| 1243 | if (encoding == NULL) |
| 1244 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1245 | |
| 1246 | /* Encode via the codec registry */ |
| 1247 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1248 | if (v == NULL) |
| 1249 | goto onError; |
| 1250 | return v; |
| 1251 | |
| 1252 | onError: |
| 1253 | return NULL; |
| 1254 | } |
| 1255 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1256 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 1257 | const char *encoding, |
| 1258 | const char *errors) |
| 1259 | { |
| 1260 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1261 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1262 | if (!PyUnicode_Check(unicode)) { |
| 1263 | PyErr_BadArgument(); |
| 1264 | goto onError; |
| 1265 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1266 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1267 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1268 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1269 | |
| 1270 | /* Shortcuts for common default encodings */ |
| 1271 | if (errors == NULL) { |
| 1272 | if (strcmp(encoding, "utf-8") == 0) |
Jeremy Hylton | 9cea41c | 2001-05-29 17:13:15 +0000 | [diff] [blame] | 1273 | return PyUnicode_AsUTF8String(unicode); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1274 | else if (strcmp(encoding, "latin-1") == 0) |
| 1275 | return PyUnicode_AsLatin1String(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1276 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 1277 | else if (strcmp(encoding, "mbcs") == 0) |
| 1278 | return PyUnicode_AsMBCSString(unicode); |
| 1279 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1280 | else if (strcmp(encoding, "ascii") == 0) |
| 1281 | return PyUnicode_AsASCIIString(unicode); |
| 1282 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1283 | |
| 1284 | /* Encode via the codec registry */ |
| 1285 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1286 | if (v == NULL) |
| 1287 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1288 | assert(PyString_Check(v)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1289 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1290 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1291 | onError: |
| 1292 | return NULL; |
| 1293 | } |
| 1294 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1295 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
| 1296 | const char *errors) |
| 1297 | { |
| 1298 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1299 | if (v) |
| 1300 | return v; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1301 | if (errors != NULL) |
| 1302 | Py_FatalError("non-NULL encoding in _PyUnicode_AsDefaultEncodedString"); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1303 | v = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Guido van Rossum | 0661009 | 2007-08-16 21:02:22 +0000 | [diff] [blame] | 1304 | PyUnicode_GET_SIZE(unicode), |
| 1305 | NULL); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1306 | if (!v) |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1307 | return NULL; |
Guido van Rossum | e7a0d39 | 2007-07-12 07:53:00 +0000 | [diff] [blame] | 1308 | ((PyUnicodeObject *)unicode)->defenc = v; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1309 | return v; |
| 1310 | } |
| 1311 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1312 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1313 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1314 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1315 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 1316 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1317 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1318 | PyObject* |
| 1319 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 1320 | { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1321 | /* During the early bootstrapping process, Py_FileSystemDefaultEncoding |
| 1322 | can be undefined. If it is case, decode using UTF-8. The following assumes |
| 1323 | that Py_FileSystemDefaultEncoding is set to a built-in encoding during the |
| 1324 | bootstrapping process where the codecs aren't ready yet. |
| 1325 | */ |
| 1326 | if (Py_FileSystemDefaultEncoding) { |
| 1327 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1328 | if (strcmp(Py_FileSystemDefaultEncoding, "mbcs") == 0) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1329 | return PyUnicode_DecodeMBCS(s, size, "replace"); |
| 1330 | } |
| 1331 | #elif defined(__APPLE__) |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1332 | if (strcmp(Py_FileSystemDefaultEncoding, "utf-8") == 0) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1333 | return PyUnicode_DecodeUTF8(s, size, "replace"); |
| 1334 | } |
| 1335 | #endif |
| 1336 | return PyUnicode_Decode(s, size, |
| 1337 | Py_FileSystemDefaultEncoding, |
| 1338 | "replace"); |
| 1339 | } |
| 1340 | else { |
| 1341 | return PyUnicode_DecodeUTF8(s, size, "replace"); |
| 1342 | } |
| 1343 | } |
| 1344 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1345 | char* |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1346 | PyUnicode_AsStringAndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1347 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1348 | PyObject *bytes; |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 1349 | if (!PyUnicode_Check(unicode)) { |
| 1350 | PyErr_BadArgument(); |
| 1351 | return NULL; |
| 1352 | } |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1353 | bytes = _PyUnicode_AsDefaultEncodedString(unicode, NULL); |
| 1354 | if (bytes == NULL) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1355 | return NULL; |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1356 | if (psize != NULL) |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1357 | *psize = PyString_GET_SIZE(bytes); |
| 1358 | return PyString_AS_STRING(bytes); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
| 1361 | char* |
| 1362 | PyUnicode_AsString(PyObject *unicode) |
| 1363 | { |
| 1364 | return PyUnicode_AsStringAndSize(unicode, NULL); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1367 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 1368 | { |
| 1369 | if (!PyUnicode_Check(unicode)) { |
| 1370 | PyErr_BadArgument(); |
| 1371 | goto onError; |
| 1372 | } |
| 1373 | return PyUnicode_AS_UNICODE(unicode); |
| 1374 | |
| 1375 | onError: |
| 1376 | return NULL; |
| 1377 | } |
| 1378 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1379 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1380 | { |
| 1381 | if (!PyUnicode_Check(unicode)) { |
| 1382 | PyErr_BadArgument(); |
| 1383 | goto onError; |
| 1384 | } |
| 1385 | return PyUnicode_GET_SIZE(unicode); |
| 1386 | |
| 1387 | onError: |
| 1388 | return -1; |
| 1389 | } |
| 1390 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 1391 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1392 | { |
| 1393 | return unicode_default_encoding; |
| 1394 | } |
| 1395 | |
| 1396 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 1397 | { |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1398 | if (strcmp(encoding, unicode_default_encoding) != 0) { |
| 1399 | PyErr_Format(PyExc_ValueError, |
| 1400 | "Can only set default encoding to %s", |
| 1401 | unicode_default_encoding); |
| 1402 | return -1; |
| 1403 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1404 | return 0; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1407 | /* error handling callback helper: |
| 1408 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 1409 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1410 | and adjust various state variables. |
| 1411 | return 0 on success, -1 on error |
| 1412 | */ |
| 1413 | |
| 1414 | static |
| 1415 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
| 1416 | const char *encoding, const char *reason, |
Walter Dörwald | a651d3d | 2007-08-30 15:29:21 +0000 | [diff] [blame] | 1417 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 1418 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1419 | PyObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1420 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1421 | 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] | 1422 | |
| 1423 | PyObject *restuple = NULL; |
| 1424 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1425 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1426 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1427 | Py_ssize_t requiredsize; |
| 1428 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1429 | Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1430 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1431 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1432 | int res = -1; |
| 1433 | |
| 1434 | if (*errorHandler == NULL) { |
| 1435 | *errorHandler = PyCodec_LookupError(errors); |
| 1436 | if (*errorHandler == NULL) |
| 1437 | goto onError; |
| 1438 | } |
| 1439 | |
| 1440 | if (*exceptionObject == NULL) { |
| 1441 | *exceptionObject = PyUnicodeDecodeError_Create( |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1442 | encoding, *input, *inend-*input, *startinpos, *endinpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1443 | if (*exceptionObject == NULL) |
| 1444 | goto onError; |
| 1445 | } |
| 1446 | else { |
| 1447 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 1448 | goto onError; |
| 1449 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
| 1450 | goto onError; |
| 1451 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 1452 | goto onError; |
| 1453 | } |
| 1454 | |
| 1455 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 1456 | if (restuple == NULL) |
| 1457 | goto onError; |
| 1458 | if (!PyTuple_Check(restuple)) { |
| 1459 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 1460 | goto onError; |
| 1461 | } |
| 1462 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
| 1463 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1464 | |
| 1465 | /* Copy back the bytes variables, which might have been modified by the |
| 1466 | callback */ |
| 1467 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 1468 | if (!inputobj) |
| 1469 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1470 | if (!PyString_Check(inputobj)) { |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1471 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
| 1472 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1473 | *input = PyString_AS_STRING(inputobj); |
| 1474 | insize = PyString_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1475 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 1476 | /* we can DECREF safely, as the exception has another reference, |
| 1477 | so the object won't go away. */ |
| 1478 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1479 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1480 | if (newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1481 | newpos = insize+newpos; |
| 1482 | if (newpos<0 || newpos>insize) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 1483 | 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] | 1484 | goto onError; |
| 1485 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1486 | |
| 1487 | /* need more space? (at least enough for what we |
| 1488 | have+the replacement+the rest of the string (starting |
| 1489 | at the new input position), so we won't have to check space |
| 1490 | when there are no errors in the rest of the string) */ |
| 1491 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 1492 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 1493 | requiredsize = *outpos + repsize + insize-newpos; |
| 1494 | if (requiredsize > outsize) { |
| 1495 | if (requiredsize<2*outsize) |
| 1496 | requiredsize = 2*outsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1497 | if (PyUnicode_Resize(output, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1498 | goto onError; |
| 1499 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
| 1500 | } |
| 1501 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1502 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1503 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 1504 | *outptr += repsize; |
| 1505 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1506 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1507 | /* we made it! */ |
| 1508 | res = 0; |
| 1509 | |
| 1510 | onError: |
| 1511 | Py_XDECREF(restuple); |
| 1512 | return res; |
| 1513 | } |
| 1514 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1515 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 1516 | |
| 1517 | /* see RFC2152 for details */ |
| 1518 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1519 | static |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1520 | char utf7_special[128] = { |
| 1521 | /* indicate whether a UTF-7 character is special i.e. cannot be directly |
| 1522 | encoded: |
| 1523 | 0 - not special |
| 1524 | 1 - special |
| 1525 | 2 - whitespace (optional) |
| 1526 | 3 - RFC2152 Set O (optional) */ |
| 1527 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, |
| 1528 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1529 | 2, 3, 3, 3, 3, 3, 3, 0, 0, 0, 3, 1, 0, 0, 0, 1, |
| 1530 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, |
| 1531 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1532 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, |
| 1533 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1534 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 1, 1, |
| 1535 | |
| 1536 | }; |
| 1537 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1538 | /* Note: The comparison (c) <= 0 is a trick to work-around gcc |
| 1539 | warnings about the comparison always being false; since |
| 1540 | utf7_special[0] is 1, we can safely make that one comparison |
| 1541 | true */ |
| 1542 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1543 | #define SPECIAL(c, encodeO, encodeWS) \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1544 | ((c) > 127 || (c) <= 0 || utf7_special[(c)] == 1 || \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1545 | (encodeWS && (utf7_special[(c)] == 2)) || \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1546 | (encodeO && (utf7_special[(c)] == 3))) |
| 1547 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1548 | #define B64(n) \ |
| 1549 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 1550 | #define B64CHAR(c) \ |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1551 | (ISALNUM(c) || (c) == '+' || (c) == '/') |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1552 | #define UB64(c) \ |
| 1553 | ((c) == '+' ? 62 : (c) == '/' ? 63 : (c) >= 'a' ? \ |
| 1554 | (c) - 71 : (c) >= 'A' ? (c) - 65 : (c) + 4 ) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1555 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1556 | #define ENCODE(out, ch, bits) \ |
| 1557 | while (bits >= 6) { \ |
| 1558 | *out++ = B64(ch >> (bits-6)); \ |
| 1559 | bits -= 6; \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1560 | } |
| 1561 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1562 | #define DECODE(out, ch, bits, surrogate) \ |
| 1563 | while (bits >= 16) { \ |
| 1564 | Py_UNICODE outCh = (Py_UNICODE) ((ch >> (bits-16)) & 0xffff); \ |
| 1565 | bits -= 16; \ |
| 1566 | if (surrogate) { \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1567 | /* We have already generated an error for the high surrogate \ |
| 1568 | 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] | 1569 | surrogate = 0; \ |
| 1570 | } else if (0xDC00 <= outCh && outCh <= 0xDFFF) { \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1571 | /* This is a surrogate pair. Unfortunately we can't represent \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1572 | it in a 16-bit character */ \ |
| 1573 | surrogate = 1; \ |
| 1574 | errmsg = "code pairs are not supported"; \ |
| 1575 | goto utf7Error; \ |
| 1576 | } else { \ |
| 1577 | *out++ = outCh; \ |
| 1578 | } \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1579 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1580 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1581 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1582 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1583 | const char *errors) |
| 1584 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1585 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 1586 | } |
| 1587 | |
| 1588 | PyObject *PyUnicode_DecodeUTF7Stateful(const char *s, |
| 1589 | Py_ssize_t size, |
| 1590 | const char *errors, |
| 1591 | Py_ssize_t *consumed) |
| 1592 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1593 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1594 | Py_ssize_t startinpos; |
| 1595 | Py_ssize_t endinpos; |
| 1596 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1597 | const char *e; |
| 1598 | PyUnicodeObject *unicode; |
| 1599 | Py_UNICODE *p; |
| 1600 | const char *errmsg = ""; |
| 1601 | int inShift = 0; |
| 1602 | unsigned int bitsleft = 0; |
| 1603 | unsigned long charsleft = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1604 | int surrogate = 0; |
| 1605 | PyObject *errorHandler = NULL; |
| 1606 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1607 | |
| 1608 | unicode = _PyUnicode_New(size); |
| 1609 | if (!unicode) |
| 1610 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1611 | if (size == 0) { |
| 1612 | if (consumed) |
| 1613 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1614 | return (PyObject *)unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1615 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1616 | |
| 1617 | p = unicode->str; |
| 1618 | e = s + size; |
| 1619 | |
| 1620 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1621 | Py_UNICODE ch; |
| 1622 | restart: |
| 1623 | ch = *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1624 | |
| 1625 | if (inShift) { |
| 1626 | if ((ch == '-') || !B64CHAR(ch)) { |
| 1627 | inShift = 0; |
| 1628 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1629 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1630 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1631 | if (bitsleft >= 6) { |
| 1632 | /* The shift sequence has a partial character in it. If |
| 1633 | bitsleft < 6 then we could just classify it as padding |
| 1634 | but that is not the case here */ |
| 1635 | |
| 1636 | errmsg = "partial character in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1637 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1638 | } |
| 1639 | /* According to RFC2152 the remaining bits should be zero. We |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1640 | choose to signal an error/insert a replacement character |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1641 | here so indicate the potential of a misencoded character. */ |
| 1642 | |
| 1643 | /* On x86, a << b == a << (b%32) so make sure that bitsleft != 0 */ |
| 1644 | if (bitsleft && charsleft << (sizeof(charsleft) * 8 - bitsleft)) { |
| 1645 | errmsg = "non-zero padding bits in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1646 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | if (ch == '-') { |
| 1650 | if ((s < e) && (*(s) == '-')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1651 | *p++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1652 | inShift = 1; |
| 1653 | } |
| 1654 | } else if (SPECIAL(ch,0,0)) { |
| 1655 | errmsg = "unexpected special character"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1656 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1657 | } else { |
| 1658 | *p++ = ch; |
| 1659 | } |
| 1660 | } else { |
| 1661 | charsleft = (charsleft << 6) | UB64(ch); |
| 1662 | bitsleft += 6; |
| 1663 | s++; |
| 1664 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1665 | } |
| 1666 | } |
| 1667 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1668 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1669 | s++; |
| 1670 | if (s < e && *s == '-') { |
| 1671 | s++; |
| 1672 | *p++ = '+'; |
| 1673 | } else |
| 1674 | { |
| 1675 | inShift = 1; |
| 1676 | bitsleft = 0; |
| 1677 | } |
| 1678 | } |
| 1679 | else if (SPECIAL(ch,0,0)) { |
Walter Dörwald | 2b65c75 | 2007-08-30 15:35:26 +0000 | [diff] [blame] | 1680 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1681 | errmsg = "unexpected special character"; |
| 1682 | s++; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1683 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1684 | } |
| 1685 | else { |
| 1686 | *p++ = ch; |
| 1687 | s++; |
| 1688 | } |
| 1689 | continue; |
| 1690 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1691 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1692 | endinpos = s-starts; |
| 1693 | if (unicode_decode_call_errorhandler( |
| 1694 | errors, &errorHandler, |
| 1695 | "utf7", errmsg, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1696 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1697 | (PyObject **)&unicode, &outpos, &p)) |
| 1698 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1699 | } |
| 1700 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1701 | if (inShift && !consumed) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1702 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1703 | endinpos = size; |
| 1704 | if (unicode_decode_call_errorhandler( |
| 1705 | errors, &errorHandler, |
| 1706 | "utf7", "unterminated shift sequence", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1707 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1708 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1709 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1710 | if (s < e) |
| 1711 | goto restart; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1712 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1713 | if (consumed) { |
| 1714 | if(inShift) |
| 1715 | *consumed = startinpos; |
| 1716 | else |
| 1717 | *consumed = s-starts; |
| 1718 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1719 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1720 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1721 | goto onError; |
| 1722 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1723 | Py_XDECREF(errorHandler); |
| 1724 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1725 | return (PyObject *)unicode; |
| 1726 | |
| 1727 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1728 | Py_XDECREF(errorHandler); |
| 1729 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1730 | Py_DECREF(unicode); |
| 1731 | return NULL; |
| 1732 | } |
| 1733 | |
| 1734 | |
| 1735 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1736 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1737 | int encodeSetO, |
| 1738 | int encodeWhiteSpace, |
| 1739 | const char *errors) |
| 1740 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1741 | PyObject *v, *result; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1742 | /* It might be possible to tighten this worst case */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1743 | Py_ssize_t cbAllocated = 5 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1744 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1745 | Py_ssize_t i = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1746 | unsigned int bitsleft = 0; |
| 1747 | unsigned long charsleft = 0; |
| 1748 | char * out; |
| 1749 | char * start; |
| 1750 | |
| 1751 | if (size == 0) |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1752 | return PyString_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1753 | |
Walter Dörwald | 51ab414 | 2007-05-05 14:43:36 +0000 | [diff] [blame] | 1754 | v = PyBytes_FromStringAndSize(NULL, cbAllocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1755 | if (v == NULL) |
| 1756 | return NULL; |
| 1757 | |
Walter Dörwald | 51ab414 | 2007-05-05 14:43:36 +0000 | [diff] [blame] | 1758 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1759 | for (;i < size; ++i) { |
| 1760 | Py_UNICODE ch = s[i]; |
| 1761 | |
| 1762 | if (!inShift) { |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1763 | if (ch == '+') { |
| 1764 | *out++ = '+'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1765 | *out++ = '-'; |
| 1766 | } else if (SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1767 | charsleft = ch; |
| 1768 | bitsleft = 16; |
| 1769 | *out++ = '+'; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1770 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1771 | inShift = bitsleft > 0; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1772 | } else { |
| 1773 | *out++ = (char) ch; |
| 1774 | } |
| 1775 | } else { |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1776 | if (!SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1777 | *out++ = B64(charsleft << (6-bitsleft)); |
| 1778 | charsleft = 0; |
| 1779 | bitsleft = 0; |
| 1780 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 1781 | so no '-' is required, except if the character is itself a '-' */ |
| 1782 | if (B64CHAR(ch) || ch == '-') { |
| 1783 | *out++ = '-'; |
| 1784 | } |
| 1785 | inShift = 0; |
| 1786 | *out++ = (char) ch; |
| 1787 | } else { |
| 1788 | bitsleft += 16; |
| 1789 | charsleft = (charsleft << 16) | ch; |
| 1790 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
| 1791 | |
| 1792 | /* If the next character is special then we dont' need to terminate |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1793 | 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] | 1794 | or '-' then the shift sequence will be terminated implicitly and we |
| 1795 | don't have to insert a '-'. */ |
| 1796 | |
| 1797 | if (bitsleft == 0) { |
| 1798 | if (i + 1 < size) { |
| 1799 | Py_UNICODE ch2 = s[i+1]; |
| 1800 | |
| 1801 | if (SPECIAL(ch2, encodeSetO, encodeWhiteSpace)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1802 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1803 | } else if (B64CHAR(ch2) || ch2 == '-') { |
| 1804 | *out++ = '-'; |
| 1805 | inShift = 0; |
| 1806 | } else { |
| 1807 | inShift = 0; |
| 1808 | } |
| 1809 | |
| 1810 | } |
| 1811 | else { |
| 1812 | *out++ = '-'; |
| 1813 | inShift = 0; |
| 1814 | } |
| 1815 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1816 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1817 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1818 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1819 | if (bitsleft) { |
| 1820 | *out++= B64(charsleft << (6-bitsleft) ); |
| 1821 | *out++ = '-'; |
| 1822 | } |
| 1823 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1824 | result = PyString_FromStringAndSize(PyBytes_AS_STRING(v), out - start); |
| 1825 | Py_DECREF(v); |
| 1826 | return result; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1827 | } |
| 1828 | |
| 1829 | #undef SPECIAL |
| 1830 | #undef B64 |
| 1831 | #undef B64CHAR |
| 1832 | #undef UB64 |
| 1833 | #undef ENCODE |
| 1834 | #undef DECODE |
| 1835 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1836 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 1837 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1838 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1839 | char utf8_code_length[256] = { |
| 1840 | /* Map UTF-8 encoded prefix byte to sequence length. zero means |
| 1841 | illegal prefix. see RFC 2279 for details */ |
| 1842 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1843 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1844 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1845 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1846 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1847 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1848 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1849 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1850 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1851 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1852 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1853 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1854 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1855 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1856 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 1857 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 |
| 1858 | }; |
| 1859 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1860 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1861 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1862 | const char *errors) |
| 1863 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1864 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 1865 | } |
| 1866 | |
| 1867 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1868 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1869 | const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1870 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1871 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1872 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1873 | int n; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1874 | Py_ssize_t startinpos; |
| 1875 | Py_ssize_t endinpos; |
| 1876 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1877 | const char *e; |
| 1878 | PyUnicodeObject *unicode; |
| 1879 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1880 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1881 | PyObject *errorHandler = NULL; |
| 1882 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1883 | |
| 1884 | /* Note: size will always be longer than the resulting Unicode |
| 1885 | character count */ |
| 1886 | unicode = _PyUnicode_New(size); |
| 1887 | if (!unicode) |
| 1888 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1889 | if (size == 0) { |
| 1890 | if (consumed) |
| 1891 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1892 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1893 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1894 | |
| 1895 | /* Unpack UTF-8 encoded data */ |
| 1896 | p = unicode->str; |
| 1897 | e = s + size; |
| 1898 | |
| 1899 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1900 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1901 | |
| 1902 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1903 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1904 | s++; |
| 1905 | continue; |
| 1906 | } |
| 1907 | |
| 1908 | n = utf8_code_length[ch]; |
| 1909 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1910 | if (s + n > e) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1911 | if (consumed) |
| 1912 | break; |
| 1913 | else { |
| 1914 | errmsg = "unexpected end of data"; |
| 1915 | startinpos = s-starts; |
| 1916 | endinpos = size; |
| 1917 | goto utf8Error; |
| 1918 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1919 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1920 | |
| 1921 | switch (n) { |
| 1922 | |
| 1923 | case 0: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1924 | errmsg = "unexpected code byte"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1925 | startinpos = s-starts; |
| 1926 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1927 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1928 | |
| 1929 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1930 | errmsg = "internal error"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1931 | startinpos = s-starts; |
| 1932 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1933 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1934 | |
| 1935 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1936 | if ((s[1] & 0xc0) != 0x80) { |
| 1937 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1938 | startinpos = s-starts; |
| 1939 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1940 | goto utf8Error; |
| 1941 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1942 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1943 | if (ch < 0x80) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1944 | startinpos = s-starts; |
| 1945 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1946 | errmsg = "illegal encoding"; |
| 1947 | goto utf8Error; |
| 1948 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1949 | else |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1950 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1951 | break; |
| 1952 | |
| 1953 | case 3: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1954 | if ((s[1] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1955 | (s[2] & 0xc0) != 0x80) { |
| 1956 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1957 | startinpos = s-starts; |
| 1958 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1959 | goto utf8Error; |
| 1960 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1961 | 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] | 1962 | if (ch < 0x0800) { |
| 1963 | /* Note: UTF-8 encodings of surrogates are considered |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1964 | legal UTF-8 sequences; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1965 | |
| 1966 | XXX For wide builds (UCS-4) we should probably try |
| 1967 | to recombine the surrogates into a single code |
| 1968 | unit. |
| 1969 | */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1970 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1971 | startinpos = s-starts; |
| 1972 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1973 | goto utf8Error; |
| 1974 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1975 | else |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1976 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1977 | break; |
| 1978 | |
| 1979 | case 4: |
| 1980 | if ((s[1] & 0xc0) != 0x80 || |
| 1981 | (s[2] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1982 | (s[3] & 0xc0) != 0x80) { |
| 1983 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1984 | startinpos = s-starts; |
| 1985 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1986 | goto utf8Error; |
| 1987 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1988 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 1989 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 1990 | /* validate and convert to UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1991 | if ((ch < 0x10000) /* minimum value allowed for 4 |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1992 | byte encoding */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1993 | || (ch > 0x10ffff)) /* maximum value allowed for |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1994 | UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1995 | { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1996 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1997 | startinpos = s-starts; |
| 1998 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1999 | goto utf8Error; |
| 2000 | } |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2001 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2002 | *p++ = (Py_UNICODE)ch; |
| 2003 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2004 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2005 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2006 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 2007 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2008 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2009 | /* high surrogate = top 10 bits added to D800 */ |
| 2010 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2011 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2012 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2013 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2014 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2015 | break; |
| 2016 | |
| 2017 | default: |
| 2018 | /* Other sizes are only needed for UCS-4 */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2019 | errmsg = "unsupported Unicode code range"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2020 | startinpos = s-starts; |
| 2021 | endinpos = startinpos+n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2022 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2023 | } |
| 2024 | s += n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2025 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2026 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2027 | utf8Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2028 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2029 | if (unicode_decode_call_errorhandler( |
| 2030 | errors, &errorHandler, |
| 2031 | "utf8", errmsg, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2032 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2033 | (PyObject **)&unicode, &outpos, &p)) |
| 2034 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2035 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2036 | if (consumed) |
| 2037 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2038 | |
| 2039 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2040 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2041 | goto onError; |
| 2042 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2043 | Py_XDECREF(errorHandler); |
| 2044 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2045 | return (PyObject *)unicode; |
| 2046 | |
| 2047 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2048 | Py_XDECREF(errorHandler); |
| 2049 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2050 | Py_DECREF(unicode); |
| 2051 | return NULL; |
| 2052 | } |
| 2053 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2054 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 2055 | and allocate exactly as much space needed at the end. Else allocate the |
| 2056 | maximum possible needed (4 result bytes per Unicode character), and return |
| 2057 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2058 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 2059 | PyObject * |
| 2060 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2061 | Py_ssize_t size, |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 2062 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2063 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2064 | #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] | 2065 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2066 | Py_ssize_t i; /* index into s of next input byte */ |
| 2067 | PyObject *result; /* result string object */ |
| 2068 | char *p; /* next free byte in output buffer */ |
| 2069 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 2070 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2071 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2072 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2073 | assert(s != NULL); |
| 2074 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2075 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2076 | if (size <= MAX_SHORT_UNICHARS) { |
| 2077 | /* Write into the stack buffer; nallocated can't overflow. |
| 2078 | * At the end, we'll allocate exactly as much heap space as it |
| 2079 | * turns out we need. |
| 2080 | */ |
| 2081 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2082 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2083 | p = stackbuf; |
| 2084 | } |
| 2085 | else { |
| 2086 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 2087 | nallocated = size * 4; |
| 2088 | if (nallocated / 4 != size) /* overflow! */ |
| 2089 | return PyErr_NoMemory(); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2090 | result = PyString_FromStringAndSize(NULL, nallocated); |
| 2091 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2092 | return NULL; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2093 | p = PyString_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2094 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2095 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2096 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2097 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2098 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2099 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2100 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2101 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2102 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2103 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2104 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 2105 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 2106 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2107 | } |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2108 | else { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2109 | /* Encode UCS2 Unicode ordinals */ |
| 2110 | if (ch < 0x10000) { |
| 2111 | /* Special case: check for high surrogate */ |
| 2112 | if (0xD800 <= ch && ch <= 0xDBFF && i != size) { |
| 2113 | Py_UCS4 ch2 = s[i]; |
| 2114 | /* Check for low surrogate and combine the two to |
| 2115 | form a UCS4 value */ |
| 2116 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2117 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2118 | i++; |
| 2119 | goto encodeUCS4; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2120 | } |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2121 | /* Fall through: handles isolated high surrogates */ |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2122 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2123 | *p++ = (char)(0xe0 | (ch >> 12)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2124 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2125 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2126 | continue; |
| 2127 | } |
| 2128 | encodeUCS4: |
| 2129 | /* Encode UCS4 Unicode ordinals */ |
| 2130 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2131 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 2132 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2133 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2134 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2135 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 2136 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2137 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2138 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2139 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2140 | assert(nneeded <= nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2141 | result = PyString_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2142 | } |
| 2143 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 2144 | /* Cut back to size actually needed. */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2145 | nneeded = p - PyString_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2146 | assert(nneeded <= nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2147 | _PyString_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2148 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2149 | return result; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2150 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2151 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2152 | } |
| 2153 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2154 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 2155 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2156 | if (!PyUnicode_Check(unicode)) { |
| 2157 | PyErr_BadArgument(); |
| 2158 | return NULL; |
| 2159 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 2160 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 2161 | PyUnicode_GET_SIZE(unicode), |
| 2162 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2163 | } |
| 2164 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2165 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 2166 | |
| 2167 | PyObject * |
| 2168 | PyUnicode_DecodeUTF32(const char *s, |
| 2169 | Py_ssize_t size, |
| 2170 | const char *errors, |
| 2171 | int *byteorder) |
| 2172 | { |
| 2173 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 2174 | } |
| 2175 | |
| 2176 | PyObject * |
| 2177 | PyUnicode_DecodeUTF32Stateful(const char *s, |
| 2178 | Py_ssize_t size, |
| 2179 | const char *errors, |
| 2180 | int *byteorder, |
| 2181 | Py_ssize_t *consumed) |
| 2182 | { |
| 2183 | const char *starts = s; |
| 2184 | Py_ssize_t startinpos; |
| 2185 | Py_ssize_t endinpos; |
| 2186 | Py_ssize_t outpos; |
| 2187 | PyUnicodeObject *unicode; |
| 2188 | Py_UNICODE *p; |
| 2189 | #ifndef Py_UNICODE_WIDE |
| 2190 | int i, pairs; |
| 2191 | #else |
| 2192 | const int pairs = 0; |
| 2193 | #endif |
| 2194 | const unsigned char *q, *e; |
| 2195 | int bo = 0; /* assume native ordering by default */ |
| 2196 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2197 | /* Offsets from q for retrieving bytes in the right order. */ |
| 2198 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2199 | int iorder[] = {0, 1, 2, 3}; |
| 2200 | #else |
| 2201 | int iorder[] = {3, 2, 1, 0}; |
| 2202 | #endif |
| 2203 | PyObject *errorHandler = NULL; |
| 2204 | PyObject *exc = NULL; |
Guido van Rossum | 8d991ed | 2007-08-17 15:41:00 +0000 | [diff] [blame] | 2205 | /* On narrow builds we split characters outside the BMP into two |
| 2206 | codepoints => count how much extra space we need. */ |
| 2207 | #ifndef Py_UNICODE_WIDE |
| 2208 | for (i = pairs = 0; i < size/4; i++) |
| 2209 | if (((Py_UCS4 *)s)[i] >= 0x10000) |
| 2210 | pairs++; |
| 2211 | #endif |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2212 | |
| 2213 | /* This might be one to much, because of a BOM */ |
| 2214 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 2215 | if (!unicode) |
| 2216 | return NULL; |
| 2217 | if (size == 0) |
| 2218 | return (PyObject *)unicode; |
| 2219 | |
| 2220 | /* Unpack UTF-32 encoded data */ |
| 2221 | p = unicode->str; |
| 2222 | q = (unsigned char *)s; |
| 2223 | e = q + size; |
| 2224 | |
| 2225 | if (byteorder) |
| 2226 | bo = *byteorder; |
| 2227 | |
| 2228 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2229 | byte order setting accordingly. In native mode, the leading BOM |
| 2230 | mark is skipped, in all other modes, it is copied to the output |
| 2231 | stream as-is (giving a ZWNBSP character). */ |
| 2232 | if (bo == 0) { |
| 2233 | if (size >= 4) { |
| 2234 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2235 | (q[iorder[1]] << 8) | q[iorder[0]]; |
| 2236 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2237 | if (bom == 0x0000FEFF) { |
| 2238 | q += 4; |
| 2239 | bo = -1; |
| 2240 | } |
| 2241 | else if (bom == 0xFFFE0000) { |
| 2242 | q += 4; |
| 2243 | bo = 1; |
| 2244 | } |
| 2245 | #else |
| 2246 | if (bom == 0x0000FEFF) { |
| 2247 | q += 4; |
| 2248 | bo = 1; |
| 2249 | } |
| 2250 | else if (bom == 0xFFFE0000) { |
| 2251 | q += 4; |
| 2252 | bo = -1; |
| 2253 | } |
| 2254 | #endif |
| 2255 | } |
| 2256 | } |
| 2257 | |
| 2258 | if (bo == -1) { |
| 2259 | /* force LE */ |
| 2260 | iorder[0] = 0; |
| 2261 | iorder[1] = 1; |
| 2262 | iorder[2] = 2; |
| 2263 | iorder[3] = 3; |
| 2264 | } |
| 2265 | else if (bo == 1) { |
| 2266 | /* force BE */ |
| 2267 | iorder[0] = 3; |
| 2268 | iorder[1] = 2; |
| 2269 | iorder[2] = 1; |
| 2270 | iorder[3] = 0; |
| 2271 | } |
| 2272 | |
| 2273 | while (q < e) { |
| 2274 | Py_UCS4 ch; |
| 2275 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 2276 | if (e-q<4) { |
| 2277 | if (consumed) |
| 2278 | break; |
| 2279 | errmsg = "truncated data"; |
| 2280 | startinpos = ((const char *)q)-starts; |
| 2281 | endinpos = ((const char *)e)-starts; |
| 2282 | goto utf32Error; |
| 2283 | /* The remaining input chars are ignored if the callback |
| 2284 | chooses to skip the input */ |
| 2285 | } |
| 2286 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2287 | (q[iorder[1]] << 8) | q[iorder[0]]; |
| 2288 | |
| 2289 | if (ch >= 0x110000) |
| 2290 | { |
| 2291 | errmsg = "codepoint not in range(0x110000)"; |
| 2292 | startinpos = ((const char *)q)-starts; |
| 2293 | endinpos = startinpos+4; |
| 2294 | goto utf32Error; |
| 2295 | } |
| 2296 | #ifndef Py_UNICODE_WIDE |
| 2297 | if (ch >= 0x10000) |
| 2298 | { |
| 2299 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 2300 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2301 | } |
| 2302 | else |
| 2303 | #endif |
| 2304 | *p++ = ch; |
| 2305 | q += 4; |
| 2306 | continue; |
| 2307 | utf32Error: |
| 2308 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2309 | if (unicode_decode_call_errorhandler( |
| 2310 | errors, &errorHandler, |
| 2311 | "utf32", errmsg, |
| 2312 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 2313 | (PyObject **)&unicode, &outpos, &p)) |
| 2314 | goto onError; |
| 2315 | } |
| 2316 | |
| 2317 | if (byteorder) |
| 2318 | *byteorder = bo; |
| 2319 | |
| 2320 | if (consumed) |
| 2321 | *consumed = (const char *)q-starts; |
| 2322 | |
| 2323 | /* Adjust length */ |
| 2324 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
| 2325 | goto onError; |
| 2326 | |
| 2327 | Py_XDECREF(errorHandler); |
| 2328 | Py_XDECREF(exc); |
| 2329 | return (PyObject *)unicode; |
| 2330 | |
| 2331 | onError: |
| 2332 | Py_DECREF(unicode); |
| 2333 | Py_XDECREF(errorHandler); |
| 2334 | Py_XDECREF(exc); |
| 2335 | return NULL; |
| 2336 | } |
| 2337 | |
| 2338 | PyObject * |
| 2339 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 2340 | Py_ssize_t size, |
| 2341 | const char *errors, |
| 2342 | int byteorder) |
| 2343 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2344 | PyObject *v, *result; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2345 | unsigned char *p; |
| 2346 | #ifndef Py_UNICODE_WIDE |
| 2347 | int i, pairs; |
| 2348 | #else |
| 2349 | const int pairs = 0; |
| 2350 | #endif |
| 2351 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2352 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2353 | int iorder[] = {0, 1, 2, 3}; |
| 2354 | #else |
| 2355 | int iorder[] = {3, 2, 1, 0}; |
| 2356 | #endif |
| 2357 | |
| 2358 | #define STORECHAR(CH) \ |
| 2359 | do { \ |
| 2360 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 2361 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 2362 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 2363 | p[iorder[0]] = (CH) & 0xff; \ |
| 2364 | p += 4; \ |
| 2365 | } while(0) |
| 2366 | |
| 2367 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 2368 | so we need less space. */ |
| 2369 | #ifndef Py_UNICODE_WIDE |
| 2370 | for (i = pairs = 0; i < size-1; i++) |
| 2371 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 2372 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 2373 | pairs++; |
| 2374 | #endif |
| 2375 | v = PyBytes_FromStringAndSize(NULL, |
| 2376 | 4 * (size - pairs + (byteorder == 0))); |
| 2377 | if (v == NULL) |
| 2378 | return NULL; |
| 2379 | |
| 2380 | p = (unsigned char *)PyBytes_AS_STRING(v); |
| 2381 | if (byteorder == 0) |
| 2382 | STORECHAR(0xFEFF); |
| 2383 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2384 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2385 | |
| 2386 | if (byteorder == -1) { |
| 2387 | /* force LE */ |
| 2388 | iorder[0] = 0; |
| 2389 | iorder[1] = 1; |
| 2390 | iorder[2] = 2; |
| 2391 | iorder[3] = 3; |
| 2392 | } |
| 2393 | else if (byteorder == 1) { |
| 2394 | /* force BE */ |
| 2395 | iorder[0] = 3; |
| 2396 | iorder[1] = 2; |
| 2397 | iorder[2] = 1; |
| 2398 | iorder[3] = 0; |
| 2399 | } |
| 2400 | |
| 2401 | while (size-- > 0) { |
| 2402 | Py_UCS4 ch = *s++; |
| 2403 | #ifndef Py_UNICODE_WIDE |
| 2404 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 2405 | Py_UCS4 ch2 = *s; |
| 2406 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 2407 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 2408 | s++; |
| 2409 | size--; |
| 2410 | } |
| 2411 | } |
| 2412 | #endif |
| 2413 | STORECHAR(ch); |
| 2414 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2415 | |
| 2416 | done: |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2417 | result = PyString_FromStringAndSize(PyBytes_AS_STRING(v), Py_SIZE(v)); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2418 | Py_DECREF(v); |
| 2419 | return result; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2420 | #undef STORECHAR |
| 2421 | } |
| 2422 | |
| 2423 | PyObject *PyUnicode_AsUTF32String(PyObject *unicode) |
| 2424 | { |
| 2425 | if (!PyUnicode_Check(unicode)) { |
| 2426 | PyErr_BadArgument(); |
| 2427 | return NULL; |
| 2428 | } |
| 2429 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
| 2430 | PyUnicode_GET_SIZE(unicode), |
| 2431 | NULL, |
| 2432 | 0); |
| 2433 | } |
| 2434 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2435 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 2436 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2437 | PyObject * |
| 2438 | PyUnicode_DecodeUTF16(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2439 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2440 | const char *errors, |
| 2441 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2442 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2443 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 2444 | } |
| 2445 | |
| 2446 | PyObject * |
| 2447 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2448 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2449 | const char *errors, |
| 2450 | int *byteorder, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2451 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2452 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2453 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2454 | Py_ssize_t startinpos; |
| 2455 | Py_ssize_t endinpos; |
| 2456 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2457 | PyUnicodeObject *unicode; |
| 2458 | Py_UNICODE *p; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2459 | const unsigned char *q, *e; |
| 2460 | int bo = 0; /* assume native ordering by default */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2461 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2462 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 2463 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2464 | int ihi = 1, ilo = 0; |
| 2465 | #else |
| 2466 | int ihi = 0, ilo = 1; |
| 2467 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2468 | PyObject *errorHandler = NULL; |
| 2469 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2470 | |
| 2471 | /* Note: size will always be longer than the resulting Unicode |
| 2472 | character count */ |
| 2473 | unicode = _PyUnicode_New(size); |
| 2474 | if (!unicode) |
| 2475 | return NULL; |
| 2476 | if (size == 0) |
| 2477 | return (PyObject *)unicode; |
| 2478 | |
| 2479 | /* Unpack UTF-16 encoded data */ |
| 2480 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2481 | q = (unsigned char *)s; |
| 2482 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2483 | |
| 2484 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2485 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2486 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2487 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2488 | byte order setting accordingly. In native mode, the leading BOM |
| 2489 | mark is skipped, in all other modes, it is copied to the output |
| 2490 | stream as-is (giving a ZWNBSP character). */ |
| 2491 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2492 | if (size >= 2) { |
| 2493 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2494 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2495 | if (bom == 0xFEFF) { |
| 2496 | q += 2; |
| 2497 | bo = -1; |
| 2498 | } |
| 2499 | else if (bom == 0xFFFE) { |
| 2500 | q += 2; |
| 2501 | bo = 1; |
| 2502 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2503 | #else |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2504 | if (bom == 0xFEFF) { |
| 2505 | q += 2; |
| 2506 | bo = 1; |
| 2507 | } |
| 2508 | else if (bom == 0xFFFE) { |
| 2509 | q += 2; |
| 2510 | bo = -1; |
| 2511 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2512 | #endif |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2513 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2514 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2515 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2516 | if (bo == -1) { |
| 2517 | /* force LE */ |
| 2518 | ihi = 1; |
| 2519 | ilo = 0; |
| 2520 | } |
| 2521 | else if (bo == 1) { |
| 2522 | /* force BE */ |
| 2523 | ihi = 0; |
| 2524 | ilo = 1; |
| 2525 | } |
| 2526 | |
| 2527 | while (q < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2528 | Py_UNICODE ch; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2529 | /* remaining bytes at the end? (size should be even) */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2530 | if (e-q<2) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2531 | if (consumed) |
| 2532 | break; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2533 | errmsg = "truncated data"; |
| 2534 | startinpos = ((const char *)q)-starts; |
| 2535 | endinpos = ((const char *)e)-starts; |
| 2536 | goto utf16Error; |
| 2537 | /* The remaining input chars are ignored if the callback |
| 2538 | chooses to skip the input */ |
| 2539 | } |
| 2540 | ch = (q[ihi] << 8) | q[ilo]; |
| 2541 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2542 | q += 2; |
| 2543 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2544 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 2545 | *p++ = ch; |
| 2546 | continue; |
| 2547 | } |
| 2548 | |
| 2549 | /* UTF-16 code pair: */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2550 | if (q >= e) { |
| 2551 | errmsg = "unexpected end of data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2552 | startinpos = (((const char *)q)-2)-starts; |
| 2553 | endinpos = ((const char *)e)-starts; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2554 | goto utf16Error; |
| 2555 | } |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 2556 | if (0xD800 <= ch && ch <= 0xDBFF) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2557 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 2558 | q += 2; |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 2559 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2560 | #ifndef Py_UNICODE_WIDE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2561 | *p++ = ch; |
| 2562 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2563 | #else |
| 2564 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2565 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2566 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2567 | } |
| 2568 | else { |
| 2569 | errmsg = "illegal UTF-16 surrogate"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2570 | startinpos = (((const char *)q)-4)-starts; |
| 2571 | endinpos = startinpos+2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2572 | goto utf16Error; |
| 2573 | } |
| 2574 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2575 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2576 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2577 | startinpos = (((const char *)q)-2)-starts; |
| 2578 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2579 | /* Fall through to report the error */ |
| 2580 | |
| 2581 | utf16Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2582 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2583 | if (unicode_decode_call_errorhandler( |
| 2584 | errors, &errorHandler, |
| 2585 | "utf16", errmsg, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2586 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2587 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2588 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2589 | } |
| 2590 | |
| 2591 | if (byteorder) |
| 2592 | *byteorder = bo; |
| 2593 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2594 | if (consumed) |
| 2595 | *consumed = (const char *)q-starts; |
| 2596 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2597 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2598 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2599 | goto onError; |
| 2600 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2601 | Py_XDECREF(errorHandler); |
| 2602 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2603 | return (PyObject *)unicode; |
| 2604 | |
| 2605 | onError: |
| 2606 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2607 | Py_XDECREF(errorHandler); |
| 2608 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2609 | return NULL; |
| 2610 | } |
| 2611 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2612 | PyObject * |
| 2613 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2614 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2615 | const char *errors, |
| 2616 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2617 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2618 | PyObject *v, *result; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2619 | unsigned char *p; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2620 | #ifdef Py_UNICODE_WIDE |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2621 | int i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2622 | #else |
| 2623 | const int pairs = 0; |
| 2624 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2625 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2626 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2627 | int ihi = 1, ilo = 0; |
| 2628 | #else |
| 2629 | int ihi = 0, ilo = 1; |
| 2630 | #endif |
| 2631 | |
| 2632 | #define STORECHAR(CH) \ |
| 2633 | do { \ |
| 2634 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 2635 | p[ilo] = (CH) & 0xff; \ |
| 2636 | p += 2; \ |
| 2637 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2638 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2639 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2640 | for (i = pairs = 0; i < size; i++) |
| 2641 | if (s[i] >= 0x10000) |
| 2642 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2643 | #endif |
Walter Dörwald | 3cc3452 | 2007-05-04 10:48:27 +0000 | [diff] [blame] | 2644 | v = PyBytes_FromStringAndSize(NULL, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2645 | 2 * (size + pairs + (byteorder == 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2646 | if (v == NULL) |
| 2647 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2648 | |
Walter Dörwald | 3cc3452 | 2007-05-04 10:48:27 +0000 | [diff] [blame] | 2649 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2650 | if (byteorder == 0) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2651 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2652 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2653 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2654 | |
| 2655 | if (byteorder == -1) { |
| 2656 | /* force LE */ |
| 2657 | ihi = 1; |
| 2658 | ilo = 0; |
| 2659 | } |
| 2660 | else if (byteorder == 1) { |
| 2661 | /* force BE */ |
| 2662 | ihi = 0; |
| 2663 | ilo = 1; |
| 2664 | } |
| 2665 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2666 | while (size-- > 0) { |
| 2667 | Py_UNICODE ch = *s++; |
| 2668 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2669 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2670 | if (ch >= 0x10000) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2671 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2672 | ch = 0xD800 | ((ch-0x10000) >> 10); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2673 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2674 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2675 | STORECHAR(ch); |
| 2676 | if (ch2) |
| 2677 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2678 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2679 | |
| 2680 | done: |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2681 | result = PyString_FromStringAndSize(PyBytes_AS_STRING(v), Py_SIZE(v)); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2682 | Py_DECREF(v); |
| 2683 | return result; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2684 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2685 | } |
| 2686 | |
| 2687 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 2688 | { |
| 2689 | if (!PyUnicode_Check(unicode)) { |
| 2690 | PyErr_BadArgument(); |
| 2691 | return NULL; |
| 2692 | } |
| 2693 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
| 2694 | PyUnicode_GET_SIZE(unicode), |
| 2695 | NULL, |
| 2696 | 0); |
| 2697 | } |
| 2698 | |
| 2699 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 2700 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2701 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 2702 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2703 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2704 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2705 | const char *errors) |
| 2706 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2707 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2708 | Py_ssize_t startinpos; |
| 2709 | Py_ssize_t endinpos; |
| 2710 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2711 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2712 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2713 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2714 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2715 | char* message; |
| 2716 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2717 | PyObject *errorHandler = NULL; |
| 2718 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2719 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2720 | /* Escaped strings will always be longer than the resulting |
| 2721 | 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] | 2722 | length after conversion to the true value. |
| 2723 | (but if the error callback returns a long replacement string |
| 2724 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2725 | v = _PyUnicode_New(size); |
| 2726 | if (v == NULL) |
| 2727 | goto onError; |
| 2728 | if (size == 0) |
| 2729 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2730 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2731 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2732 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2733 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2734 | while (s < end) { |
| 2735 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2736 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2737 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2738 | |
| 2739 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 2740 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2741 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2742 | continue; |
| 2743 | } |
| 2744 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2745 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2746 | /* \ - Escapes */ |
| 2747 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2748 | c = *s++; |
| 2749 | if (s > end) |
| 2750 | c = '\0'; /* Invalid after \ */ |
| 2751 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2752 | |
| 2753 | /* \x escapes */ |
| 2754 | case '\n': break; |
| 2755 | case '\\': *p++ = '\\'; break; |
| 2756 | case '\'': *p++ = '\''; break; |
| 2757 | case '\"': *p++ = '\"'; break; |
| 2758 | case 'b': *p++ = '\b'; break; |
| 2759 | case 'f': *p++ = '\014'; break; /* FF */ |
| 2760 | case 't': *p++ = '\t'; break; |
| 2761 | case 'n': *p++ = '\n'; break; |
| 2762 | case 'r': *p++ = '\r'; break; |
| 2763 | case 'v': *p++ = '\013'; break; /* VT */ |
| 2764 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 2765 | |
| 2766 | /* \OOO (octal) escapes */ |
| 2767 | case '0': case '1': case '2': case '3': |
| 2768 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2769 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2770 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2771 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2772 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2773 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2774 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2775 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2776 | break; |
| 2777 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2778 | /* hex escapes */ |
| 2779 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2780 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2781 | digits = 2; |
| 2782 | message = "truncated \\xXX escape"; |
| 2783 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2784 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2785 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2786 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2787 | digits = 4; |
| 2788 | message = "truncated \\uXXXX escape"; |
| 2789 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2790 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2791 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2792 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2793 | digits = 8; |
| 2794 | message = "truncated \\UXXXXXXXX escape"; |
| 2795 | hexescape: |
| 2796 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2797 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2798 | if (s+digits>end) { |
| 2799 | endinpos = size; |
| 2800 | if (unicode_decode_call_errorhandler( |
| 2801 | errors, &errorHandler, |
| 2802 | "unicodeescape", "end of string in escape sequence", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2803 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2804 | (PyObject **)&v, &outpos, &p)) |
| 2805 | goto onError; |
| 2806 | goto nextByte; |
| 2807 | } |
| 2808 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2809 | c = (unsigned char) s[i]; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2810 | if (!ISXDIGIT(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2811 | endinpos = (s+i+1)-starts; |
| 2812 | if (unicode_decode_call_errorhandler( |
| 2813 | errors, &errorHandler, |
| 2814 | "unicodeescape", message, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2815 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2816 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2817 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2818 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2819 | } |
| 2820 | chr = (chr<<4) & ~0xF; |
| 2821 | if (c >= '0' && c <= '9') |
| 2822 | chr += c - '0'; |
| 2823 | else if (c >= 'a' && c <= 'f') |
| 2824 | chr += 10 + c - 'a'; |
| 2825 | else |
| 2826 | chr += 10 + c - 'A'; |
| 2827 | } |
| 2828 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 2829 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2830 | /* _decoding_error will have already written into the |
| 2831 | target buffer. */ |
| 2832 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2833 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2834 | /* when we get here, chr is a 32-bit unicode character */ |
| 2835 | if (chr <= 0xffff) |
| 2836 | /* UCS-2 character */ |
| 2837 | *p++ = (Py_UNICODE) chr; |
| 2838 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2839 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2840 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2841 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2842 | *p++ = chr; |
| 2843 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2844 | chr -= 0x10000L; |
| 2845 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2846 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2847 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2848 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2849 | endinpos = s-starts; |
| 2850 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2851 | if (unicode_decode_call_errorhandler( |
| 2852 | errors, &errorHandler, |
| 2853 | "unicodeescape", "illegal Unicode character", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2854 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2855 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2856 | goto onError; |
| 2857 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2858 | break; |
| 2859 | |
| 2860 | /* \N{name} */ |
| 2861 | case 'N': |
| 2862 | message = "malformed \\N character escape"; |
| 2863 | if (ucnhash_CAPI == NULL) { |
| 2864 | /* load the unicode data module */ |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2865 | PyObject *m, *api; |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 2866 | m = PyImport_ImportModuleNoBlock("unicodedata"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2867 | if (m == NULL) |
| 2868 | goto ucnhashError; |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2869 | api = PyObject_GetAttrString(m, "ucnhash_CAPI"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2870 | Py_DECREF(m); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2871 | if (api == NULL) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2872 | goto ucnhashError; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2873 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCObject_AsVoidPtr(api); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2874 | Py_DECREF(api); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2875 | if (ucnhash_CAPI == NULL) |
| 2876 | goto ucnhashError; |
| 2877 | } |
| 2878 | if (*s == '{') { |
| 2879 | const char *start = s+1; |
| 2880 | /* look for the closing brace */ |
| 2881 | while (*s != '}' && s < end) |
| 2882 | s++; |
| 2883 | if (s > start && s < end && *s == '}') { |
| 2884 | /* found a name. look it up in the unicode database */ |
| 2885 | message = "unknown Unicode character name"; |
| 2886 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 2887 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2888 | goto store; |
| 2889 | } |
| 2890 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2891 | endinpos = s-starts; |
| 2892 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2893 | if (unicode_decode_call_errorhandler( |
| 2894 | errors, &errorHandler, |
| 2895 | "unicodeescape", message, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2896 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2897 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2898 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2899 | break; |
| 2900 | |
| 2901 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2902 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2903 | message = "\\ at end of string"; |
| 2904 | s--; |
| 2905 | endinpos = s-starts; |
| 2906 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2907 | if (unicode_decode_call_errorhandler( |
| 2908 | errors, &errorHandler, |
| 2909 | "unicodeescape", message, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2910 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2911 | (PyObject **)&v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2912 | goto onError; |
| 2913 | } |
| 2914 | else { |
| 2915 | *p++ = '\\'; |
| 2916 | *p++ = (unsigned char)s[-1]; |
| 2917 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2918 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2919 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2920 | nextByte: |
| 2921 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2922 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2923 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2924 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 2925 | Py_XDECREF(errorHandler); |
| 2926 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2927 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2928 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2929 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2930 | PyErr_SetString( |
| 2931 | PyExc_UnicodeError, |
| 2932 | "\\N escapes not supported (can't load unicodedata module)" |
| 2933 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2934 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2935 | Py_XDECREF(errorHandler); |
| 2936 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 2937 | return NULL; |
| 2938 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2939 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2940 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2941 | Py_XDECREF(errorHandler); |
| 2942 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2943 | return NULL; |
| 2944 | } |
| 2945 | |
| 2946 | /* Return a Unicode-Escape string version of the Unicode object. |
| 2947 | |
| 2948 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 2949 | appropriate. |
| 2950 | |
| 2951 | */ |
| 2952 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2953 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
| 2954 | Py_ssize_t size, |
| 2955 | Py_UNICODE ch) |
| 2956 | { |
| 2957 | /* like wcschr, but doesn't stop at NULL characters */ |
| 2958 | |
| 2959 | while (size-- > 0) { |
| 2960 | if (*s == ch) |
| 2961 | return s; |
| 2962 | s++; |
| 2963 | } |
| 2964 | |
| 2965 | return NULL; |
| 2966 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 2967 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2968 | static const char *hexdigits = "0123456789abcdef"; |
| 2969 | |
| 2970 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 2971 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2972 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2973 | PyObject *repr, *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2974 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2975 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2976 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 2977 | better to choose a different scheme. Perhaps scan the |
| 2978 | first N-chars of the string and allocate based on that size. |
| 2979 | */ |
| 2980 | /* Initial allocation is based on the longest-possible unichr |
| 2981 | escape. |
| 2982 | |
| 2983 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 2984 | unichr, so in this case it's the longest unichr escape. In |
| 2985 | narrow (UTF-16) builds this is five chars per source unichr |
| 2986 | since there are two unichrs in the surrogate pair, so in narrow |
| 2987 | (UTF-16) builds it's not the longest unichr escape. |
| 2988 | |
| 2989 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 2990 | so in the narrow (UTF-16) build case it's the longest unichr |
| 2991 | escape. |
| 2992 | */ |
| 2993 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 2994 | repr = PyBytes_FromStringAndSize(NULL, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2995 | #ifdef Py_UNICODE_WIDE |
| 2996 | + 10*size |
| 2997 | #else |
| 2998 | + 6*size |
| 2999 | #endif |
| 3000 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3001 | if (repr == NULL) |
| 3002 | return NULL; |
| 3003 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3004 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3005 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3006 | while (size-- > 0) { |
| 3007 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3008 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3009 | /* Escape backslashes */ |
| 3010 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3011 | *p++ = '\\'; |
| 3012 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3013 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3014 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3015 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 3016 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3017 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 3018 | else if (ch >= 0x10000) { |
| 3019 | *p++ = '\\'; |
| 3020 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3021 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 3022 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 3023 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 3024 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 3025 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 3026 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 3027 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 3028 | *p++ = hexdigits[ch & 0x0000000F]; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3029 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3030 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3031 | #else |
| 3032 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3033 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 3034 | Py_UNICODE ch2; |
| 3035 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3036 | |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3037 | ch2 = *s++; |
| 3038 | size--; |
| 3039 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 3040 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 3041 | *p++ = '\\'; |
| 3042 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3043 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 3044 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 3045 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 3046 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 3047 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 3048 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 3049 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 3050 | *p++ = hexdigits[ucs & 0x0000000F]; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3051 | continue; |
| 3052 | } |
| 3053 | /* Fall through: isolated surrogates are copied as-is */ |
| 3054 | s--; |
| 3055 | size++; |
| 3056 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3057 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3058 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3059 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3060 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3061 | *p++ = '\\'; |
| 3062 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3063 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 3064 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 3065 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 3066 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3067 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3068 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3069 | /* Map special whitespace to '\t', \n', '\r' */ |
| 3070 | else if (ch == '\t') { |
| 3071 | *p++ = '\\'; |
| 3072 | *p++ = 't'; |
| 3073 | } |
| 3074 | else if (ch == '\n') { |
| 3075 | *p++ = '\\'; |
| 3076 | *p++ = 'n'; |
| 3077 | } |
| 3078 | else if (ch == '\r') { |
| 3079 | *p++ = '\\'; |
| 3080 | *p++ = 'r'; |
| 3081 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3082 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3083 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 3084 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3085 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3086 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3087 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 3088 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3089 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3090 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3091 | /* Copy everything else as-is */ |
| 3092 | else |
| 3093 | *p++ = (char) ch; |
| 3094 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3095 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3096 | result = PyString_FromStringAndSize(PyBytes_AS_STRING(repr), |
| 3097 | p - PyBytes_AS_STRING(repr)); |
| 3098 | Py_DECREF(repr); |
| 3099 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3100 | } |
| 3101 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3102 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
| 3103 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3104 | PyObject *s, *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3105 | if (!PyUnicode_Check(unicode)) { |
| 3106 | PyErr_BadArgument(); |
| 3107 | return NULL; |
| 3108 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3109 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3110 | PyUnicode_GET_SIZE(unicode)); |
| 3111 | |
| 3112 | if (!s) |
| 3113 | return NULL; |
| 3114 | result = PyString_FromStringAndSize(PyBytes_AS_STRING(s), |
| 3115 | PyBytes_GET_SIZE(s)); |
| 3116 | Py_DECREF(s); |
| 3117 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3118 | } |
| 3119 | |
| 3120 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 3121 | |
| 3122 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3123 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3124 | const char *errors) |
| 3125 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3126 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3127 | Py_ssize_t startinpos; |
| 3128 | Py_ssize_t endinpos; |
| 3129 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3130 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3131 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3132 | const char *end; |
| 3133 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3134 | PyObject *errorHandler = NULL; |
| 3135 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3136 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3137 | /* Escaped strings will always be longer than the resulting |
| 3138 | 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] | 3139 | length after conversion to the true value. (But decoding error |
| 3140 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3141 | v = _PyUnicode_New(size); |
| 3142 | if (v == NULL) |
| 3143 | goto onError; |
| 3144 | if (size == 0) |
| 3145 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3146 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3147 | end = s + size; |
| 3148 | while (s < end) { |
| 3149 | unsigned char c; |
Martin v. Löwis | 047c05e | 2002-03-21 08:55:28 +0000 | [diff] [blame] | 3150 | Py_UCS4 x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3151 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3152 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3153 | |
| 3154 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3155 | if (*s != '\\') { |
| 3156 | *p++ = (unsigned char)*s++; |
| 3157 | continue; |
| 3158 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3159 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3160 | |
| 3161 | /* \u-escapes are only interpreted iff the number of leading |
| 3162 | backslashes if odd */ |
| 3163 | bs = s; |
| 3164 | for (;s < end;) { |
| 3165 | if (*s != '\\') |
| 3166 | break; |
| 3167 | *p++ = (unsigned char)*s++; |
| 3168 | } |
| 3169 | if (((s - bs) & 1) == 0 || |
| 3170 | s >= end || |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3171 | (*s != 'u' && *s != 'U')) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3172 | continue; |
| 3173 | } |
| 3174 | p--; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3175 | count = *s=='u' ? 4 : 8; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3176 | s++; |
| 3177 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3178 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3179 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3180 | for (x = 0, i = 0; i < count; ++i, ++s) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3181 | c = (unsigned char)*s; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3182 | if (!ISXDIGIT(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3183 | endinpos = s-starts; |
| 3184 | if (unicode_decode_call_errorhandler( |
| 3185 | errors, &errorHandler, |
| 3186 | "rawunicodeescape", "truncated \\uXXXX", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3187 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3188 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3189 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3190 | goto nextByte; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3191 | } |
| 3192 | x = (x<<4) & ~0xF; |
| 3193 | if (c >= '0' && c <= '9') |
| 3194 | x += c - '0'; |
| 3195 | else if (c >= 'a' && c <= 'f') |
| 3196 | x += 10 + c - 'a'; |
| 3197 | else |
| 3198 | x += 10 + c - 'A'; |
| 3199 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3200 | #ifndef Py_UNICODE_WIDE |
| 3201 | if (x > 0x10000) { |
| 3202 | if (unicode_decode_call_errorhandler( |
| 3203 | errors, &errorHandler, |
| 3204 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3205 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3206 | (PyObject **)&v, &outpos, &p)) |
| 3207 | goto onError; |
| 3208 | } |
| 3209 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3210 | *p++ = x; |
| 3211 | nextByte: |
| 3212 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3213 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3214 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3215 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3216 | Py_XDECREF(errorHandler); |
| 3217 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3218 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3219 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3220 | onError: |
| 3221 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3222 | Py_XDECREF(errorHandler); |
| 3223 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3224 | return NULL; |
| 3225 | } |
| 3226 | |
| 3227 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3228 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3229 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3230 | PyObject *repr, *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3231 | char *p; |
| 3232 | char *q; |
| 3233 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3234 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3235 | repr = PyBytes_FromStringAndSize(NULL, 10 * size); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3236 | #else |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3237 | repr = PyBytes_FromStringAndSize(NULL, 6 * size); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3238 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3239 | if (repr == NULL) |
| 3240 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3241 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3242 | goto done; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3243 | |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3244 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3245 | while (size-- > 0) { |
| 3246 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3247 | #ifdef Py_UNICODE_WIDE |
| 3248 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 3249 | if (ch >= 0x10000) { |
| 3250 | *p++ = '\\'; |
| 3251 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 3252 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 3253 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 3254 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 3255 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 3256 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 3257 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 3258 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 3259 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3260 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3261 | else |
| 3262 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3263 | /* Map 16-bit characters to '\uxxxx' */ |
| 3264 | if (ch >= 256) { |
| 3265 | *p++ = '\\'; |
| 3266 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 3267 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 3268 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 3269 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 3270 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3271 | } |
| 3272 | /* Copy everything else as-is */ |
| 3273 | else |
| 3274 | *p++ = (char) ch; |
| 3275 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3276 | size = p - q; |
| 3277 | |
| 3278 | done: |
| 3279 | result = PyString_FromStringAndSize(PyBytes_AS_STRING(repr), size); |
| 3280 | Py_DECREF(repr); |
| 3281 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3282 | } |
| 3283 | |
| 3284 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 3285 | { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3286 | PyObject *s, *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3287 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3288 | PyErr_BadArgument(); |
| 3289 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3290 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3291 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3292 | PyUnicode_GET_SIZE(unicode)); |
| 3293 | |
| 3294 | if (!s) |
| 3295 | return NULL; |
| 3296 | result = PyString_FromStringAndSize(PyBytes_AS_STRING(s), |
| 3297 | PyBytes_GET_SIZE(s)); |
| 3298 | Py_DECREF(s); |
| 3299 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3300 | } |
| 3301 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3302 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 3303 | |
| 3304 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3305 | Py_ssize_t size, |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3306 | const char *errors) |
| 3307 | { |
| 3308 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3309 | Py_ssize_t startinpos; |
| 3310 | Py_ssize_t endinpos; |
| 3311 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3312 | PyUnicodeObject *v; |
| 3313 | Py_UNICODE *p; |
| 3314 | const char *end; |
| 3315 | const char *reason; |
| 3316 | PyObject *errorHandler = NULL; |
| 3317 | PyObject *exc = NULL; |
| 3318 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 3319 | #ifdef Py_UNICODE_WIDE |
| 3320 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 3321 | #endif |
| 3322 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3323 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3324 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 3325 | if (v == NULL) |
| 3326 | goto onError; |
| 3327 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
| 3328 | return (PyObject *)v; |
| 3329 | p = PyUnicode_AS_UNICODE(v); |
| 3330 | end = s + size; |
| 3331 | |
| 3332 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3333 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3334 | /* We have to sanity check the raw data, otherwise doom looms for |
| 3335 | some malformed UCS-4 data. */ |
| 3336 | if ( |
| 3337 | #ifdef Py_UNICODE_WIDE |
| 3338 | *p > unimax || *p < 0 || |
| 3339 | #endif |
| 3340 | end-s < Py_UNICODE_SIZE |
| 3341 | ) |
| 3342 | { |
| 3343 | startinpos = s - starts; |
| 3344 | if (end-s < Py_UNICODE_SIZE) { |
| 3345 | endinpos = end-starts; |
| 3346 | reason = "truncated input"; |
| 3347 | } |
| 3348 | else { |
| 3349 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 3350 | reason = "illegal code point (> 0x10FFFF)"; |
| 3351 | } |
| 3352 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 3353 | if (unicode_decode_call_errorhandler( |
| 3354 | errors, &errorHandler, |
| 3355 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3356 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3357 | (PyObject **)&v, &outpos, &p)) { |
| 3358 | goto onError; |
| 3359 | } |
| 3360 | } |
| 3361 | else { |
| 3362 | p++; |
| 3363 | s += Py_UNICODE_SIZE; |
| 3364 | } |
| 3365 | } |
| 3366 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3367 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3368 | goto onError; |
| 3369 | Py_XDECREF(errorHandler); |
| 3370 | Py_XDECREF(exc); |
| 3371 | return (PyObject *)v; |
| 3372 | |
| 3373 | onError: |
| 3374 | Py_XDECREF(v); |
| 3375 | Py_XDECREF(errorHandler); |
| 3376 | Py_XDECREF(exc); |
| 3377 | return NULL; |
| 3378 | } |
| 3379 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3380 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 3381 | |
| 3382 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3383 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3384 | const char *errors) |
| 3385 | { |
| 3386 | PyUnicodeObject *v; |
| 3387 | Py_UNICODE *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3388 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3389 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3390 | if (size == 1) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3391 | Py_UNICODE r = *(unsigned char*)s; |
| 3392 | return PyUnicode_FromUnicode(&r, 1); |
| 3393 | } |
| 3394 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3395 | v = _PyUnicode_New(size); |
| 3396 | if (v == NULL) |
| 3397 | goto onError; |
| 3398 | if (size == 0) |
| 3399 | return (PyObject *)v; |
| 3400 | p = PyUnicode_AS_UNICODE(v); |
| 3401 | while (size-- > 0) |
| 3402 | *p++ = (unsigned char)*s++; |
| 3403 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3404 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3405 | onError: |
| 3406 | Py_XDECREF(v); |
| 3407 | return NULL; |
| 3408 | } |
| 3409 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3410 | /* create or adjust a UnicodeEncodeError */ |
| 3411 | static void make_encode_exception(PyObject **exceptionObject, |
| 3412 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3413 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3414 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3415 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3416 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3417 | if (*exceptionObject == NULL) { |
| 3418 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 3419 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3420 | } |
| 3421 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3422 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 3423 | goto onError; |
| 3424 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 3425 | goto onError; |
| 3426 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 3427 | goto onError; |
| 3428 | return; |
| 3429 | onError: |
| 3430 | Py_DECREF(*exceptionObject); |
| 3431 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3432 | } |
| 3433 | } |
| 3434 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3435 | /* raises a UnicodeEncodeError */ |
| 3436 | static void raise_encode_exception(PyObject **exceptionObject, |
| 3437 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3438 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3439 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3440 | const char *reason) |
| 3441 | { |
| 3442 | make_encode_exception(exceptionObject, |
| 3443 | encoding, unicode, size, startpos, endpos, reason); |
| 3444 | if (*exceptionObject != NULL) |
| 3445 | PyCodec_StrictErrors(*exceptionObject); |
| 3446 | } |
| 3447 | |
| 3448 | /* error handling callback helper: |
| 3449 | build arguments, call the callback and check the arguments, |
| 3450 | put the result into newpos and return the replacement string, which |
| 3451 | has to be freed by the caller */ |
| 3452 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
| 3453 | PyObject **errorHandler, |
| 3454 | const char *encoding, const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3455 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 3456 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3457 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3458 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3459 | 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] | 3460 | |
| 3461 | PyObject *restuple; |
| 3462 | PyObject *resunicode; |
| 3463 | |
| 3464 | if (*errorHandler == NULL) { |
| 3465 | *errorHandler = PyCodec_LookupError(errors); |
| 3466 | if (*errorHandler == NULL) |
| 3467 | return NULL; |
| 3468 | } |
| 3469 | |
| 3470 | make_encode_exception(exceptionObject, |
| 3471 | encoding, unicode, size, startpos, endpos, reason); |
| 3472 | if (*exceptionObject == NULL) |
| 3473 | return NULL; |
| 3474 | |
| 3475 | restuple = PyObject_CallFunctionObjArgs( |
| 3476 | *errorHandler, *exceptionObject, NULL); |
| 3477 | if (restuple == NULL) |
| 3478 | return NULL; |
| 3479 | if (!PyTuple_Check(restuple)) { |
| 3480 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 3481 | Py_DECREF(restuple); |
| 3482 | return NULL; |
| 3483 | } |
| 3484 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
| 3485 | &resunicode, newpos)) { |
| 3486 | Py_DECREF(restuple); |
| 3487 | return NULL; |
| 3488 | } |
| 3489 | if (*newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3490 | *newpos = size+*newpos; |
| 3491 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 3492 | 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] | 3493 | Py_DECREF(restuple); |
| 3494 | return NULL; |
| 3495 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3496 | Py_INCREF(resunicode); |
| 3497 | Py_DECREF(restuple); |
| 3498 | return resunicode; |
| 3499 | } |
| 3500 | |
| 3501 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3502 | Py_ssize_t size, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3503 | const char *errors, |
| 3504 | int limit) |
| 3505 | { |
| 3506 | /* output object */ |
| 3507 | PyObject *res; |
| 3508 | /* pointers to the beginning and end+1 of input */ |
| 3509 | const Py_UNICODE *startp = p; |
| 3510 | const Py_UNICODE *endp = p + size; |
| 3511 | /* pointer to the beginning of the unencodable characters */ |
| 3512 | /* const Py_UNICODE *badp = NULL; */ |
| 3513 | /* pointer into the output */ |
| 3514 | char *str; |
| 3515 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3516 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3517 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 3518 | 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] | 3519 | PyObject *errorHandler = NULL; |
| 3520 | PyObject *exc = NULL; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3521 | PyObject *result = NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3522 | /* the following variable is used for caching string comparisons |
| 3523 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 3524 | int known_errorHandler = -1; |
| 3525 | |
| 3526 | /* allocate enough for a simple encoding without |
| 3527 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3528 | if (size == 0) |
| 3529 | return PyString_FromStringAndSize(NULL, 0); |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3530 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3531 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3532 | return NULL; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3533 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3534 | ressize = size; |
| 3535 | |
| 3536 | while (p<endp) { |
| 3537 | Py_UNICODE c = *p; |
| 3538 | |
| 3539 | /* can we encode this? */ |
| 3540 | if (c<limit) { |
| 3541 | /* no overflow check, because we know that the space is enough */ |
| 3542 | *str++ = (char)c; |
| 3543 | ++p; |
| 3544 | } |
| 3545 | else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3546 | Py_ssize_t unicodepos = p-startp; |
| 3547 | Py_ssize_t requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3548 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3549 | Py_ssize_t repsize; |
| 3550 | Py_ssize_t newpos; |
| 3551 | Py_ssize_t respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3552 | Py_UNICODE *uni2; |
| 3553 | /* startpos for collecting unencodable chars */ |
| 3554 | const Py_UNICODE *collstart = p; |
| 3555 | const Py_UNICODE *collend = p; |
| 3556 | /* find all unecodable characters */ |
| 3557 | while ((collend < endp) && ((*collend)>=limit)) |
| 3558 | ++collend; |
| 3559 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 3560 | if (known_errorHandler==-1) { |
| 3561 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3562 | known_errorHandler = 1; |
| 3563 | else if (!strcmp(errors, "replace")) |
| 3564 | known_errorHandler = 2; |
| 3565 | else if (!strcmp(errors, "ignore")) |
| 3566 | known_errorHandler = 3; |
| 3567 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3568 | known_errorHandler = 4; |
| 3569 | else |
| 3570 | known_errorHandler = 0; |
| 3571 | } |
| 3572 | switch (known_errorHandler) { |
| 3573 | case 1: /* strict */ |
| 3574 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 3575 | goto onError; |
| 3576 | case 2: /* replace */ |
| 3577 | while (collstart++<collend) |
| 3578 | *str++ = '?'; /* fall through */ |
| 3579 | case 3: /* ignore */ |
| 3580 | p = collend; |
| 3581 | break; |
| 3582 | case 4: /* xmlcharrefreplace */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3583 | respos = str - PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3584 | /* determine replacement size (temporarily (mis)uses p) */ |
| 3585 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 3586 | if (*p<10) |
| 3587 | repsize += 2+1+1; |
| 3588 | else if (*p<100) |
| 3589 | repsize += 2+2+1; |
| 3590 | else if (*p<1000) |
| 3591 | repsize += 2+3+1; |
| 3592 | else if (*p<10000) |
| 3593 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 3594 | #ifndef Py_UNICODE_WIDE |
| 3595 | else |
| 3596 | repsize += 2+5+1; |
| 3597 | #else |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3598 | else if (*p<100000) |
| 3599 | repsize += 2+5+1; |
| 3600 | else if (*p<1000000) |
| 3601 | repsize += 2+6+1; |
| 3602 | else |
| 3603 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3604 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3605 | } |
| 3606 | requiredsize = respos+repsize+(endp-collend); |
| 3607 | if (requiredsize > ressize) { |
| 3608 | if (requiredsize<2*ressize) |
| 3609 | requiredsize = 2*ressize; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3610 | if (PyBytes_Resize(res, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3611 | goto onError; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3612 | str = PyBytes_AS_STRING(res) + respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3613 | ressize = requiredsize; |
| 3614 | } |
| 3615 | /* generate replacement (temporarily (mis)uses p) */ |
| 3616 | for (p = collstart; p < collend; ++p) { |
| 3617 | str += sprintf(str, "&#%d;", (int)*p); |
| 3618 | } |
| 3619 | p = collend; |
| 3620 | break; |
| 3621 | default: |
| 3622 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 3623 | encoding, reason, startp, size, &exc, |
| 3624 | collstart-startp, collend-startp, &newpos); |
| 3625 | if (repunicode == NULL) |
| 3626 | goto onError; |
| 3627 | /* need more space? (at least enough for what we |
| 3628 | have+the replacement+the rest of the string, so |
| 3629 | we won't have to check space for encodable characters) */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3630 | respos = str - PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3631 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3632 | requiredsize = respos+repsize+(endp-collend); |
| 3633 | if (requiredsize > ressize) { |
| 3634 | if (requiredsize<2*ressize) |
| 3635 | requiredsize = 2*ressize; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3636 | if (PyBytes_Resize(res, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3637 | Py_DECREF(repunicode); |
| 3638 | goto onError; |
| 3639 | } |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 3640 | str = PyBytes_AS_STRING(res) + respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3641 | ressize = requiredsize; |
| 3642 | } |
| 3643 | /* check if there is anything unencodable in the replacement |
| 3644 | and copy it to the output */ |
| 3645 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 3646 | c = *uni2; |
| 3647 | if (c >= limit) { |
| 3648 | raise_encode_exception(&exc, encoding, startp, size, |
| 3649 | unicodepos, unicodepos+1, reason); |
| 3650 | Py_DECREF(repunicode); |
| 3651 | goto onError; |
| 3652 | } |
| 3653 | *str = (char)c; |
| 3654 | } |
| 3655 | p = startp + newpos; |
| 3656 | Py_DECREF(repunicode); |
| 3657 | } |
| 3658 | } |
| 3659 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3660 | result = PyString_FromStringAndSize(PyBytes_AS_STRING(res), |
| 3661 | str - PyBytes_AS_STRING(res)); |
| 3662 | onError: |
| 3663 | Py_DECREF(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3664 | Py_XDECREF(errorHandler); |
| 3665 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3666 | return result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3667 | } |
| 3668 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3669 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3670 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3671 | const char *errors) |
| 3672 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3673 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3674 | } |
| 3675 | |
| 3676 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 3677 | { |
| 3678 | if (!PyUnicode_Check(unicode)) { |
| 3679 | PyErr_BadArgument(); |
| 3680 | return NULL; |
| 3681 | } |
| 3682 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 3683 | PyUnicode_GET_SIZE(unicode), |
| 3684 | NULL); |
| 3685 | } |
| 3686 | |
| 3687 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 3688 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3689 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3690 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3691 | const char *errors) |
| 3692 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3693 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3694 | PyUnicodeObject *v; |
| 3695 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3696 | Py_ssize_t startinpos; |
| 3697 | Py_ssize_t endinpos; |
| 3698 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3699 | const char *e; |
| 3700 | PyObject *errorHandler = NULL; |
| 3701 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3702 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3703 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3704 | if (size == 1 && *(unsigned char*)s < 128) { |
| 3705 | Py_UNICODE r = *(unsigned char*)s; |
| 3706 | return PyUnicode_FromUnicode(&r, 1); |
| 3707 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3708 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3709 | v = _PyUnicode_New(size); |
| 3710 | if (v == NULL) |
| 3711 | goto onError; |
| 3712 | if (size == 0) |
| 3713 | return (PyObject *)v; |
| 3714 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3715 | e = s + size; |
| 3716 | while (s < e) { |
| 3717 | register unsigned char c = (unsigned char)*s; |
| 3718 | if (c < 128) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3719 | *p++ = c; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3720 | ++s; |
| 3721 | } |
| 3722 | else { |
| 3723 | startinpos = s-starts; |
| 3724 | endinpos = startinpos + 1; |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 3725 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3726 | if (unicode_decode_call_errorhandler( |
| 3727 | errors, &errorHandler, |
| 3728 | "ascii", "ordinal not in range(128)", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3729 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3730 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3731 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3732 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3733 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3734 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3735 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3736 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3737 | Py_XDECREF(errorHandler); |
| 3738 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3739 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3740 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3741 | onError: |
| 3742 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3743 | Py_XDECREF(errorHandler); |
| 3744 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3745 | return NULL; |
| 3746 | } |
| 3747 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3748 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3749 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3750 | const char *errors) |
| 3751 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3752 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3753 | } |
| 3754 | |
| 3755 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 3756 | { |
| 3757 | if (!PyUnicode_Check(unicode)) { |
| 3758 | PyErr_BadArgument(); |
| 3759 | return NULL; |
| 3760 | } |
| 3761 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 3762 | PyUnicode_GET_SIZE(unicode), |
| 3763 | NULL); |
| 3764 | } |
| 3765 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3766 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3767 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3768 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3769 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3770 | #if SIZEOF_INT < SIZEOF_SSIZE_T |
| 3771 | #define NEED_RETRY |
| 3772 | #endif |
| 3773 | |
| 3774 | /* XXX This code is limited to "true" double-byte encodings, as |
| 3775 | a) it assumes an incomplete character consists of a single byte, and |
| 3776 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
| 3777 | encodings, see IsDBCSLeadByteEx documentation. */ |
| 3778 | |
| 3779 | static int is_dbcs_lead_byte(const char *s, int offset) |
| 3780 | { |
| 3781 | const char *curr = s + offset; |
| 3782 | |
| 3783 | if (IsDBCSLeadByte(*curr)) { |
| 3784 | const char *prev = CharPrev(s, curr); |
| 3785 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
| 3786 | } |
| 3787 | return 0; |
| 3788 | } |
| 3789 | |
| 3790 | /* |
| 3791 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 3792 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 3793 | */ |
| 3794 | static int decode_mbcs(PyUnicodeObject **v, |
| 3795 | const char *s, /* MBCS string */ |
| 3796 | int size, /* sizeof MBCS string */ |
| 3797 | int final) |
| 3798 | { |
| 3799 | Py_UNICODE *p; |
| 3800 | Py_ssize_t n = 0; |
| 3801 | int usize = 0; |
| 3802 | |
| 3803 | assert(size >= 0); |
| 3804 | |
| 3805 | /* Skip trailing lead-byte unless 'final' is set */ |
| 3806 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
| 3807 | --size; |
| 3808 | |
| 3809 | /* First get the size of the result */ |
| 3810 | if (size > 0) { |
| 3811 | usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); |
| 3812 | if (usize == 0) { |
| 3813 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3814 | return -1; |
| 3815 | } |
| 3816 | } |
| 3817 | |
| 3818 | if (*v == NULL) { |
| 3819 | /* Create unicode object */ |
| 3820 | *v = _PyUnicode_New(usize); |
| 3821 | if (*v == NULL) |
| 3822 | return -1; |
| 3823 | } |
| 3824 | else { |
| 3825 | /* Extend unicode object */ |
| 3826 | n = PyUnicode_GET_SIZE(*v); |
| 3827 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 3828 | return -1; |
| 3829 | } |
| 3830 | |
| 3831 | /* Do the conversion */ |
| 3832 | if (size > 0) { |
| 3833 | p = PyUnicode_AS_UNICODE(*v) + n; |
| 3834 | if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { |
| 3835 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3836 | return -1; |
| 3837 | } |
| 3838 | } |
| 3839 | |
| 3840 | return size; |
| 3841 | } |
| 3842 | |
| 3843 | PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
| 3844 | Py_ssize_t size, |
| 3845 | const char *errors, |
| 3846 | Py_ssize_t *consumed) |
| 3847 | { |
| 3848 | PyUnicodeObject *v = NULL; |
| 3849 | int done; |
| 3850 | |
| 3851 | if (consumed) |
| 3852 | *consumed = 0; |
| 3853 | |
| 3854 | #ifdef NEED_RETRY |
| 3855 | retry: |
| 3856 | if (size > INT_MAX) |
| 3857 | done = decode_mbcs(&v, s, INT_MAX, 0); |
| 3858 | else |
| 3859 | #endif |
| 3860 | done = decode_mbcs(&v, s, (int)size, !consumed); |
| 3861 | |
| 3862 | if (done < 0) { |
| 3863 | Py_XDECREF(v); |
| 3864 | return NULL; |
| 3865 | } |
| 3866 | |
| 3867 | if (consumed) |
| 3868 | *consumed += done; |
| 3869 | |
| 3870 | #ifdef NEED_RETRY |
| 3871 | if (size > INT_MAX) { |
| 3872 | s += done; |
| 3873 | size -= done; |
| 3874 | goto retry; |
| 3875 | } |
| 3876 | #endif |
| 3877 | |
| 3878 | return (PyObject *)v; |
| 3879 | } |
| 3880 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3881 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3882 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3883 | const char *errors) |
| 3884 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3885 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 3886 | } |
| 3887 | |
| 3888 | /* |
| 3889 | * Convert unicode into string object (MBCS). |
| 3890 | * Returns 0 if succeed, -1 otherwise. |
| 3891 | */ |
| 3892 | static int encode_mbcs(PyObject **repr, |
| 3893 | const Py_UNICODE *p, /* unicode */ |
| 3894 | int size) /* size of unicode */ |
| 3895 | { |
| 3896 | int mbcssize = 0; |
| 3897 | Py_ssize_t n = 0; |
| 3898 | |
| 3899 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3900 | |
| 3901 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3902 | if (size > 0) { |
| 3903 | mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); |
| 3904 | if (mbcssize == 0) { |
| 3905 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3906 | return -1; |
| 3907 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3908 | } |
| 3909 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3910 | if (*repr == NULL) { |
| 3911 | /* Create string object */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3912 | *repr = PyString_FromStringAndSize(NULL, mbcssize); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3913 | if (*repr == NULL) |
| 3914 | return -1; |
| 3915 | } |
| 3916 | else { |
| 3917 | /* Extend string object */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3918 | n = PyString_Size(*repr); |
| 3919 | if (_PyString_Resize(repr, n + mbcssize) < 0) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3920 | return -1; |
| 3921 | } |
| 3922 | |
| 3923 | /* Do the conversion */ |
| 3924 | if (size > 0) { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3925 | char *s = PyString_AS_STRING(*repr) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3926 | if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { |
| 3927 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3928 | return -1; |
| 3929 | } |
| 3930 | } |
| 3931 | |
| 3932 | return 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3933 | } |
| 3934 | |
| 3935 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3936 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3937 | const char *errors) |
| 3938 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3939 | PyObject *repr = NULL; |
| 3940 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 3941 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3942 | #ifdef NEED_RETRY |
| 3943 | retry: |
| 3944 | if (size > INT_MAX) |
| 3945 | ret = encode_mbcs(&repr, p, INT_MAX); |
| 3946 | else |
| 3947 | #endif |
| 3948 | ret = encode_mbcs(&repr, p, (int)size); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3949 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3950 | if (ret < 0) { |
| 3951 | Py_XDECREF(repr); |
| 3952 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3953 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3954 | |
| 3955 | #ifdef NEED_RETRY |
| 3956 | if (size > INT_MAX) { |
| 3957 | p += INT_MAX; |
| 3958 | size -= INT_MAX; |
| 3959 | goto retry; |
| 3960 | } |
| 3961 | #endif |
| 3962 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3963 | return repr; |
| 3964 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3965 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3966 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 3967 | { |
| 3968 | if (!PyUnicode_Check(unicode)) { |
| 3969 | PyErr_BadArgument(); |
| 3970 | return NULL; |
| 3971 | } |
| 3972 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 3973 | PyUnicode_GET_SIZE(unicode), |
| 3974 | NULL); |
| 3975 | } |
| 3976 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3977 | #undef NEED_RETRY |
| 3978 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3979 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3980 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3981 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 3982 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3983 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3984 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3985 | PyObject *mapping, |
| 3986 | const char *errors) |
| 3987 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3988 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3989 | Py_ssize_t startinpos; |
| 3990 | Py_ssize_t endinpos; |
| 3991 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3992 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3993 | PyUnicodeObject *v; |
| 3994 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3995 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3996 | PyObject *errorHandler = NULL; |
| 3997 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3998 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3999 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4000 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4001 | /* Default to Latin-1 */ |
| 4002 | if (mapping == NULL) |
| 4003 | return PyUnicode_DecodeLatin1(s, size, errors); |
| 4004 | |
| 4005 | v = _PyUnicode_New(size); |
| 4006 | if (v == NULL) |
| 4007 | goto onError; |
| 4008 | if (size == 0) |
| 4009 | return (PyObject *)v; |
| 4010 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4011 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4012 | if (PyUnicode_CheckExact(mapping)) { |
| 4013 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 4014 | maplen = PyUnicode_GET_SIZE(mapping); |
| 4015 | while (s < e) { |
| 4016 | unsigned char ch = *s; |
| 4017 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4018 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4019 | if (ch < maplen) |
| 4020 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4021 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4022 | if (x == 0xfffe) { |
| 4023 | /* undefined mapping */ |
| 4024 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4025 | startinpos = s-starts; |
| 4026 | endinpos = startinpos+1; |
| 4027 | if (unicode_decode_call_errorhandler( |
| 4028 | errors, &errorHandler, |
| 4029 | "charmap", "character maps to <undefined>", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4030 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4031 | (PyObject **)&v, &outpos, &p)) { |
| 4032 | goto onError; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 4033 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4034 | continue; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 4035 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4036 | *p++ = x; |
| 4037 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4038 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4039 | } |
| 4040 | else { |
| 4041 | while (s < e) { |
| 4042 | unsigned char ch = *s; |
| 4043 | PyObject *w, *x; |
| 4044 | |
| 4045 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4046 | w = PyLong_FromLong((long)ch); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4047 | if (w == NULL) |
| 4048 | goto onError; |
| 4049 | x = PyObject_GetItem(mapping, w); |
| 4050 | Py_DECREF(w); |
| 4051 | if (x == NULL) { |
| 4052 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4053 | /* No mapping found means: mapping is undefined. */ |
| 4054 | PyErr_Clear(); |
| 4055 | x = Py_None; |
| 4056 | Py_INCREF(x); |
| 4057 | } else |
| 4058 | goto onError; |
| 4059 | } |
| 4060 | |
| 4061 | /* Apply mapping */ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4062 | if (PyLong_Check(x)) { |
| 4063 | long value = PyLong_AS_LONG(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4064 | if (value < 0 || value > 65535) { |
| 4065 | PyErr_SetString(PyExc_TypeError, |
| 4066 | "character mapping must be in range(65536)"); |
| 4067 | Py_DECREF(x); |
| 4068 | goto onError; |
| 4069 | } |
| 4070 | *p++ = (Py_UNICODE)value; |
| 4071 | } |
| 4072 | else if (x == Py_None) { |
| 4073 | /* undefined mapping */ |
| 4074 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4075 | startinpos = s-starts; |
| 4076 | endinpos = startinpos+1; |
| 4077 | if (unicode_decode_call_errorhandler( |
| 4078 | errors, &errorHandler, |
| 4079 | "charmap", "character maps to <undefined>", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4080 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4081 | (PyObject **)&v, &outpos, &p)) { |
| 4082 | Py_DECREF(x); |
| 4083 | goto onError; |
| 4084 | } |
Walter Dörwald | d4fff17 | 2005-11-28 22:15:56 +0000 | [diff] [blame] | 4085 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4086 | continue; |
| 4087 | } |
| 4088 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4089 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4090 | |
| 4091 | if (targetsize == 1) |
| 4092 | /* 1-1 mapping */ |
| 4093 | *p++ = *PyUnicode_AS_UNICODE(x); |
| 4094 | |
| 4095 | else if (targetsize > 1) { |
| 4096 | /* 1-n mapping */ |
| 4097 | if (targetsize > extrachars) { |
| 4098 | /* resize first */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4099 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 4100 | Py_ssize_t needed = (targetsize - extrachars) + \ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4101 | (targetsize << 2); |
| 4102 | extrachars += needed; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4103 | /* XXX overflow detection missing */ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4104 | if (_PyUnicode_Resize(&v, |
| 4105 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 4106 | Py_DECREF(x); |
| 4107 | goto onError; |
| 4108 | } |
| 4109 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 4110 | } |
| 4111 | Py_UNICODE_COPY(p, |
| 4112 | PyUnicode_AS_UNICODE(x), |
| 4113 | targetsize); |
| 4114 | p += targetsize; |
| 4115 | extrachars -= targetsize; |
| 4116 | } |
| 4117 | /* 1-0 mapping: skip the character */ |
| 4118 | } |
| 4119 | else { |
| 4120 | /* wrong return value */ |
| 4121 | PyErr_SetString(PyExc_TypeError, |
| 4122 | "character mapping must return integer, None or unicode"); |
| 4123 | Py_DECREF(x); |
| 4124 | goto onError; |
| 4125 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4126 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4127 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4128 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4129 | } |
| 4130 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4131 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4132 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4133 | Py_XDECREF(errorHandler); |
| 4134 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4135 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4136 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4137 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4138 | Py_XDECREF(errorHandler); |
| 4139 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4140 | Py_XDECREF(v); |
| 4141 | return NULL; |
| 4142 | } |
| 4143 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4144 | /* Charmap encoding: the lookup table */ |
| 4145 | |
| 4146 | struct encoding_map{ |
| 4147 | PyObject_HEAD |
| 4148 | unsigned char level1[32]; |
| 4149 | int count2, count3; |
| 4150 | unsigned char level23[1]; |
| 4151 | }; |
| 4152 | |
| 4153 | static PyObject* |
| 4154 | encoding_map_size(PyObject *obj, PyObject* args) |
| 4155 | { |
| 4156 | struct encoding_map *map = (struct encoding_map*)obj; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4157 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4158 | 128*map->count3); |
| 4159 | } |
| 4160 | |
| 4161 | static PyMethodDef encoding_map_methods[] = { |
| 4162 | {"size", encoding_map_size, METH_NOARGS, |
| 4163 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 4164 | { 0 } |
| 4165 | }; |
| 4166 | |
| 4167 | static void |
| 4168 | encoding_map_dealloc(PyObject* o) |
| 4169 | { |
| 4170 | PyObject_FREE(o); |
| 4171 | } |
| 4172 | |
| 4173 | static PyTypeObject EncodingMapType = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 4174 | PyVarObject_HEAD_INIT(NULL, 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4175 | "EncodingMap", /*tp_name*/ |
| 4176 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 4177 | 0, /*tp_itemsize*/ |
| 4178 | /* methods */ |
| 4179 | encoding_map_dealloc, /*tp_dealloc*/ |
| 4180 | 0, /*tp_print*/ |
| 4181 | 0, /*tp_getattr*/ |
| 4182 | 0, /*tp_setattr*/ |
| 4183 | 0, /*tp_compare*/ |
| 4184 | 0, /*tp_repr*/ |
| 4185 | 0, /*tp_as_number*/ |
| 4186 | 0, /*tp_as_sequence*/ |
| 4187 | 0, /*tp_as_mapping*/ |
| 4188 | 0, /*tp_hash*/ |
| 4189 | 0, /*tp_call*/ |
| 4190 | 0, /*tp_str*/ |
| 4191 | 0, /*tp_getattro*/ |
| 4192 | 0, /*tp_setattro*/ |
| 4193 | 0, /*tp_as_buffer*/ |
| 4194 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 4195 | 0, /*tp_doc*/ |
| 4196 | 0, /*tp_traverse*/ |
| 4197 | 0, /*tp_clear*/ |
| 4198 | 0, /*tp_richcompare*/ |
| 4199 | 0, /*tp_weaklistoffset*/ |
| 4200 | 0, /*tp_iter*/ |
| 4201 | 0, /*tp_iternext*/ |
| 4202 | encoding_map_methods, /*tp_methods*/ |
| 4203 | 0, /*tp_members*/ |
| 4204 | 0, /*tp_getset*/ |
| 4205 | 0, /*tp_base*/ |
| 4206 | 0, /*tp_dict*/ |
| 4207 | 0, /*tp_descr_get*/ |
| 4208 | 0, /*tp_descr_set*/ |
| 4209 | 0, /*tp_dictoffset*/ |
| 4210 | 0, /*tp_init*/ |
| 4211 | 0, /*tp_alloc*/ |
| 4212 | 0, /*tp_new*/ |
| 4213 | 0, /*tp_free*/ |
| 4214 | 0, /*tp_is_gc*/ |
| 4215 | }; |
| 4216 | |
| 4217 | PyObject* |
| 4218 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 4219 | { |
| 4220 | Py_UNICODE *decode; |
| 4221 | PyObject *result; |
| 4222 | struct encoding_map *mresult; |
| 4223 | int i; |
| 4224 | int need_dict = 0; |
| 4225 | unsigned char level1[32]; |
| 4226 | unsigned char level2[512]; |
| 4227 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 4228 | int count2 = 0, count3 = 0; |
| 4229 | |
| 4230 | if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { |
| 4231 | PyErr_BadArgument(); |
| 4232 | return NULL; |
| 4233 | } |
| 4234 | decode = PyUnicode_AS_UNICODE(string); |
| 4235 | memset(level1, 0xFF, sizeof level1); |
| 4236 | memset(level2, 0xFF, sizeof level2); |
| 4237 | |
| 4238 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 4239 | or if there are non-BMP characters, we need to use |
| 4240 | a mapping dictionary. */ |
| 4241 | if (decode[0] != 0) |
| 4242 | need_dict = 1; |
| 4243 | for (i = 1; i < 256; i++) { |
| 4244 | int l1, l2; |
| 4245 | if (decode[i] == 0 |
| 4246 | #ifdef Py_UNICODE_WIDE |
| 4247 | || decode[i] > 0xFFFF |
| 4248 | #endif |
| 4249 | ) { |
| 4250 | need_dict = 1; |
| 4251 | break; |
| 4252 | } |
| 4253 | if (decode[i] == 0xFFFE) |
| 4254 | /* unmapped character */ |
| 4255 | continue; |
| 4256 | l1 = decode[i] >> 11; |
| 4257 | l2 = decode[i] >> 7; |
| 4258 | if (level1[l1] == 0xFF) |
| 4259 | level1[l1] = count2++; |
| 4260 | if (level2[l2] == 0xFF) |
| 4261 | level2[l2] = count3++; |
| 4262 | } |
| 4263 | |
| 4264 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 4265 | need_dict = 1; |
| 4266 | |
| 4267 | if (need_dict) { |
| 4268 | PyObject *result = PyDict_New(); |
| 4269 | PyObject *key, *value; |
| 4270 | if (!result) |
| 4271 | return NULL; |
| 4272 | for (i = 0; i < 256; i++) { |
| 4273 | key = value = NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4274 | key = PyLong_FromLong(decode[i]); |
| 4275 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4276 | if (!key || !value) |
| 4277 | goto failed1; |
| 4278 | if (PyDict_SetItem(result, key, value) == -1) |
| 4279 | goto failed1; |
| 4280 | Py_DECREF(key); |
| 4281 | Py_DECREF(value); |
| 4282 | } |
| 4283 | return result; |
| 4284 | failed1: |
| 4285 | Py_XDECREF(key); |
| 4286 | Py_XDECREF(value); |
| 4287 | Py_DECREF(result); |
| 4288 | return NULL; |
| 4289 | } |
| 4290 | |
| 4291 | /* Create a three-level trie */ |
| 4292 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 4293 | 16*count2 + 128*count3 - 1); |
| 4294 | if (!result) |
| 4295 | return PyErr_NoMemory(); |
| 4296 | PyObject_Init(result, &EncodingMapType); |
| 4297 | mresult = (struct encoding_map*)result; |
| 4298 | mresult->count2 = count2; |
| 4299 | mresult->count3 = count3; |
| 4300 | mlevel1 = mresult->level1; |
| 4301 | mlevel2 = mresult->level23; |
| 4302 | mlevel3 = mresult->level23 + 16*count2; |
| 4303 | memcpy(mlevel1, level1, 32); |
| 4304 | memset(mlevel2, 0xFF, 16*count2); |
| 4305 | memset(mlevel3, 0, 128*count3); |
| 4306 | count3 = 0; |
| 4307 | for (i = 1; i < 256; i++) { |
| 4308 | int o1, o2, o3, i2, i3; |
| 4309 | if (decode[i] == 0xFFFE) |
| 4310 | /* unmapped character */ |
| 4311 | continue; |
| 4312 | o1 = decode[i]>>11; |
| 4313 | o2 = (decode[i]>>7) & 0xF; |
| 4314 | i2 = 16*mlevel1[o1] + o2; |
| 4315 | if (mlevel2[i2] == 0xFF) |
| 4316 | mlevel2[i2] = count3++; |
| 4317 | o3 = decode[i] & 0x7F; |
| 4318 | i3 = 128*mlevel2[i2] + o3; |
| 4319 | mlevel3[i3] = i; |
| 4320 | } |
| 4321 | return result; |
| 4322 | } |
| 4323 | |
| 4324 | static int |
| 4325 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 4326 | { |
| 4327 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 4328 | int l1 = c>>11; |
| 4329 | int l2 = (c>>7) & 0xF; |
| 4330 | int l3 = c & 0x7F; |
| 4331 | int i; |
| 4332 | |
| 4333 | #ifdef Py_UNICODE_WIDE |
| 4334 | if (c > 0xFFFF) { |
| 4335 | return -1; |
| 4336 | } |
| 4337 | #endif |
| 4338 | if (c == 0) |
| 4339 | return 0; |
| 4340 | /* level 1*/ |
| 4341 | i = map->level1[l1]; |
| 4342 | if (i == 0xFF) { |
| 4343 | return -1; |
| 4344 | } |
| 4345 | /* level 2*/ |
| 4346 | i = map->level23[16*i+l2]; |
| 4347 | if (i == 0xFF) { |
| 4348 | return -1; |
| 4349 | } |
| 4350 | /* level 3 */ |
| 4351 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 4352 | if (i == 0) { |
| 4353 | return -1; |
| 4354 | } |
| 4355 | return i; |
| 4356 | } |
| 4357 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4358 | /* Lookup the character ch in the mapping. If the character |
| 4359 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 4360 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4361 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4362 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4363 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4364 | PyObject *x; |
| 4365 | |
| 4366 | if (w == NULL) |
| 4367 | return NULL; |
| 4368 | x = PyObject_GetItem(mapping, w); |
| 4369 | Py_DECREF(w); |
| 4370 | if (x == NULL) { |
| 4371 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4372 | /* No mapping found means: mapping is undefined. */ |
| 4373 | PyErr_Clear(); |
| 4374 | x = Py_None; |
| 4375 | Py_INCREF(x); |
| 4376 | return x; |
| 4377 | } else |
| 4378 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4379 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 4380 | else if (x == Py_None) |
| 4381 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4382 | else if (PyLong_Check(x)) { |
| 4383 | long value = PyLong_AS_LONG(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4384 | if (value < 0 || value > 255) { |
| 4385 | PyErr_SetString(PyExc_TypeError, |
| 4386 | "character mapping must be in range(256)"); |
| 4387 | Py_DECREF(x); |
| 4388 | return NULL; |
| 4389 | } |
| 4390 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4391 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4392 | else if (PyString_Check(x)) |
| 4393 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4394 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4395 | /* wrong return value */ |
Walter Dörwald | 580ceed | 2007-05-09 10:39:19 +0000 | [diff] [blame] | 4396 | PyErr_Format(PyExc_TypeError, |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 4397 | "character mapping must return integer, bytes or None, not %.400s", |
Walter Dörwald | 580ceed | 2007-05-09 10:39:19 +0000 | [diff] [blame] | 4398 | x->ob_type->tp_name); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4399 | Py_DECREF(x); |
| 4400 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4401 | } |
| 4402 | } |
| 4403 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4404 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4405 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4406 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4407 | Py_ssize_t outsize = PyString_GET_SIZE(*outobj); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4408 | /* exponentially overallocate to minimize reallocations */ |
| 4409 | if (requiredsize < 2*outsize) |
| 4410 | requiredsize = 2*outsize; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4411 | if (_PyString_Resize(outobj, requiredsize)) |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4412 | return -1; |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4413 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4414 | } |
| 4415 | |
| 4416 | typedef enum charmapencode_result { |
| 4417 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
| 4418 | }charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4419 | /* 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] | 4420 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4421 | space is available. Return a new reference to the object that |
| 4422 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 4423 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 4424 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4425 | static |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4426 | charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4427 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4428 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4429 | PyObject *rep; |
| 4430 | char *outstart; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4431 | Py_ssize_t outsize = PyString_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4432 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 4433 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4434 | int res = encoding_map_lookup(c, mapping); |
| 4435 | Py_ssize_t requiredsize = *outpos+1; |
| 4436 | if (res == -1) |
| 4437 | return enc_FAILED; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4438 | if (outsize<requiredsize) |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4439 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4440 | return enc_EXCEPTION; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4441 | outstart = PyString_AS_STRING(*outobj); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4442 | outstart[(*outpos)++] = (char)res; |
| 4443 | return enc_SUCCESS; |
| 4444 | } |
| 4445 | |
| 4446 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4447 | if (rep==NULL) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4448 | return enc_EXCEPTION; |
| 4449 | else if (rep==Py_None) { |
| 4450 | Py_DECREF(rep); |
| 4451 | return enc_FAILED; |
| 4452 | } else { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4453 | if (PyLong_Check(rep)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4454 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4455 | if (outsize<requiredsize) |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4456 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4457 | Py_DECREF(rep); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4458 | return enc_EXCEPTION; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4459 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4460 | outstart = PyString_AS_STRING(*outobj); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4461 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4462 | } |
| 4463 | else { |
| 4464 | const char *repchars = PyString_AS_STRING(rep); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4465 | Py_ssize_t repsize = PyString_GET_SIZE(rep); |
| 4466 | Py_ssize_t requiredsize = *outpos+repsize; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4467 | if (outsize<requiredsize) |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4468 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4469 | Py_DECREF(rep); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4470 | return enc_EXCEPTION; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4471 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4472 | outstart = PyString_AS_STRING(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4473 | memcpy(outstart + *outpos, repchars, repsize); |
| 4474 | *outpos += repsize; |
| 4475 | } |
| 4476 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4477 | Py_DECREF(rep); |
| 4478 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4479 | } |
| 4480 | |
| 4481 | /* handle an error in PyUnicode_EncodeCharmap |
| 4482 | Return 0 on success, -1 on error */ |
| 4483 | static |
| 4484 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4485 | 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] | 4486 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4487 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4488 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4489 | { |
| 4490 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4491 | Py_ssize_t repsize; |
| 4492 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4493 | Py_UNICODE *uni2; |
| 4494 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4495 | Py_ssize_t collstartpos = *inpos; |
| 4496 | Py_ssize_t collendpos = *inpos+1; |
| 4497 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4498 | char *encoding = "charmap"; |
| 4499 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4500 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4501 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4502 | /* find all unencodable characters */ |
| 4503 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4504 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 4505 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4506 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 4507 | if (res != -1) |
| 4508 | break; |
| 4509 | ++collendpos; |
| 4510 | continue; |
| 4511 | } |
| 4512 | |
| 4513 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 4514 | if (rep==NULL) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4515 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4516 | else if (rep!=Py_None) { |
| 4517 | Py_DECREF(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4518 | break; |
| 4519 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4520 | Py_DECREF(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4521 | ++collendpos; |
| 4522 | } |
| 4523 | /* cache callback name lookup |
| 4524 | * (if not done yet, i.e. it's the first error) */ |
| 4525 | if (*known_errorHandler==-1) { |
| 4526 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4527 | *known_errorHandler = 1; |
| 4528 | else if (!strcmp(errors, "replace")) |
| 4529 | *known_errorHandler = 2; |
| 4530 | else if (!strcmp(errors, "ignore")) |
| 4531 | *known_errorHandler = 3; |
| 4532 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4533 | *known_errorHandler = 4; |
| 4534 | else |
| 4535 | *known_errorHandler = 0; |
| 4536 | } |
| 4537 | switch (*known_errorHandler) { |
| 4538 | case 1: /* strict */ |
| 4539 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4540 | return -1; |
| 4541 | case 2: /* replace */ |
| 4542 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
| 4543 | x = charmapencode_output('?', mapping, res, respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4544 | if (x==enc_EXCEPTION) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4545 | return -1; |
| 4546 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4547 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4548 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4549 | return -1; |
| 4550 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4551 | } |
| 4552 | /* fall through */ |
| 4553 | case 3: /* ignore */ |
| 4554 | *inpos = collendpos; |
| 4555 | break; |
| 4556 | case 4: /* xmlcharrefreplace */ |
| 4557 | /* generate replacement (temporarily (mis)uses p) */ |
| 4558 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
| 4559 | char buffer[2+29+1+1]; |
| 4560 | char *cp; |
| 4561 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 4562 | for (cp = buffer; *cp; ++cp) { |
| 4563 | x = charmapencode_output(*cp, mapping, res, respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4564 | if (x==enc_EXCEPTION) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4565 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4566 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4567 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4568 | return -1; |
| 4569 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4570 | } |
| 4571 | } |
| 4572 | *inpos = collendpos; |
| 4573 | break; |
| 4574 | default: |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4575 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4576 | encoding, reason, p, size, exceptionObject, |
| 4577 | collstartpos, collendpos, &newpos); |
| 4578 | if (repunicode == NULL) |
| 4579 | return -1; |
| 4580 | /* generate replacement */ |
| 4581 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4582 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 4583 | x = charmapencode_output(*uni2, mapping, res, respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4584 | if (x==enc_EXCEPTION) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4585 | return -1; |
| 4586 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4587 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4588 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4589 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4590 | return -1; |
| 4591 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4592 | } |
| 4593 | *inpos = newpos; |
| 4594 | Py_DECREF(repunicode); |
| 4595 | } |
| 4596 | return 0; |
| 4597 | } |
| 4598 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4599 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4600 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4601 | PyObject *mapping, |
| 4602 | const char *errors) |
| 4603 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4604 | /* output object */ |
| 4605 | PyObject *res = NULL; |
| 4606 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4607 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4608 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4609 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4610 | PyObject *errorHandler = NULL; |
| 4611 | PyObject *exc = NULL; |
| 4612 | /* the following variable is used for caching string comparisons |
| 4613 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 4614 | * 3=ignore, 4=xmlcharrefreplace */ |
| 4615 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4616 | |
| 4617 | /* Default to Latin-1 */ |
| 4618 | if (mapping == NULL) |
| 4619 | return PyUnicode_EncodeLatin1(p, size, errors); |
| 4620 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4621 | /* allocate enough for a simple encoding without |
| 4622 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4623 | res = PyString_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4624 | if (res == NULL) |
| 4625 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 4626 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4627 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4628 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4629 | while (inpos<size) { |
| 4630 | /* try to encode it */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4631 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4632 | if (x==enc_EXCEPTION) /* error */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4633 | goto onError; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4634 | if (x==enc_FAILED) { /* unencodable character */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4635 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 4636 | &exc, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4637 | &known_errorHandler, &errorHandler, errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4638 | &res, &respos)) { |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 4639 | goto onError; |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 4640 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4641 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4642 | else |
| 4643 | /* done with this character => adjust input position */ |
| 4644 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4645 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4646 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4647 | /* Resize if we allocated to much */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4648 | if (respos<PyString_GET_SIZE(res)) |
| 4649 | _PyString_Resize(&res, respos); |
| 4650 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4651 | Py_XDECREF(exc); |
| 4652 | Py_XDECREF(errorHandler); |
| 4653 | return res; |
| 4654 | |
| 4655 | onError: |
| 4656 | Py_XDECREF(res); |
| 4657 | Py_XDECREF(exc); |
| 4658 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4659 | return NULL; |
| 4660 | } |
| 4661 | |
| 4662 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
| 4663 | PyObject *mapping) |
| 4664 | { |
| 4665 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
| 4666 | PyErr_BadArgument(); |
| 4667 | return NULL; |
| 4668 | } |
| 4669 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
| 4670 | PyUnicode_GET_SIZE(unicode), |
| 4671 | mapping, |
| 4672 | NULL); |
| 4673 | } |
| 4674 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4675 | /* create or adjust a UnicodeTranslateError */ |
| 4676 | static void make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4677 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4678 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4679 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4680 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4681 | if (*exceptionObject == NULL) { |
| 4682 | *exceptionObject = PyUnicodeTranslateError_Create( |
| 4683 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4684 | } |
| 4685 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4686 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 4687 | goto onError; |
| 4688 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 4689 | goto onError; |
| 4690 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 4691 | goto onError; |
| 4692 | return; |
| 4693 | onError: |
| 4694 | Py_DECREF(*exceptionObject); |
| 4695 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4696 | } |
| 4697 | } |
| 4698 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4699 | /* raises a UnicodeTranslateError */ |
| 4700 | static void raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4701 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4702 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4703 | const char *reason) |
| 4704 | { |
| 4705 | make_translate_exception(exceptionObject, |
| 4706 | unicode, size, startpos, endpos, reason); |
| 4707 | if (*exceptionObject != NULL) |
| 4708 | PyCodec_StrictErrors(*exceptionObject); |
| 4709 | } |
| 4710 | |
| 4711 | /* error handling callback helper: |
| 4712 | build arguments, call the callback and check the arguments, |
| 4713 | put the result into newpos and return the replacement string, which |
| 4714 | has to be freed by the caller */ |
| 4715 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
| 4716 | PyObject **errorHandler, |
| 4717 | const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4718 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 4719 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4720 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4721 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4722 | 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] | 4723 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4724 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4725 | PyObject *restuple; |
| 4726 | PyObject *resunicode; |
| 4727 | |
| 4728 | if (*errorHandler == NULL) { |
| 4729 | *errorHandler = PyCodec_LookupError(errors); |
| 4730 | if (*errorHandler == NULL) |
| 4731 | return NULL; |
| 4732 | } |
| 4733 | |
| 4734 | make_translate_exception(exceptionObject, |
| 4735 | unicode, size, startpos, endpos, reason); |
| 4736 | if (*exceptionObject == NULL) |
| 4737 | return NULL; |
| 4738 | |
| 4739 | restuple = PyObject_CallFunctionObjArgs( |
| 4740 | *errorHandler, *exceptionObject, NULL); |
| 4741 | if (restuple == NULL) |
| 4742 | return NULL; |
| 4743 | if (!PyTuple_Check(restuple)) { |
| 4744 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 4745 | Py_DECREF(restuple); |
| 4746 | return NULL; |
| 4747 | } |
| 4748 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4749 | &resunicode, &i_newpos)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4750 | Py_DECREF(restuple); |
| 4751 | return NULL; |
| 4752 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4753 | if (i_newpos<0) |
| 4754 | *newpos = size+i_newpos; |
| 4755 | else |
| 4756 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4757 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 4758 | 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] | 4759 | Py_DECREF(restuple); |
| 4760 | return NULL; |
| 4761 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4762 | Py_INCREF(resunicode); |
| 4763 | Py_DECREF(restuple); |
| 4764 | return resunicode; |
| 4765 | } |
| 4766 | |
| 4767 | /* Lookup the character ch in the mapping and put the result in result, |
| 4768 | which must be decrefed by the caller. |
| 4769 | Return 0 on success, -1 on error */ |
| 4770 | static |
| 4771 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 4772 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4773 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4774 | PyObject *x; |
| 4775 | |
| 4776 | if (w == NULL) |
| 4777 | return -1; |
| 4778 | x = PyObject_GetItem(mapping, w); |
| 4779 | Py_DECREF(w); |
| 4780 | if (x == NULL) { |
| 4781 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4782 | /* No mapping found means: use 1:1 mapping. */ |
| 4783 | PyErr_Clear(); |
| 4784 | *result = NULL; |
| 4785 | return 0; |
| 4786 | } else |
| 4787 | return -1; |
| 4788 | } |
| 4789 | else if (x == Py_None) { |
| 4790 | *result = x; |
| 4791 | return 0; |
| 4792 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4793 | else if (PyLong_Check(x)) { |
| 4794 | long value = PyLong_AS_LONG(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4795 | long max = PyUnicode_GetMax(); |
| 4796 | if (value < 0 || value > max) { |
| 4797 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 4798 | "character mapping must be in range(0x%x)", max+1); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4799 | Py_DECREF(x); |
| 4800 | return -1; |
| 4801 | } |
| 4802 | *result = x; |
| 4803 | return 0; |
| 4804 | } |
| 4805 | else if (PyUnicode_Check(x)) { |
| 4806 | *result = x; |
| 4807 | return 0; |
| 4808 | } |
| 4809 | else { |
| 4810 | /* wrong return value */ |
| 4811 | PyErr_SetString(PyExc_TypeError, |
| 4812 | "character mapping must return integer, None or unicode"); |
Walter Dörwald | 150523e | 2003-08-15 16:52:19 +0000 | [diff] [blame] | 4813 | Py_DECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4814 | return -1; |
| 4815 | } |
| 4816 | } |
| 4817 | /* ensure that *outobj is at least requiredsize characters long, |
| 4818 | if not reallocate and adjust various state variables. |
| 4819 | Return 0 on success, -1 on error */ |
| 4820 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4821 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4822 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4823 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4824 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4825 | if (requiredsize > oldsize) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4826 | /* remember old output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4827 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4828 | /* exponentially overallocate to minimize reallocations */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4829 | if (requiredsize < 2 * oldsize) |
| 4830 | requiredsize = 2 * oldsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 4831 | if (_PyUnicode_Resize(outobj, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4832 | return -1; |
| 4833 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4834 | } |
| 4835 | return 0; |
| 4836 | } |
| 4837 | /* lookup the character, put the result in the output string and adjust |
| 4838 | various state variables. Return a new reference to the object that |
| 4839 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 4840 | undefined (in which case no character was written). |
| 4841 | The called must decref result. |
| 4842 | Return 0 on success, -1 on error. */ |
| 4843 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4844 | 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] | 4845 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4846 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4847 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4848 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4849 | return -1; |
| 4850 | if (*res==NULL) { |
| 4851 | /* not found => default to 1:1 mapping */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4852 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4853 | } |
| 4854 | else if (*res==Py_None) |
| 4855 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4856 | else if (PyLong_Check(*res)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4857 | /* no overflow check, because we know that the space is enough */ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4858 | *(*outp)++ = (Py_UNICODE)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4859 | } |
| 4860 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4861 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4862 | if (repsize==1) { |
| 4863 | /* no overflow check, because we know that the space is enough */ |
| 4864 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 4865 | } |
| 4866 | else if (repsize!=0) { |
| 4867 | /* more than one character */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4868 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
Walter Dörwald | cd736e7 | 2004-02-05 17:36:00 +0000 | [diff] [blame] | 4869 | (insize - (curinp-startinp)) + |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4870 | repsize - 1; |
| 4871 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4872 | return -1; |
| 4873 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 4874 | *outp += repsize; |
| 4875 | } |
| 4876 | } |
| 4877 | else |
| 4878 | return -1; |
| 4879 | return 0; |
| 4880 | } |
| 4881 | |
| 4882 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4883 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4884 | PyObject *mapping, |
| 4885 | const char *errors) |
| 4886 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4887 | /* output object */ |
| 4888 | PyObject *res = NULL; |
| 4889 | /* pointers to the beginning and end+1 of input */ |
| 4890 | const Py_UNICODE *startp = p; |
| 4891 | const Py_UNICODE *endp = p + size; |
| 4892 | /* pointer into the output */ |
| 4893 | Py_UNICODE *str; |
| 4894 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4895 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4896 | char *reason = "character maps to <undefined>"; |
| 4897 | PyObject *errorHandler = NULL; |
| 4898 | PyObject *exc = NULL; |
| 4899 | /* the following variable is used for caching string comparisons |
| 4900 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 4901 | * 3=ignore, 4=xmlcharrefreplace */ |
| 4902 | int known_errorHandler = -1; |
| 4903 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4904 | if (mapping == NULL) { |
| 4905 | PyErr_BadArgument(); |
| 4906 | return NULL; |
| 4907 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4908 | |
| 4909 | /* allocate enough for a simple 1:1 translation without |
| 4910 | replacements, if we need more, we'll resize */ |
| 4911 | res = PyUnicode_FromUnicode(NULL, size); |
| 4912 | if (res == NULL) |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4913 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4914 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4915 | return res; |
| 4916 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4917 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4918 | while (p<endp) { |
| 4919 | /* try to encode it */ |
| 4920 | PyObject *x = NULL; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4921 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4922 | Py_XDECREF(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4923 | goto onError; |
| 4924 | } |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 4925 | Py_XDECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4926 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 4927 | ++p; |
| 4928 | else { /* untranslatable character */ |
| 4929 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4930 | Py_ssize_t repsize; |
| 4931 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4932 | Py_UNICODE *uni2; |
| 4933 | /* startpos for collecting untranslatable chars */ |
| 4934 | const Py_UNICODE *collstart = p; |
| 4935 | const Py_UNICODE *collend = p+1; |
| 4936 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4937 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4938 | /* find all untranslatable characters */ |
| 4939 | while (collend < endp) { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4940 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4941 | goto onError; |
| 4942 | Py_XDECREF(x); |
| 4943 | if (x!=Py_None) |
| 4944 | break; |
| 4945 | ++collend; |
| 4946 | } |
| 4947 | /* cache callback name lookup |
| 4948 | * (if not done yet, i.e. it's the first error) */ |
| 4949 | if (known_errorHandler==-1) { |
| 4950 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4951 | known_errorHandler = 1; |
| 4952 | else if (!strcmp(errors, "replace")) |
| 4953 | known_errorHandler = 2; |
| 4954 | else if (!strcmp(errors, "ignore")) |
| 4955 | known_errorHandler = 3; |
| 4956 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4957 | known_errorHandler = 4; |
| 4958 | else |
| 4959 | known_errorHandler = 0; |
| 4960 | } |
| 4961 | switch (known_errorHandler) { |
| 4962 | case 1: /* strict */ |
| 4963 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
| 4964 | goto onError; |
| 4965 | case 2: /* replace */ |
| 4966 | /* No need to check for space, this is a 1:1 replacement */ |
| 4967 | for (coll = collstart; coll<collend; ++coll) |
| 4968 | *str++ = '?'; |
| 4969 | /* fall through */ |
| 4970 | case 3: /* ignore */ |
| 4971 | p = collend; |
| 4972 | break; |
| 4973 | case 4: /* xmlcharrefreplace */ |
| 4974 | /* generate replacement (temporarily (mis)uses p) */ |
| 4975 | for (p = collstart; p < collend; ++p) { |
| 4976 | char buffer[2+29+1+1]; |
| 4977 | char *cp; |
| 4978 | sprintf(buffer, "&#%d;", (int)*p); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4979 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4980 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 4981 | goto onError; |
| 4982 | for (cp = buffer; *cp; ++cp) |
| 4983 | *str++ = *cp; |
| 4984 | } |
| 4985 | p = collend; |
| 4986 | break; |
| 4987 | default: |
| 4988 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 4989 | reason, startp, size, &exc, |
| 4990 | collstart-startp, collend-startp, &newpos); |
| 4991 | if (repunicode == NULL) |
| 4992 | goto onError; |
| 4993 | /* generate replacement */ |
| 4994 | repsize = PyUnicode_GET_SIZE(repunicode); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4995 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4996 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 4997 | Py_DECREF(repunicode); |
| 4998 | goto onError; |
| 4999 | } |
| 5000 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 5001 | *str++ = *uni2; |
| 5002 | p = startp + newpos; |
| 5003 | Py_DECREF(repunicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5004 | } |
| 5005 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5006 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5007 | /* Resize if we allocated to much */ |
| 5008 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5009 | if (respos<PyUnicode_GET_SIZE(res)) { |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 5010 | if (_PyUnicode_Resize(&res, respos) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 5011 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5012 | } |
| 5013 | Py_XDECREF(exc); |
| 5014 | Py_XDECREF(errorHandler); |
| 5015 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5016 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5017 | onError: |
| 5018 | Py_XDECREF(res); |
| 5019 | Py_XDECREF(exc); |
| 5020 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5021 | return NULL; |
| 5022 | } |
| 5023 | |
| 5024 | PyObject *PyUnicode_Translate(PyObject *str, |
| 5025 | PyObject *mapping, |
| 5026 | const char *errors) |
| 5027 | { |
| 5028 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5029 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5030 | str = PyUnicode_FromObject(str); |
| 5031 | if (str == NULL) |
| 5032 | goto onError; |
| 5033 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
| 5034 | PyUnicode_GET_SIZE(str), |
| 5035 | mapping, |
| 5036 | errors); |
| 5037 | Py_DECREF(str); |
| 5038 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5039 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5040 | onError: |
| 5041 | Py_XDECREF(str); |
| 5042 | return NULL; |
| 5043 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5044 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5045 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 5046 | |
| 5047 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5048 | Py_ssize_t length, |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5049 | char *output, |
| 5050 | const char *errors) |
| 5051 | { |
| 5052 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5053 | PyObject *errorHandler = NULL; |
| 5054 | PyObject *exc = NULL; |
| 5055 | const char *encoding = "decimal"; |
| 5056 | const char *reason = "invalid decimal Unicode string"; |
| 5057 | /* the following variable is used for caching string comparisons |
| 5058 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 5059 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5060 | |
| 5061 | if (output == NULL) { |
| 5062 | PyErr_BadArgument(); |
| 5063 | return -1; |
| 5064 | } |
| 5065 | |
| 5066 | p = s; |
| 5067 | end = s + length; |
| 5068 | while (p < end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5069 | register Py_UNICODE ch = *p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5070 | int decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5071 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5072 | Py_ssize_t repsize; |
| 5073 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5074 | Py_UNICODE *uni2; |
| 5075 | Py_UNICODE *collstart; |
| 5076 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5077 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5078 | if (Py_UNICODE_ISSPACE(ch)) { |
| 5079 | *output++ = ' '; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5080 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5081 | continue; |
| 5082 | } |
| 5083 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5084 | if (decimal >= 0) { |
| 5085 | *output++ = '0' + decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5086 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5087 | continue; |
| 5088 | } |
Guido van Rossum | ba47704 | 2000-04-06 18:18:10 +0000 | [diff] [blame] | 5089 | if (0 < ch && ch < 256) { |
Guido van Rossum | 42c29aa | 2000-05-03 23:58:29 +0000 | [diff] [blame] | 5090 | *output++ = (char)ch; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5091 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5092 | continue; |
| 5093 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5094 | /* All other characters are considered unencodable */ |
| 5095 | collstart = p; |
| 5096 | collend = p+1; |
| 5097 | while (collend < end) { |
| 5098 | if ((0 < *collend && *collend < 256) || |
| 5099 | !Py_UNICODE_ISSPACE(*collend) || |
| 5100 | Py_UNICODE_TODECIMAL(*collend)) |
| 5101 | break; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5102 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5103 | /* cache callback name lookup |
| 5104 | * (if not done yet, i.e. it's the first error) */ |
| 5105 | if (known_errorHandler==-1) { |
| 5106 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5107 | known_errorHandler = 1; |
| 5108 | else if (!strcmp(errors, "replace")) |
| 5109 | known_errorHandler = 2; |
| 5110 | else if (!strcmp(errors, "ignore")) |
| 5111 | known_errorHandler = 3; |
| 5112 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5113 | known_errorHandler = 4; |
| 5114 | else |
| 5115 | known_errorHandler = 0; |
| 5116 | } |
| 5117 | switch (known_errorHandler) { |
| 5118 | case 1: /* strict */ |
| 5119 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 5120 | goto onError; |
| 5121 | case 2: /* replace */ |
| 5122 | for (p = collstart; p < collend; ++p) |
| 5123 | *output++ = '?'; |
| 5124 | /* fall through */ |
| 5125 | case 3: /* ignore */ |
| 5126 | p = collend; |
| 5127 | break; |
| 5128 | case 4: /* xmlcharrefreplace */ |
| 5129 | /* generate replacement (temporarily (mis)uses p) */ |
| 5130 | for (p = collstart; p < collend; ++p) |
| 5131 | output += sprintf(output, "&#%d;", (int)*p); |
| 5132 | p = collend; |
| 5133 | break; |
| 5134 | default: |
| 5135 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 5136 | encoding, reason, s, length, &exc, |
| 5137 | collstart-s, collend-s, &newpos); |
| 5138 | if (repunicode == NULL) |
| 5139 | goto onError; |
| 5140 | /* generate replacement */ |
| 5141 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5142 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 5143 | Py_UNICODE ch = *uni2; |
| 5144 | if (Py_UNICODE_ISSPACE(ch)) |
| 5145 | *output++ = ' '; |
| 5146 | else { |
| 5147 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5148 | if (decimal >= 0) |
| 5149 | *output++ = '0' + decimal; |
| 5150 | else if (0 < ch && ch < 256) |
| 5151 | *output++ = (char)ch; |
| 5152 | else { |
| 5153 | Py_DECREF(repunicode); |
| 5154 | raise_encode_exception(&exc, encoding, |
| 5155 | s, length, collstart-s, collend-s, reason); |
| 5156 | goto onError; |
| 5157 | } |
| 5158 | } |
| 5159 | } |
| 5160 | p = s + newpos; |
| 5161 | Py_DECREF(repunicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5162 | } |
| 5163 | } |
| 5164 | /* 0-terminate the output string */ |
| 5165 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5166 | Py_XDECREF(exc); |
| 5167 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5168 | return 0; |
| 5169 | |
| 5170 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5171 | Py_XDECREF(exc); |
| 5172 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5173 | return -1; |
| 5174 | } |
| 5175 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5176 | /* --- Helpers ------------------------------------------------------------ */ |
| 5177 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 5178 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5179 | #include "stringlib/fastsearch.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5180 | #include "stringlib/count.h" |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 5181 | /* Include _ParseTupleFinds from find.h */ |
| 5182 | #define FROM_UNICODE |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5183 | #include "stringlib/find.h" |
| 5184 | #include "stringlib/partition.h" |
| 5185 | |
| 5186 | /* helper macro to fixup start/end slice values */ |
| 5187 | #define FIX_START_END(obj) \ |
| 5188 | if (start < 0) \ |
| 5189 | start += (obj)->length; \ |
| 5190 | if (start < 0) \ |
| 5191 | start = 0; \ |
| 5192 | if (end > (obj)->length) \ |
| 5193 | end = (obj)->length; \ |
| 5194 | if (end < 0) \ |
| 5195 | end += (obj)->length; \ |
| 5196 | if (end < 0) \ |
| 5197 | end = 0; |
| 5198 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5199 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5200 | PyObject *substr, |
| 5201 | Py_ssize_t start, |
| 5202 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5203 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5204 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5205 | PyUnicodeObject* str_obj; |
| 5206 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5207 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5208 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 5209 | if (!str_obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5210 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5211 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 5212 | if (!sub_obj) { |
| 5213 | Py_DECREF(str_obj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5214 | return -1; |
| 5215 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5216 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5217 | FIX_START_END(str_obj); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5218 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5219 | result = stringlib_count( |
| 5220 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length |
| 5221 | ); |
| 5222 | |
| 5223 | Py_DECREF(sub_obj); |
| 5224 | Py_DECREF(str_obj); |
| 5225 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5226 | return result; |
| 5227 | } |
| 5228 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5229 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5230 | PyObject *sub, |
| 5231 | Py_ssize_t start, |
| 5232 | Py_ssize_t end, |
| 5233 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 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); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5238 | if (!str) |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 5239 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5240 | sub = PyUnicode_FromObject(sub); |
| 5241 | if (!sub) { |
Marc-André Lemburg | 4164439 | 2002-05-29 13:46:29 +0000 | [diff] [blame] | 5242 | Py_DECREF(str); |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 5243 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5244 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5245 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5246 | if (direction > 0) |
| 5247 | result = stringlib_find_slice( |
| 5248 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5249 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5250 | start, end |
| 5251 | ); |
| 5252 | else |
| 5253 | result = stringlib_rfind_slice( |
| 5254 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5255 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5256 | start, end |
| 5257 | ); |
| 5258 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5259 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5260 | Py_DECREF(sub); |
| 5261 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5262 | return result; |
| 5263 | } |
| 5264 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5265 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5266 | int tailmatch(PyUnicodeObject *self, |
| 5267 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5268 | Py_ssize_t start, |
| 5269 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5270 | int direction) |
| 5271 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5272 | if (substring->length == 0) |
| 5273 | return 1; |
| 5274 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5275 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5276 | |
| 5277 | end -= substring->length; |
| 5278 | if (end < start) |
| 5279 | return 0; |
| 5280 | |
| 5281 | if (direction > 0) { |
| 5282 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 5283 | return 1; |
| 5284 | } else { |
| 5285 | if (Py_UNICODE_MATCH(self, start, substring)) |
| 5286 | return 1; |
| 5287 | } |
| 5288 | |
| 5289 | return 0; |
| 5290 | } |
| 5291 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5292 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5293 | PyObject *substr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5294 | Py_ssize_t start, |
| 5295 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5296 | int direction) |
| 5297 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5298 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5299 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5300 | str = PyUnicode_FromObject(str); |
| 5301 | if (str == NULL) |
| 5302 | return -1; |
| 5303 | substr = PyUnicode_FromObject(substr); |
| 5304 | if (substr == NULL) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5305 | Py_DECREF(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5306 | return -1; |
| 5307 | } |
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 | result = tailmatch((PyUnicodeObject *)str, |
| 5310 | (PyUnicodeObject *)substr, |
| 5311 | start, end, direction); |
| 5312 | Py_DECREF(str); |
| 5313 | Py_DECREF(substr); |
| 5314 | return result; |
| 5315 | } |
| 5316 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5317 | /* Apply fixfct filter to the Unicode object self and return a |
| 5318 | reference to the modified object */ |
| 5319 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5320 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5321 | PyObject *fixup(PyUnicodeObject *self, |
| 5322 | int (*fixfct)(PyUnicodeObject *s)) |
| 5323 | { |
| 5324 | |
| 5325 | PyUnicodeObject *u; |
| 5326 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5327 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5328 | if (u == NULL) |
| 5329 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5330 | |
| 5331 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5332 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5333 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5334 | /* fixfct should return TRUE if it modified the buffer. If |
| 5335 | FALSE, return a reference to the original buffer instead |
| 5336 | (to save space, not time) */ |
| 5337 | Py_INCREF(self); |
| 5338 | Py_DECREF(u); |
| 5339 | return (PyObject*) self; |
| 5340 | } |
| 5341 | return (PyObject*) u; |
| 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 fixupper(PyUnicodeObject *self) |
| 5346 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5347 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +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 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5351 | while (len-- > 0) { |
| 5352 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5353 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5354 | ch = Py_UNICODE_TOUPPER(*s); |
| 5355 | if (ch != *s) { |
| 5356 | status = 1; |
| 5357 | *s = ch; |
| 5358 | } |
| 5359 | s++; |
| 5360 | } |
| 5361 | |
| 5362 | return status; |
| 5363 | } |
| 5364 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5365 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5366 | int fixlower(PyUnicodeObject *self) |
| 5367 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5368 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5369 | Py_UNICODE *s = self->str; |
| 5370 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5371 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5372 | while (len-- > 0) { |
| 5373 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5374 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5375 | ch = Py_UNICODE_TOLOWER(*s); |
| 5376 | if (ch != *s) { |
| 5377 | status = 1; |
| 5378 | *s = ch; |
| 5379 | } |
| 5380 | s++; |
| 5381 | } |
| 5382 | |
| 5383 | return status; |
| 5384 | } |
| 5385 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5386 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5387 | int fixswapcase(PyUnicodeObject *self) |
| 5388 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5389 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5390 | Py_UNICODE *s = self->str; |
| 5391 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5392 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5393 | while (len-- > 0) { |
| 5394 | if (Py_UNICODE_ISUPPER(*s)) { |
| 5395 | *s = Py_UNICODE_TOLOWER(*s); |
| 5396 | status = 1; |
| 5397 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 5398 | *s = Py_UNICODE_TOUPPER(*s); |
| 5399 | status = 1; |
| 5400 | } |
| 5401 | s++; |
| 5402 | } |
| 5403 | |
| 5404 | return status; |
| 5405 | } |
| 5406 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5407 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5408 | int fixcapitalize(PyUnicodeObject *self) |
| 5409 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5410 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5411 | Py_UNICODE *s = self->str; |
| 5412 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5413 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5414 | if (len == 0) |
| 5415 | return 0; |
| 5416 | if (Py_UNICODE_ISLOWER(*s)) { |
| 5417 | *s = Py_UNICODE_TOUPPER(*s); |
| 5418 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5419 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5420 | s++; |
| 5421 | while (--len > 0) { |
| 5422 | if (Py_UNICODE_ISUPPER(*s)) { |
| 5423 | *s = Py_UNICODE_TOLOWER(*s); |
| 5424 | status = 1; |
| 5425 | } |
| 5426 | s++; |
| 5427 | } |
| 5428 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5429 | } |
| 5430 | |
| 5431 | static |
| 5432 | int fixtitle(PyUnicodeObject *self) |
| 5433 | { |
| 5434 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5435 | register Py_UNICODE *e; |
| 5436 | int previous_is_cased; |
| 5437 | |
| 5438 | /* Shortcut for single character strings */ |
| 5439 | if (PyUnicode_GET_SIZE(self) == 1) { |
| 5440 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 5441 | if (*p != ch) { |
| 5442 | *p = ch; |
| 5443 | return 1; |
| 5444 | } |
| 5445 | else |
| 5446 | return 0; |
| 5447 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5448 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5449 | e = p + PyUnicode_GET_SIZE(self); |
| 5450 | previous_is_cased = 0; |
| 5451 | for (; p < e; p++) { |
| 5452 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5453 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5454 | if (previous_is_cased) |
| 5455 | *p = Py_UNICODE_TOLOWER(ch); |
| 5456 | else |
| 5457 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5458 | |
| 5459 | if (Py_UNICODE_ISLOWER(ch) || |
| 5460 | Py_UNICODE_ISUPPER(ch) || |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5461 | Py_UNICODE_ISTITLE(ch)) |
| 5462 | previous_is_cased = 1; |
| 5463 | else |
| 5464 | previous_is_cased = 0; |
| 5465 | } |
| 5466 | return 1; |
| 5467 | } |
| 5468 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5469 | PyObject * |
| 5470 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5471 | { |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5472 | PyObject *internal_separator = NULL; |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 5473 | const Py_UNICODE blank = ' '; |
| 5474 | const Py_UNICODE *sep = ␣ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5475 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5476 | PyUnicodeObject *res = NULL; /* the result */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5477 | Py_ssize_t res_alloc = 100; /* # allocated bytes for string in res */ |
| 5478 | Py_ssize_t res_used; /* # used bytes */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5479 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 5480 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5481 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5482 | PyObject *item; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5483 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5484 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5485 | fseq = PySequence_Fast(seq, ""); |
| 5486 | if (fseq == NULL) { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5487 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5488 | } |
| 5489 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5490 | /* Grrrr. A codec may be invoked to convert str objects to |
| 5491 | * Unicode, and so it's possible to call back into Python code |
| 5492 | * during PyUnicode_FromObject(), and so it's possible for a sick |
| 5493 | * codec to change the size of fseq (if seq is a list). Therefore |
| 5494 | * we have to keep refetching the size -- can't assume seqlen |
| 5495 | * is invariant. |
| 5496 | */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5497 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 5498 | /* If empty sequence, return u"". */ |
| 5499 | if (seqlen == 0) { |
| 5500 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 5501 | goto Done; |
| 5502 | } |
| 5503 | /* If singleton sequence with an exact Unicode, return that. */ |
| 5504 | if (seqlen == 1) { |
| 5505 | item = PySequence_Fast_GET_ITEM(fseq, 0); |
| 5506 | if (PyUnicode_CheckExact(item)) { |
| 5507 | Py_INCREF(item); |
| 5508 | res = (PyUnicodeObject *)item; |
| 5509 | goto Done; |
| 5510 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5511 | } |
| 5512 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5513 | /* At least two items to join, or one that isn't exact Unicode. */ |
| 5514 | if (seqlen > 1) { |
| 5515 | /* Set up sep and seplen -- they're needed. */ |
| 5516 | if (separator == NULL) { |
| 5517 | sep = ␣ |
| 5518 | seplen = 1; |
| 5519 | } |
| 5520 | else { |
| 5521 | internal_separator = PyUnicode_FromObject(separator); |
| 5522 | if (internal_separator == NULL) |
| 5523 | goto onError; |
| 5524 | sep = PyUnicode_AS_UNICODE(internal_separator); |
| 5525 | seplen = PyUnicode_GET_SIZE(internal_separator); |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5526 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 5527 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5528 | } |
| 5529 | } |
| 5530 | |
| 5531 | /* Get space. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5532 | res = _PyUnicode_New(res_alloc); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5533 | if (res == NULL) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5534 | goto onError; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5535 | res_p = PyUnicode_AS_UNICODE(res); |
| 5536 | res_used = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5537 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5538 | for (i = 0; i < seqlen; ++i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5539 | Py_ssize_t itemlen; |
| 5540 | Py_ssize_t new_res_used; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5541 | |
| 5542 | item = PySequence_Fast_GET_ITEM(fseq, i); |
| 5543 | /* Convert item to Unicode. */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5544 | if (!PyUnicode_Check(item)) { |
| 5545 | PyErr_Format(PyExc_TypeError, |
| 5546 | "sequence item %zd: expected str instance," |
| 5547 | " %.80s found", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5548 | i, Py_TYPE(item)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5549 | goto onError; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5550 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5551 | item = PyUnicode_FromObject(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5552 | if (item == NULL) |
| 5553 | goto onError; |
| 5554 | /* We own a reference to item from here on. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5555 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5556 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 5557 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 5558 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5559 | /* 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] | 5560 | itemlen = PyUnicode_GET_SIZE(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5561 | new_res_used = res_used + itemlen; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5562 | if (new_res_used < 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5563 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5564 | if (i < seqlen - 1) { |
| 5565 | new_res_used += seplen; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5566 | if (new_res_used < 0) |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5567 | goto Overflow; |
| 5568 | } |
| 5569 | if (new_res_used > res_alloc) { |
| 5570 | /* double allocated size until it's big enough */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5571 | do { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5572 | res_alloc += res_alloc; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5573 | if (res_alloc <= 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5574 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5575 | } while (new_res_used > res_alloc); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5576 | if (_PyUnicode_Resize(&res, res_alloc) < 0) { |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 5577 | Py_DECREF(item); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5578 | goto onError; |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 5579 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5580 | res_p = PyUnicode_AS_UNICODE(res) + res_used; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5581 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5582 | |
| 5583 | /* Copy item, and maybe the separator. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5584 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5585 | res_p += itemlen; |
| 5586 | if (i < seqlen - 1) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5587 | Py_UNICODE_COPY(res_p, sep, seplen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5588 | res_p += seplen; |
| 5589 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5590 | Py_DECREF(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5591 | res_used = new_res_used; |
| 5592 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5593 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5594 | /* Shrink res to match the used area; this probably can't fail, |
| 5595 | * but it's cheap to check. |
| 5596 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5597 | if (_PyUnicode_Resize(&res, res_used) < 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5598 | goto onError; |
| 5599 | |
| 5600 | Done: |
| 5601 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5602 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5603 | return (PyObject *)res; |
| 5604 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5605 | Overflow: |
| 5606 | PyErr_SetString(PyExc_OverflowError, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5607 | "join() result is too long for a Python string"); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5608 | Py_DECREF(item); |
| 5609 | /* fall through */ |
| 5610 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5611 | onError: |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5612 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5613 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5614 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5615 | return NULL; |
| 5616 | } |
| 5617 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5618 | static |
| 5619 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5620 | Py_ssize_t left, |
| 5621 | Py_ssize_t right, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5622 | Py_UNICODE fill) |
| 5623 | { |
| 5624 | PyUnicodeObject *u; |
| 5625 | |
| 5626 | if (left < 0) |
| 5627 | left = 0; |
| 5628 | if (right < 0) |
| 5629 | right = 0; |
| 5630 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5631 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5632 | Py_INCREF(self); |
| 5633 | return self; |
| 5634 | } |
| 5635 | |
| 5636 | u = _PyUnicode_New(left + self->length + right); |
| 5637 | if (u) { |
| 5638 | if (left) |
| 5639 | Py_UNICODE_FILL(u->str, fill, left); |
| 5640 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 5641 | if (right) |
| 5642 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 5643 | } |
| 5644 | |
| 5645 | return u; |
| 5646 | } |
| 5647 | |
| 5648 | #define SPLIT_APPEND(data, left, right) \ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5649 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5650 | if (!str) \ |
| 5651 | goto onError; \ |
| 5652 | if (PyList_Append(list, str)) { \ |
| 5653 | Py_DECREF(str); \ |
| 5654 | goto onError; \ |
| 5655 | } \ |
| 5656 | else \ |
| 5657 | Py_DECREF(str); |
| 5658 | |
| 5659 | static |
| 5660 | PyObject *split_whitespace(PyUnicodeObject *self, |
| 5661 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5662 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5663 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5664 | register Py_ssize_t i; |
| 5665 | register Py_ssize_t j; |
| 5666 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5667 | PyObject *str; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5668 | register const Py_UNICODE *buf = self->str; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5669 | |
| 5670 | for (i = j = 0; i < len; ) { |
| 5671 | /* find a token */ |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5672 | while (i < len && Py_UNICODE_ISSPACE(buf[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5673 | i++; |
| 5674 | j = i; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5675 | while (i < len && !Py_UNICODE_ISSPACE(buf[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5676 | i++; |
| 5677 | if (j < i) { |
| 5678 | if (maxcount-- <= 0) |
| 5679 | break; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5680 | SPLIT_APPEND(buf, j, i); |
| 5681 | while (i < len && Py_UNICODE_ISSPACE(buf[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5682 | i++; |
| 5683 | j = i; |
| 5684 | } |
| 5685 | } |
| 5686 | if (j < len) { |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5687 | SPLIT_APPEND(buf, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5688 | } |
| 5689 | return list; |
| 5690 | |
| 5691 | onError: |
| 5692 | Py_DECREF(list); |
| 5693 | return NULL; |
| 5694 | } |
| 5695 | |
| 5696 | PyObject *PyUnicode_Splitlines(PyObject *string, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5697 | int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5698 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5699 | register Py_ssize_t i; |
| 5700 | register Py_ssize_t j; |
| 5701 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5702 | PyObject *list; |
| 5703 | PyObject *str; |
| 5704 | Py_UNICODE *data; |
| 5705 | |
| 5706 | string = PyUnicode_FromObject(string); |
| 5707 | if (string == NULL) |
| 5708 | return NULL; |
| 5709 | data = PyUnicode_AS_UNICODE(string); |
| 5710 | len = PyUnicode_GET_SIZE(string); |
| 5711 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5712 | list = PyList_New(0); |
| 5713 | if (!list) |
| 5714 | goto onError; |
| 5715 | |
| 5716 | for (i = j = 0; i < len; ) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5717 | Py_ssize_t eol; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5718 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5719 | /* Find a line and append it */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5720 | while (i < len && !BLOOM_LINEBREAK(data[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5721 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5722 | |
| 5723 | /* Skip the line break reading CRLF as one line break */ |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5724 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5725 | if (i < len) { |
| 5726 | if (data[i] == '\r' && i + 1 < len && |
| 5727 | data[i+1] == '\n') |
| 5728 | i += 2; |
| 5729 | else |
| 5730 | i++; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5731 | if (keepends) |
| 5732 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5733 | } |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5734 | SPLIT_APPEND(data, j, eol); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5735 | j = i; |
| 5736 | } |
| 5737 | if (j < len) { |
| 5738 | SPLIT_APPEND(data, j, len); |
| 5739 | } |
| 5740 | |
| 5741 | Py_DECREF(string); |
| 5742 | return list; |
| 5743 | |
| 5744 | onError: |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5745 | Py_XDECREF(list); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5746 | Py_DECREF(string); |
| 5747 | return NULL; |
| 5748 | } |
| 5749 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5750 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5751 | PyObject *split_char(PyUnicodeObject *self, |
| 5752 | PyObject *list, |
| 5753 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5754 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5755 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5756 | register Py_ssize_t i; |
| 5757 | register Py_ssize_t j; |
| 5758 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5759 | PyObject *str; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5760 | register const Py_UNICODE *buf = self->str; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5761 | |
| 5762 | for (i = j = 0; i < len; ) { |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5763 | if (buf[i] == ch) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5764 | if (maxcount-- <= 0) |
| 5765 | break; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5766 | SPLIT_APPEND(buf, j, i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5767 | i = j = i + 1; |
| 5768 | } else |
| 5769 | i++; |
| 5770 | } |
| 5771 | if (j <= len) { |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5772 | SPLIT_APPEND(buf, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5773 | } |
| 5774 | return list; |
| 5775 | |
| 5776 | onError: |
| 5777 | Py_DECREF(list); |
| 5778 | return NULL; |
| 5779 | } |
| 5780 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5781 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5782 | PyObject *split_substring(PyUnicodeObject *self, |
| 5783 | PyObject *list, |
| 5784 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5785 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5786 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5787 | register Py_ssize_t i; |
| 5788 | register Py_ssize_t j; |
| 5789 | Py_ssize_t len = self->length; |
| 5790 | Py_ssize_t sublen = substring->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5791 | PyObject *str; |
| 5792 | |
Guido van Rossum | cda4f9a | 2000-12-19 02:23:19 +0000 | [diff] [blame] | 5793 | for (i = j = 0; i <= len - sublen; ) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5794 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 5795 | if (maxcount-- <= 0) |
| 5796 | break; |
| 5797 | SPLIT_APPEND(self->str, j, i); |
| 5798 | i = j = i + sublen; |
| 5799 | } else |
| 5800 | i++; |
| 5801 | } |
| 5802 | if (j <= len) { |
| 5803 | SPLIT_APPEND(self->str, j, len); |
| 5804 | } |
| 5805 | return list; |
| 5806 | |
| 5807 | onError: |
| 5808 | Py_DECREF(list); |
| 5809 | return NULL; |
| 5810 | } |
| 5811 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5812 | static |
| 5813 | PyObject *rsplit_whitespace(PyUnicodeObject *self, |
| 5814 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5815 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5816 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5817 | register Py_ssize_t i; |
| 5818 | register Py_ssize_t j; |
| 5819 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5820 | PyObject *str; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5821 | register const Py_UNICODE *buf = self->str; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5822 | |
| 5823 | for (i = j = len - 1; i >= 0; ) { |
| 5824 | /* find a token */ |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5825 | while (i >= 0 && Py_UNICODE_ISSPACE(buf[i])) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5826 | i--; |
| 5827 | j = i; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5828 | while (i >= 0 && !Py_UNICODE_ISSPACE(buf[i])) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5829 | i--; |
| 5830 | if (j > i) { |
| 5831 | if (maxcount-- <= 0) |
| 5832 | break; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5833 | SPLIT_APPEND(buf, i + 1, j + 1); |
| 5834 | while (i >= 0 && Py_UNICODE_ISSPACE(buf[i])) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5835 | i--; |
| 5836 | j = i; |
| 5837 | } |
| 5838 | } |
| 5839 | if (j >= 0) { |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5840 | SPLIT_APPEND(buf, 0, j + 1); |
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 | |
| 5851 | static |
| 5852 | PyObject *rsplit_char(PyUnicodeObject *self, |
| 5853 | PyObject *list, |
| 5854 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5855 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5856 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5857 | register Py_ssize_t i; |
| 5858 | register Py_ssize_t j; |
| 5859 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5860 | PyObject *str; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5861 | register const Py_UNICODE *buf = self->str; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5862 | |
| 5863 | for (i = j = len - 1; i >= 0; ) { |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5864 | if (buf[i] == ch) { |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5865 | if (maxcount-- <= 0) |
| 5866 | break; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5867 | SPLIT_APPEND(buf, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5868 | j = i = i - 1; |
| 5869 | } else |
| 5870 | i--; |
| 5871 | } |
Hye-Shik Chang | 7fc4cf5 | 2003-12-23 09:10:16 +0000 | [diff] [blame] | 5872 | if (j >= -1) { |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5873 | SPLIT_APPEND(buf, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5874 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5875 | if (PyList_Reverse(list) < 0) |
| 5876 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5877 | return list; |
| 5878 | |
| 5879 | onError: |
| 5880 | Py_DECREF(list); |
| 5881 | return NULL; |
| 5882 | } |
| 5883 | |
| 5884 | static |
| 5885 | PyObject *rsplit_substring(PyUnicodeObject *self, |
| 5886 | PyObject *list, |
| 5887 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5888 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5889 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5890 | register Py_ssize_t i; |
| 5891 | register Py_ssize_t j; |
| 5892 | Py_ssize_t len = self->length; |
| 5893 | Py_ssize_t sublen = substring->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5894 | PyObject *str; |
| 5895 | |
| 5896 | for (i = len - sublen, j = len; i >= 0; ) { |
| 5897 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 5898 | if (maxcount-- <= 0) |
| 5899 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5900 | SPLIT_APPEND(self->str, i + sublen, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5901 | j = i; |
| 5902 | i -= sublen; |
| 5903 | } else |
| 5904 | i--; |
| 5905 | } |
| 5906 | if (j >= 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5907 | SPLIT_APPEND(self->str, 0, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5908 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5909 | if (PyList_Reverse(list) < 0) |
| 5910 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5911 | return list; |
| 5912 | |
| 5913 | onError: |
| 5914 | Py_DECREF(list); |
| 5915 | return NULL; |
| 5916 | } |
| 5917 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5918 | #undef SPLIT_APPEND |
| 5919 | |
| 5920 | static |
| 5921 | PyObject *split(PyUnicodeObject *self, |
| 5922 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5923 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5924 | { |
| 5925 | PyObject *list; |
| 5926 | |
| 5927 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5928 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5929 | |
| 5930 | list = PyList_New(0); |
| 5931 | if (!list) |
| 5932 | return NULL; |
| 5933 | |
| 5934 | if (substring == NULL) |
| 5935 | return split_whitespace(self,list,maxcount); |
| 5936 | |
| 5937 | else if (substring->length == 1) |
| 5938 | return split_char(self,list,substring->str[0],maxcount); |
| 5939 | |
| 5940 | else if (substring->length == 0) { |
| 5941 | Py_DECREF(list); |
| 5942 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 5943 | return NULL; |
| 5944 | } |
| 5945 | else |
| 5946 | return split_substring(self,list,substring,maxcount); |
| 5947 | } |
| 5948 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5949 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5950 | PyObject *rsplit(PyUnicodeObject *self, |
| 5951 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5952 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5953 | { |
| 5954 | PyObject *list; |
| 5955 | |
| 5956 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5957 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5958 | |
| 5959 | list = PyList_New(0); |
| 5960 | if (!list) |
| 5961 | return NULL; |
| 5962 | |
| 5963 | if (substring == NULL) |
| 5964 | return rsplit_whitespace(self,list,maxcount); |
| 5965 | |
| 5966 | else if (substring->length == 1) |
| 5967 | return rsplit_char(self,list,substring->str[0],maxcount); |
| 5968 | |
| 5969 | else if (substring->length == 0) { |
| 5970 | Py_DECREF(list); |
| 5971 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 5972 | return NULL; |
| 5973 | } |
| 5974 | else |
| 5975 | return rsplit_substring(self,list,substring,maxcount); |
| 5976 | } |
| 5977 | |
| 5978 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5979 | PyObject *replace(PyUnicodeObject *self, |
| 5980 | PyUnicodeObject *str1, |
| 5981 | PyUnicodeObject *str2, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5982 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5983 | { |
| 5984 | PyUnicodeObject *u; |
| 5985 | |
| 5986 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5987 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5988 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5989 | if (str1->length == str2->length) { |
| 5990 | /* same length */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5991 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5992 | if (str1->length == 1) { |
| 5993 | /* replace characters */ |
| 5994 | Py_UNICODE u1, u2; |
| 5995 | if (!findchar(self->str, self->length, str1->str[0])) |
| 5996 | goto nothing; |
| 5997 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 5998 | if (!u) |
| 5999 | return NULL; |
| 6000 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6001 | u1 = str1->str[0]; |
| 6002 | u2 = str2->str[0]; |
| 6003 | for (i = 0; i < u->length; i++) |
| 6004 | if (u->str[i] == u1) { |
| 6005 | if (--maxcount < 0) |
| 6006 | break; |
| 6007 | u->str[i] = u2; |
| 6008 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6009 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6010 | i = fastsearch( |
| 6011 | self->str, self->length, str1->str, str1->length, FAST_SEARCH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6012 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6013 | if (i < 0) |
| 6014 | goto nothing; |
| 6015 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6016 | if (!u) |
| 6017 | return NULL; |
| 6018 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6019 | while (i <= self->length - str1->length) |
| 6020 | if (Py_UNICODE_MATCH(self, i, str1)) { |
| 6021 | if (--maxcount < 0) |
| 6022 | break; |
| 6023 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 6024 | i += str1->length; |
| 6025 | } else |
| 6026 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6027 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6028 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6029 | |
| 6030 | Py_ssize_t n, i, j, e; |
| 6031 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6032 | Py_UNICODE *p; |
| 6033 | |
| 6034 | /* replace strings */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6035 | n = stringlib_count(self->str, self->length, str1->str, str1->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6036 | if (n > maxcount) |
| 6037 | n = maxcount; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6038 | if (n == 0) |
| 6039 | goto nothing; |
| 6040 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 6041 | delta = (str2->length - str1->length); |
| 6042 | if (delta == 0) { |
| 6043 | new_size = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6044 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6045 | product = n * (str2->length - str1->length); |
| 6046 | if ((product / (str2->length - str1->length)) != n) { |
| 6047 | PyErr_SetString(PyExc_OverflowError, |
| 6048 | "replace string is too long"); |
| 6049 | return NULL; |
| 6050 | } |
| 6051 | new_size = self->length + product; |
| 6052 | if (new_size < 0) { |
| 6053 | PyErr_SetString(PyExc_OverflowError, |
| 6054 | "replace string is too long"); |
| 6055 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6056 | } |
| 6057 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6058 | u = _PyUnicode_New(new_size); |
| 6059 | if (!u) |
| 6060 | return NULL; |
| 6061 | i = 0; |
| 6062 | p = u->str; |
| 6063 | e = self->length - str1->length; |
| 6064 | if (str1->length > 0) { |
| 6065 | while (n-- > 0) { |
| 6066 | /* look for next match */ |
| 6067 | j = i; |
| 6068 | while (j <= e) { |
| 6069 | if (Py_UNICODE_MATCH(self, j, str1)) |
| 6070 | break; |
| 6071 | j++; |
| 6072 | } |
| 6073 | if (j > i) { |
| 6074 | if (j > e) |
| 6075 | break; |
| 6076 | /* copy unchanged part [i:j] */ |
| 6077 | Py_UNICODE_COPY(p, self->str+i, j-i); |
| 6078 | p += j - i; |
| 6079 | } |
| 6080 | /* copy substitution string */ |
| 6081 | if (str2->length > 0) { |
| 6082 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6083 | p += str2->length; |
| 6084 | } |
| 6085 | i = j + str1->length; |
| 6086 | } |
| 6087 | if (i < self->length) |
| 6088 | /* copy tail [i:] */ |
| 6089 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6090 | } else { |
| 6091 | /* interleave */ |
| 6092 | while (n > 0) { |
| 6093 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6094 | p += str2->length; |
| 6095 | if (--n <= 0) |
| 6096 | break; |
| 6097 | *p++ = self->str[i++]; |
| 6098 | } |
| 6099 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6100 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6101 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6102 | return (PyObject *) u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6103 | |
| 6104 | nothing: |
| 6105 | /* nothing to replace; return original string (when possible) */ |
| 6106 | if (PyUnicode_CheckExact(self)) { |
| 6107 | Py_INCREF(self); |
| 6108 | return (PyObject *) self; |
| 6109 | } |
| 6110 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6111 | } |
| 6112 | |
| 6113 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 6114 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6115 | PyDoc_STRVAR(title__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6116 | "S.title() -> unicode\n\ |
| 6117 | \n\ |
| 6118 | 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] | 6119 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6120 | |
| 6121 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6122 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6123 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6124 | return fixup(self, fixtitle); |
| 6125 | } |
| 6126 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6127 | PyDoc_STRVAR(capitalize__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6128 | "S.capitalize() -> unicode\n\ |
| 6129 | \n\ |
| 6130 | 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] | 6131 | have upper case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6132 | |
| 6133 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6134 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6135 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6136 | return fixup(self, fixcapitalize); |
| 6137 | } |
| 6138 | |
| 6139 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6140 | PyDoc_STRVAR(capwords__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6141 | "S.capwords() -> unicode\n\ |
| 6142 | \n\ |
| 6143 | 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] | 6144 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6145 | |
| 6146 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6147 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6148 | { |
| 6149 | PyObject *list; |
| 6150 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6151 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6152 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6153 | /* Split into words */ |
| 6154 | list = split(self, NULL, -1); |
| 6155 | if (!list) |
| 6156 | return NULL; |
| 6157 | |
| 6158 | /* Capitalize each word */ |
| 6159 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 6160 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
| 6161 | fixcapitalize); |
| 6162 | if (item == NULL) |
| 6163 | goto onError; |
| 6164 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 6165 | PyList_SET_ITEM(list, i, item); |
| 6166 | } |
| 6167 | |
| 6168 | /* Join the words to form a new string */ |
| 6169 | item = PyUnicode_Join(NULL, list); |
| 6170 | |
| 6171 | onError: |
| 6172 | Py_DECREF(list); |
| 6173 | return (PyObject *)item; |
| 6174 | } |
| 6175 | #endif |
| 6176 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6177 | /* Argument converter. Coerces to a single unicode character */ |
| 6178 | |
| 6179 | static int |
| 6180 | convert_uc(PyObject *obj, void *addr) |
| 6181 | { |
| 6182 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 6183 | PyObject *uniobj; |
| 6184 | Py_UNICODE *unistr; |
| 6185 | |
| 6186 | uniobj = PyUnicode_FromObject(obj); |
| 6187 | if (uniobj == NULL) { |
| 6188 | PyErr_SetString(PyExc_TypeError, |
| 6189 | "The fill character cannot be converted to Unicode"); |
| 6190 | return 0; |
| 6191 | } |
| 6192 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 6193 | PyErr_SetString(PyExc_TypeError, |
| 6194 | "The fill character must be exactly one character long"); |
| 6195 | Py_DECREF(uniobj); |
| 6196 | return 0; |
| 6197 | } |
| 6198 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 6199 | *fillcharloc = unistr[0]; |
| 6200 | Py_DECREF(uniobj); |
| 6201 | return 1; |
| 6202 | } |
| 6203 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6204 | PyDoc_STRVAR(center__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6205 | "S.center(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6206 | \n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6207 | Return S centered in a Unicode string of length width. Padding is\n\ |
| 6208 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6209 | |
| 6210 | static PyObject * |
| 6211 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 6212 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6213 | Py_ssize_t marg, left; |
| 6214 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6215 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6216 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 6217 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6218 | return NULL; |
| 6219 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6220 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6221 | Py_INCREF(self); |
| 6222 | return (PyObject*) self; |
| 6223 | } |
| 6224 | |
| 6225 | marg = width - self->length; |
| 6226 | left = marg / 2 + (marg & width & 1); |
| 6227 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6228 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6229 | } |
| 6230 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6231 | #if 0 |
| 6232 | |
| 6233 | /* This code should go into some future Unicode collation support |
| 6234 | module. The basic comparison should compare ordinals on a naive |
Trent Mick | 20abf57 | 2000-08-12 22:14:34 +0000 | [diff] [blame] | 6235 | basis (this is what Java does and thus JPython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6236 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6237 | /* speedy UTF-16 code point order comparison */ |
| 6238 | /* gleaned from: */ |
| 6239 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 6240 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6241 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6242 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6243 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6244 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 6245 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6246 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6247 | }; |
| 6248 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6249 | static int |
| 6250 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6251 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6252 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6253 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6254 | Py_UNICODE *s1 = str1->str; |
| 6255 | Py_UNICODE *s2 = str2->str; |
| 6256 | |
| 6257 | len1 = str1->length; |
| 6258 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6259 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6260 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6261 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6262 | |
| 6263 | c1 = *s1++; |
| 6264 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6265 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6266 | if (c1 > (1<<11) * 26) |
| 6267 | c1 += utf16Fixup[c1>>11]; |
| 6268 | if (c2 > (1<<11) * 26) |
| 6269 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6270 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6271 | |
| 6272 | if (c1 != c2) |
| 6273 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6274 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6275 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6276 | } |
| 6277 | |
| 6278 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6279 | } |
| 6280 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6281 | #else |
| 6282 | |
| 6283 | static int |
| 6284 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6285 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6286 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6287 | |
| 6288 | Py_UNICODE *s1 = str1->str; |
| 6289 | Py_UNICODE *s2 = str2->str; |
| 6290 | |
| 6291 | len1 = str1->length; |
| 6292 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6293 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6294 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6295 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6296 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6297 | c1 = *s1++; |
| 6298 | c2 = *s2++; |
| 6299 | |
| 6300 | if (c1 != c2) |
| 6301 | return (c1 < c2) ? -1 : 1; |
| 6302 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6303 | len1--; len2--; |
| 6304 | } |
| 6305 | |
| 6306 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6307 | } |
| 6308 | |
| 6309 | #endif |
| 6310 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6311 | int PyUnicode_Compare(PyObject *left, |
| 6312 | PyObject *right) |
| 6313 | { |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6314 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) |
| 6315 | return unicode_compare((PyUnicodeObject *)left, |
| 6316 | (PyUnicodeObject *)right); |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6317 | PyErr_Format(PyExc_TypeError, |
| 6318 | "Can't compare %.100s and %.100s", |
| 6319 | left->ob_type->tp_name, |
| 6320 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6321 | return -1; |
| 6322 | } |
| 6323 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6324 | int |
| 6325 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 6326 | { |
| 6327 | int i; |
| 6328 | Py_UNICODE *id; |
| 6329 | assert(PyUnicode_Check(uni)); |
| 6330 | id = PyUnicode_AS_UNICODE(uni); |
| 6331 | /* Compare Unicode string and source character set string */ |
| 6332 | for (i = 0; id[i] && str[i]; i++) |
| 6333 | if (id[i] != str[i]) |
| 6334 | return ((int)id[i] < (int)str[i]) ? -1 : 1; |
| 6335 | if (id[i]) |
| 6336 | return 1; /* uni is longer */ |
| 6337 | if (str[i]) |
| 6338 | return -1; /* str is longer */ |
| 6339 | return 0; |
| 6340 | } |
| 6341 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6342 | PyObject *PyUnicode_RichCompare(PyObject *left, |
| 6343 | PyObject *right, |
| 6344 | int op) |
| 6345 | { |
| 6346 | int result; |
| 6347 | |
| 6348 | result = PyUnicode_Compare(left, right); |
| 6349 | if (result == -1 && PyErr_Occurred()) |
| 6350 | goto onError; |
| 6351 | |
| 6352 | /* Convert the return value to a Boolean */ |
| 6353 | switch (op) { |
| 6354 | case Py_EQ: |
| 6355 | result = (result == 0); |
| 6356 | break; |
| 6357 | case Py_NE: |
| 6358 | result = (result != 0); |
| 6359 | break; |
| 6360 | case Py_LE: |
| 6361 | result = (result <= 0); |
| 6362 | break; |
| 6363 | case Py_GE: |
| 6364 | result = (result >= 0); |
| 6365 | break; |
| 6366 | case Py_LT: |
| 6367 | result = (result == -1); |
| 6368 | break; |
| 6369 | case Py_GT: |
| 6370 | result = (result == 1); |
| 6371 | break; |
| 6372 | } |
| 6373 | return PyBool_FromLong(result); |
| 6374 | |
| 6375 | onError: |
| 6376 | |
| 6377 | /* Standard case |
| 6378 | |
| 6379 | Type errors mean that PyUnicode_FromObject() could not convert |
| 6380 | one of the arguments (usually the right hand side) to Unicode, |
| 6381 | ie. we can't handle the comparison request. However, it is |
| 6382 | possible that the other object knows a comparison method, which |
| 6383 | is why we return Py_NotImplemented to give the other object a |
| 6384 | chance. |
| 6385 | |
| 6386 | */ |
| 6387 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 6388 | PyErr_Clear(); |
| 6389 | Py_INCREF(Py_NotImplemented); |
| 6390 | return Py_NotImplemented; |
| 6391 | } |
| 6392 | if (op != Py_EQ && op != Py_NE) |
| 6393 | return NULL; |
| 6394 | |
| 6395 | /* Equality comparison. |
| 6396 | |
| 6397 | This is a special case: we silence any PyExc_UnicodeDecodeError |
| 6398 | and instead turn it into a PyErr_UnicodeWarning. |
| 6399 | |
| 6400 | */ |
| 6401 | if (!PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) |
| 6402 | return NULL; |
| 6403 | PyErr_Clear(); |
Skip Montanaro | 46fc337 | 2007-08-12 11:44:53 +0000 | [diff] [blame] | 6404 | if (PyErr_WarnEx(PyExc_UnicodeWarning, |
| 6405 | (op == Py_EQ) ? |
| 6406 | "Unicode equal comparison " |
| 6407 | "failed to convert both arguments to Unicode - " |
| 6408 | "interpreting them as being unequal" |
| 6409 | : |
| 6410 | "Unicode unequal comparison " |
| 6411 | "failed to convert both arguments to Unicode - " |
| 6412 | "interpreting them as being unequal", |
| 6413 | 1) < 0) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6414 | return NULL; |
| 6415 | result = (op == Py_NE); |
| 6416 | return PyBool_FromLong(result); |
| 6417 | } |
| 6418 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6419 | int PyUnicode_Contains(PyObject *container, |
| 6420 | PyObject *element) |
| 6421 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6422 | PyObject *str, *sub; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6423 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6424 | |
| 6425 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6426 | sub = PyUnicode_FromObject(element); |
| 6427 | if (!sub) { |
Walter Dörwald | 26e0f51 | 2007-06-12 16:51:31 +0000 | [diff] [blame] | 6428 | PyErr_Format(PyExc_TypeError, |
| 6429 | "'in <string>' requires string as left operand, not %s", |
| 6430 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6431 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6432 | } |
| 6433 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6434 | str = PyUnicode_FromObject(container); |
| 6435 | if (!str) { |
| 6436 | Py_DECREF(sub); |
| 6437 | return -1; |
| 6438 | } |
| 6439 | |
| 6440 | result = stringlib_contains_obj(str, sub); |
| 6441 | |
| 6442 | Py_DECREF(str); |
| 6443 | Py_DECREF(sub); |
| 6444 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6445 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6446 | } |
| 6447 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6448 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 6449 | |
| 6450 | PyObject *PyUnicode_Concat(PyObject *left, |
| 6451 | PyObject *right) |
| 6452 | { |
| 6453 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 6454 | |
| 6455 | /* Coerce the two arguments */ |
| 6456 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 6457 | if (u == NULL) |
| 6458 | goto onError; |
| 6459 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 6460 | if (v == NULL) |
| 6461 | goto onError; |
| 6462 | |
| 6463 | /* Shortcuts */ |
| 6464 | if (v == unicode_empty) { |
| 6465 | Py_DECREF(v); |
| 6466 | return (PyObject *)u; |
| 6467 | } |
| 6468 | if (u == unicode_empty) { |
| 6469 | Py_DECREF(u); |
| 6470 | return (PyObject *)v; |
| 6471 | } |
| 6472 | |
| 6473 | /* Concat the two Unicode strings */ |
| 6474 | w = _PyUnicode_New(u->length + v->length); |
| 6475 | if (w == NULL) |
| 6476 | goto onError; |
| 6477 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 6478 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 6479 | |
| 6480 | Py_DECREF(u); |
| 6481 | Py_DECREF(v); |
| 6482 | return (PyObject *)w; |
| 6483 | |
| 6484 | onError: |
| 6485 | Py_XDECREF(u); |
| 6486 | Py_XDECREF(v); |
| 6487 | return NULL; |
| 6488 | } |
| 6489 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 6490 | void |
| 6491 | PyUnicode_Append(PyObject **pleft, PyObject *right) |
| 6492 | { |
| 6493 | PyObject *new; |
| 6494 | if (*pleft == NULL) |
| 6495 | return; |
| 6496 | if (right == NULL || !PyUnicode_Check(*pleft)) { |
| 6497 | Py_DECREF(*pleft); |
| 6498 | *pleft = NULL; |
| 6499 | return; |
| 6500 | } |
| 6501 | new = PyUnicode_Concat(*pleft, right); |
| 6502 | Py_DECREF(*pleft); |
| 6503 | *pleft = new; |
| 6504 | } |
| 6505 | |
| 6506 | void |
| 6507 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 6508 | { |
| 6509 | PyUnicode_Append(pleft, right); |
| 6510 | Py_XDECREF(right); |
| 6511 | } |
| 6512 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6513 | PyDoc_STRVAR(count__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6514 | "S.count(sub[, start[, end]]) -> int\n\ |
| 6515 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6516 | Return the number of non-overlapping occurrences of substring sub in\n\ |
| 6517 | 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] | 6518 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6519 | |
| 6520 | static PyObject * |
| 6521 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 6522 | { |
| 6523 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6524 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6525 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6526 | PyObject *result; |
| 6527 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6528 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
| 6529 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6530 | return NULL; |
| 6531 | |
| 6532 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6533 | (PyObject *)substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6534 | if (substring == NULL) |
| 6535 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6536 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6537 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6538 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6539 | result = PyLong_FromSsize_t( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6540 | stringlib_count(self->str + start, end - start, |
| 6541 | substring->str, substring->length) |
| 6542 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6543 | |
| 6544 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6545 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6546 | return result; |
| 6547 | } |
| 6548 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6549 | PyDoc_STRVAR(encode__doc__, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6550 | "S.encode([encoding[,errors]]) -> string or unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6551 | \n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6552 | Encodes S using the codec registered for encoding. encoding defaults\n\ |
| 6553 | 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] | 6554 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6555 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 6556 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 6557 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6558 | |
| 6559 | static PyObject * |
| 6560 | unicode_encode(PyUnicodeObject *self, PyObject *args) |
| 6561 | { |
| 6562 | char *encoding = NULL; |
| 6563 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6564 | PyObject *v; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 6565 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6566 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 6567 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6568 | v = PyUnicode_AsEncodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6569 | if (v == NULL) |
| 6570 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 6571 | if (!PyString_Check(v)) { |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6572 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 6573 | "encoder did not return a bytes object " |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6574 | "(type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 6575 | Py_TYPE(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6576 | Py_DECREF(v); |
| 6577 | return NULL; |
| 6578 | } |
| 6579 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6580 | |
| 6581 | onError: |
| 6582 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6583 | } |
| 6584 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6585 | PyDoc_STRVAR(expandtabs__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6586 | "S.expandtabs([tabsize]) -> unicode\n\ |
| 6587 | \n\ |
| 6588 | 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] | 6589 | 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] | 6590 | |
| 6591 | static PyObject* |
| 6592 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 6593 | { |
| 6594 | Py_UNICODE *e; |
| 6595 | Py_UNICODE *p; |
| 6596 | Py_UNICODE *q; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6597 | Py_UNICODE *qe; |
| 6598 | Py_ssize_t i, j, incr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6599 | PyUnicodeObject *u; |
| 6600 | int tabsize = 8; |
| 6601 | |
| 6602 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
| 6603 | return NULL; |
| 6604 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6605 | /* First pass: determine size of output string */ |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6606 | i = 0; /* chars up to and including most recent \n or \r */ |
| 6607 | j = 0; /* chars since most recent \n or \r (use in tab calculations) */ |
| 6608 | e = self->str + self->length; /* end of input */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6609 | for (p = self->str; p < e; p++) |
| 6610 | if (*p == '\t') { |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6611 | if (tabsize > 0) { |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6612 | incr = tabsize - (j % tabsize); /* cannot overflow */ |
| 6613 | if (j > PY_SSIZE_T_MAX - incr) |
| 6614 | goto overflow1; |
| 6615 | j += incr; |
| 6616 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6617 | } |
| 6618 | else { |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6619 | if (j > PY_SSIZE_T_MAX - 1) |
| 6620 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6621 | j++; |
| 6622 | if (*p == '\n' || *p == '\r') { |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6623 | if (i > PY_SSIZE_T_MAX - j) |
| 6624 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6625 | i += j; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6626 | j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6627 | } |
| 6628 | } |
| 6629 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6630 | if (i > PY_SSIZE_T_MAX - j) |
| 6631 | goto overflow1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6632 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6633 | /* Second pass: create output string and fill it */ |
| 6634 | u = _PyUnicode_New(i + j); |
| 6635 | if (!u) |
| 6636 | return NULL; |
| 6637 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6638 | j = 0; /* same as in first pass */ |
| 6639 | q = u->str; /* next output char */ |
| 6640 | qe = u->str + u->length; /* end of output */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6641 | |
| 6642 | for (p = self->str; p < e; p++) |
| 6643 | if (*p == '\t') { |
| 6644 | if (tabsize > 0) { |
| 6645 | i = tabsize - (j % tabsize); |
| 6646 | j += i; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6647 | while (i--) { |
| 6648 | if (q >= qe) |
| 6649 | goto overflow2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6650 | *q++ = ' '; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6651 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6652 | } |
| 6653 | } |
| 6654 | else { |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6655 | if (q >= qe) |
| 6656 | goto overflow2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6657 | *q++ = *p; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6658 | j++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6659 | if (*p == '\n' || *p == '\r') |
| 6660 | j = 0; |
| 6661 | } |
| 6662 | |
| 6663 | return (PyObject*) u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6664 | |
| 6665 | overflow2: |
| 6666 | Py_DECREF(u); |
| 6667 | overflow1: |
| 6668 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 6669 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6670 | } |
| 6671 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6672 | PyDoc_STRVAR(find__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6673 | "S.find(sub [,start [,end]]) -> int\n\ |
| 6674 | \n\ |
| 6675 | 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] | 6676 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6677 | arguments start and end are interpreted as in slice notation.\n\ |
| 6678 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6679 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6680 | |
| 6681 | static PyObject * |
| 6682 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 6683 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6684 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 6685 | Py_ssize_t start; |
| 6686 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6687 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6688 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 6689 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6690 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6691 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6692 | result = stringlib_find_slice( |
| 6693 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6694 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6695 | start, end |
| 6696 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6697 | |
| 6698 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6699 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6700 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6701 | } |
| 6702 | |
| 6703 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6704 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6705 | { |
| 6706 | if (index < 0 || index >= self->length) { |
| 6707 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 6708 | return NULL; |
| 6709 | } |
| 6710 | |
| 6711 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 6712 | } |
| 6713 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 6714 | /* Believe it or not, this produces the same value for ASCII strings |
| 6715 | as string_hash(). */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6716 | static long |
Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 6717 | unicode_hash(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6718 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 6719 | Py_ssize_t len; |
| 6720 | Py_UNICODE *p; |
| 6721 | long x; |
| 6722 | |
| 6723 | if (self->hash != -1) |
| 6724 | return self->hash; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 6725 | len = Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 6726 | p = self->str; |
| 6727 | x = *p << 7; |
| 6728 | while (--len >= 0) |
| 6729 | x = (1000003*x) ^ *p++; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 6730 | x ^= Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 6731 | if (x == -1) |
| 6732 | x = -2; |
| 6733 | self->hash = x; |
| 6734 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6735 | } |
| 6736 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6737 | PyDoc_STRVAR(index__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6738 | "S.index(sub [,start [,end]]) -> int\n\ |
| 6739 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6740 | 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] | 6741 | |
| 6742 | static PyObject * |
| 6743 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 6744 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6745 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6746 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 6747 | Py_ssize_t start; |
| 6748 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6749 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 6750 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6751 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6752 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6753 | result = stringlib_find_slice( |
| 6754 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6755 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6756 | start, end |
| 6757 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6758 | |
| 6759 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6760 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6761 | if (result < 0) { |
| 6762 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 6763 | return NULL; |
| 6764 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6765 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6766 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6767 | } |
| 6768 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6769 | PyDoc_STRVAR(islower__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6770 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6771 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6772 | 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] | 6773 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6774 | |
| 6775 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6776 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6777 | { |
| 6778 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6779 | register const Py_UNICODE *e; |
| 6780 | int cased; |
| 6781 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6782 | /* Shortcut for single character strings */ |
| 6783 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6784 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6785 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6786 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6787 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6788 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6789 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6790 | e = p + PyUnicode_GET_SIZE(self); |
| 6791 | cased = 0; |
| 6792 | for (; p < e; p++) { |
| 6793 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6794 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6795 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6796 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6797 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 6798 | cased = 1; |
| 6799 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6800 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6801 | } |
| 6802 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6803 | PyDoc_STRVAR(isupper__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6804 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6805 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6806 | 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] | 6807 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6808 | |
| 6809 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6810 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6811 | { |
| 6812 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6813 | register const Py_UNICODE *e; |
| 6814 | int cased; |
| 6815 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6816 | /* Shortcut for single character strings */ |
| 6817 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6818 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6819 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6820 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6821 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6822 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6823 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6824 | e = p + PyUnicode_GET_SIZE(self); |
| 6825 | cased = 0; |
| 6826 | for (; p < e; p++) { |
| 6827 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6828 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6829 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6830 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6831 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 6832 | cased = 1; |
| 6833 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6834 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6835 | } |
| 6836 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6837 | PyDoc_STRVAR(istitle__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6838 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6839 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6840 | Return True if S is a titlecased string and there is at least one\n\ |
| 6841 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 6842 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 6843 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6844 | |
| 6845 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6846 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6847 | { |
| 6848 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6849 | register const Py_UNICODE *e; |
| 6850 | int cased, previous_is_cased; |
| 6851 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6852 | /* Shortcut for single character strings */ |
| 6853 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6854 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 6855 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6856 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6857 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6858 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6859 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6860 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6861 | e = p + PyUnicode_GET_SIZE(self); |
| 6862 | cased = 0; |
| 6863 | previous_is_cased = 0; |
| 6864 | for (; p < e; p++) { |
| 6865 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6866 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6867 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 6868 | if (previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6869 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6870 | previous_is_cased = 1; |
| 6871 | cased = 1; |
| 6872 | } |
| 6873 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 6874 | if (!previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6875 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6876 | previous_is_cased = 1; |
| 6877 | cased = 1; |
| 6878 | } |
| 6879 | else |
| 6880 | previous_is_cased = 0; |
| 6881 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6882 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6883 | } |
| 6884 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6885 | PyDoc_STRVAR(isspace__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6886 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6887 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6888 | Return True if all characters in S are whitespace\n\ |
| 6889 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6890 | |
| 6891 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6892 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6893 | { |
| 6894 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6895 | register const Py_UNICODE *e; |
| 6896 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6897 | /* Shortcut for single character strings */ |
| 6898 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6899 | Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6900 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6901 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6902 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6903 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6904 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6905 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6906 | e = p + PyUnicode_GET_SIZE(self); |
| 6907 | for (; p < e; p++) { |
| 6908 | if (!Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6909 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6910 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6911 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6912 | } |
| 6913 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6914 | PyDoc_STRVAR(isalpha__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6915 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6916 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6917 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6918 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6919 | |
| 6920 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6921 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6922 | { |
| 6923 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6924 | register const Py_UNICODE *e; |
| 6925 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6926 | /* Shortcut for single character strings */ |
| 6927 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6928 | Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6929 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6930 | |
| 6931 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6932 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6933 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6934 | |
| 6935 | e = p + PyUnicode_GET_SIZE(self); |
| 6936 | for (; p < e; p++) { |
| 6937 | if (!Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6938 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6939 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6940 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6941 | } |
| 6942 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6943 | PyDoc_STRVAR(isalnum__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6944 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6945 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6946 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6947 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6948 | |
| 6949 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6950 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6951 | { |
| 6952 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6953 | register const Py_UNICODE *e; |
| 6954 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6955 | /* Shortcut for single character strings */ |
| 6956 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6957 | Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6958 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6959 | |
| 6960 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6961 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6962 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6963 | |
| 6964 | e = p + PyUnicode_GET_SIZE(self); |
| 6965 | for (; p < e; p++) { |
| 6966 | if (!Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6967 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6968 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6969 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6970 | } |
| 6971 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6972 | PyDoc_STRVAR(isdecimal__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6973 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6974 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6975 | 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] | 6976 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6977 | |
| 6978 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6979 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6980 | { |
| 6981 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6982 | register const Py_UNICODE *e; |
| 6983 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6984 | /* Shortcut for single character strings */ |
| 6985 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6986 | Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6987 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6988 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6989 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6990 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6991 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6992 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6993 | e = p + PyUnicode_GET_SIZE(self); |
| 6994 | for (; p < e; p++) { |
| 6995 | if (!Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6996 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6997 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6998 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6999 | } |
| 7000 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7001 | PyDoc_STRVAR(isdigit__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7002 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7003 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7004 | Return True if all characters in S are digits\n\ |
| 7005 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7006 | |
| 7007 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7008 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7009 | { |
| 7010 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7011 | register const Py_UNICODE *e; |
| 7012 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7013 | /* Shortcut for single character strings */ |
| 7014 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 7015 | Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7016 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7017 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7018 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7019 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7020 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7021 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7022 | e = p + PyUnicode_GET_SIZE(self); |
| 7023 | for (; p < e; p++) { |
| 7024 | if (!Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7025 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7026 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7027 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7028 | } |
| 7029 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7030 | PyDoc_STRVAR(isnumeric__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7031 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7032 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7033 | 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] | 7034 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7035 | |
| 7036 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7037 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7038 | { |
| 7039 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7040 | register const Py_UNICODE *e; |
| 7041 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7042 | /* Shortcut for single character strings */ |
| 7043 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 7044 | Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7045 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7046 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7047 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7048 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7049 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7050 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7051 | e = p + PyUnicode_GET_SIZE(self); |
| 7052 | for (; p < e; p++) { |
| 7053 | if (!Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7054 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7055 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7056 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7057 | } |
| 7058 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7059 | int |
| 7060 | PyUnicode_IsIdentifier(PyObject *self) |
| 7061 | { |
| 7062 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE((PyUnicodeObject*)self); |
| 7063 | register const Py_UNICODE *e; |
| 7064 | |
| 7065 | /* Special case for empty strings */ |
| 7066 | if (PyUnicode_GET_SIZE(self) == 0) |
| 7067 | return 0; |
| 7068 | |
| 7069 | /* PEP 3131 says that the first character must be in |
| 7070 | XID_Start and subsequent characters in XID_Continue, |
| 7071 | and for the ASCII range, the 2.x rules apply (i.e |
| 7072 | start with letters and underscore, continue with |
| 7073 | letters, digits, underscore). However, given the current |
| 7074 | definition of XID_Start and XID_Continue, it is sufficient |
| 7075 | to check just for these, except that _ must be allowed |
| 7076 | as starting an identifier. */ |
| 7077 | if (!_PyUnicode_IsXidStart(*p) && *p != 0x5F /* LOW LINE */) |
| 7078 | return 0; |
| 7079 | |
| 7080 | e = p + PyUnicode_GET_SIZE(self); |
| 7081 | for (p++; p < e; p++) { |
| 7082 | if (!_PyUnicode_IsXidContinue(*p)) |
| 7083 | return 0; |
| 7084 | } |
| 7085 | return 1; |
| 7086 | } |
| 7087 | |
| 7088 | PyDoc_STRVAR(isidentifier__doc__, |
| 7089 | "S.isidentifier() -> bool\n\ |
| 7090 | \n\ |
| 7091 | Return True if S is a valid identifier according\n\ |
| 7092 | to the language definition."); |
| 7093 | |
| 7094 | static PyObject* |
| 7095 | unicode_isidentifier(PyObject *self) |
| 7096 | { |
| 7097 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 7098 | } |
| 7099 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7100 | PyDoc_STRVAR(join__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7101 | "S.join(sequence) -> unicode\n\ |
| 7102 | \n\ |
| 7103 | 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] | 7104 | sequence. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7105 | |
| 7106 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7107 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7108 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7109 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7110 | } |
| 7111 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7112 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7113 | unicode_length(PyUnicodeObject *self) |
| 7114 | { |
| 7115 | return self->length; |
| 7116 | } |
| 7117 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7118 | PyDoc_STRVAR(ljust__doc__, |
Hye-Shik Chang | 974ed7c | 2004-06-02 16:49:17 +0000 | [diff] [blame] | 7119 | "S.ljust(width[, fillchar]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7120 | \n\ |
| 7121 | 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] | 7122 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7123 | |
| 7124 | static PyObject * |
| 7125 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 7126 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7127 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7128 | Py_UNICODE fillchar = ' '; |
| 7129 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7130 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7131 | return NULL; |
| 7132 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7133 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7134 | Py_INCREF(self); |
| 7135 | return (PyObject*) self; |
| 7136 | } |
| 7137 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7138 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7139 | } |
| 7140 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7141 | PyDoc_STRVAR(lower__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7142 | "S.lower() -> unicode\n\ |
| 7143 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7144 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7145 | |
| 7146 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7147 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7148 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7149 | return fixup(self, fixlower); |
| 7150 | } |
| 7151 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7152 | #define LEFTSTRIP 0 |
| 7153 | #define RIGHTSTRIP 1 |
| 7154 | #define BOTHSTRIP 2 |
| 7155 | |
| 7156 | /* Arrays indexed by above */ |
| 7157 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 7158 | |
| 7159 | #define STRIPNAME(i) (stripformat[i]+3) |
| 7160 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7161 | /* externally visible for str.strip(unicode) */ |
| 7162 | PyObject * |
| 7163 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 7164 | { |
| 7165 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7166 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7167 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7168 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 7169 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7170 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7171 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
| 7172 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7173 | i = 0; |
| 7174 | if (striptype != RIGHTSTRIP) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7175 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 7176 | i++; |
| 7177 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7178 | } |
| 7179 | |
| 7180 | j = len; |
| 7181 | if (striptype != LEFTSTRIP) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7182 | do { |
| 7183 | j--; |
| 7184 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 7185 | j++; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7186 | } |
| 7187 | |
| 7188 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7189 | Py_INCREF(self); |
| 7190 | return (PyObject*)self; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7191 | } |
| 7192 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7193 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7194 | } |
| 7195 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7196 | |
| 7197 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7198 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7199 | { |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7200 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7201 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7202 | |
| 7203 | i = 0; |
| 7204 | if (striptype != RIGHTSTRIP) { |
| 7205 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 7206 | i++; |
| 7207 | } |
| 7208 | } |
| 7209 | |
| 7210 | j = len; |
| 7211 | if (striptype != LEFTSTRIP) { |
| 7212 | do { |
| 7213 | j--; |
| 7214 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 7215 | j++; |
| 7216 | } |
| 7217 | |
| 7218 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 7219 | Py_INCREF(self); |
| 7220 | return (PyObject*)self; |
| 7221 | } |
| 7222 | else |
| 7223 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7224 | } |
| 7225 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7226 | |
| 7227 | static PyObject * |
| 7228 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 7229 | { |
| 7230 | PyObject *sep = NULL; |
| 7231 | |
| 7232 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 7233 | return NULL; |
| 7234 | |
| 7235 | if (sep != NULL && sep != Py_None) { |
| 7236 | if (PyUnicode_Check(sep)) |
| 7237 | return _PyUnicode_XStrip(self, striptype, sep); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7238 | else { |
| 7239 | PyErr_Format(PyExc_TypeError, |
| 7240 | "%s arg must be None, unicode or str", |
| 7241 | STRIPNAME(striptype)); |
| 7242 | return NULL; |
| 7243 | } |
| 7244 | } |
| 7245 | |
| 7246 | return do_strip(self, striptype); |
| 7247 | } |
| 7248 | |
| 7249 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7250 | PyDoc_STRVAR(strip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7251 | "S.strip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7252 | \n\ |
| 7253 | Return a copy of the string S with leading and trailing\n\ |
| 7254 | whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7255 | If chars is given and not None, remove characters in chars instead.\n\ |
| 7256 | 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] | 7257 | |
| 7258 | static PyObject * |
| 7259 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 7260 | { |
| 7261 | if (PyTuple_GET_SIZE(args) == 0) |
| 7262 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 7263 | else |
| 7264 | return do_argstrip(self, BOTHSTRIP, args); |
| 7265 | } |
| 7266 | |
| 7267 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7268 | PyDoc_STRVAR(lstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7269 | "S.lstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7270 | \n\ |
| 7271 | Return a copy of the string S with leading whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7272 | If chars is given and not None, remove characters in chars instead.\n\ |
| 7273 | 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] | 7274 | |
| 7275 | static PyObject * |
| 7276 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 7277 | { |
| 7278 | if (PyTuple_GET_SIZE(args) == 0) |
| 7279 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 7280 | else |
| 7281 | return do_argstrip(self, LEFTSTRIP, args); |
| 7282 | } |
| 7283 | |
| 7284 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7285 | PyDoc_STRVAR(rstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7286 | "S.rstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7287 | \n\ |
| 7288 | Return a copy of the string S with trailing whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7289 | If chars is given and not None, remove characters in chars instead.\n\ |
| 7290 | 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] | 7291 | |
| 7292 | static PyObject * |
| 7293 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 7294 | { |
| 7295 | if (PyTuple_GET_SIZE(args) == 0) |
| 7296 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 7297 | else |
| 7298 | return do_argstrip(self, RIGHTSTRIP, args); |
| 7299 | } |
| 7300 | |
| 7301 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7302 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7303 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7304 | { |
| 7305 | PyUnicodeObject *u; |
| 7306 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7307 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7308 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7309 | |
| 7310 | if (len < 0) |
| 7311 | len = 0; |
| 7312 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7313 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7314 | /* no repeat, return original string */ |
| 7315 | Py_INCREF(str); |
| 7316 | return (PyObject*) str; |
| 7317 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7318 | |
| 7319 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 7320 | * needed doesn't overflow size_t |
| 7321 | */ |
| 7322 | nchars = len * str->length; |
| 7323 | if (len && nchars / len != str->length) { |
| 7324 | PyErr_SetString(PyExc_OverflowError, |
| 7325 | "repeated string is too long"); |
| 7326 | return NULL; |
| 7327 | } |
| 7328 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 7329 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 7330 | PyErr_SetString(PyExc_OverflowError, |
| 7331 | "repeated string is too long"); |
| 7332 | return NULL; |
| 7333 | } |
| 7334 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7335 | if (!u) |
| 7336 | return NULL; |
| 7337 | |
| 7338 | p = u->str; |
| 7339 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7340 | if (str->length == 1 && len > 0) { |
| 7341 | Py_UNICODE_FILL(p, str->str[0], len); |
| 7342 | } else { |
| 7343 | Py_ssize_t done = 0; /* number of characters copied this far */ |
| 7344 | if (done < nchars) { |
| 7345 | Py_UNICODE_COPY(p, str->str, str->length); |
| 7346 | done = str->length; |
| 7347 | } |
| 7348 | while (done < nchars) { |
| 7349 | int n = (done <= nchars-done) ? done : nchars-done; |
| 7350 | Py_UNICODE_COPY(p+done, p, n); |
| 7351 | done += n; |
| 7352 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7353 | } |
| 7354 | |
| 7355 | return (PyObject*) u; |
| 7356 | } |
| 7357 | |
| 7358 | PyObject *PyUnicode_Replace(PyObject *obj, |
| 7359 | PyObject *subobj, |
| 7360 | PyObject *replobj, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7361 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7362 | { |
| 7363 | PyObject *self; |
| 7364 | PyObject *str1; |
| 7365 | PyObject *str2; |
| 7366 | PyObject *result; |
| 7367 | |
| 7368 | self = PyUnicode_FromObject(obj); |
| 7369 | if (self == NULL) |
| 7370 | return NULL; |
| 7371 | str1 = PyUnicode_FromObject(subobj); |
| 7372 | if (str1 == NULL) { |
| 7373 | Py_DECREF(self); |
| 7374 | return NULL; |
| 7375 | } |
| 7376 | str2 = PyUnicode_FromObject(replobj); |
| 7377 | if (str2 == NULL) { |
| 7378 | Py_DECREF(self); |
| 7379 | Py_DECREF(str1); |
| 7380 | return NULL; |
| 7381 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7382 | result = replace((PyUnicodeObject *)self, |
| 7383 | (PyUnicodeObject *)str1, |
| 7384 | (PyUnicodeObject *)str2, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7385 | maxcount); |
| 7386 | Py_DECREF(self); |
| 7387 | Py_DECREF(str1); |
| 7388 | Py_DECREF(str2); |
| 7389 | return result; |
| 7390 | } |
| 7391 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7392 | PyDoc_STRVAR(replace__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7393 | "S.replace (old, new[, maxsplit]) -> unicode\n\ |
| 7394 | \n\ |
| 7395 | Return a copy of S with all occurrences of substring\n\ |
| 7396 | 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] | 7397 | given, only the first maxsplit occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7398 | |
| 7399 | static PyObject* |
| 7400 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 7401 | { |
| 7402 | PyUnicodeObject *str1; |
| 7403 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7404 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7405 | PyObject *result; |
| 7406 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7407 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7408 | return NULL; |
| 7409 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 7410 | if (str1 == NULL) |
| 7411 | return NULL; |
| 7412 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7413 | if (str2 == NULL) { |
| 7414 | Py_DECREF(str1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7415 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7416 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7417 | |
| 7418 | result = replace(self, str1, str2, maxcount); |
| 7419 | |
| 7420 | Py_DECREF(str1); |
| 7421 | Py_DECREF(str2); |
| 7422 | return result; |
| 7423 | } |
| 7424 | |
| 7425 | static |
| 7426 | PyObject *unicode_repr(PyObject *unicode) |
| 7427 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7428 | PyObject *repr; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7429 | Py_UNICODE *p; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7430 | Py_UNICODE *s = PyUnicode_AS_UNICODE(unicode); |
| 7431 | Py_ssize_t size = PyUnicode_GET_SIZE(unicode); |
| 7432 | |
| 7433 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 7434 | better to choose a different scheme. Perhaps scan the |
| 7435 | first N-chars of the string and allocate based on that size. |
| 7436 | */ |
| 7437 | /* Initial allocation is based on the longest-possible unichr |
| 7438 | escape. |
| 7439 | |
| 7440 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 7441 | unichr, so in this case it's the longest unichr escape. In |
| 7442 | narrow (UTF-16) builds this is five chars per source unichr |
| 7443 | since there are two unichrs in the surrogate pair, so in narrow |
| 7444 | (UTF-16) builds it's not the longest unichr escape. |
| 7445 | |
| 7446 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 7447 | so in the narrow (UTF-16) build case it's the longest unichr |
| 7448 | escape. |
| 7449 | */ |
| 7450 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7451 | repr = PyUnicode_FromUnicode(NULL, |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7452 | 2 /* quotes */ |
| 7453 | #ifdef Py_UNICODE_WIDE |
| 7454 | + 10*size |
| 7455 | #else |
| 7456 | + 6*size |
| 7457 | #endif |
| 7458 | + 1); |
| 7459 | if (repr == NULL) |
| 7460 | return NULL; |
| 7461 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7462 | p = PyUnicode_AS_UNICODE(repr); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7463 | |
| 7464 | /* Add quote */ |
| 7465 | *p++ = (findchar(s, size, '\'') && |
| 7466 | !findchar(s, size, '"')) ? '"' : '\''; |
| 7467 | while (size-- > 0) { |
| 7468 | Py_UNICODE ch = *s++; |
| 7469 | |
| 7470 | /* Escape quotes and backslashes */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7471 | if ((ch == PyUnicode_AS_UNICODE(repr)[0]) || (ch == '\\')) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7472 | *p++ = '\\'; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7473 | *p++ = ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7474 | continue; |
| 7475 | } |
| 7476 | |
| 7477 | #ifdef Py_UNICODE_WIDE |
| 7478 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 7479 | else if (ch >= 0x10000) { |
| 7480 | *p++ = '\\'; |
| 7481 | *p++ = 'U'; |
| 7482 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 7483 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 7484 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 7485 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 7486 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 7487 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 7488 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 7489 | *p++ = hexdigits[ch & 0x0000000F]; |
| 7490 | continue; |
| 7491 | } |
| 7492 | #else |
| 7493 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 7494 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 7495 | Py_UNICODE ch2; |
| 7496 | Py_UCS4 ucs; |
| 7497 | |
| 7498 | ch2 = *s++; |
| 7499 | size--; |
| 7500 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 7501 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 7502 | *p++ = '\\'; |
| 7503 | *p++ = 'U'; |
| 7504 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 7505 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 7506 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 7507 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 7508 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 7509 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 7510 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 7511 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 7512 | continue; |
| 7513 | } |
| 7514 | /* Fall through: isolated surrogates are copied as-is */ |
| 7515 | s--; |
| 7516 | size++; |
| 7517 | } |
| 7518 | #endif |
| 7519 | |
| 7520 | /* Map 16-bit characters to '\uxxxx' */ |
| 7521 | if (ch >= 256) { |
| 7522 | *p++ = '\\'; |
| 7523 | *p++ = 'u'; |
| 7524 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 7525 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 7526 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 7527 | *p++ = hexdigits[ch & 0x000F]; |
| 7528 | } |
| 7529 | |
| 7530 | /* Map special whitespace to '\t', \n', '\r' */ |
| 7531 | else if (ch == '\t') { |
| 7532 | *p++ = '\\'; |
| 7533 | *p++ = 't'; |
| 7534 | } |
| 7535 | else if (ch == '\n') { |
| 7536 | *p++ = '\\'; |
| 7537 | *p++ = 'n'; |
| 7538 | } |
| 7539 | else if (ch == '\r') { |
| 7540 | *p++ = '\\'; |
| 7541 | *p++ = 'r'; |
| 7542 | } |
| 7543 | |
| 7544 | /* Map non-printable US ASCII to '\xhh' */ |
| 7545 | else if (ch < ' ' || ch >= 0x7F) { |
| 7546 | *p++ = '\\'; |
| 7547 | *p++ = 'x'; |
| 7548 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 7549 | *p++ = hexdigits[ch & 0x000F]; |
| 7550 | } |
| 7551 | |
| 7552 | /* Copy everything else as-is */ |
| 7553 | else |
| 7554 | *p++ = (char) ch; |
| 7555 | } |
| 7556 | /* Add quote */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7557 | *p++ = PyUnicode_AS_UNICODE(repr)[0]; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7558 | |
| 7559 | *p = '\0'; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7560 | _PyUnicode_Resize(&repr, p - PyUnicode_AS_UNICODE(repr)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7561 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7562 | } |
| 7563 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7564 | PyDoc_STRVAR(rfind__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7565 | "S.rfind(sub [,start [,end]]) -> int\n\ |
| 7566 | \n\ |
| 7567 | 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] | 7568 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7569 | arguments start and end are interpreted as in slice notation.\n\ |
| 7570 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7571 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7572 | |
| 7573 | static PyObject * |
| 7574 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 7575 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7576 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7577 | Py_ssize_t start; |
| 7578 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7579 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7580 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7581 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
| 7582 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7583 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7584 | result = stringlib_rfind_slice( |
| 7585 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7586 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7587 | start, end |
| 7588 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7589 | |
| 7590 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7591 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7592 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7593 | } |
| 7594 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7595 | PyDoc_STRVAR(rindex__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7596 | "S.rindex(sub [,start [,end]]) -> int\n\ |
| 7597 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7598 | 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] | 7599 | |
| 7600 | static PyObject * |
| 7601 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 7602 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7603 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7604 | Py_ssize_t start; |
| 7605 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7606 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7607 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7608 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
| 7609 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7610 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7611 | result = stringlib_rfind_slice( |
| 7612 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7613 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7614 | start, end |
| 7615 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7616 | |
| 7617 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7618 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7619 | if (result < 0) { |
| 7620 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 7621 | return NULL; |
| 7622 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7623 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7624 | } |
| 7625 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7626 | PyDoc_STRVAR(rjust__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7627 | "S.rjust(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7628 | \n\ |
| 7629 | 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] | 7630 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7631 | |
| 7632 | static PyObject * |
| 7633 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 7634 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7635 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7636 | Py_UNICODE fillchar = ' '; |
| 7637 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7638 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7639 | return NULL; |
| 7640 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7641 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7642 | Py_INCREF(self); |
| 7643 | return (PyObject*) self; |
| 7644 | } |
| 7645 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7646 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7647 | } |
| 7648 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7649 | PyObject *PyUnicode_Split(PyObject *s, |
| 7650 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7651 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7652 | { |
| 7653 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7654 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7655 | s = PyUnicode_FromObject(s); |
| 7656 | if (s == NULL) |
| 7657 | return NULL; |
| 7658 | if (sep != NULL) { |
| 7659 | sep = PyUnicode_FromObject(sep); |
| 7660 | if (sep == NULL) { |
| 7661 | Py_DECREF(s); |
| 7662 | return NULL; |
| 7663 | } |
| 7664 | } |
| 7665 | |
| 7666 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 7667 | |
| 7668 | Py_DECREF(s); |
| 7669 | Py_XDECREF(sep); |
| 7670 | return result; |
| 7671 | } |
| 7672 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7673 | PyDoc_STRVAR(split__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7674 | "S.split([sep [,maxsplit]]) -> list of strings\n\ |
| 7675 | \n\ |
| 7676 | Return a list of the words in S, using sep as the\n\ |
| 7677 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Thomas Heller | ca0d2cb | 2004-09-15 11:41:32 +0000 | [diff] [blame] | 7678 | 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] | 7679 | any whitespace string is a separator."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7680 | |
| 7681 | static PyObject* |
| 7682 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 7683 | { |
| 7684 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7685 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7686 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7687 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7688 | return NULL; |
| 7689 | |
| 7690 | if (substring == Py_None) |
| 7691 | return split(self, NULL, maxcount); |
| 7692 | else if (PyUnicode_Check(substring)) |
| 7693 | return split(self, (PyUnicodeObject *)substring, maxcount); |
| 7694 | else |
| 7695 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
| 7696 | } |
| 7697 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7698 | PyObject * |
| 7699 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 7700 | { |
| 7701 | PyObject* str_obj; |
| 7702 | PyObject* sep_obj; |
| 7703 | PyObject* out; |
| 7704 | |
| 7705 | str_obj = PyUnicode_FromObject(str_in); |
| 7706 | if (!str_obj) |
| 7707 | return NULL; |
| 7708 | sep_obj = PyUnicode_FromObject(sep_in); |
| 7709 | if (!sep_obj) { |
| 7710 | Py_DECREF(str_obj); |
| 7711 | return NULL; |
| 7712 | } |
| 7713 | |
| 7714 | out = stringlib_partition( |
| 7715 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 7716 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 7717 | ); |
| 7718 | |
| 7719 | Py_DECREF(sep_obj); |
| 7720 | Py_DECREF(str_obj); |
| 7721 | |
| 7722 | return out; |
| 7723 | } |
| 7724 | |
| 7725 | |
| 7726 | PyObject * |
| 7727 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 7728 | { |
| 7729 | PyObject* str_obj; |
| 7730 | PyObject* sep_obj; |
| 7731 | PyObject* out; |
| 7732 | |
| 7733 | str_obj = PyUnicode_FromObject(str_in); |
| 7734 | if (!str_obj) |
| 7735 | return NULL; |
| 7736 | sep_obj = PyUnicode_FromObject(sep_in); |
| 7737 | if (!sep_obj) { |
| 7738 | Py_DECREF(str_obj); |
| 7739 | return NULL; |
| 7740 | } |
| 7741 | |
| 7742 | out = stringlib_rpartition( |
| 7743 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 7744 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 7745 | ); |
| 7746 | |
| 7747 | Py_DECREF(sep_obj); |
| 7748 | Py_DECREF(str_obj); |
| 7749 | |
| 7750 | return out; |
| 7751 | } |
| 7752 | |
| 7753 | PyDoc_STRVAR(partition__doc__, |
| 7754 | "S.partition(sep) -> (head, sep, tail)\n\ |
| 7755 | \n\ |
| 7756 | Searches for the separator sep in S, and returns the part before it,\n\ |
| 7757 | the separator itself, and the part after it. If the separator is not\n\ |
| 7758 | found, returns S and two empty strings."); |
| 7759 | |
| 7760 | static PyObject* |
| 7761 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
| 7762 | { |
| 7763 | return PyUnicode_Partition((PyObject *)self, separator); |
| 7764 | } |
| 7765 | |
| 7766 | PyDoc_STRVAR(rpartition__doc__, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7767 | "S.rpartition(sep) -> (tail, sep, head)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7768 | \n\ |
| 7769 | Searches for the separator sep in S, starting at the end of S, and returns\n\ |
| 7770 | 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] | 7771 | separator is not found, returns two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7772 | |
| 7773 | static PyObject* |
| 7774 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 7775 | { |
| 7776 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 7777 | } |
| 7778 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7779 | PyObject *PyUnicode_RSplit(PyObject *s, |
| 7780 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7781 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7782 | { |
| 7783 | PyObject *result; |
| 7784 | |
| 7785 | s = PyUnicode_FromObject(s); |
| 7786 | if (s == NULL) |
| 7787 | return NULL; |
| 7788 | if (sep != NULL) { |
| 7789 | sep = PyUnicode_FromObject(sep); |
| 7790 | if (sep == NULL) { |
| 7791 | Py_DECREF(s); |
| 7792 | return NULL; |
| 7793 | } |
| 7794 | } |
| 7795 | |
| 7796 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 7797 | |
| 7798 | Py_DECREF(s); |
| 7799 | Py_XDECREF(sep); |
| 7800 | return result; |
| 7801 | } |
| 7802 | |
| 7803 | PyDoc_STRVAR(rsplit__doc__, |
| 7804 | "S.rsplit([sep [,maxsplit]]) -> list of strings\n\ |
| 7805 | \n\ |
| 7806 | Return a list of the words in S, using sep as the\n\ |
| 7807 | delimiter string, starting at the end of the string and\n\ |
| 7808 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 7809 | splits are done. If sep is not specified, any whitespace string\n\ |
| 7810 | is a separator."); |
| 7811 | |
| 7812 | static PyObject* |
| 7813 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 7814 | { |
| 7815 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7816 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7817 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7818 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7819 | return NULL; |
| 7820 | |
| 7821 | if (substring == Py_None) |
| 7822 | return rsplit(self, NULL, maxcount); |
| 7823 | else if (PyUnicode_Check(substring)) |
| 7824 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
| 7825 | else |
| 7826 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
| 7827 | } |
| 7828 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7829 | PyDoc_STRVAR(splitlines__doc__, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7830 | "S.splitlines([keepends]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7831 | \n\ |
| 7832 | 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] | 7833 | 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] | 7834 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7835 | |
| 7836 | static PyObject* |
| 7837 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 7838 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7839 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7840 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7841 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7842 | return NULL; |
| 7843 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7844 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7845 | } |
| 7846 | |
| 7847 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 7848 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7849 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 7850 | if (PyUnicode_CheckExact(self)) { |
| 7851 | Py_INCREF(self); |
| 7852 | return self; |
| 7853 | } else |
| 7854 | /* Subtype -- return genuine unicode string with the same value. */ |
| 7855 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(self), |
| 7856 | PyUnicode_GET_SIZE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7857 | } |
| 7858 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7859 | PyDoc_STRVAR(swapcase__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7860 | "S.swapcase() -> unicode\n\ |
| 7861 | \n\ |
| 7862 | 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] | 7863 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7864 | |
| 7865 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7866 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7867 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7868 | return fixup(self, fixswapcase); |
| 7869 | } |
| 7870 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 7871 | PyDoc_STRVAR(maketrans__doc__, |
| 7872 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
| 7873 | \n\ |
| 7874 | Return a translation table usable for str.translate().\n\ |
| 7875 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 7876 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
| 7877 | Character keys will then be converted to ordinals.\n\ |
| 7878 | If there are two arguments, they must be strings of equal length, and\n\ |
| 7879 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 7880 | character at the same position in y. If there is a third argument, it\n\ |
| 7881 | must be a string, whose characters will be mapped to None in the result."); |
| 7882 | |
| 7883 | static PyObject* |
| 7884 | unicode_maketrans(PyUnicodeObject *null, PyObject *args) |
| 7885 | { |
| 7886 | PyObject *x, *y = NULL, *z = NULL; |
| 7887 | PyObject *new = NULL, *key, *value; |
| 7888 | Py_ssize_t i = 0; |
| 7889 | int res; |
| 7890 | |
| 7891 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 7892 | return NULL; |
| 7893 | new = PyDict_New(); |
| 7894 | if (!new) |
| 7895 | return NULL; |
| 7896 | if (y != NULL) { |
| 7897 | /* x must be a string too, of equal length */ |
| 7898 | Py_ssize_t ylen = PyUnicode_GET_SIZE(y); |
| 7899 | if (!PyUnicode_Check(x)) { |
| 7900 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 7901 | "be a string if there is a second argument"); |
| 7902 | goto err; |
| 7903 | } |
| 7904 | if (PyUnicode_GET_SIZE(x) != ylen) { |
| 7905 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 7906 | "arguments must have equal length"); |
| 7907 | goto err; |
| 7908 | } |
| 7909 | /* create entries for translating chars in x to those in y */ |
| 7910 | for (i = 0; i < PyUnicode_GET_SIZE(x); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7911 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(x)[i]); |
| 7912 | value = PyLong_FromLong(PyUnicode_AS_UNICODE(y)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 7913 | if (!key || !value) |
| 7914 | goto err; |
| 7915 | res = PyDict_SetItem(new, key, value); |
| 7916 | Py_DECREF(key); |
| 7917 | Py_DECREF(value); |
| 7918 | if (res < 0) |
| 7919 | goto err; |
| 7920 | } |
| 7921 | /* create entries for deleting chars in z */ |
| 7922 | if (z != NULL) { |
| 7923 | for (i = 0; i < PyUnicode_GET_SIZE(z); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7924 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(z)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 7925 | if (!key) |
| 7926 | goto err; |
| 7927 | res = PyDict_SetItem(new, key, Py_None); |
| 7928 | Py_DECREF(key); |
| 7929 | if (res < 0) |
| 7930 | goto err; |
| 7931 | } |
| 7932 | } |
| 7933 | } else { |
| 7934 | /* x must be a dict */ |
| 7935 | if (!PyDict_Check(x)) { |
| 7936 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 7937 | "to maketrans it must be a dict"); |
| 7938 | goto err; |
| 7939 | } |
| 7940 | /* copy entries into the new dict, converting string keys to int keys */ |
| 7941 | while (PyDict_Next(x, &i, &key, &value)) { |
| 7942 | if (PyUnicode_Check(key)) { |
| 7943 | /* convert string keys to integer keys */ |
| 7944 | PyObject *newkey; |
| 7945 | if (PyUnicode_GET_SIZE(key) != 1) { |
| 7946 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 7947 | "table must be of length 1"); |
| 7948 | goto err; |
| 7949 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7950 | newkey = PyLong_FromLong(PyUnicode_AS_UNICODE(key)[0]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 7951 | if (!newkey) |
| 7952 | goto err; |
| 7953 | res = PyDict_SetItem(new, newkey, value); |
| 7954 | Py_DECREF(newkey); |
| 7955 | if (res < 0) |
| 7956 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7957 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 7958 | /* just keep integer keys */ |
| 7959 | if (PyDict_SetItem(new, key, value) < 0) |
| 7960 | goto err; |
| 7961 | } else { |
| 7962 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 7963 | "be strings or integers"); |
| 7964 | goto err; |
| 7965 | } |
| 7966 | } |
| 7967 | } |
| 7968 | return new; |
| 7969 | err: |
| 7970 | Py_DECREF(new); |
| 7971 | return NULL; |
| 7972 | } |
| 7973 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7974 | PyDoc_STRVAR(translate__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7975 | "S.translate(table) -> unicode\n\ |
| 7976 | \n\ |
| 7977 | Return a copy of the string S, where all characters have been mapped\n\ |
| 7978 | 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] | 7979 | Unicode ordinals to Unicode ordinals, Unicode strings or None.\n\ |
| 7980 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 7981 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7982 | |
| 7983 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7984 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7985 | { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 7986 | return PyUnicode_TranslateCharmap(self->str, self->length, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7987 | } |
| 7988 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7989 | PyDoc_STRVAR(upper__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7990 | "S.upper() -> unicode\n\ |
| 7991 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7992 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7993 | |
| 7994 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7995 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7996 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7997 | return fixup(self, fixupper); |
| 7998 | } |
| 7999 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8000 | PyDoc_STRVAR(zfill__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8001 | "S.zfill(width) -> unicode\n\ |
| 8002 | \n\ |
| 8003 | 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] | 8004 | of the specified width. The string x is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8005 | |
| 8006 | static PyObject * |
| 8007 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 8008 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8009 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8010 | PyUnicodeObject *u; |
| 8011 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8012 | Py_ssize_t width; |
| 8013 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8014 | return NULL; |
| 8015 | |
| 8016 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 8017 | if (PyUnicode_CheckExact(self)) { |
| 8018 | Py_INCREF(self); |
| 8019 | return (PyObject*) self; |
| 8020 | } |
| 8021 | else |
| 8022 | return PyUnicode_FromUnicode( |
| 8023 | PyUnicode_AS_UNICODE(self), |
| 8024 | PyUnicode_GET_SIZE(self) |
| 8025 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8026 | } |
| 8027 | |
| 8028 | fill = width - self->length; |
| 8029 | |
| 8030 | u = pad(self, fill, 0, '0'); |
| 8031 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8032 | if (u == NULL) |
| 8033 | return NULL; |
| 8034 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8035 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 8036 | /* move sign to beginning of string */ |
| 8037 | u->str[0] = u->str[fill]; |
| 8038 | u->str[fill] = '0'; |
| 8039 | } |
| 8040 | |
| 8041 | return (PyObject*) u; |
| 8042 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8043 | |
| 8044 | #if 0 |
| 8045 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8046 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8047 | { |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 8048 | return PyLong_FromLong(numfree); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8049 | } |
| 8050 | #endif |
| 8051 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8052 | PyDoc_STRVAR(startswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 8053 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8054 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 8055 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 8056 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8057 | With optional end, stop comparing S at that position.\n\ |
| 8058 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8059 | |
| 8060 | static PyObject * |
| 8061 | unicode_startswith(PyUnicodeObject *self, |
| 8062 | PyObject *args) |
| 8063 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8064 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8065 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8066 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8067 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8068 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8069 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8070 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 8071 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8072 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8073 | if (PyTuple_Check(subobj)) { |
| 8074 | Py_ssize_t i; |
| 8075 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 8076 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 8077 | PyTuple_GET_ITEM(subobj, i)); |
| 8078 | if (substring == NULL) |
| 8079 | return NULL; |
| 8080 | result = tailmatch(self, substring, start, end, -1); |
| 8081 | Py_DECREF(substring); |
| 8082 | if (result) { |
| 8083 | Py_RETURN_TRUE; |
| 8084 | } |
| 8085 | } |
| 8086 | /* nothing matched */ |
| 8087 | Py_RETURN_FALSE; |
| 8088 | } |
| 8089 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8090 | if (substring == NULL) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8091 | return NULL; |
| 8092 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8093 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8094 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8095 | } |
| 8096 | |
| 8097 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8098 | PyDoc_STRVAR(endswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 8099 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8100 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 8101 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 8102 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8103 | With optional end, stop comparing S at that position.\n\ |
| 8104 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8105 | |
| 8106 | static PyObject * |
| 8107 | unicode_endswith(PyUnicodeObject *self, |
| 8108 | PyObject *args) |
| 8109 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8110 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8111 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8112 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8113 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8114 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8115 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8116 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
| 8117 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8118 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8119 | if (PyTuple_Check(subobj)) { |
| 8120 | Py_ssize_t i; |
| 8121 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 8122 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 8123 | PyTuple_GET_ITEM(subobj, i)); |
| 8124 | if (substring == NULL) |
| 8125 | return NULL; |
| 8126 | result = tailmatch(self, substring, start, end, +1); |
| 8127 | Py_DECREF(substring); |
| 8128 | if (result) { |
| 8129 | Py_RETURN_TRUE; |
| 8130 | } |
| 8131 | } |
| 8132 | Py_RETURN_FALSE; |
| 8133 | } |
| 8134 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8135 | if (substring == NULL) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8136 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8137 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8138 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8139 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8140 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8141 | } |
| 8142 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8143 | #include "stringlib/string_format.h" |
| 8144 | |
| 8145 | PyDoc_STRVAR(format__doc__, |
| 8146 | "S.format(*args, **kwargs) -> unicode\n\ |
| 8147 | \n\ |
| 8148 | "); |
| 8149 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8150 | PyDoc_STRVAR(p_format__doc__, |
| 8151 | "S.__format__(format_spec) -> unicode\n\ |
| 8152 | \n\ |
| 8153 | "); |
| 8154 | |
| 8155 | static PyObject * |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8156 | unicode_getnewargs(PyUnicodeObject *v) |
| 8157 | { |
| 8158 | return Py_BuildValue("(u#)", v->str, v->length); |
| 8159 | } |
| 8160 | |
| 8161 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8162 | static PyMethodDef unicode_methods[] = { |
| 8163 | |
| 8164 | /* Order is according to common usage: often used methods should |
| 8165 | appear first, since lookup is done sequentially. */ |
| 8166 | |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8167 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS, encode__doc__}, |
| 8168 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 8169 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8170 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8171 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 8172 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 8173 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 8174 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 8175 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 8176 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 8177 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8178 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8179 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 8180 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 8181 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8182 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8183 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 8184 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 8185 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8186 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8187 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8188 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8189 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8190 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 8191 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 8192 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 8193 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 8194 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 8195 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 8196 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 8197 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 8198 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 8199 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 8200 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 8201 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 8202 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 8203 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 8204 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8205 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 8206 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
| 8207 | {"__format__", (PyCFunction) unicode_unicode__format__, METH_VARARGS, p_format__doc__}, |
Eric Smith | f6db409 | 2007-08-27 23:52:26 +0000 | [diff] [blame] | 8208 | {"_formatter_field_name_split", (PyCFunction) formatter_field_name_split, METH_NOARGS}, |
| 8209 | {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8210 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 8211 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8212 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8213 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8214 | #endif |
| 8215 | |
| 8216 | #if 0 |
| 8217 | /* This one is just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8218 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8219 | #endif |
| 8220 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8221 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8222 | {NULL, NULL} |
| 8223 | }; |
| 8224 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8225 | static PyObject * |
| 8226 | unicode_mod(PyObject *v, PyObject *w) |
| 8227 | { |
| 8228 | if (!PyUnicode_Check(v)) { |
| 8229 | Py_INCREF(Py_NotImplemented); |
| 8230 | return Py_NotImplemented; |
| 8231 | } |
| 8232 | return PyUnicode_Format(v, w); |
| 8233 | } |
| 8234 | |
| 8235 | static PyNumberMethods unicode_as_number = { |
| 8236 | 0, /*nb_add*/ |
| 8237 | 0, /*nb_subtract*/ |
| 8238 | 0, /*nb_multiply*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8239 | unicode_mod, /*nb_remainder*/ |
| 8240 | }; |
| 8241 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8242 | static PySequenceMethods unicode_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8243 | (lenfunc) unicode_length, /* sq_length */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8244 | PyUnicode_Concat, /* sq_concat */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8245 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 8246 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
Thomas Wouters | d2cf20e | 2007-08-30 22:57:53 +0000 | [diff] [blame] | 8247 | 0, /* sq_slice */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8248 | 0, /* sq_ass_item */ |
| 8249 | 0, /* sq_ass_slice */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8250 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8251 | }; |
| 8252 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8253 | static PyObject* |
| 8254 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 8255 | { |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 8256 | if (PyIndex_Check(item)) { |
| 8257 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8258 | if (i == -1 && PyErr_Occurred()) |
| 8259 | return NULL; |
| 8260 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8261 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8262 | return unicode_getitem(self, i); |
| 8263 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8264 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8265 | Py_UNICODE* source_buf; |
| 8266 | Py_UNICODE* result_buf; |
| 8267 | PyObject* result; |
| 8268 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8269 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8270 | &start, &stop, &step, &slicelength) < 0) { |
| 8271 | return NULL; |
| 8272 | } |
| 8273 | |
| 8274 | if (slicelength <= 0) { |
| 8275 | return PyUnicode_FromUnicode(NULL, 0); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 8276 | } else if (start == 0 && step == 1 && slicelength == self->length && |
| 8277 | PyUnicode_CheckExact(self)) { |
| 8278 | Py_INCREF(self); |
| 8279 | return (PyObject *)self; |
| 8280 | } else if (step == 1) { |
| 8281 | return PyUnicode_FromUnicode(self->str + start, slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8282 | } else { |
| 8283 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 8284 | result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* |
| 8285 | sizeof(Py_UNICODE)); |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8286 | |
| 8287 | if (result_buf == NULL) |
| 8288 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8289 | |
| 8290 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 8291 | result_buf[i] = source_buf[cur]; |
| 8292 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8293 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8294 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 8295 | PyObject_FREE(result_buf); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8296 | return result; |
| 8297 | } |
| 8298 | } else { |
| 8299 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 8300 | return NULL; |
| 8301 | } |
| 8302 | } |
| 8303 | |
| 8304 | static PyMappingMethods unicode_as_mapping = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8305 | (lenfunc)unicode_length, /* mp_length */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8306 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 8307 | (objobjargproc)0, /* mp_ass_subscript */ |
| 8308 | }; |
| 8309 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8310 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8311 | /* Helpers for PyUnicode_Format() */ |
| 8312 | |
| 8313 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8314 | 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] | 8315 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8316 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8317 | if (argidx < arglen) { |
| 8318 | (*p_argidx)++; |
| 8319 | if (arglen < 0) |
| 8320 | return args; |
| 8321 | else |
| 8322 | return PyTuple_GetItem(args, argidx); |
| 8323 | } |
| 8324 | PyErr_SetString(PyExc_TypeError, |
| 8325 | "not enough arguments for format string"); |
| 8326 | return NULL; |
| 8327 | } |
| 8328 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8329 | static Py_ssize_t |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8330 | strtounicode(Py_UNICODE *buffer, const char *charbuffer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8331 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8332 | register Py_ssize_t i; |
| 8333 | Py_ssize_t len = strlen(charbuffer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8334 | for (i = len - 1; i >= 0; i--) |
| 8335 | buffer[i] = (Py_UNICODE) charbuffer[i]; |
| 8336 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8337 | return len; |
| 8338 | } |
| 8339 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8340 | static int |
| 8341 | doubletounicode(Py_UNICODE *buffer, size_t len, const char *format, double x) |
| 8342 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8343 | Py_ssize_t result; |
| 8344 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8345 | PyOS_ascii_formatd((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8346 | result = strtounicode(buffer, (char *)buffer); |
| 8347 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8348 | } |
| 8349 | |
Christian Heimes | 3fd1399 | 2008-03-21 01:05:49 +0000 | [diff] [blame^] | 8350 | #if 0 |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8351 | static int |
| 8352 | longtounicode(Py_UNICODE *buffer, size_t len, const char *format, long x) |
| 8353 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8354 | Py_ssize_t result; |
| 8355 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8356 | PyOS_snprintf((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8357 | result = strtounicode(buffer, (char *)buffer); |
| 8358 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8359 | } |
Christian Heimes | 3fd1399 | 2008-03-21 01:05:49 +0000 | [diff] [blame^] | 8360 | #endif |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8361 | |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 8362 | /* XXX To save some code duplication, formatfloat/long/int could have been |
| 8363 | shared with stringobject.c, converting from 8-bit to Unicode after the |
| 8364 | formatting is done. */ |
| 8365 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8366 | static int |
| 8367 | formatfloat(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8368 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8369 | int flags, |
| 8370 | int prec, |
| 8371 | int type, |
| 8372 | PyObject *v) |
| 8373 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8374 | /* fmt = '%#.' + `prec` + `type` |
| 8375 | 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] | 8376 | char fmt[20]; |
| 8377 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8378 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8379 | x = PyFloat_AsDouble(v); |
| 8380 | if (x == -1.0 && PyErr_Occurred()) |
| 8381 | return -1; |
| 8382 | if (prec < 0) |
| 8383 | prec = 6; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8384 | if (type == 'f' && (fabs(x) / 1e25) >= 1e25) |
| 8385 | type = 'g'; |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8386 | /* Worst case length calc to ensure no buffer overrun: |
| 8387 | |
| 8388 | 'g' formats: |
| 8389 | fmt = %#.<prec>g |
| 8390 | buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
| 8391 | for any double rep.) |
| 8392 | len = 1 + prec + 1 + 2 + 5 = 9 + prec |
| 8393 | |
| 8394 | 'f' formats: |
| 8395 | buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
| 8396 | len = 1 + 50 + 1 + prec = 52 + prec |
| 8397 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8398 | If prec=0 the effective precision is 1 (the leading digit is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8399 | always given), therefore increase the length by one. |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8400 | |
| 8401 | */ |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 8402 | if (((type == 'g' || type == 'G') && |
| 8403 | buflen <= (size_t)10 + (size_t)prec) || |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8404 | (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8405 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8406 | "formatted float is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8407 | return -1; |
| 8408 | } |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8409 | PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
| 8410 | (flags&F_ALT) ? "#" : "", |
| 8411 | prec, type); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8412 | return doubletounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8413 | } |
| 8414 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8415 | static PyObject* |
| 8416 | formatlong(PyObject *val, int flags, int prec, int type) |
| 8417 | { |
| 8418 | char *buf; |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8419 | int len; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8420 | PyObject *str; /* temporary string object. */ |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8421 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8422 | |
| 8423 | str = _PyString_FormatLong(val, flags, prec, type, &buf, &len); |
| 8424 | if (!str) |
| 8425 | return NULL; |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8426 | result = PyUnicode_FromStringAndSize(buf, len); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8427 | Py_DECREF(str); |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8428 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8429 | } |
| 8430 | |
Christian Heimes | 3fd1399 | 2008-03-21 01:05:49 +0000 | [diff] [blame^] | 8431 | #if 0 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8432 | static int |
| 8433 | formatint(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8434 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8435 | int flags, |
| 8436 | int prec, |
| 8437 | int type, |
| 8438 | PyObject *v) |
| 8439 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8440 | /* fmt = '%#.' + `prec` + 'l' + `type` |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8441 | * worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
| 8442 | * + 1 + 1 |
| 8443 | * = 24 |
| 8444 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8445 | char fmt[64]; /* plenty big enough! */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8446 | char *sign; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8447 | long x; |
| 8448 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8449 | x = PyLong_AsLong(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8450 | if (x == -1 && PyErr_Occurred()) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8451 | return -1; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8452 | if (x < 0 && type == 'u') { |
| 8453 | type = 'd'; |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 8454 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8455 | if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) |
| 8456 | sign = "-"; |
| 8457 | else |
| 8458 | sign = ""; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8459 | if (prec < 0) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8460 | prec = 1; |
| 8461 | |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8462 | /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) |
| 8463 | * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8464 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8465 | if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8466 | PyErr_SetString(PyExc_OverflowError, |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8467 | "formatted integer is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8468 | return -1; |
| 8469 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8470 | |
| 8471 | if ((flags & F_ALT) && |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 8472 | (type == 'x' || type == 'X' || type == 'o')) { |
| 8473 | /* When converting under %#o, %#x or %#X, there are a number |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8474 | * of issues that cause pain: |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 8475 | * - for %#o, we want a different base marker than C |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8476 | * - when 0 is being converted, the C standard leaves off |
| 8477 | * the '0x' or '0X', which is inconsistent with other |
| 8478 | * %#x/%#X conversions and inconsistent with Python's |
| 8479 | * hex() function |
| 8480 | * - there are platforms that violate the standard and |
| 8481 | * convert 0 with the '0x' or '0X' |
| 8482 | * (Metrowerks, Compaq Tru64) |
| 8483 | * - there are platforms that give '0x' when converting |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8484 | * under %#X, but convert 0 in accordance with the |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8485 | * standard (OS/2 EMX) |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8486 | * |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8487 | * We can achieve the desired consistency by inserting our |
| 8488 | * own '0x' or '0X' prefix, and substituting %x/%X in place |
| 8489 | * of %#x/%#X. |
| 8490 | * |
| 8491 | * Note that this is the same approach as used in |
| 8492 | * formatint() in stringobject.c |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 8493 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8494 | PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", |
| 8495 | sign, type, prec, type); |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 8496 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8497 | else { |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8498 | PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", |
| 8499 | sign, (flags&F_ALT) ? "#" : "", |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8500 | prec, type); |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 8501 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8502 | if (sign[0]) |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8503 | return longtounicode(buf, buflen, fmt, -x); |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8504 | else |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8505 | return longtounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8506 | } |
Christian Heimes | 3fd1399 | 2008-03-21 01:05:49 +0000 | [diff] [blame^] | 8507 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8508 | |
| 8509 | static int |
| 8510 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8511 | size_t buflen, |
| 8512 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8513 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8514 | /* presume that the buffer is at least 2 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8515 | if (PyUnicode_Check(v)) { |
| 8516 | if (PyUnicode_GET_SIZE(v) != 1) |
| 8517 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8518 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8519 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8520 | else { |
| 8521 | /* Integer input truncated to a character */ |
| 8522 | long x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8523 | x = PyLong_AsLong(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8524 | if (x == -1 && PyErr_Occurred()) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8525 | goto onError; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8526 | #ifdef Py_UNICODE_WIDE |
| 8527 | if (x < 0 || x > 0x10ffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 8528 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8529 | "%c arg not in range(0x110000) " |
| 8530 | "(wide Python build)"); |
| 8531 | return -1; |
| 8532 | } |
| 8533 | #else |
| 8534 | if (x < 0 || x > 0xffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 8535 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8536 | "%c arg not in range(0x10000) " |
| 8537 | "(narrow Python build)"); |
| 8538 | return -1; |
| 8539 | } |
| 8540 | #endif |
| 8541 | buf[0] = (Py_UNICODE) x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8542 | } |
| 8543 | buf[1] = '\0'; |
| 8544 | return 1; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8545 | |
| 8546 | onError: |
| 8547 | PyErr_SetString(PyExc_TypeError, |
| 8548 | "%c requires int or char"); |
| 8549 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8550 | } |
| 8551 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8552 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 8553 | |
| 8554 | FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
| 8555 | chars are formatted. XXX This is a magic number. Each formatting |
| 8556 | routine does bounds checking to ensure no overflow, but a better |
| 8557 | solution may be to malloc a buffer of appropriate size for each |
| 8558 | format. For now, the current solution is sufficient. |
| 8559 | */ |
| 8560 | #define FORMATBUFLEN (size_t)120 |
| 8561 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8562 | PyObject *PyUnicode_Format(PyObject *format, |
| 8563 | PyObject *args) |
| 8564 | { |
| 8565 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8566 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8567 | int args_owned = 0; |
| 8568 | PyUnicodeObject *result = NULL; |
| 8569 | PyObject *dict = NULL; |
| 8570 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8571 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8572 | if (format == NULL || args == NULL) { |
| 8573 | PyErr_BadInternalCall(); |
| 8574 | return NULL; |
| 8575 | } |
| 8576 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8577 | if (uformat == NULL) |
| 8578 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8579 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 8580 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 8581 | |
| 8582 | reslen = rescnt = fmtcnt + 100; |
| 8583 | result = _PyUnicode_New(reslen); |
| 8584 | if (result == NULL) |
| 8585 | goto onError; |
| 8586 | res = PyUnicode_AS_UNICODE(result); |
| 8587 | |
| 8588 | if (PyTuple_Check(args)) { |
| 8589 | arglen = PyTuple_Size(args); |
| 8590 | argidx = 0; |
| 8591 | } |
| 8592 | else { |
| 8593 | arglen = -1; |
| 8594 | argidx = -2; |
| 8595 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8596 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 8597 | !PyUnicode_Check(args)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8598 | dict = args; |
| 8599 | |
| 8600 | while (--fmtcnt >= 0) { |
| 8601 | if (*fmt != '%') { |
| 8602 | if (--rescnt < 0) { |
| 8603 | rescnt = fmtcnt + 100; |
| 8604 | reslen += rescnt; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8605 | if (_PyUnicode_Resize(&result, reslen) < 0) |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 8606 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8607 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 8608 | --rescnt; |
| 8609 | } |
| 8610 | *res++ = *fmt++; |
| 8611 | } |
| 8612 | else { |
| 8613 | /* Got a format specifier */ |
| 8614 | int flags = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8615 | Py_ssize_t width = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8616 | int prec = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8617 | Py_UNICODE c = '\0'; |
| 8618 | Py_UNICODE fill; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 8619 | int isnumok; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8620 | PyObject *v = NULL; |
| 8621 | PyObject *temp = NULL; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8622 | Py_UNICODE *pbuf; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8623 | Py_UNICODE sign; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8624 | Py_ssize_t len; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8625 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8626 | |
| 8627 | fmt++; |
| 8628 | if (*fmt == '(') { |
| 8629 | Py_UNICODE *keystart; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8630 | Py_ssize_t keylen; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8631 | PyObject *key; |
| 8632 | int pcount = 1; |
| 8633 | |
| 8634 | if (dict == NULL) { |
| 8635 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8636 | "format requires a mapping"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8637 | goto onError; |
| 8638 | } |
| 8639 | ++fmt; |
| 8640 | --fmtcnt; |
| 8641 | keystart = fmt; |
| 8642 | /* Skip over balanced parentheses */ |
| 8643 | while (pcount > 0 && --fmtcnt >= 0) { |
| 8644 | if (*fmt == ')') |
| 8645 | --pcount; |
| 8646 | else if (*fmt == '(') |
| 8647 | ++pcount; |
| 8648 | fmt++; |
| 8649 | } |
| 8650 | keylen = fmt - keystart - 1; |
| 8651 | if (fmtcnt < 0 || pcount > 0) { |
| 8652 | PyErr_SetString(PyExc_ValueError, |
| 8653 | "incomplete format key"); |
| 8654 | goto onError; |
| 8655 | } |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 8656 | #if 0 |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8657 | /* keys are converted to strings using UTF-8 and |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8658 | then looked up since Python uses strings to hold |
| 8659 | variables names etc. in its namespaces and we |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8660 | wouldn't want to break common idioms. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8661 | key = PyUnicode_EncodeUTF8(keystart, |
| 8662 | keylen, |
| 8663 | NULL); |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 8664 | #else |
| 8665 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 8666 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8667 | if (key == NULL) |
| 8668 | goto onError; |
| 8669 | if (args_owned) { |
| 8670 | Py_DECREF(args); |
| 8671 | args_owned = 0; |
| 8672 | } |
| 8673 | args = PyObject_GetItem(dict, key); |
| 8674 | Py_DECREF(key); |
| 8675 | if (args == NULL) { |
| 8676 | goto onError; |
| 8677 | } |
| 8678 | args_owned = 1; |
| 8679 | arglen = -1; |
| 8680 | argidx = -2; |
| 8681 | } |
| 8682 | while (--fmtcnt >= 0) { |
| 8683 | switch (c = *fmt++) { |
| 8684 | case '-': flags |= F_LJUST; continue; |
| 8685 | case '+': flags |= F_SIGN; continue; |
| 8686 | case ' ': flags |= F_BLANK; continue; |
| 8687 | case '#': flags |= F_ALT; continue; |
| 8688 | case '0': flags |= F_ZERO; continue; |
| 8689 | } |
| 8690 | break; |
| 8691 | } |
| 8692 | if (c == '*') { |
| 8693 | v = getnextarg(args, arglen, &argidx); |
| 8694 | if (v == NULL) |
| 8695 | goto onError; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8696 | if (!PyLong_Check(v)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8697 | PyErr_SetString(PyExc_TypeError, |
| 8698 | "* wants int"); |
| 8699 | goto onError; |
| 8700 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8701 | width = PyLong_AsLong(v); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 8702 | if (width == -1 && PyErr_Occurred()) |
| 8703 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8704 | if (width < 0) { |
| 8705 | flags |= F_LJUST; |
| 8706 | width = -width; |
| 8707 | } |
| 8708 | if (--fmtcnt >= 0) |
| 8709 | c = *fmt++; |
| 8710 | } |
| 8711 | else if (c >= '0' && c <= '9') { |
| 8712 | width = c - '0'; |
| 8713 | while (--fmtcnt >= 0) { |
| 8714 | c = *fmt++; |
| 8715 | if (c < '0' || c > '9') |
| 8716 | break; |
| 8717 | if ((width*10) / 10 != width) { |
| 8718 | PyErr_SetString(PyExc_ValueError, |
| 8719 | "width too big"); |
| 8720 | goto onError; |
| 8721 | } |
| 8722 | width = width*10 + (c - '0'); |
| 8723 | } |
| 8724 | } |
| 8725 | if (c == '.') { |
| 8726 | prec = 0; |
| 8727 | if (--fmtcnt >= 0) |
| 8728 | c = *fmt++; |
| 8729 | if (c == '*') { |
| 8730 | v = getnextarg(args, arglen, &argidx); |
| 8731 | if (v == NULL) |
| 8732 | goto onError; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8733 | if (!PyLong_Check(v)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8734 | PyErr_SetString(PyExc_TypeError, |
| 8735 | "* wants int"); |
| 8736 | goto onError; |
| 8737 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8738 | prec = PyLong_AsLong(v); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 8739 | if (prec == -1 && PyErr_Occurred()) |
| 8740 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8741 | if (prec < 0) |
| 8742 | prec = 0; |
| 8743 | if (--fmtcnt >= 0) |
| 8744 | c = *fmt++; |
| 8745 | } |
| 8746 | else if (c >= '0' && c <= '9') { |
| 8747 | prec = c - '0'; |
| 8748 | while (--fmtcnt >= 0) { |
| 8749 | c = Py_CHARMASK(*fmt++); |
| 8750 | if (c < '0' || c > '9') |
| 8751 | break; |
| 8752 | if ((prec*10) / 10 != prec) { |
| 8753 | PyErr_SetString(PyExc_ValueError, |
| 8754 | "prec too big"); |
| 8755 | goto onError; |
| 8756 | } |
| 8757 | prec = prec*10 + (c - '0'); |
| 8758 | } |
| 8759 | } |
| 8760 | } /* prec */ |
| 8761 | if (fmtcnt >= 0) { |
| 8762 | if (c == 'h' || c == 'l' || c == 'L') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8763 | if (--fmtcnt >= 0) |
| 8764 | c = *fmt++; |
| 8765 | } |
| 8766 | } |
| 8767 | if (fmtcnt < 0) { |
| 8768 | PyErr_SetString(PyExc_ValueError, |
| 8769 | "incomplete format"); |
| 8770 | goto onError; |
| 8771 | } |
| 8772 | if (c != '%') { |
| 8773 | v = getnextarg(args, arglen, &argidx); |
| 8774 | if (v == NULL) |
| 8775 | goto onError; |
| 8776 | } |
| 8777 | sign = 0; |
| 8778 | fill = ' '; |
| 8779 | switch (c) { |
| 8780 | |
| 8781 | case '%': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8782 | pbuf = formatbuf; |
| 8783 | /* presume that buffer length is at least 1 */ |
| 8784 | pbuf[0] = '%'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8785 | len = 1; |
| 8786 | break; |
| 8787 | |
| 8788 | case 's': |
| 8789 | case 'r': |
| 8790 | if (PyUnicode_Check(v) && c == 's') { |
| 8791 | temp = v; |
| 8792 | Py_INCREF(temp); |
| 8793 | } |
| 8794 | else { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8795 | if (c == 's') |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 8796 | temp = PyObject_Str(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8797 | else |
| 8798 | temp = PyObject_Repr(v); |
| 8799 | if (temp == NULL) |
| 8800 | goto onError; |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8801 | if (PyUnicode_Check(temp)) |
| 8802 | /* nothing to do */; |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8803 | else { |
| 8804 | Py_DECREF(temp); |
| 8805 | PyErr_SetString(PyExc_TypeError, |
| 8806 | "%s argument has non-string str()"); |
| 8807 | goto onError; |
| 8808 | } |
| 8809 | } |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8810 | pbuf = PyUnicode_AS_UNICODE(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8811 | len = PyUnicode_GET_SIZE(temp); |
| 8812 | if (prec >= 0 && len > prec) |
| 8813 | len = prec; |
| 8814 | break; |
| 8815 | |
| 8816 | case 'i': |
| 8817 | case 'd': |
| 8818 | case 'u': |
| 8819 | case 'o': |
| 8820 | case 'x': |
| 8821 | case 'X': |
| 8822 | if (c == 'i') |
| 8823 | c = 'd'; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 8824 | isnumok = 0; |
| 8825 | if (PyNumber_Check(v)) { |
| 8826 | PyObject *iobj=NULL; |
| 8827 | |
| 8828 | if (PyLong_Check(v)) { |
| 8829 | iobj = v; |
| 8830 | Py_INCREF(iobj); |
| 8831 | } |
| 8832 | else { |
| 8833 | iobj = PyNumber_Long(v); |
| 8834 | } |
| 8835 | if (iobj!=NULL) { |
| 8836 | if (PyLong_Check(iobj)) { |
| 8837 | isnumok = 1; |
| 8838 | temp = formatlong(iobj, flags, prec, c); |
| 8839 | Py_DECREF(iobj); |
| 8840 | if (!temp) |
| 8841 | goto onError; |
| 8842 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 8843 | len = PyUnicode_GET_SIZE(temp); |
| 8844 | sign = 1; |
| 8845 | } |
| 8846 | else { |
| 8847 | Py_DECREF(iobj); |
| 8848 | } |
| 8849 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8850 | } |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 8851 | if (!isnumok) { |
| 8852 | PyErr_Format(PyExc_TypeError, |
| 8853 | "%%%c format: a number is required, " |
| 8854 | "not %.200s", c, Py_TYPE(v)->tp_name); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8855 | goto onError; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8856 | } |
| 8857 | if (flags & F_ZERO) |
| 8858 | fill = '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8859 | break; |
| 8860 | |
| 8861 | case 'e': |
| 8862 | case 'E': |
| 8863 | case 'f': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 8864 | case 'F': |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8865 | case 'g': |
| 8866 | case 'G': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 8867 | if (c == 'F') |
| 8868 | c = 'f'; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8869 | pbuf = formatbuf; |
| 8870 | len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 8871 | flags, prec, c, v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8872 | if (len < 0) |
| 8873 | goto onError; |
| 8874 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8875 | if (flags & F_ZERO) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8876 | fill = '0'; |
| 8877 | break; |
| 8878 | |
| 8879 | case 'c': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8880 | pbuf = formatbuf; |
| 8881 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8882 | if (len < 0) |
| 8883 | goto onError; |
| 8884 | break; |
| 8885 | |
| 8886 | default: |
| 8887 | PyErr_Format(PyExc_ValueError, |
Andrew M. Kuchling | 6ca8917 | 2000-12-15 13:07:46 +0000 | [diff] [blame] | 8888 | "unsupported format character '%c' (0x%x) " |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 8889 | "at index %zd", |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8890 | (31<=c && c<=126) ? (char)c : '?', |
Marc-André Lemburg | 24e53b6 | 2002-09-24 09:32:14 +0000 | [diff] [blame] | 8891 | (int)c, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 8892 | (Py_ssize_t)(fmt - 1 - |
| 8893 | PyUnicode_AS_UNICODE(uformat))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8894 | goto onError; |
| 8895 | } |
| 8896 | if (sign) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8897 | if (*pbuf == '-' || *pbuf == '+') { |
| 8898 | sign = *pbuf++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8899 | len--; |
| 8900 | } |
| 8901 | else if (flags & F_SIGN) |
| 8902 | sign = '+'; |
| 8903 | else if (flags & F_BLANK) |
| 8904 | sign = ' '; |
| 8905 | else |
| 8906 | sign = 0; |
| 8907 | } |
| 8908 | if (width < len) |
| 8909 | width = len; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8910 | if (rescnt - (sign != 0) < width) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8911 | reslen -= rescnt; |
| 8912 | rescnt = width + fmtcnt + 100; |
| 8913 | reslen += rescnt; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8914 | if (reslen < 0) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 8915 | Py_XDECREF(temp); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8916 | PyErr_NoMemory(); |
| 8917 | goto onError; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8918 | } |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8919 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 8920 | Py_XDECREF(temp); |
| 8921 | goto onError; |
| 8922 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8923 | res = PyUnicode_AS_UNICODE(result) |
| 8924 | + reslen - rescnt; |
| 8925 | } |
| 8926 | if (sign) { |
| 8927 | if (fill != ' ') |
| 8928 | *res++ = sign; |
| 8929 | rescnt--; |
| 8930 | if (width > len) |
| 8931 | width--; |
| 8932 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 8933 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8934 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8935 | assert(pbuf[1] == c); |
| 8936 | if (fill != ' ') { |
| 8937 | *res++ = *pbuf++; |
| 8938 | *res++ = *pbuf++; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8939 | } |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8940 | rescnt -= 2; |
| 8941 | width -= 2; |
| 8942 | if (width < 0) |
| 8943 | width = 0; |
| 8944 | len -= 2; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8945 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8946 | if (width > len && !(flags & F_LJUST)) { |
| 8947 | do { |
| 8948 | --rescnt; |
| 8949 | *res++ = fill; |
| 8950 | } while (--width > len); |
| 8951 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8952 | if (fill == ' ') { |
| 8953 | if (sign) |
| 8954 | *res++ = sign; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 8955 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8956 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8957 | assert(pbuf[1] == c); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8958 | *res++ = *pbuf++; |
| 8959 | *res++ = *pbuf++; |
| 8960 | } |
| 8961 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8962 | Py_UNICODE_COPY(res, pbuf, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8963 | res += len; |
| 8964 | rescnt -= len; |
| 8965 | while (--width >= len) { |
| 8966 | --rescnt; |
| 8967 | *res++ = ' '; |
| 8968 | } |
| 8969 | if (dict && (argidx < arglen) && c != '%') { |
| 8970 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 8971 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8972 | Py_XDECREF(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8973 | goto onError; |
| 8974 | } |
| 8975 | Py_XDECREF(temp); |
| 8976 | } /* '%' */ |
| 8977 | } /* until end */ |
| 8978 | if (argidx < arglen && !dict) { |
| 8979 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 8980 | "not all arguments converted during string formatting"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8981 | goto onError; |
| 8982 | } |
| 8983 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8984 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
| 8985 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8986 | if (args_owned) { |
| 8987 | Py_DECREF(args); |
| 8988 | } |
| 8989 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8990 | return (PyObject *)result; |
| 8991 | |
| 8992 | onError: |
| 8993 | Py_XDECREF(result); |
| 8994 | Py_DECREF(uformat); |
| 8995 | if (args_owned) { |
| 8996 | Py_DECREF(args); |
| 8997 | } |
| 8998 | return NULL; |
| 8999 | } |
| 9000 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 9001 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9002 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 9003 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9004 | static PyObject * |
| 9005 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9006 | { |
| 9007 | PyObject *x = NULL; |
Guido van Rossum | 55b4a7b | 2007-07-11 09:28:11 +0000 | [diff] [blame] | 9008 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9009 | char *encoding = NULL; |
| 9010 | char *errors = NULL; |
| 9011 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9012 | if (type != &PyUnicode_Type) |
| 9013 | return unicode_subtype_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9014 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:unicode", |
| 9015 | kwlist, &x, &encoding, &errors)) |
| 9016 | return NULL; |
| 9017 | if (x == NULL) |
| 9018 | return (PyObject *)_PyUnicode_New(0); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 9019 | if (encoding == NULL && errors == NULL) |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 9020 | return PyObject_Str(x); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 9021 | else |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9022 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
| 9023 | } |
| 9024 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9025 | static PyObject * |
| 9026 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9027 | { |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 9028 | PyUnicodeObject *tmp, *pnew; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9029 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9030 | |
| 9031 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 9032 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 9033 | if (tmp == NULL) |
| 9034 | return NULL; |
| 9035 | assert(PyUnicode_Check(tmp)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 9036 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 9037 | if (pnew == NULL) { |
| 9038 | Py_DECREF(tmp); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9039 | return NULL; |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 9040 | } |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 9041 | pnew->str = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 9042 | if (pnew->str == NULL) { |
| 9043 | _Py_ForgetReference((PyObject *)pnew); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 9044 | PyObject_Del(pnew); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 9045 | Py_DECREF(tmp); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 9046 | return PyErr_NoMemory(); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9047 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 9048 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 9049 | pnew->length = n; |
| 9050 | pnew->hash = tmp->hash; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9051 | Py_DECREF(tmp); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 9052 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9053 | } |
| 9054 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9055 | PyDoc_STRVAR(unicode_doc, |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 9056 | "str(string [, encoding[, errors]]) -> object\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9057 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 9058 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 9059 | encoding defaults to the current default string encoding.\n\ |
| 9060 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9061 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9062 | static PyObject *unicode_iter(PyObject *seq); |
| 9063 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9064 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 9065 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | 84fc66d | 2007-05-03 17:18:26 +0000 | [diff] [blame] | 9066 | "str", /* tp_name */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9067 | sizeof(PyUnicodeObject), /* tp_size */ |
| 9068 | 0, /* tp_itemsize */ |
| 9069 | /* Slots */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 9070 | (destructor)unicode_dealloc, /* tp_dealloc */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9071 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9072 | 0, /* tp_getattr */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9073 | 0, /* tp_setattr */ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9074 | 0, /* tp_compare */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9075 | unicode_repr, /* tp_repr */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 9076 | &unicode_as_number, /* tp_as_number */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9077 | &unicode_as_sequence, /* tp_as_sequence */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9078 | &unicode_as_mapping, /* tp_as_mapping */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9079 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 9080 | 0, /* tp_call*/ |
| 9081 | (reprfunc) unicode_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9082 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9083 | 0, /* tp_setattro */ |
Alexandre Vassalotti | 70a2371 | 2007-10-14 02:05:51 +0000 | [diff] [blame] | 9084 | 0, /* tp_as_buffer */ |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 9085 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 9086 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9087 | unicode_doc, /* tp_doc */ |
| 9088 | 0, /* tp_traverse */ |
| 9089 | 0, /* tp_clear */ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9090 | PyUnicode_RichCompare, /* tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9091 | 0, /* tp_weaklistoffset */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9092 | unicode_iter, /* tp_iter */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9093 | 0, /* tp_iternext */ |
| 9094 | unicode_methods, /* tp_methods */ |
| 9095 | 0, /* tp_members */ |
| 9096 | 0, /* tp_getset */ |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 9097 | &PyBaseObject_Type, /* tp_base */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9098 | 0, /* tp_dict */ |
| 9099 | 0, /* tp_descr_get */ |
| 9100 | 0, /* tp_descr_set */ |
| 9101 | 0, /* tp_dictoffset */ |
| 9102 | 0, /* tp_init */ |
| 9103 | 0, /* tp_alloc */ |
| 9104 | unicode_new, /* tp_new */ |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 9105 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9106 | }; |
| 9107 | |
| 9108 | /* Initialize the Unicode implementation */ |
| 9109 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9110 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9111 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9112 | int i; |
| 9113 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9114 | /* XXX - move this array to unicodectype.c ? */ |
| 9115 | Py_UNICODE linebreak[] = { |
| 9116 | 0x000A, /* LINE FEED */ |
| 9117 | 0x000D, /* CARRIAGE RETURN */ |
| 9118 | 0x001C, /* FILE SEPARATOR */ |
| 9119 | 0x001D, /* GROUP SEPARATOR */ |
| 9120 | 0x001E, /* RECORD SEPARATOR */ |
| 9121 | 0x0085, /* NEXT LINE */ |
| 9122 | 0x2028, /* LINE SEPARATOR */ |
| 9123 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 9124 | }; |
| 9125 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 9126 | /* Init the implementation */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 9127 | free_list = NULL; |
| 9128 | numfree = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9129 | unicode_empty = _PyUnicode_New(0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9130 | if (!unicode_empty) |
| 9131 | return; |
| 9132 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9133 | for (i = 0; i < 256; i++) |
| 9134 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 9135 | if (PyType_Ready(&PyUnicode_Type) < 0) |
| 9136 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9137 | |
| 9138 | /* initialize the linebreak bloom filter */ |
| 9139 | bloom_linebreak = make_bloom_mask( |
| 9140 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 9141 | ); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9142 | |
| 9143 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9144 | } |
| 9145 | |
| 9146 | /* Finalize the Unicode implementation */ |
| 9147 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9148 | int |
| 9149 | PyUnicode_ClearFreeList(void) |
| 9150 | { |
| 9151 | int freelist_size = numfree; |
| 9152 | PyUnicodeObject *u; |
| 9153 | |
| 9154 | for (u = free_list; u != NULL;) { |
| 9155 | PyUnicodeObject *v = u; |
| 9156 | u = *(PyUnicodeObject **)u; |
| 9157 | if (v->str) |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 9158 | PyObject_DEL(v->str); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9159 | Py_XDECREF(v->defenc); |
| 9160 | PyObject_Del(v); |
| 9161 | numfree--; |
| 9162 | } |
| 9163 | free_list = NULL; |
| 9164 | assert(numfree == 0); |
| 9165 | return freelist_size; |
| 9166 | } |
| 9167 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9168 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9169 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9170 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9171 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9172 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 9173 | Py_XDECREF(unicode_empty); |
| 9174 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 9175 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9176 | for (i = 0; i < 256; i++) { |
| 9177 | if (unicode_latin1[i]) { |
| 9178 | Py_DECREF(unicode_latin1[i]); |
| 9179 | unicode_latin1[i] = NULL; |
| 9180 | } |
| 9181 | } |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9182 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9183 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9184 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9185 | void |
| 9186 | PyUnicode_InternInPlace(PyObject **p) |
| 9187 | { |
| 9188 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 9189 | PyObject *t; |
| 9190 | if (s == NULL || !PyUnicode_Check(s)) |
| 9191 | Py_FatalError( |
| 9192 | "PyUnicode_InternInPlace: unicode strings only please!"); |
| 9193 | /* If it's a subclass, we don't really know what putting |
| 9194 | it in the interned dict might do. */ |
| 9195 | if (!PyUnicode_CheckExact(s)) |
| 9196 | return; |
| 9197 | if (PyUnicode_CHECK_INTERNED(s)) |
| 9198 | return; |
| 9199 | if (interned == NULL) { |
| 9200 | interned = PyDict_New(); |
| 9201 | if (interned == NULL) { |
| 9202 | PyErr_Clear(); /* Don't leave an exception */ |
| 9203 | return; |
| 9204 | } |
| 9205 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9206 | /* It might be that the GetItem call fails even |
| 9207 | though the key is present in the dictionary, |
| 9208 | namely when this happens during a stack overflow. */ |
| 9209 | Py_ALLOW_RECURSION |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9210 | t = PyDict_GetItem(interned, (PyObject *)s); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9211 | Py_END_ALLOW_RECURSION |
| 9212 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9213 | if (t) { |
| 9214 | Py_INCREF(t); |
| 9215 | Py_DECREF(*p); |
| 9216 | *p = t; |
| 9217 | return; |
| 9218 | } |
| 9219 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9220 | PyThreadState_GET()->recursion_critical = 1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9221 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 9222 | PyErr_Clear(); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9223 | PyThreadState_GET()->recursion_critical = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9224 | return; |
| 9225 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9226 | PyThreadState_GET()->recursion_critical = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9227 | /* The two references in interned are not counted by refcnt. |
| 9228 | The deallocator will take care of this */ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 9229 | Py_REFCNT(s) -= 2; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9230 | PyUnicode_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL; |
| 9231 | } |
| 9232 | |
| 9233 | void |
| 9234 | PyUnicode_InternImmortal(PyObject **p) |
| 9235 | { |
| 9236 | PyUnicode_InternInPlace(p); |
| 9237 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
| 9238 | PyUnicode_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL; |
| 9239 | Py_INCREF(*p); |
| 9240 | } |
| 9241 | } |
| 9242 | |
| 9243 | PyObject * |
| 9244 | PyUnicode_InternFromString(const char *cp) |
| 9245 | { |
| 9246 | PyObject *s = PyUnicode_FromString(cp); |
| 9247 | if (s == NULL) |
| 9248 | return NULL; |
| 9249 | PyUnicode_InternInPlace(&s); |
| 9250 | return s; |
| 9251 | } |
| 9252 | |
| 9253 | void _Py_ReleaseInternedUnicodeStrings(void) |
| 9254 | { |
| 9255 | PyObject *keys; |
| 9256 | PyUnicodeObject *s; |
| 9257 | Py_ssize_t i, n; |
| 9258 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
| 9259 | |
| 9260 | if (interned == NULL || !PyDict_Check(interned)) |
| 9261 | return; |
| 9262 | keys = PyDict_Keys(interned); |
| 9263 | if (keys == NULL || !PyList_Check(keys)) { |
| 9264 | PyErr_Clear(); |
| 9265 | return; |
| 9266 | } |
| 9267 | |
| 9268 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 9269 | detector, interned unicode strings are not forcibly deallocated; |
| 9270 | rather, we give them their stolen references back, and then clear |
| 9271 | and DECREF the interned dict. */ |
| 9272 | |
| 9273 | n = PyList_GET_SIZE(keys); |
| 9274 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
| 9275 | n); |
| 9276 | for (i = 0; i < n; i++) { |
| 9277 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
| 9278 | switch (s->state) { |
| 9279 | case SSTATE_NOT_INTERNED: |
| 9280 | /* XXX Shouldn't happen */ |
| 9281 | break; |
| 9282 | case SSTATE_INTERNED_IMMORTAL: |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 9283 | Py_REFCNT(s) += 1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9284 | immortal_size += s->length; |
| 9285 | break; |
| 9286 | case SSTATE_INTERNED_MORTAL: |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 9287 | Py_REFCNT(s) += 2; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9288 | mortal_size += s->length; |
| 9289 | break; |
| 9290 | default: |
| 9291 | Py_FatalError("Inconsistent interned string state."); |
| 9292 | } |
| 9293 | s->state = SSTATE_NOT_INTERNED; |
| 9294 | } |
| 9295 | fprintf(stderr, "total size of all interned strings: " |
| 9296 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 9297 | "mortal/immortal\n", mortal_size, immortal_size); |
| 9298 | Py_DECREF(keys); |
| 9299 | PyDict_Clear(interned); |
| 9300 | Py_DECREF(interned); |
| 9301 | interned = NULL; |
| 9302 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9303 | |
| 9304 | |
| 9305 | /********************* Unicode Iterator **************************/ |
| 9306 | |
| 9307 | typedef struct { |
| 9308 | PyObject_HEAD |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9309 | Py_ssize_t it_index; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9310 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
| 9311 | } unicodeiterobject; |
| 9312 | |
| 9313 | static void |
| 9314 | unicodeiter_dealloc(unicodeiterobject *it) |
| 9315 | { |
| 9316 | _PyObject_GC_UNTRACK(it); |
| 9317 | Py_XDECREF(it->it_seq); |
| 9318 | PyObject_GC_Del(it); |
| 9319 | } |
| 9320 | |
| 9321 | static int |
| 9322 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 9323 | { |
| 9324 | Py_VISIT(it->it_seq); |
| 9325 | return 0; |
| 9326 | } |
| 9327 | |
| 9328 | static PyObject * |
| 9329 | unicodeiter_next(unicodeiterobject *it) |
| 9330 | { |
| 9331 | PyUnicodeObject *seq; |
| 9332 | PyObject *item; |
| 9333 | |
| 9334 | assert(it != NULL); |
| 9335 | seq = it->it_seq; |
| 9336 | if (seq == NULL) |
| 9337 | return NULL; |
| 9338 | assert(PyUnicode_Check(seq)); |
| 9339 | |
| 9340 | if (it->it_index < PyUnicode_GET_SIZE(seq)) { |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9341 | item = PyUnicode_FromUnicode( |
| 9342 | PyUnicode_AS_UNICODE(seq)+it->it_index, 1); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9343 | if (item != NULL) |
| 9344 | ++it->it_index; |
| 9345 | return item; |
| 9346 | } |
| 9347 | |
| 9348 | Py_DECREF(seq); |
| 9349 | it->it_seq = NULL; |
| 9350 | return NULL; |
| 9351 | } |
| 9352 | |
| 9353 | static PyObject * |
| 9354 | unicodeiter_len(unicodeiterobject *it) |
| 9355 | { |
| 9356 | Py_ssize_t len = 0; |
| 9357 | if (it->it_seq) |
| 9358 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9359 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9360 | } |
| 9361 | |
| 9362 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 9363 | |
| 9364 | static PyMethodDef unicodeiter_methods[] = { |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9365 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
| 9366 | length_hint_doc}, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9367 | {NULL, NULL} /* sentinel */ |
| 9368 | }; |
| 9369 | |
| 9370 | PyTypeObject PyUnicodeIter_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 9371 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Christian Heimes | f83be4e | 2007-11-28 09:44:38 +0000 | [diff] [blame] | 9372 | "str_iterator", /* tp_name */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9373 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 9374 | 0, /* tp_itemsize */ |
| 9375 | /* methods */ |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9376 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9377 | 0, /* tp_print */ |
| 9378 | 0, /* tp_getattr */ |
| 9379 | 0, /* tp_setattr */ |
| 9380 | 0, /* tp_compare */ |
| 9381 | 0, /* tp_repr */ |
| 9382 | 0, /* tp_as_number */ |
| 9383 | 0, /* tp_as_sequence */ |
| 9384 | 0, /* tp_as_mapping */ |
| 9385 | 0, /* tp_hash */ |
| 9386 | 0, /* tp_call */ |
| 9387 | 0, /* tp_str */ |
| 9388 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9389 | 0, /* tp_setattro */ |
| 9390 | 0, /* tp_as_buffer */ |
| 9391 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 9392 | 0, /* tp_doc */ |
| 9393 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 9394 | 0, /* tp_clear */ |
| 9395 | 0, /* tp_richcompare */ |
| 9396 | 0, /* tp_weaklistoffset */ |
| 9397 | PyObject_SelfIter, /* tp_iter */ |
| 9398 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 9399 | unicodeiter_methods, /* tp_methods */ |
| 9400 | 0, |
| 9401 | }; |
| 9402 | |
| 9403 | static PyObject * |
| 9404 | unicode_iter(PyObject *seq) |
| 9405 | { |
| 9406 | unicodeiterobject *it; |
| 9407 | |
| 9408 | if (!PyUnicode_Check(seq)) { |
| 9409 | PyErr_BadInternalCall(); |
| 9410 | return NULL; |
| 9411 | } |
| 9412 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 9413 | if (it == NULL) |
| 9414 | return NULL; |
| 9415 | it->it_index = 0; |
| 9416 | Py_INCREF(seq); |
| 9417 | it->it_seq = (PyUnicodeObject *)seq; |
| 9418 | _PyObject_GC_TRACK(it); |
| 9419 | return (PyObject *)it; |
| 9420 | } |
| 9421 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9422 | size_t |
| 9423 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 9424 | { |
| 9425 | int res = 0; |
| 9426 | while(*u++) |
| 9427 | res++; |
| 9428 | return res; |
| 9429 | } |
| 9430 | |
| 9431 | Py_UNICODE* |
| 9432 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9433 | { |
| 9434 | Py_UNICODE *u = s1; |
| 9435 | while ((*u++ = *s2++)); |
| 9436 | return s1; |
| 9437 | } |
| 9438 | |
| 9439 | Py_UNICODE* |
| 9440 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 9441 | { |
| 9442 | Py_UNICODE *u = s1; |
| 9443 | while ((*u++ = *s2++)) |
| 9444 | if (n-- == 0) |
| 9445 | break; |
| 9446 | return s1; |
| 9447 | } |
| 9448 | |
| 9449 | int |
| 9450 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9451 | { |
| 9452 | while (*s1 && *s2 && *s1 == *s2) |
| 9453 | s1++, s2++; |
| 9454 | if (*s1 && *s2) |
| 9455 | return (*s1 < *s2) ? -1 : +1; |
| 9456 | if (*s1) |
| 9457 | return 1; |
| 9458 | if (*s2) |
| 9459 | return -1; |
| 9460 | return 0; |
| 9461 | } |
| 9462 | |
| 9463 | Py_UNICODE* |
| 9464 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 9465 | { |
| 9466 | const Py_UNICODE *p; |
| 9467 | for (p = s; *p; p++) |
| 9468 | if (*p == c) |
| 9469 | return (Py_UNICODE*)p; |
| 9470 | return NULL; |
| 9471 | } |
| 9472 | |
| 9473 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9474 | #ifdef __cplusplus |
| 9475 | } |
| 9476 | #endif |
| 9477 | |
| 9478 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9479 | /* |
| 9480 | Local variables: |
| 9481 | c-basic-offset: 4 |
| 9482 | indent-tabs-mode: nil |
| 9483 | End: |
| 9484 | */ |