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 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 49 | #ifdef MS_WINDOWS |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 50 | #include <windows.h> |
| 51 | #endif |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 52 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 53 | /* Limit for the Unicode object free list */ |
| 54 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 55 | #define PyUnicode_MAXFREELIST 1024 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 56 | |
| 57 | /* Limit for the Unicode object free list stay alive optimization. |
| 58 | |
| 59 | The implementation will keep allocated Unicode memory intact for |
| 60 | all objects on the free list having a size less than this |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 61 | limit. This reduces malloc() overhead for small Unicode objects. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 62 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 63 | At worst this will result in PyUnicode_MAXFREELIST * |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 64 | (sizeof(PyUnicodeObject) + KEEPALIVE_SIZE_LIMIT + |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 65 | malloc()-overhead) bytes of unused garbage. |
| 66 | |
| 67 | Setting the limit to 0 effectively turns the feature off. |
| 68 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 69 | Note: This is an experimental feature ! If you get core dumps when |
| 70 | using Unicode objects, turn this feature off. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 71 | |
| 72 | */ |
| 73 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 74 | #define KEEPALIVE_SIZE_LIMIT 9 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 75 | |
| 76 | /* Endianness switches; defaults to little endian */ |
| 77 | |
| 78 | #ifdef WORDS_BIGENDIAN |
| 79 | # define BYTEORDER_IS_BIG_ENDIAN |
| 80 | #else |
| 81 | # define BYTEORDER_IS_LITTLE_ENDIAN |
| 82 | #endif |
| 83 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 84 | /* --- Globals ------------------------------------------------------------ |
| 85 | |
| 86 | The globals are initialized by the _PyUnicode_Init() API and should |
| 87 | not be used before calling that API. |
| 88 | |
| 89 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 90 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 91 | |
| 92 | #ifdef __cplusplus |
| 93 | extern "C" { |
| 94 | #endif |
| 95 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 96 | /* This dictionary holds all interned unicode strings. Note that references |
| 97 | to strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 98 | When the interned string reaches a refcnt of 0 the string deallocation |
| 99 | function will delete the reference from this dictionary. |
| 100 | |
| 101 | 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] | 102 | 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] | 103 | */ |
| 104 | static PyObject *interned; |
| 105 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 106 | /* Free list for Unicode objects */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 107 | static PyUnicodeObject *free_list; |
| 108 | static int numfree; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 109 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 110 | /* The empty Unicode object is shared to improve performance. */ |
| 111 | static PyUnicodeObject *unicode_empty; |
| 112 | |
| 113 | /* Single character Unicode strings in the Latin-1 range are being |
| 114 | shared as well. */ |
| 115 | static PyUnicodeObject *unicode_latin1[256]; |
| 116 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 117 | /* 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] | 118 | parameter; it is fixed to "utf-8". Always use the |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 119 | PyUnicode_GetDefaultEncoding() API to access this global. |
| 120 | |
Alexandre Vassalotti | 3d2fd7f | 2007-10-16 00:26:33 +0000 | [diff] [blame] | 121 | Don't forget to alter Py_FileSystemDefaultEncoding if you change the |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 122 | hard coded default! |
| 123 | */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 124 | static const char unicode_default_encoding[] = "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 125 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 126 | /* Fast detection of the most frequent whitespace characters */ |
| 127 | const unsigned char _Py_ascii_whitespace[] = { |
| 128 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 129 | // case 0x0009: /* HORIZONTAL TABULATION */ |
| 130 | // case 0x000A: /* LINE FEED */ |
| 131 | // case 0x000B: /* VERTICAL TABULATION */ |
| 132 | // case 0x000C: /* FORM FEED */ |
| 133 | // case 0x000D: /* CARRIAGE RETURN */ |
| 134 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 135 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 136 | // case 0x001C: /* FILE SEPARATOR */ |
| 137 | // case 0x001D: /* GROUP SEPARATOR */ |
| 138 | // case 0x001E: /* RECORD SEPARATOR */ |
| 139 | // case 0x001F: /* UNIT SEPARATOR */ |
| 140 | 0, 0, 0, 0, 1, 1, 1, 1, |
| 141 | // case 0x0020: /* SPACE */ |
| 142 | 1, 0, 0, 0, 0, 0, 0, 0, |
| 143 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 144 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 145 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 146 | |
| 147 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 148 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 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 | }; |
| 156 | |
| 157 | /* Same for linebreaks */ |
| 158 | static unsigned char ascii_linebreak[] = { |
| 159 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 160 | // 0x000A, /* LINE FEED */ |
| 161 | // 0x000D, /* CARRIAGE RETURN */ |
| 162 | 0, 0, 1, 0, 0, 1, 0, 0, |
| 163 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 164 | // 0x001C, /* FILE SEPARATOR */ |
| 165 | // 0x001D, /* GROUP SEPARATOR */ |
| 166 | // 0x001E, /* RECORD SEPARATOR */ |
| 167 | 0, 0, 0, 0, 1, 1, 1, 0, |
| 168 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 169 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 170 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 171 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 172 | |
| 173 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 174 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 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 | }; |
| 182 | |
| 183 | |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 184 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 185 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 186 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 187 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 188 | return 0x10FFFF; |
| 189 | #else |
| 190 | /* This is actually an illegal character, so it should |
| 191 | not be passed to unichr. */ |
| 192 | return 0xFFFF; |
| 193 | #endif |
| 194 | } |
| 195 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 196 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 197 | |
| 198 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 199 | to keep things simple, we use a single bitmask, using the least 5 |
| 200 | bits from each unicode characters as the bit index. */ |
| 201 | |
| 202 | /* the linebreak mask is set up by Unicode_Init below */ |
| 203 | |
| 204 | #define BLOOM_MASK unsigned long |
| 205 | |
| 206 | static BLOOM_MASK bloom_linebreak; |
| 207 | |
| 208 | #define BLOOM(mask, ch) ((mask & (1 << ((ch) & 0x1F)))) |
| 209 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 210 | #define BLOOM_LINEBREAK(ch) \ |
| 211 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 212 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 213 | |
| 214 | Py_LOCAL_INLINE(BLOOM_MASK) make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len) |
| 215 | { |
| 216 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 217 | |
| 218 | long mask; |
| 219 | Py_ssize_t i; |
| 220 | |
| 221 | mask = 0; |
| 222 | for (i = 0; i < len; i++) |
| 223 | mask |= (1 << (ptr[i] & 0x1F)); |
| 224 | |
| 225 | return mask; |
| 226 | } |
| 227 | |
| 228 | Py_LOCAL_INLINE(int) unicode_member(Py_UNICODE chr, Py_UNICODE* set, Py_ssize_t setlen) |
| 229 | { |
| 230 | Py_ssize_t i; |
| 231 | |
| 232 | for (i = 0; i < setlen; i++) |
| 233 | if (set[i] == chr) |
| 234 | return 1; |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | #define BLOOM_MEMBER(mask, chr, set, setlen)\ |
| 240 | BLOOM(mask, chr) && unicode_member(chr, set, setlen) |
| 241 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 242 | /* --- Unicode Object ----------------------------------------------------- */ |
| 243 | |
| 244 | static |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 245 | int unicode_resize(register PyUnicodeObject *unicode, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 246 | Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 247 | { |
| 248 | void *oldstr; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 249 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 250 | /* Shortcut if there's nothing much to do. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 251 | if (unicode->length == length) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 252 | goto reset; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 253 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 254 | /* Resizing shared object (unicode_empty or single character |
| 255 | objects) in-place is not allowed. Use PyUnicode_Resize() |
| 256 | instead ! */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 257 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 258 | if (unicode == unicode_empty || |
| 259 | (unicode->length == 1 && |
| 260 | unicode->str[0] < 256U && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 261 | unicode_latin1[unicode->str[0]] == unicode)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 262 | PyErr_SetString(PyExc_SystemError, |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 263 | "can't resize shared unicode objects"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 264 | return -1; |
| 265 | } |
| 266 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 267 | /* We allocate one more byte to make sure the string is Ux0000 terminated. |
| 268 | The overallocation is also used by fastsearch, which assumes that it's |
| 269 | safe to look at str[length] (without making any assumptions about what |
| 270 | it contains). */ |
| 271 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 272 | oldstr = unicode->str; |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 273 | unicode->str = PyObject_REALLOC(unicode->str, |
| 274 | sizeof(Py_UNICODE) * (length + 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 275 | if (!unicode->str) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 276 | unicode->str = (Py_UNICODE *)oldstr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 277 | PyErr_NoMemory(); |
| 278 | return -1; |
| 279 | } |
| 280 | unicode->str[length] = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 281 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 282 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 283 | reset: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 284 | /* Reset the object caches */ |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 285 | if (unicode->defenc) { |
| 286 | Py_DECREF(unicode->defenc); |
| 287 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 288 | } |
| 289 | unicode->hash = -1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 290 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | /* 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] | 295 | Ux0000 terminated; some code (e.g. new_identifier) |
| 296 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 297 | |
| 298 | XXX This allocator could further be enhanced by assuring that the |
| 299 | free list never reduces its size below 1. |
| 300 | |
| 301 | */ |
| 302 | |
| 303 | static |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 304 | PyUnicodeObject *_PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 305 | { |
| 306 | register PyUnicodeObject *unicode; |
| 307 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 308 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 309 | if (length == 0 && unicode_empty != NULL) { |
| 310 | Py_INCREF(unicode_empty); |
| 311 | return unicode_empty; |
| 312 | } |
| 313 | |
| 314 | /* Unicode freelist & memory allocation */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 315 | if (free_list) { |
| 316 | unicode = free_list; |
| 317 | free_list = *(PyUnicodeObject **)unicode; |
| 318 | numfree--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 319 | if (unicode->str) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 320 | /* Keep-Alive optimization: we only upsize the buffer, |
| 321 | never downsize it. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 322 | if ((unicode->length < length) && |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 323 | unicode_resize(unicode, length) < 0) { |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 324 | PyObject_DEL(unicode->str); |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 325 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 326 | } |
| 327 | } |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 328 | else { |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 329 | size_t new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 330 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 331 | } |
| 332 | PyObject_INIT(unicode, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 333 | } |
| 334 | else { |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 335 | size_t new_size; |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 336 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 337 | if (unicode == NULL) |
| 338 | return NULL; |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 339 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 340 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 343 | if (!unicode->str) { |
| 344 | PyErr_NoMemory(); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 345 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 346 | } |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 347 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 348 | * the caller fails before initializing str -- unicode_resize() |
| 349 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 350 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 351 | * We don't want unicode_resize to read uninitialized memory in |
| 352 | * that case. |
| 353 | */ |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 354 | unicode->str[0] = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 355 | unicode->str[length] = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 356 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 357 | unicode->hash = -1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 358 | unicode->state = 0; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 359 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 360 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 361 | |
| 362 | onError: |
| 363 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 364 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 365 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | static |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 369 | void unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 370 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 371 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
| 372 | case SSTATE_NOT_INTERNED: |
| 373 | break; |
| 374 | |
| 375 | case SSTATE_INTERNED_MORTAL: |
| 376 | /* revive dead object temporarily for DelItem */ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 377 | Py_REFCNT(unicode) = 3; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 378 | if (PyDict_DelItem(interned, (PyObject *)unicode) != 0) |
| 379 | Py_FatalError( |
| 380 | "deletion of interned unicode string failed"); |
| 381 | break; |
| 382 | |
| 383 | case SSTATE_INTERNED_IMMORTAL: |
| 384 | Py_FatalError("Immortal interned unicode string died."); |
| 385 | |
| 386 | default: |
| 387 | Py_FatalError("Inconsistent interned unicode string state."); |
| 388 | } |
| 389 | |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 390 | if (PyUnicode_CheckExact(unicode) && |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 391 | numfree < PyUnicode_MAXFREELIST) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 392 | /* Keep-Alive optimization */ |
| 393 | if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 394 | PyObject_DEL(unicode->str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 395 | unicode->str = NULL; |
| 396 | unicode->length = 0; |
| 397 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 398 | if (unicode->defenc) { |
| 399 | Py_DECREF(unicode->defenc); |
| 400 | unicode->defenc = NULL; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 401 | } |
| 402 | /* Add to free list */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 403 | *(PyUnicodeObject **)unicode = free_list; |
| 404 | free_list = unicode; |
| 405 | numfree++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 406 | } |
| 407 | else { |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 408 | PyObject_DEL(unicode->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 409 | Py_XDECREF(unicode->defenc); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 410 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 414 | int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 415 | { |
| 416 | register PyUnicodeObject *v; |
| 417 | |
| 418 | /* Argument checks */ |
| 419 | if (unicode == NULL) { |
| 420 | PyErr_BadInternalCall(); |
| 421 | return -1; |
| 422 | } |
| 423 | v = (PyUnicodeObject *)*unicode; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 424 | 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] | 425 | PyErr_BadInternalCall(); |
| 426 | return -1; |
| 427 | } |
| 428 | |
| 429 | /* Resizing unicode_empty and single character objects is not |
| 430 | possible since these are being shared. We simply return a fresh |
| 431 | copy with the same Unicode content. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 432 | if (v->length != length && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 433 | (v == unicode_empty || v->length == 1)) { |
| 434 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 435 | if (w == NULL) |
| 436 | return -1; |
| 437 | Py_UNICODE_COPY(w->str, v->str, |
| 438 | length < v->length ? length : v->length); |
Raymond Hettinger | c8df578 | 2003-03-09 07:30:43 +0000 | [diff] [blame] | 439 | Py_DECREF(*unicode); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 440 | *unicode = (PyObject *)w; |
| 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 445 | objects, since we can modify them in-place. */ |
| 446 | return unicode_resize(v, length); |
| 447 | } |
| 448 | |
| 449 | /* Internal API for use in unicodeobject.c only ! */ |
| 450 | #define _PyUnicode_Resize(unicodevar, length) \ |
| 451 | PyUnicode_Resize(((PyObject **)(unicodevar)), length) |
| 452 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 453 | PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 454 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 455 | { |
| 456 | PyUnicodeObject *unicode; |
| 457 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 458 | /* If the Unicode data is known at construction time, we can apply |
| 459 | some optimizations which share commonly used objects. */ |
| 460 | if (u != NULL) { |
| 461 | |
| 462 | /* Optimization for empty strings */ |
| 463 | if (size == 0 && unicode_empty != NULL) { |
| 464 | Py_INCREF(unicode_empty); |
| 465 | return (PyObject *)unicode_empty; |
| 466 | } |
| 467 | |
| 468 | /* Single character Unicode objects in the Latin-1 range are |
| 469 | shared when using this constructor */ |
| 470 | if (size == 1 && *u < 256) { |
| 471 | unicode = unicode_latin1[*u]; |
| 472 | if (!unicode) { |
| 473 | unicode = _PyUnicode_New(1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 474 | if (!unicode) |
| 475 | return NULL; |
Marc-André Lemburg | 8879a33 | 2001-06-07 12:26:56 +0000 | [diff] [blame] | 476 | unicode->str[0] = *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 477 | unicode_latin1[*u] = unicode; |
| 478 | } |
| 479 | Py_INCREF(unicode); |
| 480 | return (PyObject *)unicode; |
| 481 | } |
| 482 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 483 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 484 | unicode = _PyUnicode_New(size); |
| 485 | if (!unicode) |
| 486 | return NULL; |
| 487 | |
| 488 | /* Copy the Unicode data into the new object */ |
| 489 | if (u != NULL) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 490 | Py_UNICODE_COPY(unicode->str, u, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 491 | |
| 492 | return (PyObject *)unicode; |
| 493 | } |
| 494 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 495 | PyObject *PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 496 | { |
| 497 | PyUnicodeObject *unicode; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 498 | |
| 499 | if (size < 0) { |
| 500 | PyErr_SetString(PyExc_SystemError, |
| 501 | "Negative size passed to PyUnicode_FromStringAndSize"); |
| 502 | return NULL; |
| 503 | } |
| 504 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 505 | /* 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] | 506 | some optimizations which share commonly used objects. |
| 507 | Also, this means the input must be UTF-8, so fall back to the |
| 508 | UTF-8 decoder at the end. */ |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 509 | if (u != NULL) { |
| 510 | |
| 511 | /* Optimization for empty strings */ |
| 512 | if (size == 0 && unicode_empty != NULL) { |
| 513 | Py_INCREF(unicode_empty); |
| 514 | return (PyObject *)unicode_empty; |
| 515 | } |
| 516 | |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 517 | /* Single characters are shared when using this constructor. |
| 518 | Restrict to ASCII, since the input must be UTF-8. */ |
| 519 | if (size == 1 && Py_CHARMASK(*u) < 128) { |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 520 | unicode = unicode_latin1[Py_CHARMASK(*u)]; |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 521 | if (!unicode) { |
| 522 | unicode = _PyUnicode_New(1); |
| 523 | if (!unicode) |
| 524 | return NULL; |
Guido van Rossum | 00058aa | 2007-07-19 18:21:28 +0000 | [diff] [blame] | 525 | unicode->str[0] = Py_CHARMASK(*u); |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 526 | unicode_latin1[Py_CHARMASK(*u)] = unicode; |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 527 | } |
| 528 | Py_INCREF(unicode); |
| 529 | return (PyObject *)unicode; |
| 530 | } |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 531 | |
| 532 | return PyUnicode_DecodeUTF8(u, size, NULL); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Walter Dörwald | 5550731 | 2007-05-18 13:12:10 +0000 | [diff] [blame] | 535 | unicode = _PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 536 | if (!unicode) |
| 537 | return NULL; |
| 538 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 539 | return (PyObject *)unicode; |
| 540 | } |
| 541 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 542 | PyObject *PyUnicode_FromString(const char *u) |
| 543 | { |
| 544 | size_t size = strlen(u); |
| 545 | if (size > PY_SSIZE_T_MAX) { |
| 546 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 547 | return NULL; |
| 548 | } |
| 549 | |
| 550 | return PyUnicode_FromStringAndSize(u, size); |
| 551 | } |
| 552 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 553 | #ifdef HAVE_WCHAR_H |
| 554 | |
| 555 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 556 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 557 | { |
| 558 | PyUnicodeObject *unicode; |
| 559 | |
| 560 | if (w == NULL) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 561 | if (size == 0) |
| 562 | return PyUnicode_FromStringAndSize(NULL, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 563 | PyErr_BadInternalCall(); |
| 564 | return NULL; |
| 565 | } |
| 566 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 567 | if (size == -1) { |
| 568 | size = wcslen(w); |
| 569 | } |
| 570 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 571 | unicode = _PyUnicode_New(size); |
| 572 | if (!unicode) |
| 573 | return NULL; |
| 574 | |
| 575 | /* Copy the wchar_t data into the new object */ |
| 576 | #ifdef HAVE_USABLE_WCHAR_T |
| 577 | memcpy(unicode->str, w, size * sizeof(wchar_t)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 578 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 579 | { |
| 580 | register Py_UNICODE *u; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 581 | register Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 582 | u = PyUnicode_AS_UNICODE(unicode); |
Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 583 | for (i = size; i > 0; i--) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 584 | *u++ = *w++; |
| 585 | } |
| 586 | #endif |
| 587 | |
| 588 | return (PyObject *)unicode; |
| 589 | } |
| 590 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 591 | static void |
| 592 | makefmt(char *fmt, int longflag, int size_tflag, int zeropad, int width, int precision, char c) |
| 593 | { |
| 594 | *fmt++ = '%'; |
| 595 | if (width) { |
| 596 | if (zeropad) |
| 597 | *fmt++ = '0'; |
| 598 | fmt += sprintf(fmt, "%d", width); |
| 599 | } |
| 600 | if (precision) |
| 601 | fmt += sprintf(fmt, ".%d", precision); |
| 602 | if (longflag) |
| 603 | *fmt++ = 'l'; |
| 604 | else if (size_tflag) { |
| 605 | char *f = PY_FORMAT_SIZE_T; |
| 606 | while (*f) |
| 607 | *fmt++ = *f++; |
| 608 | } |
| 609 | *fmt++ = c; |
| 610 | *fmt = '\0'; |
| 611 | } |
| 612 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 613 | #define appendstring(string) {for (copy = string;*copy;) *s++ = *copy++;} |
| 614 | |
| 615 | PyObject * |
| 616 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 617 | { |
| 618 | va_list count; |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 619 | Py_ssize_t callcount = 0; |
| 620 | PyObject **callresults = NULL; |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 621 | PyObject **callresult = NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 622 | Py_ssize_t n = 0; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 623 | int width = 0; |
| 624 | int precision = 0; |
| 625 | int zeropad; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 626 | const char* f; |
| 627 | Py_UNICODE *s; |
| 628 | PyObject *string; |
| 629 | /* used by sprintf */ |
| 630 | char buffer[21]; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 631 | /* use abuffer instead of buffer, if we need more space |
| 632 | * (which can happen if there's a format specifier with width). */ |
| 633 | char *abuffer = NULL; |
| 634 | char *realbuffer; |
| 635 | Py_ssize_t abuffersize = 0; |
| 636 | char fmt[60]; /* should be enough for %0width.precisionld */ |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 637 | const char *copy; |
| 638 | |
| 639 | #ifdef VA_LIST_IS_ARRAY |
| 640 | Py_MEMCPY(count, vargs, sizeof(va_list)); |
| 641 | #else |
| 642 | #ifdef __va_copy |
| 643 | __va_copy(count, vargs); |
| 644 | #else |
| 645 | count = vargs; |
| 646 | #endif |
| 647 | #endif |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 648 | /* step 1: count the number of %S/%R/%A format specifications |
| 649 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII() for |
| 650 | * these objects once during step 3 and put the result in |
| 651 | an array) */ |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 652 | for (f = format; *f; f++) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 653 | if (*f == '%' && (*(f+1)=='S' || *(f+1)=='R' || *(f+1)=='A')) |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 654 | ++callcount; |
| 655 | } |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 656 | /* step 2: allocate memory for the results of |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 657 | * PyObject_Str()/PyObject_Repr() calls */ |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 658 | if (callcount) { |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 659 | callresults = PyObject_Malloc(sizeof(PyObject *)*callcount); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 660 | if (!callresults) { |
| 661 | PyErr_NoMemory(); |
| 662 | return NULL; |
| 663 | } |
| 664 | callresult = callresults; |
| 665 | } |
| 666 | /* step 3: figure out how large a buffer we need */ |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 667 | for (f = format; *f; f++) { |
| 668 | if (*f == '%') { |
| 669 | const char* p = f; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 670 | width = 0; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 671 | while (ISDIGIT((unsigned)*f)) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 672 | width = (width*10) + *f++ - '0'; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 673 | while (*++f && *f != '%' && !ISALPHA((unsigned)*f)) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 674 | ; |
| 675 | |
| 676 | /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
| 677 | * they don't affect the amount of space we reserve. |
| 678 | */ |
| 679 | if ((*f == 'l' || *f == 'z') && |
| 680 | (f[1] == 'd' || f[1] == 'u')) |
Eric Smith | ddd2582 | 2007-08-27 11:33:42 +0000 | [diff] [blame] | 681 | ++f; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 682 | |
| 683 | switch (*f) { |
| 684 | case 'c': |
| 685 | (void)va_arg(count, int); |
| 686 | /* fall through... */ |
| 687 | case '%': |
| 688 | n++; |
| 689 | break; |
| 690 | case 'd': case 'u': case 'i': case 'x': |
| 691 | (void) va_arg(count, int); |
| 692 | /* 20 bytes is enough to hold a 64-bit |
| 693 | integer. Decimal takes the most space. |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 694 | This isn't enough for octal. |
| 695 | If a width is specified we need more |
| 696 | (which we allocate later). */ |
| 697 | if (width < 20) |
| 698 | width = 20; |
| 699 | n += width; |
| 700 | if (abuffersize < width) |
| 701 | abuffersize = width; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 702 | break; |
| 703 | case 's': |
Martin v. Löwis | 90d1fcd | 2007-08-31 11:01:23 +0000 | [diff] [blame] | 704 | { |
| 705 | /* UTF-8 */ |
| 706 | unsigned char*s; |
| 707 | s = va_arg(count, unsigned char*); |
| 708 | while (*s) { |
| 709 | if (*s < 128) { |
| 710 | n++; s++; |
| 711 | } else if (*s < 0xc0) { |
| 712 | /* invalid UTF-8 */ |
| 713 | n++; s++; |
| 714 | } else if (*s < 0xc0) { |
| 715 | n++; |
| 716 | s++; if(!*s)break; |
| 717 | s++; |
| 718 | } else if (*s < 0xe0) { |
| 719 | n++; |
| 720 | s++; if(!*s)break; |
| 721 | s++; if(!*s)break; |
| 722 | s++; |
| 723 | } else { |
| 724 | #ifdef Py_UNICODE_WIDE |
| 725 | n++; |
| 726 | #else |
| 727 | n+=2; |
| 728 | #endif |
| 729 | s++; if(!*s)break; |
| 730 | s++; if(!*s)break; |
| 731 | s++; if(!*s)break; |
| 732 | s++; |
| 733 | } |
| 734 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 735 | break; |
Martin v. Löwis | 90d1fcd | 2007-08-31 11:01:23 +0000 | [diff] [blame] | 736 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 737 | case 'U': |
| 738 | { |
| 739 | PyObject *obj = va_arg(count, PyObject *); |
| 740 | assert(obj && PyUnicode_Check(obj)); |
| 741 | n += PyUnicode_GET_SIZE(obj); |
| 742 | break; |
| 743 | } |
Walter Dörwald | d7fb764 | 2007-06-11 16:36:59 +0000 | [diff] [blame] | 744 | case 'V': |
| 745 | { |
| 746 | PyObject *obj = va_arg(count, PyObject *); |
| 747 | const char *str = va_arg(count, const char *); |
| 748 | assert(obj || str); |
| 749 | assert(!obj || PyUnicode_Check(obj)); |
| 750 | if (obj) |
| 751 | n += PyUnicode_GET_SIZE(obj); |
| 752 | else |
| 753 | n += strlen(str); |
| 754 | break; |
| 755 | } |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 756 | case 'S': |
| 757 | { |
| 758 | PyObject *obj = va_arg(count, PyObject *); |
| 759 | PyObject *str; |
| 760 | assert(obj); |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 761 | str = PyObject_Str(obj); |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 762 | if (!str) |
| 763 | goto fail; |
| 764 | n += PyUnicode_GET_SIZE(str); |
| 765 | /* Remember the str and switch to the next slot */ |
| 766 | *callresult++ = str; |
| 767 | break; |
| 768 | } |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 769 | case 'R': |
| 770 | { |
| 771 | PyObject *obj = va_arg(count, PyObject *); |
| 772 | PyObject *repr; |
| 773 | assert(obj); |
| 774 | repr = PyObject_Repr(obj); |
| 775 | if (!repr) |
| 776 | goto fail; |
| 777 | n += PyUnicode_GET_SIZE(repr); |
| 778 | /* Remember the repr and switch to the next slot */ |
| 779 | *callresult++ = repr; |
| 780 | break; |
| 781 | } |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 782 | case 'A': |
| 783 | { |
| 784 | PyObject *obj = va_arg(count, PyObject *); |
| 785 | PyObject *ascii; |
| 786 | assert(obj); |
| 787 | ascii = PyObject_ASCII(obj); |
| 788 | if (!ascii) |
| 789 | goto fail; |
| 790 | n += PyUnicode_GET_SIZE(ascii); |
| 791 | /* Remember the repr and switch to the next slot */ |
| 792 | *callresult++ = ascii; |
| 793 | break; |
| 794 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 795 | case 'p': |
| 796 | (void) va_arg(count, int); |
| 797 | /* maximum 64-bit pointer representation: |
| 798 | * 0xffffffffffffffff |
| 799 | * so 19 characters is enough. |
| 800 | * XXX I count 18 -- what's the extra for? |
| 801 | */ |
| 802 | n += 19; |
| 803 | break; |
| 804 | default: |
| 805 | /* if we stumble upon an unknown |
| 806 | formatting code, copy the rest of |
| 807 | the format string to the output |
| 808 | string. (we cannot just skip the |
| 809 | code, since there's no way to know |
| 810 | what's in the argument list) */ |
| 811 | n += strlen(p); |
| 812 | goto expand; |
| 813 | } |
| 814 | } else |
| 815 | n++; |
| 816 | } |
| 817 | expand: |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 818 | if (abuffersize > 20) { |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 819 | abuffer = PyObject_Malloc(abuffersize); |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 820 | if (!abuffer) { |
| 821 | PyErr_NoMemory(); |
| 822 | goto fail; |
| 823 | } |
| 824 | realbuffer = abuffer; |
| 825 | } |
| 826 | else |
| 827 | realbuffer = buffer; |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 828 | /* step 4: fill the buffer */ |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 829 | /* 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] | 830 | we don't have to resize the string. |
| 831 | There can be no errors beyond this point. */ |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 832 | string = PyUnicode_FromUnicode(NULL, n); |
| 833 | if (!string) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 834 | goto fail; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 835 | |
| 836 | s = PyUnicode_AS_UNICODE(string); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 837 | callresult = callresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 838 | |
| 839 | for (f = format; *f; f++) { |
| 840 | if (*f == '%') { |
| 841 | const char* p = f++; |
| 842 | int longflag = 0; |
| 843 | int size_tflag = 0; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 844 | zeropad = (*f == '0'); |
| 845 | /* parse the width.precision part */ |
| 846 | width = 0; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 847 | while (ISDIGIT((unsigned)*f)) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 848 | width = (width*10) + *f++ - '0'; |
| 849 | precision = 0; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 850 | if (*f == '.') { |
| 851 | f++; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 852 | while (ISDIGIT((unsigned)*f)) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 853 | precision = (precision*10) + *f++ - '0'; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 854 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 855 | /* handle the long flag, but only for %ld and %lu. |
| 856 | others can be added when necessary. */ |
| 857 | if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) { |
| 858 | longflag = 1; |
| 859 | ++f; |
| 860 | } |
| 861 | /* handle the size_t flag. */ |
| 862 | if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
| 863 | size_tflag = 1; |
| 864 | ++f; |
| 865 | } |
| 866 | |
| 867 | switch (*f) { |
| 868 | case 'c': |
| 869 | *s++ = va_arg(vargs, int); |
| 870 | break; |
| 871 | case 'd': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 872 | makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'd'); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 873 | if (longflag) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 874 | sprintf(realbuffer, fmt, va_arg(vargs, long)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 875 | else if (size_tflag) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 876 | sprintf(realbuffer, fmt, va_arg(vargs, Py_ssize_t)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 877 | else |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 878 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 879 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 880 | break; |
| 881 | case 'u': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 882 | makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'u'); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 883 | if (longflag) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 884 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned long)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 885 | else if (size_tflag) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 886 | sprintf(realbuffer, fmt, va_arg(vargs, size_t)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 887 | else |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 888 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned int)); |
| 889 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 890 | break; |
| 891 | case 'i': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 892 | makefmt(fmt, 0, 0, zeropad, width, precision, 'i'); |
| 893 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 894 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 895 | break; |
| 896 | case 'x': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 897 | makefmt(fmt, 0, 0, zeropad, width, precision, 'x'); |
| 898 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 899 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 900 | break; |
| 901 | case 's': |
Martin v. Löwis | 90d1fcd | 2007-08-31 11:01:23 +0000 | [diff] [blame] | 902 | { |
| 903 | /* Parameter must be UTF-8 encoded. |
| 904 | In case of encoding errors, use |
| 905 | the replacement character. */ |
| 906 | PyObject *u; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 907 | p = va_arg(vargs, char*); |
Martin v. Löwis | 90d1fcd | 2007-08-31 11:01:23 +0000 | [diff] [blame] | 908 | u = PyUnicode_DecodeUTF8(p, strlen(p), |
| 909 | "replace"); |
| 910 | if (!u) |
| 911 | goto fail; |
| 912 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(u), |
| 913 | PyUnicode_GET_SIZE(u)); |
| 914 | s += PyUnicode_GET_SIZE(u); |
| 915 | Py_DECREF(u); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 916 | break; |
Martin v. Löwis | 90d1fcd | 2007-08-31 11:01:23 +0000 | [diff] [blame] | 917 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 918 | case 'U': |
| 919 | { |
| 920 | PyObject *obj = va_arg(vargs, PyObject *); |
Walter Dörwald | 5c2fab6 | 2007-05-24 19:51:02 +0000 | [diff] [blame] | 921 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 922 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 923 | s += size; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 924 | break; |
| 925 | } |
Walter Dörwald | d7fb764 | 2007-06-11 16:36:59 +0000 | [diff] [blame] | 926 | case 'V': |
| 927 | { |
| 928 | PyObject *obj = va_arg(vargs, PyObject *); |
| 929 | const char *str = va_arg(vargs, const char *); |
| 930 | if (obj) { |
| 931 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 932 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 933 | s += size; |
| 934 | } else { |
| 935 | appendstring(str); |
| 936 | } |
| 937 | break; |
| 938 | } |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 939 | case 'S': |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 940 | case 'R': |
| 941 | { |
Guido van Rossum | 755114a | 2007-06-13 01:04:27 +0000 | [diff] [blame] | 942 | Py_UNICODE *ucopy; |
| 943 | Py_ssize_t usize; |
| 944 | Py_ssize_t upos; |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 945 | /* unused, since we already have the result */ |
| 946 | (void) va_arg(vargs, PyObject *); |
Guido van Rossum | 755114a | 2007-06-13 01:04:27 +0000 | [diff] [blame] | 947 | ucopy = PyUnicode_AS_UNICODE(*callresult); |
| 948 | usize = PyUnicode_GET_SIZE(*callresult); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 949 | for (upos = 0; upos<usize;) |
| 950 | *s++ = ucopy[upos++]; |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 951 | /* We're done with the unicode()/repr() => forget it */ |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 952 | Py_DECREF(*callresult); |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 953 | /* switch to next unicode()/repr() result */ |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 954 | ++callresult; |
| 955 | break; |
| 956 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 957 | case 'p': |
| 958 | sprintf(buffer, "%p", va_arg(vargs, void*)); |
| 959 | /* %p is ill-defined: ensure leading 0x. */ |
| 960 | if (buffer[1] == 'X') |
| 961 | buffer[1] = 'x'; |
| 962 | else if (buffer[1] != 'x') { |
| 963 | memmove(buffer+2, buffer, strlen(buffer)+1); |
| 964 | buffer[0] = '0'; |
| 965 | buffer[1] = 'x'; |
| 966 | } |
| 967 | appendstring(buffer); |
| 968 | break; |
| 969 | case '%': |
| 970 | *s++ = '%'; |
| 971 | break; |
| 972 | default: |
| 973 | appendstring(p); |
| 974 | goto end; |
| 975 | } |
| 976 | } else |
| 977 | *s++ = *f; |
| 978 | } |
| 979 | |
| 980 | end: |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 981 | if (callresults) |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 982 | PyObject_Free(callresults); |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 983 | if (abuffer) |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 984 | PyObject_Free(abuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 985 | _PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string)); |
| 986 | return string; |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 987 | fail: |
| 988 | if (callresults) { |
| 989 | PyObject **callresult2 = callresults; |
Guido van Rossum | 307fa8c | 2007-07-16 20:46:27 +0000 | [diff] [blame] | 990 | while (callresult2 < callresult) { |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 991 | Py_DECREF(*callresult2); |
| 992 | ++callresult2; |
| 993 | } |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 994 | PyObject_Free(callresults); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 995 | } |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 996 | if (abuffer) |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 997 | PyObject_Free(abuffer); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 998 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | #undef appendstring |
| 1002 | |
| 1003 | PyObject * |
| 1004 | PyUnicode_FromFormat(const char *format, ...) |
| 1005 | { |
| 1006 | PyObject* ret; |
| 1007 | va_list vargs; |
| 1008 | |
| 1009 | #ifdef HAVE_STDARG_PROTOTYPES |
| 1010 | va_start(vargs, format); |
| 1011 | #else |
| 1012 | va_start(vargs); |
| 1013 | #endif |
| 1014 | ret = PyUnicode_FromFormatV(format, vargs); |
| 1015 | va_end(vargs); |
| 1016 | return ret; |
| 1017 | } |
| 1018 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1019 | Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
| 1020 | wchar_t *w, |
| 1021 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1022 | { |
| 1023 | if (unicode == NULL) { |
| 1024 | PyErr_BadInternalCall(); |
| 1025 | return -1; |
| 1026 | } |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1027 | |
| 1028 | /* If possible, try to copy the 0-termination as well */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1029 | if (size > PyUnicode_GET_SIZE(unicode)) |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1030 | size = PyUnicode_GET_SIZE(unicode) + 1; |
| 1031 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1032 | #ifdef HAVE_USABLE_WCHAR_T |
| 1033 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 1034 | #else |
| 1035 | { |
| 1036 | register Py_UNICODE *u; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1037 | register Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1038 | u = PyUnicode_AS_UNICODE(unicode); |
Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 1039 | for (i = size; i > 0; i--) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1040 | *w++ = *u++; |
| 1041 | } |
| 1042 | #endif |
| 1043 | |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1044 | if (size > PyUnicode_GET_SIZE(unicode)) |
| 1045 | return PyUnicode_GET_SIZE(unicode); |
| 1046 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1047 | return size; |
| 1048 | } |
| 1049 | |
| 1050 | #endif |
| 1051 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1052 | PyObject *PyUnicode_FromOrdinal(int ordinal) |
| 1053 | { |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1054 | Py_UNICODE s[2]; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1055 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1056 | if (ordinal < 0 || ordinal > 0x10ffff) { |
| 1057 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1058 | "chr() arg not in range(0x110000)"); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1059 | return NULL; |
| 1060 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1061 | |
| 1062 | #ifndef Py_UNICODE_WIDE |
| 1063 | if (ordinal > 0xffff) { |
| 1064 | ordinal -= 0x10000; |
| 1065 | s[0] = 0xD800 | (ordinal >> 10); |
| 1066 | s[1] = 0xDC00 | (ordinal & 0x3FF); |
| 1067 | return PyUnicode_FromUnicode(s, 2); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1068 | } |
| 1069 | #endif |
| 1070 | |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 1071 | s[0] = (Py_UNICODE)ordinal; |
| 1072 | return PyUnicode_FromUnicode(s, 1); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1075 | PyObject *PyUnicode_FromObject(register PyObject *obj) |
| 1076 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1077 | /* XXX Perhaps we should make this API an alias of |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 1078 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1079 | if (PyUnicode_CheckExact(obj)) { |
| 1080 | Py_INCREF(obj); |
| 1081 | return obj; |
| 1082 | } |
| 1083 | if (PyUnicode_Check(obj)) { |
| 1084 | /* For a Unicode subtype that's not a Unicode object, |
| 1085 | return a true Unicode object with the same data. */ |
| 1086 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 1087 | PyUnicode_GET_SIZE(obj)); |
| 1088 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1089 | PyErr_Format(PyExc_TypeError, |
| 1090 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1091 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1092 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
| 1096 | const char *encoding, |
| 1097 | const char *errors) |
| 1098 | { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1099 | const char *s = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1100 | Py_ssize_t len; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1101 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1102 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1103 | if (obj == NULL) { |
| 1104 | PyErr_BadInternalCall(); |
| 1105 | return NULL; |
| 1106 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1107 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1108 | if (PyUnicode_Check(obj)) { |
| 1109 | PyErr_SetString(PyExc_TypeError, |
| 1110 | "decoding Unicode is not supported"); |
| 1111 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1112 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1113 | |
| 1114 | /* Coerce object */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1115 | if (PyBytes_Check(obj)) { |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1116 | s = PyBytes_AS_STRING(obj); |
| 1117 | len = PyBytes_GET_SIZE(obj); |
| 1118 | } |
| 1119 | else if (PyByteArray_Check(obj)) { |
| 1120 | s = PyByteArray_AS_STRING(obj); |
| 1121 | len = PyByteArray_GET_SIZE(obj); |
| 1122 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1123 | else if (PyObject_AsCharBuffer(obj, &s, &len)) { |
| 1124 | /* Overwrite the error message with something more useful in |
| 1125 | case of a TypeError. */ |
| 1126 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1127 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1128 | "coercing to Unicode: need string or buffer, " |
| 1129 | "%.80s found", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1130 | Py_TYPE(obj)->tp_name); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1131 | goto onError; |
| 1132 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1133 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1134 | /* Convert to Unicode */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1135 | if (len == 0) { |
| 1136 | Py_INCREF(unicode_empty); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1137 | v = (PyObject *)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1138 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1139 | else |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1140 | v = PyUnicode_Decode(s, len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 1141 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1142 | return v; |
| 1143 | |
| 1144 | onError: |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1145 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | PyObject *PyUnicode_Decode(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1149 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1150 | const char *encoding, |
| 1151 | const char *errors) |
| 1152 | { |
| 1153 | PyObject *buffer = NULL, *unicode; |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1154 | Py_buffer info; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1155 | char lower[20]; /* Enough for any encoding name we recognize */ |
| 1156 | char *l; |
| 1157 | const char *e; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1158 | |
| 1159 | if (encoding == NULL) |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1160 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1161 | |
| 1162 | /* Convert encoding to lower case and replace '_' with '-' in order to |
| 1163 | catch e.g. UTF_8 */ |
| 1164 | e = encoding; |
| 1165 | l = lower; |
| 1166 | while (*e && l < &lower[(sizeof lower) - 2]) { |
| 1167 | if (ISUPPER(*e)) { |
| 1168 | *l++ = TOLOWER(*e++); |
| 1169 | } |
| 1170 | else if (*e == '_') { |
| 1171 | *l++ = '-'; |
| 1172 | e++; |
| 1173 | } |
| 1174 | else { |
| 1175 | *l++ = *e++; |
| 1176 | } |
| 1177 | } |
| 1178 | *l = '\0'; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1179 | |
| 1180 | /* Shortcuts for common default encodings */ |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1181 | if (strcmp(lower, "utf-8") == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1182 | return PyUnicode_DecodeUTF8(s, size, errors); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1183 | else if ((strcmp(lower, "latin-1") == 0) || |
| 1184 | (strcmp(lower, "iso-8859-1") == 0)) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1185 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1186 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1187 | else if (strcmp(lower, "mbcs") == 0) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1188 | return PyUnicode_DecodeMBCS(s, size, errors); |
| 1189 | #endif |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1190 | else if (strcmp(lower, "ascii") == 0) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1191 | return PyUnicode_DecodeASCII(s, size, errors); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1192 | else if (strcmp(lower, "utf-16") == 0) |
| 1193 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 1194 | else if (strcmp(lower, "utf-32") == 0) |
| 1195 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1196 | |
| 1197 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1198 | buffer = NULL; |
| 1199 | if (PyBuffer_FillInfo(&info, (void *)s, size, 1, PyBUF_SIMPLE) < 0) |
| 1200 | goto onError; |
| 1201 | buffer = PyMemoryView_FromMemory(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1202 | if (buffer == NULL) |
| 1203 | goto onError; |
| 1204 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 1205 | if (unicode == NULL) |
| 1206 | goto onError; |
| 1207 | if (!PyUnicode_Check(unicode)) { |
| 1208 | PyErr_Format(PyExc_TypeError, |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1209 | "decoder did not return a unicode object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1210 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1211 | Py_DECREF(unicode); |
| 1212 | goto onError; |
| 1213 | } |
| 1214 | Py_DECREF(buffer); |
| 1215 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1216 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1217 | onError: |
| 1218 | Py_XDECREF(buffer); |
| 1219 | return NULL; |
| 1220 | } |
| 1221 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1222 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 1223 | const char *encoding, |
| 1224 | const char *errors) |
| 1225 | { |
| 1226 | PyObject *v; |
| 1227 | |
| 1228 | if (!PyUnicode_Check(unicode)) { |
| 1229 | PyErr_BadArgument(); |
| 1230 | goto onError; |
| 1231 | } |
| 1232 | |
| 1233 | if (encoding == NULL) |
| 1234 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1235 | |
| 1236 | /* Decode via the codec registry */ |
| 1237 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1238 | if (v == NULL) |
| 1239 | goto onError; |
| 1240 | return v; |
| 1241 | |
| 1242 | onError: |
| 1243 | return NULL; |
| 1244 | } |
| 1245 | |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1246 | PyObject *PyUnicode_AsDecodedUnicode(PyObject *unicode, |
| 1247 | const char *encoding, |
| 1248 | const char *errors) |
| 1249 | { |
| 1250 | PyObject *v; |
| 1251 | |
| 1252 | if (!PyUnicode_Check(unicode)) { |
| 1253 | PyErr_BadArgument(); |
| 1254 | goto onError; |
| 1255 | } |
| 1256 | |
| 1257 | if (encoding == NULL) |
| 1258 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1259 | |
| 1260 | /* Decode via the codec registry */ |
| 1261 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1262 | if (v == NULL) |
| 1263 | goto onError; |
| 1264 | if (!PyUnicode_Check(v)) { |
| 1265 | PyErr_Format(PyExc_TypeError, |
| 1266 | "decoder did not return a unicode object (type=%.400s)", |
| 1267 | Py_TYPE(v)->tp_name); |
| 1268 | Py_DECREF(v); |
| 1269 | goto onError; |
| 1270 | } |
| 1271 | return v; |
| 1272 | |
| 1273 | onError: |
| 1274 | return NULL; |
| 1275 | } |
| 1276 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1277 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1278 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1279 | const char *encoding, |
| 1280 | const char *errors) |
| 1281 | { |
| 1282 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1283 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1284 | unicode = PyUnicode_FromUnicode(s, size); |
| 1285 | if (unicode == NULL) |
| 1286 | return NULL; |
| 1287 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 1288 | Py_DECREF(unicode); |
| 1289 | return v; |
| 1290 | } |
| 1291 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1292 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 1293 | const char *encoding, |
| 1294 | const char *errors) |
| 1295 | { |
| 1296 | PyObject *v; |
| 1297 | |
| 1298 | if (!PyUnicode_Check(unicode)) { |
| 1299 | PyErr_BadArgument(); |
| 1300 | goto onError; |
| 1301 | } |
| 1302 | |
| 1303 | if (encoding == NULL) |
| 1304 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1305 | |
| 1306 | /* Encode via the codec registry */ |
| 1307 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1308 | if (v == NULL) |
| 1309 | goto onError; |
| 1310 | return v; |
| 1311 | |
| 1312 | onError: |
| 1313 | return NULL; |
| 1314 | } |
| 1315 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1316 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 1317 | const char *encoding, |
| 1318 | const char *errors) |
| 1319 | { |
| 1320 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1321 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1322 | if (!PyUnicode_Check(unicode)) { |
| 1323 | PyErr_BadArgument(); |
| 1324 | goto onError; |
| 1325 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1326 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1327 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1328 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1329 | |
| 1330 | /* Shortcuts for common default encodings */ |
| 1331 | if (errors == NULL) { |
| 1332 | if (strcmp(encoding, "utf-8") == 0) |
Jeremy Hylton | 9cea41c | 2001-05-29 17:13:15 +0000 | [diff] [blame] | 1333 | return PyUnicode_AsUTF8String(unicode); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1334 | else if (strcmp(encoding, "latin-1") == 0) |
| 1335 | return PyUnicode_AsLatin1String(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1336 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 1337 | else if (strcmp(encoding, "mbcs") == 0) |
| 1338 | return PyUnicode_AsMBCSString(unicode); |
| 1339 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1340 | else if (strcmp(encoding, "ascii") == 0) |
| 1341 | return PyUnicode_AsASCIIString(unicode); |
| 1342 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1343 | |
| 1344 | /* Encode via the codec registry */ |
| 1345 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1346 | if (v == NULL) |
| 1347 | goto onError; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1348 | if (PyByteArray_Check(v)) { |
| 1349 | char msg[100]; |
| 1350 | PyOS_snprintf(msg, sizeof(msg), |
| 1351 | "encoder %s returned buffer instead of bytes", |
| 1352 | encoding); |
| 1353 | if (PyErr_WarnEx(PyExc_RuntimeWarning, msg, 1) < 0) { |
| 1354 | v = NULL; |
| 1355 | goto onError; |
| 1356 | } |
| 1357 | v = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 1358 | } |
| 1359 | else if (!PyBytes_Check(v)) { |
| 1360 | PyErr_Format(PyExc_TypeError, |
| 1361 | "encoder did not return a bytes object (type=%.400s)", |
| 1362 | Py_TYPE(v)->tp_name); |
| 1363 | v = NULL; |
| 1364 | } |
| 1365 | return v; |
| 1366 | |
| 1367 | onError: |
| 1368 | return NULL; |
| 1369 | } |
| 1370 | |
| 1371 | PyObject *PyUnicode_AsEncodedUnicode(PyObject *unicode, |
| 1372 | const char *encoding, |
| 1373 | const char *errors) |
| 1374 | { |
| 1375 | PyObject *v; |
| 1376 | |
| 1377 | if (!PyUnicode_Check(unicode)) { |
| 1378 | PyErr_BadArgument(); |
| 1379 | goto onError; |
| 1380 | } |
| 1381 | |
| 1382 | if (encoding == NULL) |
| 1383 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1384 | |
| 1385 | /* Encode via the codec registry */ |
| 1386 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1387 | if (v == NULL) |
| 1388 | goto onError; |
| 1389 | if (!PyUnicode_Check(v)) { |
| 1390 | PyErr_Format(PyExc_TypeError, |
| 1391 | "encoder did not return an unicode object (type=%.400s)", |
| 1392 | Py_TYPE(v)->tp_name); |
| 1393 | Py_DECREF(v); |
| 1394 | goto onError; |
| 1395 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1396 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1397 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1398 | onError: |
| 1399 | return NULL; |
| 1400 | } |
| 1401 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1402 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
| 1403 | const char *errors) |
| 1404 | { |
| 1405 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1406 | if (v) |
| 1407 | return v; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1408 | if (errors != NULL) |
| 1409 | Py_FatalError("non-NULL encoding in _PyUnicode_AsDefaultEncodedString"); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1410 | v = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Guido van Rossum | 0661009 | 2007-08-16 21:02:22 +0000 | [diff] [blame] | 1411 | PyUnicode_GET_SIZE(unicode), |
| 1412 | NULL); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1413 | if (!v) |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1414 | return NULL; |
Guido van Rossum | e7a0d39 | 2007-07-12 07:53:00 +0000 | [diff] [blame] | 1415 | ((PyUnicodeObject *)unicode)->defenc = v; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1416 | return v; |
| 1417 | } |
| 1418 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1419 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1420 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1421 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1422 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 1423 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1424 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1425 | PyObject* |
| 1426 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 1427 | { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1428 | /* During the early bootstrapping process, Py_FileSystemDefaultEncoding |
| 1429 | can be undefined. If it is case, decode using UTF-8. The following assumes |
| 1430 | that Py_FileSystemDefaultEncoding is set to a built-in encoding during the |
| 1431 | bootstrapping process where the codecs aren't ready yet. |
| 1432 | */ |
| 1433 | if (Py_FileSystemDefaultEncoding) { |
| 1434 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1435 | if (strcmp(Py_FileSystemDefaultEncoding, "mbcs") == 0) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1436 | return PyUnicode_DecodeMBCS(s, size, "replace"); |
| 1437 | } |
| 1438 | #elif defined(__APPLE__) |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1439 | if (strcmp(Py_FileSystemDefaultEncoding, "utf-8") == 0) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1440 | return PyUnicode_DecodeUTF8(s, size, "replace"); |
| 1441 | } |
| 1442 | #endif |
| 1443 | return PyUnicode_Decode(s, size, |
| 1444 | Py_FileSystemDefaultEncoding, |
| 1445 | "replace"); |
| 1446 | } |
| 1447 | else { |
| 1448 | return PyUnicode_DecodeUTF8(s, size, "replace"); |
| 1449 | } |
| 1450 | } |
| 1451 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1452 | char* |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1453 | PyUnicode_AsStringAndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1454 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1455 | PyObject *bytes; |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 1456 | if (!PyUnicode_Check(unicode)) { |
| 1457 | PyErr_BadArgument(); |
| 1458 | return NULL; |
| 1459 | } |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1460 | bytes = _PyUnicode_AsDefaultEncodedString(unicode, NULL); |
| 1461 | if (bytes == NULL) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1462 | return NULL; |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1463 | if (psize != NULL) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1464 | *psize = PyBytes_GET_SIZE(bytes); |
| 1465 | return PyBytes_AS_STRING(bytes); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | char* |
| 1469 | PyUnicode_AsString(PyObject *unicode) |
| 1470 | { |
| 1471 | return PyUnicode_AsStringAndSize(unicode, NULL); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1474 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 1475 | { |
| 1476 | if (!PyUnicode_Check(unicode)) { |
| 1477 | PyErr_BadArgument(); |
| 1478 | goto onError; |
| 1479 | } |
| 1480 | return PyUnicode_AS_UNICODE(unicode); |
| 1481 | |
| 1482 | onError: |
| 1483 | return NULL; |
| 1484 | } |
| 1485 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1486 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1487 | { |
| 1488 | if (!PyUnicode_Check(unicode)) { |
| 1489 | PyErr_BadArgument(); |
| 1490 | goto onError; |
| 1491 | } |
| 1492 | return PyUnicode_GET_SIZE(unicode); |
| 1493 | |
| 1494 | onError: |
| 1495 | return -1; |
| 1496 | } |
| 1497 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 1498 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1499 | { |
| 1500 | return unicode_default_encoding; |
| 1501 | } |
| 1502 | |
| 1503 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 1504 | { |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1505 | if (strcmp(encoding, unicode_default_encoding) != 0) { |
| 1506 | PyErr_Format(PyExc_ValueError, |
| 1507 | "Can only set default encoding to %s", |
| 1508 | unicode_default_encoding); |
| 1509 | return -1; |
| 1510 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1511 | return 0; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1512 | } |
| 1513 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1514 | /* error handling callback helper: |
| 1515 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 1516 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1517 | and adjust various state variables. |
| 1518 | return 0 on success, -1 on error |
| 1519 | */ |
| 1520 | |
| 1521 | static |
| 1522 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
| 1523 | const char *encoding, const char *reason, |
Walter Dörwald | a651d3d | 2007-08-30 15:29:21 +0000 | [diff] [blame] | 1524 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 1525 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1526 | PyObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1527 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1528 | 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] | 1529 | |
| 1530 | PyObject *restuple = NULL; |
| 1531 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1532 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1533 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1534 | Py_ssize_t requiredsize; |
| 1535 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1536 | Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1537 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1538 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1539 | int res = -1; |
| 1540 | |
| 1541 | if (*errorHandler == NULL) { |
| 1542 | *errorHandler = PyCodec_LookupError(errors); |
| 1543 | if (*errorHandler == NULL) |
| 1544 | goto onError; |
| 1545 | } |
| 1546 | |
| 1547 | if (*exceptionObject == NULL) { |
| 1548 | *exceptionObject = PyUnicodeDecodeError_Create( |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1549 | encoding, *input, *inend-*input, *startinpos, *endinpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1550 | if (*exceptionObject == NULL) |
| 1551 | goto onError; |
| 1552 | } |
| 1553 | else { |
| 1554 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 1555 | goto onError; |
| 1556 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
| 1557 | goto onError; |
| 1558 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 1559 | goto onError; |
| 1560 | } |
| 1561 | |
| 1562 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 1563 | if (restuple == NULL) |
| 1564 | goto onError; |
| 1565 | if (!PyTuple_Check(restuple)) { |
| 1566 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 1567 | goto onError; |
| 1568 | } |
| 1569 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
| 1570 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1571 | |
| 1572 | /* Copy back the bytes variables, which might have been modified by the |
| 1573 | callback */ |
| 1574 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 1575 | if (!inputobj) |
| 1576 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1577 | if (!PyBytes_Check(inputobj)) { |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1578 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
| 1579 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1580 | *input = PyBytes_AS_STRING(inputobj); |
| 1581 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1582 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 1583 | /* we can DECREF safely, as the exception has another reference, |
| 1584 | so the object won't go away. */ |
| 1585 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1586 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1587 | if (newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1588 | newpos = insize+newpos; |
| 1589 | if (newpos<0 || newpos>insize) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 1590 | 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] | 1591 | goto onError; |
| 1592 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1593 | |
| 1594 | /* need more space? (at least enough for what we |
| 1595 | have+the replacement+the rest of the string (starting |
| 1596 | at the new input position), so we won't have to check space |
| 1597 | when there are no errors in the rest of the string) */ |
| 1598 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 1599 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 1600 | requiredsize = *outpos + repsize + insize-newpos; |
| 1601 | if (requiredsize > outsize) { |
| 1602 | if (requiredsize<2*outsize) |
| 1603 | requiredsize = 2*outsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1604 | if (PyUnicode_Resize(output, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1605 | goto onError; |
| 1606 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
| 1607 | } |
| 1608 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1609 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1610 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 1611 | *outptr += repsize; |
| 1612 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1613 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1614 | /* we made it! */ |
| 1615 | res = 0; |
| 1616 | |
| 1617 | onError: |
| 1618 | Py_XDECREF(restuple); |
| 1619 | return res; |
| 1620 | } |
| 1621 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1622 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 1623 | |
| 1624 | /* see RFC2152 for details */ |
| 1625 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1626 | static |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1627 | char utf7_special[128] = { |
| 1628 | /* indicate whether a UTF-7 character is special i.e. cannot be directly |
| 1629 | encoded: |
| 1630 | 0 - not special |
| 1631 | 1 - special |
| 1632 | 2 - whitespace (optional) |
| 1633 | 3 - RFC2152 Set O (optional) */ |
| 1634 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, |
| 1635 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1636 | 2, 3, 3, 3, 3, 3, 3, 0, 0, 0, 3, 1, 0, 0, 0, 1, |
| 1637 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, |
| 1638 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1639 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, |
| 1640 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1641 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 1, 1, |
| 1642 | |
| 1643 | }; |
| 1644 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1645 | /* Note: The comparison (c) <= 0 is a trick to work-around gcc |
| 1646 | warnings about the comparison always being false; since |
| 1647 | utf7_special[0] is 1, we can safely make that one comparison |
| 1648 | true */ |
| 1649 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1650 | #define SPECIAL(c, encodeO, encodeWS) \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1651 | ((c) > 127 || (c) <= 0 || utf7_special[(c)] == 1 || \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1652 | (encodeWS && (utf7_special[(c)] == 2)) || \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1653 | (encodeO && (utf7_special[(c)] == 3))) |
| 1654 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1655 | #define B64(n) \ |
| 1656 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 1657 | #define B64CHAR(c) \ |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1658 | (ISALNUM(c) || (c) == '+' || (c) == '/') |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1659 | #define UB64(c) \ |
| 1660 | ((c) == '+' ? 62 : (c) == '/' ? 63 : (c) >= 'a' ? \ |
| 1661 | (c) - 71 : (c) >= 'A' ? (c) - 65 : (c) + 4 ) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1662 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1663 | #define ENCODE(out, ch, bits) \ |
| 1664 | while (bits >= 6) { \ |
| 1665 | *out++ = B64(ch >> (bits-6)); \ |
| 1666 | bits -= 6; \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1667 | } |
| 1668 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1669 | #define DECODE(out, ch, bits, surrogate) \ |
| 1670 | while (bits >= 16) { \ |
| 1671 | Py_UNICODE outCh = (Py_UNICODE) ((ch >> (bits-16)) & 0xffff); \ |
| 1672 | bits -= 16; \ |
| 1673 | if (surrogate) { \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1674 | /* We have already generated an error for the high surrogate \ |
| 1675 | 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] | 1676 | surrogate = 0; \ |
| 1677 | } else if (0xDC00 <= outCh && outCh <= 0xDFFF) { \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1678 | /* This is a surrogate pair. Unfortunately we can't represent \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1679 | it in a 16-bit character */ \ |
| 1680 | surrogate = 1; \ |
| 1681 | errmsg = "code pairs are not supported"; \ |
| 1682 | goto utf7Error; \ |
| 1683 | } else { \ |
| 1684 | *out++ = outCh; \ |
| 1685 | } \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1686 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1687 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1688 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1689 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1690 | const char *errors) |
| 1691 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1692 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 1693 | } |
| 1694 | |
| 1695 | PyObject *PyUnicode_DecodeUTF7Stateful(const char *s, |
| 1696 | Py_ssize_t size, |
| 1697 | const char *errors, |
| 1698 | Py_ssize_t *consumed) |
| 1699 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1700 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1701 | Py_ssize_t startinpos; |
| 1702 | Py_ssize_t endinpos; |
| 1703 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1704 | const char *e; |
| 1705 | PyUnicodeObject *unicode; |
| 1706 | Py_UNICODE *p; |
| 1707 | const char *errmsg = ""; |
| 1708 | int inShift = 0; |
| 1709 | unsigned int bitsleft = 0; |
| 1710 | unsigned long charsleft = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1711 | int surrogate = 0; |
| 1712 | PyObject *errorHandler = NULL; |
| 1713 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1714 | |
| 1715 | unicode = _PyUnicode_New(size); |
| 1716 | if (!unicode) |
| 1717 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1718 | if (size == 0) { |
| 1719 | if (consumed) |
| 1720 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1721 | return (PyObject *)unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1722 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1723 | |
| 1724 | p = unicode->str; |
| 1725 | e = s + size; |
| 1726 | |
| 1727 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1728 | Py_UNICODE ch; |
| 1729 | restart: |
| 1730 | ch = *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1731 | |
| 1732 | if (inShift) { |
| 1733 | if ((ch == '-') || !B64CHAR(ch)) { |
| 1734 | inShift = 0; |
| 1735 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1736 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1737 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1738 | if (bitsleft >= 6) { |
| 1739 | /* The shift sequence has a partial character in it. If |
| 1740 | bitsleft < 6 then we could just classify it as padding |
| 1741 | but that is not the case here */ |
| 1742 | |
| 1743 | errmsg = "partial character in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1744 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1745 | } |
| 1746 | /* According to RFC2152 the remaining bits should be zero. We |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1747 | choose to signal an error/insert a replacement character |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1748 | here so indicate the potential of a misencoded character. */ |
| 1749 | |
| 1750 | /* On x86, a << b == a << (b%32) so make sure that bitsleft != 0 */ |
| 1751 | if (bitsleft && charsleft << (sizeof(charsleft) * 8 - bitsleft)) { |
| 1752 | errmsg = "non-zero padding bits in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1753 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
| 1756 | if (ch == '-') { |
| 1757 | if ((s < e) && (*(s) == '-')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1758 | *p++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1759 | inShift = 1; |
| 1760 | } |
| 1761 | } else if (SPECIAL(ch,0,0)) { |
| 1762 | errmsg = "unexpected special character"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1763 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1764 | } else { |
| 1765 | *p++ = ch; |
| 1766 | } |
| 1767 | } else { |
| 1768 | charsleft = (charsleft << 6) | UB64(ch); |
| 1769 | bitsleft += 6; |
| 1770 | s++; |
| 1771 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1772 | } |
| 1773 | } |
| 1774 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1775 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1776 | s++; |
| 1777 | if (s < e && *s == '-') { |
| 1778 | s++; |
| 1779 | *p++ = '+'; |
| 1780 | } else |
| 1781 | { |
| 1782 | inShift = 1; |
| 1783 | bitsleft = 0; |
| 1784 | } |
| 1785 | } |
| 1786 | else if (SPECIAL(ch,0,0)) { |
Walter Dörwald | 2b65c75 | 2007-08-30 15:35:26 +0000 | [diff] [blame] | 1787 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1788 | errmsg = "unexpected special character"; |
| 1789 | s++; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1790 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1791 | } |
| 1792 | else { |
| 1793 | *p++ = ch; |
| 1794 | s++; |
| 1795 | } |
| 1796 | continue; |
| 1797 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1798 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1799 | endinpos = s-starts; |
| 1800 | if (unicode_decode_call_errorhandler( |
| 1801 | errors, &errorHandler, |
| 1802 | "utf7", errmsg, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1803 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1804 | (PyObject **)&unicode, &outpos, &p)) |
| 1805 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1806 | } |
| 1807 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1808 | if (inShift && !consumed) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1809 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1810 | endinpos = size; |
| 1811 | if (unicode_decode_call_errorhandler( |
| 1812 | errors, &errorHandler, |
| 1813 | "utf7", "unterminated shift sequence", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1814 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1815 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1816 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1817 | if (s < e) |
| 1818 | goto restart; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1819 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1820 | if (consumed) { |
| 1821 | if(inShift) |
| 1822 | *consumed = startinpos; |
| 1823 | else |
| 1824 | *consumed = s-starts; |
| 1825 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1826 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1827 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1828 | goto onError; |
| 1829 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1830 | Py_XDECREF(errorHandler); |
| 1831 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1832 | return (PyObject *)unicode; |
| 1833 | |
| 1834 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1835 | Py_XDECREF(errorHandler); |
| 1836 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1837 | Py_DECREF(unicode); |
| 1838 | return NULL; |
| 1839 | } |
| 1840 | |
| 1841 | |
| 1842 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1843 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1844 | int encodeSetO, |
| 1845 | int encodeWhiteSpace, |
| 1846 | const char *errors) |
| 1847 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1848 | PyObject *v, *result; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1849 | /* It might be possible to tighten this worst case */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1850 | Py_ssize_t cbAllocated = 5 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1851 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1852 | Py_ssize_t i = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1853 | unsigned int bitsleft = 0; |
| 1854 | unsigned long charsleft = 0; |
| 1855 | char * out; |
| 1856 | char * start; |
| 1857 | |
| 1858 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1859 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1860 | |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 1861 | v = PyByteArray_FromStringAndSize(NULL, cbAllocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1862 | if (v == NULL) |
| 1863 | return NULL; |
| 1864 | |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 1865 | start = out = PyByteArray_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1866 | for (;i < size; ++i) { |
| 1867 | Py_UNICODE ch = s[i]; |
| 1868 | |
| 1869 | if (!inShift) { |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1870 | if (ch == '+') { |
| 1871 | *out++ = '+'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1872 | *out++ = '-'; |
| 1873 | } else if (SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1874 | charsleft = ch; |
| 1875 | bitsleft = 16; |
| 1876 | *out++ = '+'; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1877 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1878 | inShift = bitsleft > 0; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1879 | } else { |
| 1880 | *out++ = (char) ch; |
| 1881 | } |
| 1882 | } else { |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1883 | if (!SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1884 | *out++ = B64(charsleft << (6-bitsleft)); |
| 1885 | charsleft = 0; |
| 1886 | bitsleft = 0; |
| 1887 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 1888 | so no '-' is required, except if the character is itself a '-' */ |
| 1889 | if (B64CHAR(ch) || ch == '-') { |
| 1890 | *out++ = '-'; |
| 1891 | } |
| 1892 | inShift = 0; |
| 1893 | *out++ = (char) ch; |
| 1894 | } else { |
| 1895 | bitsleft += 16; |
| 1896 | charsleft = (charsleft << 16) | ch; |
| 1897 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
| 1898 | |
| 1899 | /* If the next character is special then we dont' need to terminate |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1900 | 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] | 1901 | or '-' then the shift sequence will be terminated implicitly and we |
| 1902 | don't have to insert a '-'. */ |
| 1903 | |
| 1904 | if (bitsleft == 0) { |
| 1905 | if (i + 1 < size) { |
| 1906 | Py_UNICODE ch2 = s[i+1]; |
| 1907 | |
| 1908 | if (SPECIAL(ch2, encodeSetO, encodeWhiteSpace)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1909 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1910 | } else if (B64CHAR(ch2) || ch2 == '-') { |
| 1911 | *out++ = '-'; |
| 1912 | inShift = 0; |
| 1913 | } else { |
| 1914 | inShift = 0; |
| 1915 | } |
| 1916 | |
| 1917 | } |
| 1918 | else { |
| 1919 | *out++ = '-'; |
| 1920 | inShift = 0; |
| 1921 | } |
| 1922 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1923 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1924 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1925 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1926 | if (bitsleft) { |
| 1927 | *out++= B64(charsleft << (6-bitsleft) ); |
| 1928 | *out++ = '-'; |
| 1929 | } |
| 1930 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1931 | result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), out - start); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1932 | Py_DECREF(v); |
| 1933 | return result; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1934 | } |
| 1935 | |
| 1936 | #undef SPECIAL |
| 1937 | #undef B64 |
| 1938 | #undef B64CHAR |
| 1939 | #undef UB64 |
| 1940 | #undef ENCODE |
| 1941 | #undef DECODE |
| 1942 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1943 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 1944 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1945 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1946 | char utf8_code_length[256] = { |
| 1947 | /* Map UTF-8 encoded prefix byte to sequence length. zero means |
| 1948 | illegal prefix. see RFC 2279 for details */ |
| 1949 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1950 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1951 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1952 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1953 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1954 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1955 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1956 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1957 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1958 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1959 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1960 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1961 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1962 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1963 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 1964 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 |
| 1965 | }; |
| 1966 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1967 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1968 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1969 | const char *errors) |
| 1970 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1971 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 1972 | } |
| 1973 | |
| 1974 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1975 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1976 | const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1977 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1978 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1979 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1980 | int n; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1981 | Py_ssize_t startinpos; |
| 1982 | Py_ssize_t endinpos; |
| 1983 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1984 | const char *e; |
| 1985 | PyUnicodeObject *unicode; |
| 1986 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1987 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1988 | PyObject *errorHandler = NULL; |
| 1989 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1990 | |
| 1991 | /* Note: size will always be longer than the resulting Unicode |
| 1992 | character count */ |
| 1993 | unicode = _PyUnicode_New(size); |
| 1994 | if (!unicode) |
| 1995 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1996 | if (size == 0) { |
| 1997 | if (consumed) |
| 1998 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1999 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2000 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2001 | |
| 2002 | /* Unpack UTF-8 encoded data */ |
| 2003 | p = unicode->str; |
| 2004 | e = s + size; |
| 2005 | |
| 2006 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2007 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2008 | |
| 2009 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2010 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2011 | s++; |
| 2012 | continue; |
| 2013 | } |
| 2014 | |
| 2015 | n = utf8_code_length[ch]; |
| 2016 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2017 | if (s + n > e) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2018 | if (consumed) |
| 2019 | break; |
| 2020 | else { |
| 2021 | errmsg = "unexpected end of data"; |
| 2022 | startinpos = s-starts; |
| 2023 | endinpos = size; |
| 2024 | goto utf8Error; |
| 2025 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2026 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2027 | |
| 2028 | switch (n) { |
| 2029 | |
| 2030 | case 0: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2031 | errmsg = "unexpected code byte"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2032 | startinpos = s-starts; |
| 2033 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2034 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2035 | |
| 2036 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2037 | errmsg = "internal error"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2038 | startinpos = s-starts; |
| 2039 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2040 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2041 | |
| 2042 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2043 | if ((s[1] & 0xc0) != 0x80) { |
| 2044 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2045 | startinpos = s-starts; |
| 2046 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2047 | goto utf8Error; |
| 2048 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2049 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2050 | if (ch < 0x80) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2051 | startinpos = s-starts; |
| 2052 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2053 | errmsg = "illegal encoding"; |
| 2054 | goto utf8Error; |
| 2055 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2056 | else |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2057 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2058 | break; |
| 2059 | |
| 2060 | case 3: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2061 | if ((s[1] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2062 | (s[2] & 0xc0) != 0x80) { |
| 2063 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2064 | startinpos = s-starts; |
| 2065 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2066 | goto utf8Error; |
| 2067 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2068 | 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] | 2069 | if (ch < 0x0800) { |
| 2070 | /* Note: UTF-8 encodings of surrogates are considered |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2071 | legal UTF-8 sequences; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2072 | |
| 2073 | XXX For wide builds (UCS-4) we should probably try |
| 2074 | to recombine the surrogates into a single code |
| 2075 | unit. |
| 2076 | */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2077 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2078 | startinpos = s-starts; |
| 2079 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2080 | goto utf8Error; |
| 2081 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2082 | else |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2083 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2084 | break; |
| 2085 | |
| 2086 | case 4: |
| 2087 | if ((s[1] & 0xc0) != 0x80 || |
| 2088 | (s[2] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2089 | (s[3] & 0xc0) != 0x80) { |
| 2090 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2091 | startinpos = s-starts; |
| 2092 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2093 | goto utf8Error; |
| 2094 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2095 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 2096 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 2097 | /* validate and convert to UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2098 | if ((ch < 0x10000) /* minimum value allowed for 4 |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2099 | byte encoding */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2100 | || (ch > 0x10ffff)) /* maximum value allowed for |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2101 | UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2102 | { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2103 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2104 | startinpos = s-starts; |
| 2105 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2106 | goto utf8Error; |
| 2107 | } |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2108 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2109 | *p++ = (Py_UNICODE)ch; |
| 2110 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2111 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2112 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2113 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 2114 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2115 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2116 | /* high surrogate = top 10 bits added to D800 */ |
| 2117 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2118 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2119 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2120 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2121 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2122 | break; |
| 2123 | |
| 2124 | default: |
| 2125 | /* Other sizes are only needed for UCS-4 */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2126 | errmsg = "unsupported Unicode code range"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2127 | startinpos = s-starts; |
| 2128 | endinpos = startinpos+n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2129 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2130 | } |
| 2131 | s += n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2132 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2133 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2134 | utf8Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2135 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2136 | if (unicode_decode_call_errorhandler( |
| 2137 | errors, &errorHandler, |
| 2138 | "utf8", errmsg, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2139 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2140 | (PyObject **)&unicode, &outpos, &p)) |
| 2141 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2142 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2143 | if (consumed) |
| 2144 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2145 | |
| 2146 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2147 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2148 | goto onError; |
| 2149 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2150 | Py_XDECREF(errorHandler); |
| 2151 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2152 | return (PyObject *)unicode; |
| 2153 | |
| 2154 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2155 | Py_XDECREF(errorHandler); |
| 2156 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2157 | Py_DECREF(unicode); |
| 2158 | return NULL; |
| 2159 | } |
| 2160 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2161 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 2162 | and allocate exactly as much space needed at the end. Else allocate the |
| 2163 | maximum possible needed (4 result bytes per Unicode character), and return |
| 2164 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2165 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 2166 | PyObject * |
| 2167 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2168 | Py_ssize_t size, |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 2169 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2170 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2171 | #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] | 2172 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2173 | Py_ssize_t i; /* index into s of next input byte */ |
| 2174 | PyObject *result; /* result string object */ |
| 2175 | char *p; /* next free byte in output buffer */ |
| 2176 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 2177 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2178 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2179 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2180 | assert(s != NULL); |
| 2181 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2182 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2183 | if (size <= MAX_SHORT_UNICHARS) { |
| 2184 | /* Write into the stack buffer; nallocated can't overflow. |
| 2185 | * At the end, we'll allocate exactly as much heap space as it |
| 2186 | * turns out we need. |
| 2187 | */ |
| 2188 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2189 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2190 | p = stackbuf; |
| 2191 | } |
| 2192 | else { |
| 2193 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 2194 | nallocated = size * 4; |
| 2195 | if (nallocated / 4 != size) /* overflow! */ |
| 2196 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2197 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2198 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2199 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2200 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2201 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2202 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2203 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2204 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2205 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2206 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2207 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2208 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2209 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2210 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2211 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 2212 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 2213 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2214 | } |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2215 | else { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2216 | /* Encode UCS2 Unicode ordinals */ |
| 2217 | if (ch < 0x10000) { |
| 2218 | /* Special case: check for high surrogate */ |
| 2219 | if (0xD800 <= ch && ch <= 0xDBFF && i != size) { |
| 2220 | Py_UCS4 ch2 = s[i]; |
| 2221 | /* Check for low surrogate and combine the two to |
| 2222 | form a UCS4 value */ |
| 2223 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2224 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2225 | i++; |
| 2226 | goto encodeUCS4; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2227 | } |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2228 | /* Fall through: handles isolated high surrogates */ |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2229 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2230 | *p++ = (char)(0xe0 | (ch >> 12)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2231 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2232 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2233 | continue; |
| 2234 | } |
| 2235 | encodeUCS4: |
| 2236 | /* Encode UCS4 Unicode ordinals */ |
| 2237 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2238 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 2239 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2240 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2241 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2242 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 2243 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2244 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2245 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2246 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2247 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2248 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2249 | } |
| 2250 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 2251 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2252 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2253 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2254 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2255 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2256 | return result; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2257 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2258 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2259 | } |
| 2260 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2261 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 2262 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2263 | if (!PyUnicode_Check(unicode)) { |
| 2264 | PyErr_BadArgument(); |
| 2265 | return NULL; |
| 2266 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 2267 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 2268 | PyUnicode_GET_SIZE(unicode), |
| 2269 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2270 | } |
| 2271 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2272 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 2273 | |
| 2274 | PyObject * |
| 2275 | PyUnicode_DecodeUTF32(const char *s, |
| 2276 | Py_ssize_t size, |
| 2277 | const char *errors, |
| 2278 | int *byteorder) |
| 2279 | { |
| 2280 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 2281 | } |
| 2282 | |
| 2283 | PyObject * |
| 2284 | PyUnicode_DecodeUTF32Stateful(const char *s, |
| 2285 | Py_ssize_t size, |
| 2286 | const char *errors, |
| 2287 | int *byteorder, |
| 2288 | Py_ssize_t *consumed) |
| 2289 | { |
| 2290 | const char *starts = s; |
| 2291 | Py_ssize_t startinpos; |
| 2292 | Py_ssize_t endinpos; |
| 2293 | Py_ssize_t outpos; |
| 2294 | PyUnicodeObject *unicode; |
| 2295 | Py_UNICODE *p; |
| 2296 | #ifndef Py_UNICODE_WIDE |
| 2297 | int i, pairs; |
| 2298 | #else |
| 2299 | const int pairs = 0; |
| 2300 | #endif |
| 2301 | const unsigned char *q, *e; |
| 2302 | int bo = 0; /* assume native ordering by default */ |
| 2303 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2304 | /* Offsets from q for retrieving bytes in the right order. */ |
| 2305 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2306 | int iorder[] = {0, 1, 2, 3}; |
| 2307 | #else |
| 2308 | int iorder[] = {3, 2, 1, 0}; |
| 2309 | #endif |
| 2310 | PyObject *errorHandler = NULL; |
| 2311 | PyObject *exc = NULL; |
Guido van Rossum | 8d991ed | 2007-08-17 15:41:00 +0000 | [diff] [blame] | 2312 | /* On narrow builds we split characters outside the BMP into two |
| 2313 | codepoints => count how much extra space we need. */ |
| 2314 | #ifndef Py_UNICODE_WIDE |
| 2315 | for (i = pairs = 0; i < size/4; i++) |
| 2316 | if (((Py_UCS4 *)s)[i] >= 0x10000) |
| 2317 | pairs++; |
| 2318 | #endif |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2319 | |
| 2320 | /* This might be one to much, because of a BOM */ |
| 2321 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 2322 | if (!unicode) |
| 2323 | return NULL; |
| 2324 | if (size == 0) |
| 2325 | return (PyObject *)unicode; |
| 2326 | |
| 2327 | /* Unpack UTF-32 encoded data */ |
| 2328 | p = unicode->str; |
| 2329 | q = (unsigned char *)s; |
| 2330 | e = q + size; |
| 2331 | |
| 2332 | if (byteorder) |
| 2333 | bo = *byteorder; |
| 2334 | |
| 2335 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2336 | byte order setting accordingly. In native mode, the leading BOM |
| 2337 | mark is skipped, in all other modes, it is copied to the output |
| 2338 | stream as-is (giving a ZWNBSP character). */ |
| 2339 | if (bo == 0) { |
| 2340 | if (size >= 4) { |
| 2341 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2342 | (q[iorder[1]] << 8) | q[iorder[0]]; |
| 2343 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2344 | if (bom == 0x0000FEFF) { |
| 2345 | q += 4; |
| 2346 | bo = -1; |
| 2347 | } |
| 2348 | else if (bom == 0xFFFE0000) { |
| 2349 | q += 4; |
| 2350 | bo = 1; |
| 2351 | } |
| 2352 | #else |
| 2353 | if (bom == 0x0000FEFF) { |
| 2354 | q += 4; |
| 2355 | bo = 1; |
| 2356 | } |
| 2357 | else if (bom == 0xFFFE0000) { |
| 2358 | q += 4; |
| 2359 | bo = -1; |
| 2360 | } |
| 2361 | #endif |
| 2362 | } |
| 2363 | } |
| 2364 | |
| 2365 | if (bo == -1) { |
| 2366 | /* force LE */ |
| 2367 | iorder[0] = 0; |
| 2368 | iorder[1] = 1; |
| 2369 | iorder[2] = 2; |
| 2370 | iorder[3] = 3; |
| 2371 | } |
| 2372 | else if (bo == 1) { |
| 2373 | /* force BE */ |
| 2374 | iorder[0] = 3; |
| 2375 | iorder[1] = 2; |
| 2376 | iorder[2] = 1; |
| 2377 | iorder[3] = 0; |
| 2378 | } |
| 2379 | |
| 2380 | while (q < e) { |
| 2381 | Py_UCS4 ch; |
| 2382 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 2383 | if (e-q<4) { |
| 2384 | if (consumed) |
| 2385 | break; |
| 2386 | errmsg = "truncated data"; |
| 2387 | startinpos = ((const char *)q)-starts; |
| 2388 | endinpos = ((const char *)e)-starts; |
| 2389 | goto utf32Error; |
| 2390 | /* The remaining input chars are ignored if the callback |
| 2391 | chooses to skip the input */ |
| 2392 | } |
| 2393 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2394 | (q[iorder[1]] << 8) | q[iorder[0]]; |
| 2395 | |
| 2396 | if (ch >= 0x110000) |
| 2397 | { |
| 2398 | errmsg = "codepoint not in range(0x110000)"; |
| 2399 | startinpos = ((const char *)q)-starts; |
| 2400 | endinpos = startinpos+4; |
| 2401 | goto utf32Error; |
| 2402 | } |
| 2403 | #ifndef Py_UNICODE_WIDE |
| 2404 | if (ch >= 0x10000) |
| 2405 | { |
| 2406 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 2407 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2408 | } |
| 2409 | else |
| 2410 | #endif |
| 2411 | *p++ = ch; |
| 2412 | q += 4; |
| 2413 | continue; |
| 2414 | utf32Error: |
| 2415 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2416 | if (unicode_decode_call_errorhandler( |
| 2417 | errors, &errorHandler, |
| 2418 | "utf32", errmsg, |
| 2419 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 2420 | (PyObject **)&unicode, &outpos, &p)) |
| 2421 | goto onError; |
| 2422 | } |
| 2423 | |
| 2424 | if (byteorder) |
| 2425 | *byteorder = bo; |
| 2426 | |
| 2427 | if (consumed) |
| 2428 | *consumed = (const char *)q-starts; |
| 2429 | |
| 2430 | /* Adjust length */ |
| 2431 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
| 2432 | goto onError; |
| 2433 | |
| 2434 | Py_XDECREF(errorHandler); |
| 2435 | Py_XDECREF(exc); |
| 2436 | return (PyObject *)unicode; |
| 2437 | |
| 2438 | onError: |
| 2439 | Py_DECREF(unicode); |
| 2440 | Py_XDECREF(errorHandler); |
| 2441 | Py_XDECREF(exc); |
| 2442 | return NULL; |
| 2443 | } |
| 2444 | |
| 2445 | PyObject * |
| 2446 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 2447 | Py_ssize_t size, |
| 2448 | const char *errors, |
| 2449 | int byteorder) |
| 2450 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2451 | PyObject *v, *result; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2452 | unsigned char *p; |
| 2453 | #ifndef Py_UNICODE_WIDE |
| 2454 | int i, pairs; |
| 2455 | #else |
| 2456 | const int pairs = 0; |
| 2457 | #endif |
| 2458 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2459 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2460 | int iorder[] = {0, 1, 2, 3}; |
| 2461 | #else |
| 2462 | int iorder[] = {3, 2, 1, 0}; |
| 2463 | #endif |
| 2464 | |
| 2465 | #define STORECHAR(CH) \ |
| 2466 | do { \ |
| 2467 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 2468 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 2469 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 2470 | p[iorder[0]] = (CH) & 0xff; \ |
| 2471 | p += 4; \ |
| 2472 | } while(0) |
| 2473 | |
| 2474 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 2475 | so we need less space. */ |
| 2476 | #ifndef Py_UNICODE_WIDE |
| 2477 | for (i = pairs = 0; i < size-1; i++) |
| 2478 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 2479 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 2480 | pairs++; |
| 2481 | #endif |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 2482 | v = PyByteArray_FromStringAndSize(NULL, |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2483 | 4 * (size - pairs + (byteorder == 0))); |
| 2484 | if (v == NULL) |
| 2485 | return NULL; |
| 2486 | |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 2487 | p = (unsigned char *)PyByteArray_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2488 | if (byteorder == 0) |
| 2489 | STORECHAR(0xFEFF); |
| 2490 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2491 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2492 | |
| 2493 | if (byteorder == -1) { |
| 2494 | /* force LE */ |
| 2495 | iorder[0] = 0; |
| 2496 | iorder[1] = 1; |
| 2497 | iorder[2] = 2; |
| 2498 | iorder[3] = 3; |
| 2499 | } |
| 2500 | else if (byteorder == 1) { |
| 2501 | /* force BE */ |
| 2502 | iorder[0] = 3; |
| 2503 | iorder[1] = 2; |
| 2504 | iorder[2] = 1; |
| 2505 | iorder[3] = 0; |
| 2506 | } |
| 2507 | |
| 2508 | while (size-- > 0) { |
| 2509 | Py_UCS4 ch = *s++; |
| 2510 | #ifndef Py_UNICODE_WIDE |
| 2511 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 2512 | Py_UCS4 ch2 = *s; |
| 2513 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 2514 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 2515 | s++; |
| 2516 | size--; |
| 2517 | } |
| 2518 | } |
| 2519 | #endif |
| 2520 | STORECHAR(ch); |
| 2521 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2522 | |
| 2523 | done: |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2524 | result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2525 | Py_DECREF(v); |
| 2526 | return result; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2527 | #undef STORECHAR |
| 2528 | } |
| 2529 | |
| 2530 | PyObject *PyUnicode_AsUTF32String(PyObject *unicode) |
| 2531 | { |
| 2532 | if (!PyUnicode_Check(unicode)) { |
| 2533 | PyErr_BadArgument(); |
| 2534 | return NULL; |
| 2535 | } |
| 2536 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
| 2537 | PyUnicode_GET_SIZE(unicode), |
| 2538 | NULL, |
| 2539 | 0); |
| 2540 | } |
| 2541 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2542 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 2543 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2544 | PyObject * |
| 2545 | PyUnicode_DecodeUTF16(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2546 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2547 | const char *errors, |
| 2548 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2549 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2550 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 2551 | } |
| 2552 | |
| 2553 | PyObject * |
| 2554 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2555 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2556 | const char *errors, |
| 2557 | int *byteorder, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2558 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2559 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2560 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2561 | Py_ssize_t startinpos; |
| 2562 | Py_ssize_t endinpos; |
| 2563 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2564 | PyUnicodeObject *unicode; |
| 2565 | Py_UNICODE *p; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2566 | const unsigned char *q, *e; |
| 2567 | int bo = 0; /* assume native ordering by default */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2568 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2569 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 2570 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2571 | int ihi = 1, ilo = 0; |
| 2572 | #else |
| 2573 | int ihi = 0, ilo = 1; |
| 2574 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2575 | PyObject *errorHandler = NULL; |
| 2576 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2577 | |
| 2578 | /* Note: size will always be longer than the resulting Unicode |
| 2579 | character count */ |
| 2580 | unicode = _PyUnicode_New(size); |
| 2581 | if (!unicode) |
| 2582 | return NULL; |
| 2583 | if (size == 0) |
| 2584 | return (PyObject *)unicode; |
| 2585 | |
| 2586 | /* Unpack UTF-16 encoded data */ |
| 2587 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2588 | q = (unsigned char *)s; |
| 2589 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2590 | |
| 2591 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2592 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2593 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2594 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2595 | byte order setting accordingly. In native mode, the leading BOM |
| 2596 | mark is skipped, in all other modes, it is copied to the output |
| 2597 | stream as-is (giving a ZWNBSP character). */ |
| 2598 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2599 | if (size >= 2) { |
| 2600 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2601 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2602 | if (bom == 0xFEFF) { |
| 2603 | q += 2; |
| 2604 | bo = -1; |
| 2605 | } |
| 2606 | else if (bom == 0xFFFE) { |
| 2607 | q += 2; |
| 2608 | bo = 1; |
| 2609 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2610 | #else |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2611 | if (bom == 0xFEFF) { |
| 2612 | q += 2; |
| 2613 | bo = 1; |
| 2614 | } |
| 2615 | else if (bom == 0xFFFE) { |
| 2616 | q += 2; |
| 2617 | bo = -1; |
| 2618 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2619 | #endif |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2620 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2621 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2622 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2623 | if (bo == -1) { |
| 2624 | /* force LE */ |
| 2625 | ihi = 1; |
| 2626 | ilo = 0; |
| 2627 | } |
| 2628 | else if (bo == 1) { |
| 2629 | /* force BE */ |
| 2630 | ihi = 0; |
| 2631 | ilo = 1; |
| 2632 | } |
| 2633 | |
| 2634 | while (q < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2635 | Py_UNICODE ch; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2636 | /* remaining bytes at the end? (size should be even) */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2637 | if (e-q<2) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2638 | if (consumed) |
| 2639 | break; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2640 | errmsg = "truncated data"; |
| 2641 | startinpos = ((const char *)q)-starts; |
| 2642 | endinpos = ((const char *)e)-starts; |
| 2643 | goto utf16Error; |
| 2644 | /* The remaining input chars are ignored if the callback |
| 2645 | chooses to skip the input */ |
| 2646 | } |
| 2647 | ch = (q[ihi] << 8) | q[ilo]; |
| 2648 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2649 | q += 2; |
| 2650 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2651 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 2652 | *p++ = ch; |
| 2653 | continue; |
| 2654 | } |
| 2655 | |
| 2656 | /* UTF-16 code pair: */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2657 | if (q >= e) { |
| 2658 | errmsg = "unexpected end of data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2659 | startinpos = (((const char *)q)-2)-starts; |
| 2660 | endinpos = ((const char *)e)-starts; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2661 | goto utf16Error; |
| 2662 | } |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 2663 | if (0xD800 <= ch && ch <= 0xDBFF) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2664 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 2665 | q += 2; |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 2666 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2667 | #ifndef Py_UNICODE_WIDE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2668 | *p++ = ch; |
| 2669 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2670 | #else |
| 2671 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2672 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2673 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2674 | } |
| 2675 | else { |
| 2676 | errmsg = "illegal UTF-16 surrogate"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2677 | startinpos = (((const char *)q)-4)-starts; |
| 2678 | endinpos = startinpos+2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2679 | goto utf16Error; |
| 2680 | } |
| 2681 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2682 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2683 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2684 | startinpos = (((const char *)q)-2)-starts; |
| 2685 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2686 | /* Fall through to report the error */ |
| 2687 | |
| 2688 | utf16Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2689 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2690 | if (unicode_decode_call_errorhandler( |
| 2691 | errors, &errorHandler, |
| 2692 | "utf16", errmsg, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2693 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2694 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2695 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2696 | } |
| 2697 | |
| 2698 | if (byteorder) |
| 2699 | *byteorder = bo; |
| 2700 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2701 | if (consumed) |
| 2702 | *consumed = (const char *)q-starts; |
| 2703 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2704 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2705 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2706 | goto onError; |
| 2707 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2708 | Py_XDECREF(errorHandler); |
| 2709 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2710 | return (PyObject *)unicode; |
| 2711 | |
| 2712 | onError: |
| 2713 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2714 | Py_XDECREF(errorHandler); |
| 2715 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2716 | return NULL; |
| 2717 | } |
| 2718 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2719 | PyObject * |
| 2720 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2721 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2722 | const char *errors, |
| 2723 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2724 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2725 | PyObject *v, *result; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2726 | unsigned char *p; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2727 | #ifdef Py_UNICODE_WIDE |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2728 | int i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2729 | #else |
| 2730 | const int pairs = 0; |
| 2731 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2732 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2733 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2734 | int ihi = 1, ilo = 0; |
| 2735 | #else |
| 2736 | int ihi = 0, ilo = 1; |
| 2737 | #endif |
| 2738 | |
| 2739 | #define STORECHAR(CH) \ |
| 2740 | do { \ |
| 2741 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 2742 | p[ilo] = (CH) & 0xff; \ |
| 2743 | p += 2; \ |
| 2744 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2745 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2746 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2747 | for (i = pairs = 0; i < size; i++) |
| 2748 | if (s[i] >= 0x10000) |
| 2749 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2750 | #endif |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 2751 | v = PyByteArray_FromStringAndSize(NULL, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2752 | 2 * (size + pairs + (byteorder == 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2753 | if (v == NULL) |
| 2754 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2755 | |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 2756 | p = (unsigned char *)PyByteArray_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2757 | if (byteorder == 0) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2758 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2759 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2760 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2761 | |
| 2762 | if (byteorder == -1) { |
| 2763 | /* force LE */ |
| 2764 | ihi = 1; |
| 2765 | ilo = 0; |
| 2766 | } |
| 2767 | else if (byteorder == 1) { |
| 2768 | /* force BE */ |
| 2769 | ihi = 0; |
| 2770 | ilo = 1; |
| 2771 | } |
| 2772 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2773 | while (size-- > 0) { |
| 2774 | Py_UNICODE ch = *s++; |
| 2775 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2776 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2777 | if (ch >= 0x10000) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2778 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2779 | ch = 0xD800 | ((ch-0x10000) >> 10); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2780 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2781 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2782 | STORECHAR(ch); |
| 2783 | if (ch2) |
| 2784 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2785 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2786 | |
| 2787 | done: |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2788 | result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2789 | Py_DECREF(v); |
| 2790 | return result; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2791 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2792 | } |
| 2793 | |
| 2794 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 2795 | { |
| 2796 | if (!PyUnicode_Check(unicode)) { |
| 2797 | PyErr_BadArgument(); |
| 2798 | return NULL; |
| 2799 | } |
| 2800 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
| 2801 | PyUnicode_GET_SIZE(unicode), |
| 2802 | NULL, |
| 2803 | 0); |
| 2804 | } |
| 2805 | |
| 2806 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 2807 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2808 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 2809 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2810 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2811 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2812 | const char *errors) |
| 2813 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2814 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2815 | Py_ssize_t startinpos; |
| 2816 | Py_ssize_t endinpos; |
| 2817 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2818 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2819 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2820 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2821 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2822 | char* message; |
| 2823 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2824 | PyObject *errorHandler = NULL; |
| 2825 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2826 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2827 | /* Escaped strings will always be longer than the resulting |
| 2828 | 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] | 2829 | length after conversion to the true value. |
| 2830 | (but if the error callback returns a long replacement string |
| 2831 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2832 | v = _PyUnicode_New(size); |
| 2833 | if (v == NULL) |
| 2834 | goto onError; |
| 2835 | if (size == 0) |
| 2836 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2837 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2838 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2839 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2840 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2841 | while (s < end) { |
| 2842 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2843 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2844 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2845 | |
| 2846 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 2847 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2848 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2849 | continue; |
| 2850 | } |
| 2851 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2852 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2853 | /* \ - Escapes */ |
| 2854 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2855 | c = *s++; |
| 2856 | if (s > end) |
| 2857 | c = '\0'; /* Invalid after \ */ |
| 2858 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2859 | |
| 2860 | /* \x escapes */ |
| 2861 | case '\n': break; |
| 2862 | case '\\': *p++ = '\\'; break; |
| 2863 | case '\'': *p++ = '\''; break; |
| 2864 | case '\"': *p++ = '\"'; break; |
| 2865 | case 'b': *p++ = '\b'; break; |
| 2866 | case 'f': *p++ = '\014'; break; /* FF */ |
| 2867 | case 't': *p++ = '\t'; break; |
| 2868 | case 'n': *p++ = '\n'; break; |
| 2869 | case 'r': *p++ = '\r'; break; |
| 2870 | case 'v': *p++ = '\013'; break; /* VT */ |
| 2871 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 2872 | |
| 2873 | /* \OOO (octal) escapes */ |
| 2874 | case '0': case '1': case '2': case '3': |
| 2875 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2876 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2877 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2878 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2879 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2880 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2881 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2882 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2883 | break; |
| 2884 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2885 | /* hex escapes */ |
| 2886 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2887 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2888 | digits = 2; |
| 2889 | message = "truncated \\xXX escape"; |
| 2890 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2891 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2892 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2893 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2894 | digits = 4; |
| 2895 | message = "truncated \\uXXXX escape"; |
| 2896 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2897 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2898 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2899 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2900 | digits = 8; |
| 2901 | message = "truncated \\UXXXXXXXX escape"; |
| 2902 | hexescape: |
| 2903 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2904 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2905 | if (s+digits>end) { |
| 2906 | endinpos = size; |
| 2907 | if (unicode_decode_call_errorhandler( |
| 2908 | errors, &errorHandler, |
| 2909 | "unicodeescape", "end of string in escape sequence", |
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)) |
| 2912 | goto onError; |
| 2913 | goto nextByte; |
| 2914 | } |
| 2915 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2916 | c = (unsigned char) s[i]; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2917 | if (!ISXDIGIT(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2918 | endinpos = (s+i+1)-starts; |
| 2919 | if (unicode_decode_call_errorhandler( |
| 2920 | errors, &errorHandler, |
| 2921 | "unicodeescape", message, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2922 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2923 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2924 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2925 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2926 | } |
| 2927 | chr = (chr<<4) & ~0xF; |
| 2928 | if (c >= '0' && c <= '9') |
| 2929 | chr += c - '0'; |
| 2930 | else if (c >= 'a' && c <= 'f') |
| 2931 | chr += 10 + c - 'a'; |
| 2932 | else |
| 2933 | chr += 10 + c - 'A'; |
| 2934 | } |
| 2935 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 2936 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2937 | /* _decoding_error will have already written into the |
| 2938 | target buffer. */ |
| 2939 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2940 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2941 | /* when we get here, chr is a 32-bit unicode character */ |
| 2942 | if (chr <= 0xffff) |
| 2943 | /* UCS-2 character */ |
| 2944 | *p++ = (Py_UNICODE) chr; |
| 2945 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2946 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2947 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2948 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2949 | *p++ = chr; |
| 2950 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2951 | chr -= 0x10000L; |
| 2952 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2953 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2954 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2955 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2956 | endinpos = s-starts; |
| 2957 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2958 | if (unicode_decode_call_errorhandler( |
| 2959 | errors, &errorHandler, |
| 2960 | "unicodeescape", "illegal Unicode character", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2961 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2962 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2963 | goto onError; |
| 2964 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2965 | break; |
| 2966 | |
| 2967 | /* \N{name} */ |
| 2968 | case 'N': |
| 2969 | message = "malformed \\N character escape"; |
| 2970 | if (ucnhash_CAPI == NULL) { |
| 2971 | /* load the unicode data module */ |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2972 | PyObject *m, *api; |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 2973 | m = PyImport_ImportModuleNoBlock("unicodedata"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2974 | if (m == NULL) |
| 2975 | goto ucnhashError; |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2976 | api = PyObject_GetAttrString(m, "ucnhash_CAPI"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2977 | Py_DECREF(m); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2978 | if (api == NULL) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2979 | goto ucnhashError; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2980 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCObject_AsVoidPtr(api); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2981 | Py_DECREF(api); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2982 | if (ucnhash_CAPI == NULL) |
| 2983 | goto ucnhashError; |
| 2984 | } |
| 2985 | if (*s == '{') { |
| 2986 | const char *start = s+1; |
| 2987 | /* look for the closing brace */ |
| 2988 | while (*s != '}' && s < end) |
| 2989 | s++; |
| 2990 | if (s > start && s < end && *s == '}') { |
| 2991 | /* found a name. look it up in the unicode database */ |
| 2992 | message = "unknown Unicode character name"; |
| 2993 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 2994 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2995 | goto store; |
| 2996 | } |
| 2997 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2998 | endinpos = s-starts; |
| 2999 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3000 | if (unicode_decode_call_errorhandler( |
| 3001 | errors, &errorHandler, |
| 3002 | "unicodeescape", message, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3003 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3004 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3005 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3006 | break; |
| 3007 | |
| 3008 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3009 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3010 | message = "\\ at end of string"; |
| 3011 | s--; |
| 3012 | endinpos = s-starts; |
| 3013 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3014 | if (unicode_decode_call_errorhandler( |
| 3015 | errors, &errorHandler, |
| 3016 | "unicodeescape", message, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3017 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3018 | (PyObject **)&v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3019 | goto onError; |
| 3020 | } |
| 3021 | else { |
| 3022 | *p++ = '\\'; |
| 3023 | *p++ = (unsigned char)s[-1]; |
| 3024 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3025 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3026 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3027 | nextByte: |
| 3028 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3029 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3030 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3031 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 3032 | Py_XDECREF(errorHandler); |
| 3033 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3034 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3035 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3036 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 3037 | PyErr_SetString( |
| 3038 | PyExc_UnicodeError, |
| 3039 | "\\N escapes not supported (can't load unicodedata module)" |
| 3040 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3041 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3042 | Py_XDECREF(errorHandler); |
| 3043 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 3044 | return NULL; |
| 3045 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3046 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3047 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3048 | Py_XDECREF(errorHandler); |
| 3049 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3050 | return NULL; |
| 3051 | } |
| 3052 | |
| 3053 | /* Return a Unicode-Escape string version of the Unicode object. |
| 3054 | |
| 3055 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 3056 | appropriate. |
| 3057 | |
| 3058 | */ |
| 3059 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3060 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
| 3061 | Py_ssize_t size, |
| 3062 | Py_UNICODE ch) |
| 3063 | { |
| 3064 | /* like wcschr, but doesn't stop at NULL characters */ |
| 3065 | |
| 3066 | while (size-- > 0) { |
| 3067 | if (*s == ch) |
| 3068 | return s; |
| 3069 | s++; |
| 3070 | } |
| 3071 | |
| 3072 | return NULL; |
| 3073 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 3074 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3075 | static const char *hexdigits = "0123456789abcdef"; |
| 3076 | |
| 3077 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 3078 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3079 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3080 | PyObject *repr, *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3081 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3082 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3083 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 3084 | better to choose a different scheme. Perhaps scan the |
| 3085 | first N-chars of the string and allocate based on that size. |
| 3086 | */ |
| 3087 | /* Initial allocation is based on the longest-possible unichr |
| 3088 | escape. |
| 3089 | |
| 3090 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 3091 | unichr, so in this case it's the longest unichr escape. In |
| 3092 | narrow (UTF-16) builds this is five chars per source unichr |
| 3093 | since there are two unichrs in the surrogate pair, so in narrow |
| 3094 | (UTF-16) builds it's not the longest unichr escape. |
| 3095 | |
| 3096 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 3097 | so in the narrow (UTF-16) build case it's the longest unichr |
| 3098 | escape. |
| 3099 | */ |
| 3100 | |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 3101 | repr = PyByteArray_FromStringAndSize(NULL, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3102 | #ifdef Py_UNICODE_WIDE |
| 3103 | + 10*size |
| 3104 | #else |
| 3105 | + 6*size |
| 3106 | #endif |
| 3107 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3108 | if (repr == NULL) |
| 3109 | return NULL; |
| 3110 | |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 3111 | p = PyByteArray_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3112 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3113 | while (size-- > 0) { |
| 3114 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3115 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3116 | /* Escape backslashes */ |
| 3117 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3118 | *p++ = '\\'; |
| 3119 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3120 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3121 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3122 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 3123 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3124 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 3125 | else if (ch >= 0x10000) { |
| 3126 | *p++ = '\\'; |
| 3127 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3128 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 3129 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 3130 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 3131 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 3132 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 3133 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 3134 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 3135 | *p++ = hexdigits[ch & 0x0000000F]; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3136 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3137 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3138 | #else |
| 3139 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3140 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 3141 | Py_UNICODE ch2; |
| 3142 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3143 | |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3144 | ch2 = *s++; |
| 3145 | size--; |
| 3146 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 3147 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 3148 | *p++ = '\\'; |
| 3149 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3150 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 3151 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 3152 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 3153 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 3154 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 3155 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 3156 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 3157 | *p++ = hexdigits[ucs & 0x0000000F]; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3158 | continue; |
| 3159 | } |
| 3160 | /* Fall through: isolated surrogates are copied as-is */ |
| 3161 | s--; |
| 3162 | size++; |
| 3163 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3164 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3165 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3166 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3167 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3168 | *p++ = '\\'; |
| 3169 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3170 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 3171 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 3172 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 3173 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3174 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3175 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3176 | /* Map special whitespace to '\t', \n', '\r' */ |
| 3177 | else if (ch == '\t') { |
| 3178 | *p++ = '\\'; |
| 3179 | *p++ = 't'; |
| 3180 | } |
| 3181 | else if (ch == '\n') { |
| 3182 | *p++ = '\\'; |
| 3183 | *p++ = 'n'; |
| 3184 | } |
| 3185 | else if (ch == '\r') { |
| 3186 | *p++ = '\\'; |
| 3187 | *p++ = 'r'; |
| 3188 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3189 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3190 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 3191 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3192 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3193 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3194 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 3195 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3196 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3197 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3198 | /* Copy everything else as-is */ |
| 3199 | else |
| 3200 | *p++ = (char) ch; |
| 3201 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3202 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3203 | result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(repr), |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 3204 | p - PyByteArray_AS_STRING(repr)); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3205 | Py_DECREF(repr); |
| 3206 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3207 | } |
| 3208 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3209 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
| 3210 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3211 | PyObject *s, *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3212 | if (!PyUnicode_Check(unicode)) { |
| 3213 | PyErr_BadArgument(); |
| 3214 | return NULL; |
| 3215 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3216 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3217 | PyUnicode_GET_SIZE(unicode)); |
| 3218 | |
| 3219 | if (!s) |
| 3220 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3221 | result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(s), |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 3222 | PyByteArray_GET_SIZE(s)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3223 | Py_DECREF(s); |
| 3224 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3225 | } |
| 3226 | |
| 3227 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 3228 | |
| 3229 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3230 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3231 | const char *errors) |
| 3232 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3233 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3234 | Py_ssize_t startinpos; |
| 3235 | Py_ssize_t endinpos; |
| 3236 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3237 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3238 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3239 | const char *end; |
| 3240 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3241 | PyObject *errorHandler = NULL; |
| 3242 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3243 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3244 | /* Escaped strings will always be longer than the resulting |
| 3245 | 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] | 3246 | length after conversion to the true value. (But decoding error |
| 3247 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3248 | v = _PyUnicode_New(size); |
| 3249 | if (v == NULL) |
| 3250 | goto onError; |
| 3251 | if (size == 0) |
| 3252 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3253 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3254 | end = s + size; |
| 3255 | while (s < end) { |
| 3256 | unsigned char c; |
Martin v. Löwis | 047c05e | 2002-03-21 08:55:28 +0000 | [diff] [blame] | 3257 | Py_UCS4 x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3258 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3259 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3260 | |
| 3261 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3262 | if (*s != '\\') { |
| 3263 | *p++ = (unsigned char)*s++; |
| 3264 | continue; |
| 3265 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3266 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3267 | |
| 3268 | /* \u-escapes are only interpreted iff the number of leading |
| 3269 | backslashes if odd */ |
| 3270 | bs = s; |
| 3271 | for (;s < end;) { |
| 3272 | if (*s != '\\') |
| 3273 | break; |
| 3274 | *p++ = (unsigned char)*s++; |
| 3275 | } |
| 3276 | if (((s - bs) & 1) == 0 || |
| 3277 | s >= end || |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3278 | (*s != 'u' && *s != 'U')) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3279 | continue; |
| 3280 | } |
| 3281 | p--; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3282 | count = *s=='u' ? 4 : 8; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3283 | s++; |
| 3284 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3285 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3286 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3287 | for (x = 0, i = 0; i < count; ++i, ++s) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3288 | c = (unsigned char)*s; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3289 | if (!ISXDIGIT(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3290 | endinpos = s-starts; |
| 3291 | if (unicode_decode_call_errorhandler( |
| 3292 | errors, &errorHandler, |
| 3293 | "rawunicodeescape", "truncated \\uXXXX", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3294 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3295 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3296 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3297 | goto nextByte; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3298 | } |
| 3299 | x = (x<<4) & ~0xF; |
| 3300 | if (c >= '0' && c <= '9') |
| 3301 | x += c - '0'; |
| 3302 | else if (c >= 'a' && c <= 'f') |
| 3303 | x += 10 + c - 'a'; |
| 3304 | else |
| 3305 | x += 10 + c - 'A'; |
| 3306 | } |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3307 | if (x <= 0xffff) |
| 3308 | /* UCS-2 character */ |
| 3309 | *p++ = (Py_UNICODE) x; |
| 3310 | else if (x <= 0x10ffff) { |
| 3311 | /* UCS-4 character. Either store directly, or as |
| 3312 | surrogate pair. */ |
| 3313 | #ifdef Py_UNICODE_WIDE |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 3314 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3315 | #else |
| 3316 | x -= 0x10000L; |
| 3317 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 3318 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
| 3319 | #endif |
| 3320 | } else { |
| 3321 | endinpos = s-starts; |
| 3322 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3323 | if (unicode_decode_call_errorhandler( |
| 3324 | errors, &errorHandler, |
| 3325 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3326 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3327 | (PyObject **)&v, &outpos, &p)) |
| 3328 | goto onError; |
| 3329 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3330 | nextByte: |
| 3331 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3332 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3333 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3334 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3335 | Py_XDECREF(errorHandler); |
| 3336 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3337 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3338 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3339 | onError: |
| 3340 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3341 | Py_XDECREF(errorHandler); |
| 3342 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3343 | return NULL; |
| 3344 | } |
| 3345 | |
| 3346 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3347 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3348 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3349 | PyObject *repr, *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3350 | char *p; |
| 3351 | char *q; |
| 3352 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3353 | #ifdef Py_UNICODE_WIDE |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 3354 | repr = PyByteArray_FromStringAndSize(NULL, 10 * size); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3355 | #else |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 3356 | repr = PyByteArray_FromStringAndSize(NULL, 6 * size); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3357 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3358 | if (repr == NULL) |
| 3359 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3360 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3361 | goto done; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3362 | |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 3363 | p = q = PyByteArray_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3364 | while (size-- > 0) { |
| 3365 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3366 | #ifdef Py_UNICODE_WIDE |
| 3367 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 3368 | if (ch >= 0x10000) { |
| 3369 | *p++ = '\\'; |
| 3370 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 3371 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 3372 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 3373 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 3374 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 3375 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 3376 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 3377 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 3378 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3379 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3380 | else |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3381 | #else |
| 3382 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 3383 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 3384 | Py_UNICODE ch2; |
| 3385 | Py_UCS4 ucs; |
| 3386 | |
| 3387 | ch2 = *s++; |
| 3388 | size--; |
| 3389 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 3390 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 3391 | *p++ = '\\'; |
| 3392 | *p++ = 'U'; |
| 3393 | *p++ = hexdigits[(ucs >> 28) & 0xf]; |
| 3394 | *p++ = hexdigits[(ucs >> 24) & 0xf]; |
| 3395 | *p++ = hexdigits[(ucs >> 20) & 0xf]; |
| 3396 | *p++ = hexdigits[(ucs >> 16) & 0xf]; |
| 3397 | *p++ = hexdigits[(ucs >> 12) & 0xf]; |
| 3398 | *p++ = hexdigits[(ucs >> 8) & 0xf]; |
| 3399 | *p++ = hexdigits[(ucs >> 4) & 0xf]; |
| 3400 | *p++ = hexdigits[ucs & 0xf]; |
| 3401 | continue; |
| 3402 | } |
| 3403 | /* Fall through: isolated surrogates are copied as-is */ |
| 3404 | s--; |
| 3405 | size++; |
| 3406 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3407 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3408 | /* Map 16-bit characters to '\uxxxx' */ |
| 3409 | if (ch >= 256) { |
| 3410 | *p++ = '\\'; |
| 3411 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 3412 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 3413 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 3414 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 3415 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3416 | } |
| 3417 | /* Copy everything else as-is */ |
| 3418 | else |
| 3419 | *p++ = (char) ch; |
| 3420 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3421 | size = p - q; |
| 3422 | |
| 3423 | done: |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3424 | result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(repr), size); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3425 | Py_DECREF(repr); |
| 3426 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3427 | } |
| 3428 | |
| 3429 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 3430 | { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3431 | PyObject *s, *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3432 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3433 | PyErr_BadArgument(); |
| 3434 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3435 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3436 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3437 | PyUnicode_GET_SIZE(unicode)); |
| 3438 | |
| 3439 | if (!s) |
| 3440 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3441 | result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(s), |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 3442 | PyByteArray_GET_SIZE(s)); |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3443 | Py_DECREF(s); |
| 3444 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3445 | } |
| 3446 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3447 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 3448 | |
| 3449 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3450 | Py_ssize_t size, |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3451 | const char *errors) |
| 3452 | { |
| 3453 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3454 | Py_ssize_t startinpos; |
| 3455 | Py_ssize_t endinpos; |
| 3456 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3457 | PyUnicodeObject *v; |
| 3458 | Py_UNICODE *p; |
| 3459 | const char *end; |
| 3460 | const char *reason; |
| 3461 | PyObject *errorHandler = NULL; |
| 3462 | PyObject *exc = NULL; |
| 3463 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 3464 | #ifdef Py_UNICODE_WIDE |
| 3465 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 3466 | #endif |
| 3467 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3468 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3469 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 3470 | if (v == NULL) |
| 3471 | goto onError; |
| 3472 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
| 3473 | return (PyObject *)v; |
| 3474 | p = PyUnicode_AS_UNICODE(v); |
| 3475 | end = s + size; |
| 3476 | |
| 3477 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3478 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3479 | /* We have to sanity check the raw data, otherwise doom looms for |
| 3480 | some malformed UCS-4 data. */ |
| 3481 | if ( |
| 3482 | #ifdef Py_UNICODE_WIDE |
| 3483 | *p > unimax || *p < 0 || |
| 3484 | #endif |
| 3485 | end-s < Py_UNICODE_SIZE |
| 3486 | ) |
| 3487 | { |
| 3488 | startinpos = s - starts; |
| 3489 | if (end-s < Py_UNICODE_SIZE) { |
| 3490 | endinpos = end-starts; |
| 3491 | reason = "truncated input"; |
| 3492 | } |
| 3493 | else { |
| 3494 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 3495 | reason = "illegal code point (> 0x10FFFF)"; |
| 3496 | } |
| 3497 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 3498 | if (unicode_decode_call_errorhandler( |
| 3499 | errors, &errorHandler, |
| 3500 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3501 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3502 | (PyObject **)&v, &outpos, &p)) { |
| 3503 | goto onError; |
| 3504 | } |
| 3505 | } |
| 3506 | else { |
| 3507 | p++; |
| 3508 | s += Py_UNICODE_SIZE; |
| 3509 | } |
| 3510 | } |
| 3511 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3512 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3513 | goto onError; |
| 3514 | Py_XDECREF(errorHandler); |
| 3515 | Py_XDECREF(exc); |
| 3516 | return (PyObject *)v; |
| 3517 | |
| 3518 | onError: |
| 3519 | Py_XDECREF(v); |
| 3520 | Py_XDECREF(errorHandler); |
| 3521 | Py_XDECREF(exc); |
| 3522 | return NULL; |
| 3523 | } |
| 3524 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3525 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 3526 | |
| 3527 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3528 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3529 | const char *errors) |
| 3530 | { |
| 3531 | PyUnicodeObject *v; |
| 3532 | Py_UNICODE *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3533 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3534 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3535 | if (size == 1) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3536 | Py_UNICODE r = *(unsigned char*)s; |
| 3537 | return PyUnicode_FromUnicode(&r, 1); |
| 3538 | } |
| 3539 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3540 | v = _PyUnicode_New(size); |
| 3541 | if (v == NULL) |
| 3542 | goto onError; |
| 3543 | if (size == 0) |
| 3544 | return (PyObject *)v; |
| 3545 | p = PyUnicode_AS_UNICODE(v); |
| 3546 | while (size-- > 0) |
| 3547 | *p++ = (unsigned char)*s++; |
| 3548 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3549 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3550 | onError: |
| 3551 | Py_XDECREF(v); |
| 3552 | return NULL; |
| 3553 | } |
| 3554 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3555 | /* create or adjust a UnicodeEncodeError */ |
| 3556 | static void make_encode_exception(PyObject **exceptionObject, |
| 3557 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3558 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3559 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3560 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3561 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3562 | if (*exceptionObject == NULL) { |
| 3563 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 3564 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3565 | } |
| 3566 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3567 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 3568 | goto onError; |
| 3569 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 3570 | goto onError; |
| 3571 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 3572 | goto onError; |
| 3573 | return; |
| 3574 | onError: |
| 3575 | Py_DECREF(*exceptionObject); |
| 3576 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3577 | } |
| 3578 | } |
| 3579 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3580 | /* raises a UnicodeEncodeError */ |
| 3581 | static void raise_encode_exception(PyObject **exceptionObject, |
| 3582 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3583 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3584 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3585 | const char *reason) |
| 3586 | { |
| 3587 | make_encode_exception(exceptionObject, |
| 3588 | encoding, unicode, size, startpos, endpos, reason); |
| 3589 | if (*exceptionObject != NULL) |
| 3590 | PyCodec_StrictErrors(*exceptionObject); |
| 3591 | } |
| 3592 | |
| 3593 | /* error handling callback helper: |
| 3594 | build arguments, call the callback and check the arguments, |
| 3595 | put the result into newpos and return the replacement string, which |
| 3596 | has to be freed by the caller */ |
| 3597 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
| 3598 | PyObject **errorHandler, |
| 3599 | const char *encoding, const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3600 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 3601 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3602 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3603 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3604 | 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] | 3605 | |
| 3606 | PyObject *restuple; |
| 3607 | PyObject *resunicode; |
| 3608 | |
| 3609 | if (*errorHandler == NULL) { |
| 3610 | *errorHandler = PyCodec_LookupError(errors); |
| 3611 | if (*errorHandler == NULL) |
| 3612 | return NULL; |
| 3613 | } |
| 3614 | |
| 3615 | make_encode_exception(exceptionObject, |
| 3616 | encoding, unicode, size, startpos, endpos, reason); |
| 3617 | if (*exceptionObject == NULL) |
| 3618 | return NULL; |
| 3619 | |
| 3620 | restuple = PyObject_CallFunctionObjArgs( |
| 3621 | *errorHandler, *exceptionObject, NULL); |
| 3622 | if (restuple == NULL) |
| 3623 | return NULL; |
| 3624 | if (!PyTuple_Check(restuple)) { |
| 3625 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 3626 | Py_DECREF(restuple); |
| 3627 | return NULL; |
| 3628 | } |
| 3629 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
| 3630 | &resunicode, newpos)) { |
| 3631 | Py_DECREF(restuple); |
| 3632 | return NULL; |
| 3633 | } |
| 3634 | if (*newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3635 | *newpos = size+*newpos; |
| 3636 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 3637 | 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] | 3638 | Py_DECREF(restuple); |
| 3639 | return NULL; |
| 3640 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3641 | Py_INCREF(resunicode); |
| 3642 | Py_DECREF(restuple); |
| 3643 | return resunicode; |
| 3644 | } |
| 3645 | |
| 3646 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3647 | Py_ssize_t size, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3648 | const char *errors, |
| 3649 | int limit) |
| 3650 | { |
| 3651 | /* output object */ |
| 3652 | PyObject *res; |
| 3653 | /* pointers to the beginning and end+1 of input */ |
| 3654 | const Py_UNICODE *startp = p; |
| 3655 | const Py_UNICODE *endp = p + size; |
| 3656 | /* pointer to the beginning of the unencodable characters */ |
| 3657 | /* const Py_UNICODE *badp = NULL; */ |
| 3658 | /* pointer into the output */ |
| 3659 | char *str; |
| 3660 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3661 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3662 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 3663 | 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] | 3664 | PyObject *errorHandler = NULL; |
| 3665 | PyObject *exc = NULL; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3666 | PyObject *result = NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3667 | /* the following variable is used for caching string comparisons |
| 3668 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 3669 | int known_errorHandler = -1; |
| 3670 | |
| 3671 | /* allocate enough for a simple encoding without |
| 3672 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3673 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3674 | return PyBytes_FromStringAndSize(NULL, 0); |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 3675 | res = PyByteArray_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3676 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3677 | return NULL; |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 3678 | str = PyByteArray_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3679 | ressize = size; |
| 3680 | |
| 3681 | while (p<endp) { |
| 3682 | Py_UNICODE c = *p; |
| 3683 | |
| 3684 | /* can we encode this? */ |
| 3685 | if (c<limit) { |
| 3686 | /* no overflow check, because we know that the space is enough */ |
| 3687 | *str++ = (char)c; |
| 3688 | ++p; |
| 3689 | } |
| 3690 | else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3691 | Py_ssize_t unicodepos = p-startp; |
| 3692 | Py_ssize_t requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3693 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3694 | Py_ssize_t repsize; |
| 3695 | Py_ssize_t newpos; |
| 3696 | Py_ssize_t respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3697 | Py_UNICODE *uni2; |
| 3698 | /* startpos for collecting unencodable chars */ |
| 3699 | const Py_UNICODE *collstart = p; |
| 3700 | const Py_UNICODE *collend = p; |
| 3701 | /* find all unecodable characters */ |
| 3702 | while ((collend < endp) && ((*collend)>=limit)) |
| 3703 | ++collend; |
| 3704 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 3705 | if (known_errorHandler==-1) { |
| 3706 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3707 | known_errorHandler = 1; |
| 3708 | else if (!strcmp(errors, "replace")) |
| 3709 | known_errorHandler = 2; |
| 3710 | else if (!strcmp(errors, "ignore")) |
| 3711 | known_errorHandler = 3; |
| 3712 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3713 | known_errorHandler = 4; |
| 3714 | else |
| 3715 | known_errorHandler = 0; |
| 3716 | } |
| 3717 | switch (known_errorHandler) { |
| 3718 | case 1: /* strict */ |
| 3719 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 3720 | goto onError; |
| 3721 | case 2: /* replace */ |
| 3722 | while (collstart++<collend) |
| 3723 | *str++ = '?'; /* fall through */ |
| 3724 | case 3: /* ignore */ |
| 3725 | p = collend; |
| 3726 | break; |
| 3727 | case 4: /* xmlcharrefreplace */ |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 3728 | respos = str - PyByteArray_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3729 | /* determine replacement size (temporarily (mis)uses p) */ |
| 3730 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 3731 | if (*p<10) |
| 3732 | repsize += 2+1+1; |
| 3733 | else if (*p<100) |
| 3734 | repsize += 2+2+1; |
| 3735 | else if (*p<1000) |
| 3736 | repsize += 2+3+1; |
| 3737 | else if (*p<10000) |
| 3738 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 3739 | #ifndef Py_UNICODE_WIDE |
| 3740 | else |
| 3741 | repsize += 2+5+1; |
| 3742 | #else |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3743 | else if (*p<100000) |
| 3744 | repsize += 2+5+1; |
| 3745 | else if (*p<1000000) |
| 3746 | repsize += 2+6+1; |
| 3747 | else |
| 3748 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3749 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3750 | } |
| 3751 | requiredsize = respos+repsize+(endp-collend); |
| 3752 | if (requiredsize > ressize) { |
| 3753 | if (requiredsize<2*ressize) |
| 3754 | requiredsize = 2*ressize; |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 3755 | if (PyByteArray_Resize(res, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3756 | goto onError; |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 3757 | str = PyByteArray_AS_STRING(res) + respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3758 | ressize = requiredsize; |
| 3759 | } |
| 3760 | /* generate replacement (temporarily (mis)uses p) */ |
| 3761 | for (p = collstart; p < collend; ++p) { |
| 3762 | str += sprintf(str, "&#%d;", (int)*p); |
| 3763 | } |
| 3764 | p = collend; |
| 3765 | break; |
| 3766 | default: |
| 3767 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 3768 | encoding, reason, startp, size, &exc, |
| 3769 | collstart-startp, collend-startp, &newpos); |
| 3770 | if (repunicode == NULL) |
| 3771 | goto onError; |
| 3772 | /* need more space? (at least enough for what we |
| 3773 | have+the replacement+the rest of the string, so |
| 3774 | we won't have to check space for encodable characters) */ |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 3775 | respos = str - PyByteArray_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3776 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3777 | requiredsize = respos+repsize+(endp-collend); |
| 3778 | if (requiredsize > ressize) { |
| 3779 | if (requiredsize<2*ressize) |
| 3780 | requiredsize = 2*ressize; |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 3781 | if (PyByteArray_Resize(res, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3782 | Py_DECREF(repunicode); |
| 3783 | goto onError; |
| 3784 | } |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 3785 | str = PyByteArray_AS_STRING(res) + respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3786 | ressize = requiredsize; |
| 3787 | } |
| 3788 | /* check if there is anything unencodable in the replacement |
| 3789 | and copy it to the output */ |
| 3790 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 3791 | c = *uni2; |
| 3792 | if (c >= limit) { |
| 3793 | raise_encode_exception(&exc, encoding, startp, size, |
| 3794 | unicodepos, unicodepos+1, reason); |
| 3795 | Py_DECREF(repunicode); |
| 3796 | goto onError; |
| 3797 | } |
| 3798 | *str = (char)c; |
| 3799 | } |
| 3800 | p = startp + newpos; |
| 3801 | Py_DECREF(repunicode); |
| 3802 | } |
| 3803 | } |
| 3804 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3805 | result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(res), |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 3806 | str - PyByteArray_AS_STRING(res)); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3807 | onError: |
| 3808 | Py_DECREF(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3809 | Py_XDECREF(errorHandler); |
| 3810 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3811 | return result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3812 | } |
| 3813 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3814 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3815 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3816 | const char *errors) |
| 3817 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3818 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3819 | } |
| 3820 | |
| 3821 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 3822 | { |
| 3823 | if (!PyUnicode_Check(unicode)) { |
| 3824 | PyErr_BadArgument(); |
| 3825 | return NULL; |
| 3826 | } |
| 3827 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 3828 | PyUnicode_GET_SIZE(unicode), |
| 3829 | NULL); |
| 3830 | } |
| 3831 | |
| 3832 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 3833 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3834 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3835 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3836 | const char *errors) |
| 3837 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3838 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3839 | PyUnicodeObject *v; |
| 3840 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3841 | Py_ssize_t startinpos; |
| 3842 | Py_ssize_t endinpos; |
| 3843 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3844 | const char *e; |
| 3845 | PyObject *errorHandler = NULL; |
| 3846 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3847 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3848 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3849 | if (size == 1 && *(unsigned char*)s < 128) { |
| 3850 | Py_UNICODE r = *(unsigned char*)s; |
| 3851 | return PyUnicode_FromUnicode(&r, 1); |
| 3852 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3853 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3854 | v = _PyUnicode_New(size); |
| 3855 | if (v == NULL) |
| 3856 | goto onError; |
| 3857 | if (size == 0) |
| 3858 | return (PyObject *)v; |
| 3859 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3860 | e = s + size; |
| 3861 | while (s < e) { |
| 3862 | register unsigned char c = (unsigned char)*s; |
| 3863 | if (c < 128) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3864 | *p++ = c; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3865 | ++s; |
| 3866 | } |
| 3867 | else { |
| 3868 | startinpos = s-starts; |
| 3869 | endinpos = startinpos + 1; |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 3870 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3871 | if (unicode_decode_call_errorhandler( |
| 3872 | errors, &errorHandler, |
| 3873 | "ascii", "ordinal not in range(128)", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3874 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3875 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3876 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3877 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3878 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3879 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3880 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3881 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3882 | Py_XDECREF(errorHandler); |
| 3883 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3884 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3885 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3886 | onError: |
| 3887 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3888 | Py_XDECREF(errorHandler); |
| 3889 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3890 | return NULL; |
| 3891 | } |
| 3892 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3893 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3894 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3895 | const char *errors) |
| 3896 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3897 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3898 | } |
| 3899 | |
| 3900 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 3901 | { |
| 3902 | if (!PyUnicode_Check(unicode)) { |
| 3903 | PyErr_BadArgument(); |
| 3904 | return NULL; |
| 3905 | } |
| 3906 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 3907 | PyUnicode_GET_SIZE(unicode), |
| 3908 | NULL); |
| 3909 | } |
| 3910 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3911 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3912 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3913 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3914 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3915 | #if SIZEOF_INT < SIZEOF_SSIZE_T |
| 3916 | #define NEED_RETRY |
| 3917 | #endif |
| 3918 | |
| 3919 | /* XXX This code is limited to "true" double-byte encodings, as |
| 3920 | a) it assumes an incomplete character consists of a single byte, and |
| 3921 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
| 3922 | encodings, see IsDBCSLeadByteEx documentation. */ |
| 3923 | |
| 3924 | static int is_dbcs_lead_byte(const char *s, int offset) |
| 3925 | { |
| 3926 | const char *curr = s + offset; |
| 3927 | |
| 3928 | if (IsDBCSLeadByte(*curr)) { |
| 3929 | const char *prev = CharPrev(s, curr); |
| 3930 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
| 3931 | } |
| 3932 | return 0; |
| 3933 | } |
| 3934 | |
| 3935 | /* |
| 3936 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 3937 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 3938 | */ |
| 3939 | static int decode_mbcs(PyUnicodeObject **v, |
| 3940 | const char *s, /* MBCS string */ |
| 3941 | int size, /* sizeof MBCS string */ |
| 3942 | int final) |
| 3943 | { |
| 3944 | Py_UNICODE *p; |
| 3945 | Py_ssize_t n = 0; |
| 3946 | int usize = 0; |
| 3947 | |
| 3948 | assert(size >= 0); |
| 3949 | |
| 3950 | /* Skip trailing lead-byte unless 'final' is set */ |
| 3951 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
| 3952 | --size; |
| 3953 | |
| 3954 | /* First get the size of the result */ |
| 3955 | if (size > 0) { |
| 3956 | usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); |
| 3957 | if (usize == 0) { |
| 3958 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3959 | return -1; |
| 3960 | } |
| 3961 | } |
| 3962 | |
| 3963 | if (*v == NULL) { |
| 3964 | /* Create unicode object */ |
| 3965 | *v = _PyUnicode_New(usize); |
| 3966 | if (*v == NULL) |
| 3967 | return -1; |
| 3968 | } |
| 3969 | else { |
| 3970 | /* Extend unicode object */ |
| 3971 | n = PyUnicode_GET_SIZE(*v); |
| 3972 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 3973 | return -1; |
| 3974 | } |
| 3975 | |
| 3976 | /* Do the conversion */ |
| 3977 | if (size > 0) { |
| 3978 | p = PyUnicode_AS_UNICODE(*v) + n; |
| 3979 | if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { |
| 3980 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3981 | return -1; |
| 3982 | } |
| 3983 | } |
| 3984 | |
| 3985 | return size; |
| 3986 | } |
| 3987 | |
| 3988 | PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
| 3989 | Py_ssize_t size, |
| 3990 | const char *errors, |
| 3991 | Py_ssize_t *consumed) |
| 3992 | { |
| 3993 | PyUnicodeObject *v = NULL; |
| 3994 | int done; |
| 3995 | |
| 3996 | if (consumed) |
| 3997 | *consumed = 0; |
| 3998 | |
| 3999 | #ifdef NEED_RETRY |
| 4000 | retry: |
| 4001 | if (size > INT_MAX) |
| 4002 | done = decode_mbcs(&v, s, INT_MAX, 0); |
| 4003 | else |
| 4004 | #endif |
| 4005 | done = decode_mbcs(&v, s, (int)size, !consumed); |
| 4006 | |
| 4007 | if (done < 0) { |
| 4008 | Py_XDECREF(v); |
| 4009 | return NULL; |
| 4010 | } |
| 4011 | |
| 4012 | if (consumed) |
| 4013 | *consumed += done; |
| 4014 | |
| 4015 | #ifdef NEED_RETRY |
| 4016 | if (size > INT_MAX) { |
| 4017 | s += done; |
| 4018 | size -= done; |
| 4019 | goto retry; |
| 4020 | } |
| 4021 | #endif |
| 4022 | |
| 4023 | return (PyObject *)v; |
| 4024 | } |
| 4025 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4026 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4027 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4028 | const char *errors) |
| 4029 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4030 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 4031 | } |
| 4032 | |
| 4033 | /* |
| 4034 | * Convert unicode into string object (MBCS). |
| 4035 | * Returns 0 if succeed, -1 otherwise. |
| 4036 | */ |
| 4037 | static int encode_mbcs(PyObject **repr, |
| 4038 | const Py_UNICODE *p, /* unicode */ |
| 4039 | int size) /* size of unicode */ |
| 4040 | { |
| 4041 | int mbcssize = 0; |
| 4042 | Py_ssize_t n = 0; |
| 4043 | |
| 4044 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4045 | |
| 4046 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4047 | if (size > 0) { |
| 4048 | mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); |
| 4049 | if (mbcssize == 0) { |
| 4050 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4051 | return -1; |
| 4052 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4053 | } |
| 4054 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4055 | if (*repr == NULL) { |
| 4056 | /* Create string object */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4057 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4058 | if (*repr == NULL) |
| 4059 | return -1; |
| 4060 | } |
| 4061 | else { |
| 4062 | /* Extend string object */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4063 | n = PyBytes_Size(*repr); |
| 4064 | if (_PyBytes_Resize(repr, n + mbcssize) < 0) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4065 | return -1; |
| 4066 | } |
| 4067 | |
| 4068 | /* Do the conversion */ |
| 4069 | if (size > 0) { |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4070 | char *s = PyBytes_AS_STRING(*repr) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4071 | if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { |
| 4072 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4073 | return -1; |
| 4074 | } |
| 4075 | } |
| 4076 | |
| 4077 | return 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4078 | } |
| 4079 | |
| 4080 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4081 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4082 | const char *errors) |
| 4083 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4084 | PyObject *repr = NULL; |
| 4085 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 4086 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4087 | #ifdef NEED_RETRY |
| 4088 | retry: |
| 4089 | if (size > INT_MAX) |
| 4090 | ret = encode_mbcs(&repr, p, INT_MAX); |
| 4091 | else |
| 4092 | #endif |
| 4093 | ret = encode_mbcs(&repr, p, (int)size); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4094 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4095 | if (ret < 0) { |
| 4096 | Py_XDECREF(repr); |
| 4097 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4098 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4099 | |
| 4100 | #ifdef NEED_RETRY |
| 4101 | if (size > INT_MAX) { |
| 4102 | p += INT_MAX; |
| 4103 | size -= INT_MAX; |
| 4104 | goto retry; |
| 4105 | } |
| 4106 | #endif |
| 4107 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4108 | return repr; |
| 4109 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4110 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 4111 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 4112 | { |
| 4113 | if (!PyUnicode_Check(unicode)) { |
| 4114 | PyErr_BadArgument(); |
| 4115 | return NULL; |
| 4116 | } |
| 4117 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 4118 | PyUnicode_GET_SIZE(unicode), |
| 4119 | NULL); |
| 4120 | } |
| 4121 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4122 | #undef NEED_RETRY |
| 4123 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4124 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4125 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4126 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 4127 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4128 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4129 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4130 | PyObject *mapping, |
| 4131 | const char *errors) |
| 4132 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4133 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4134 | Py_ssize_t startinpos; |
| 4135 | Py_ssize_t endinpos; |
| 4136 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4137 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4138 | PyUnicodeObject *v; |
| 4139 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4140 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4141 | PyObject *errorHandler = NULL; |
| 4142 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4143 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4144 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4145 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4146 | /* Default to Latin-1 */ |
| 4147 | if (mapping == NULL) |
| 4148 | return PyUnicode_DecodeLatin1(s, size, errors); |
| 4149 | |
| 4150 | v = _PyUnicode_New(size); |
| 4151 | if (v == NULL) |
| 4152 | goto onError; |
| 4153 | if (size == 0) |
| 4154 | return (PyObject *)v; |
| 4155 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4156 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4157 | if (PyUnicode_CheckExact(mapping)) { |
| 4158 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 4159 | maplen = PyUnicode_GET_SIZE(mapping); |
| 4160 | while (s < e) { |
| 4161 | unsigned char ch = *s; |
| 4162 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4163 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4164 | if (ch < maplen) |
| 4165 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4166 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4167 | if (x == 0xfffe) { |
| 4168 | /* undefined mapping */ |
| 4169 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4170 | startinpos = s-starts; |
| 4171 | endinpos = startinpos+1; |
| 4172 | if (unicode_decode_call_errorhandler( |
| 4173 | errors, &errorHandler, |
| 4174 | "charmap", "character maps to <undefined>", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4175 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4176 | (PyObject **)&v, &outpos, &p)) { |
| 4177 | goto onError; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 4178 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4179 | continue; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 4180 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4181 | *p++ = x; |
| 4182 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4183 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4184 | } |
| 4185 | else { |
| 4186 | while (s < e) { |
| 4187 | unsigned char ch = *s; |
| 4188 | PyObject *w, *x; |
| 4189 | |
| 4190 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4191 | w = PyLong_FromLong((long)ch); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4192 | if (w == NULL) |
| 4193 | goto onError; |
| 4194 | x = PyObject_GetItem(mapping, w); |
| 4195 | Py_DECREF(w); |
| 4196 | if (x == NULL) { |
| 4197 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4198 | /* No mapping found means: mapping is undefined. */ |
| 4199 | PyErr_Clear(); |
| 4200 | x = Py_None; |
| 4201 | Py_INCREF(x); |
| 4202 | } else |
| 4203 | goto onError; |
| 4204 | } |
| 4205 | |
| 4206 | /* Apply mapping */ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4207 | if (PyLong_Check(x)) { |
| 4208 | long value = PyLong_AS_LONG(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4209 | if (value < 0 || value > 65535) { |
| 4210 | PyErr_SetString(PyExc_TypeError, |
| 4211 | "character mapping must be in range(65536)"); |
| 4212 | Py_DECREF(x); |
| 4213 | goto onError; |
| 4214 | } |
| 4215 | *p++ = (Py_UNICODE)value; |
| 4216 | } |
| 4217 | else if (x == Py_None) { |
| 4218 | /* undefined mapping */ |
| 4219 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4220 | startinpos = s-starts; |
| 4221 | endinpos = startinpos+1; |
| 4222 | if (unicode_decode_call_errorhandler( |
| 4223 | errors, &errorHandler, |
| 4224 | "charmap", "character maps to <undefined>", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4225 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4226 | (PyObject **)&v, &outpos, &p)) { |
| 4227 | Py_DECREF(x); |
| 4228 | goto onError; |
| 4229 | } |
Walter Dörwald | d4fff17 | 2005-11-28 22:15:56 +0000 | [diff] [blame] | 4230 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4231 | continue; |
| 4232 | } |
| 4233 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4234 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4235 | |
| 4236 | if (targetsize == 1) |
| 4237 | /* 1-1 mapping */ |
| 4238 | *p++ = *PyUnicode_AS_UNICODE(x); |
| 4239 | |
| 4240 | else if (targetsize > 1) { |
| 4241 | /* 1-n mapping */ |
| 4242 | if (targetsize > extrachars) { |
| 4243 | /* resize first */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4244 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 4245 | Py_ssize_t needed = (targetsize - extrachars) + \ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4246 | (targetsize << 2); |
| 4247 | extrachars += needed; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4248 | /* XXX overflow detection missing */ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4249 | if (_PyUnicode_Resize(&v, |
| 4250 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 4251 | Py_DECREF(x); |
| 4252 | goto onError; |
| 4253 | } |
| 4254 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 4255 | } |
| 4256 | Py_UNICODE_COPY(p, |
| 4257 | PyUnicode_AS_UNICODE(x), |
| 4258 | targetsize); |
| 4259 | p += targetsize; |
| 4260 | extrachars -= targetsize; |
| 4261 | } |
| 4262 | /* 1-0 mapping: skip the character */ |
| 4263 | } |
| 4264 | else { |
| 4265 | /* wrong return value */ |
| 4266 | PyErr_SetString(PyExc_TypeError, |
| 4267 | "character mapping must return integer, None or unicode"); |
| 4268 | Py_DECREF(x); |
| 4269 | goto onError; |
| 4270 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4271 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4272 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4273 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4274 | } |
| 4275 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4276 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4277 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4278 | Py_XDECREF(errorHandler); |
| 4279 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4280 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4281 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4282 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4283 | Py_XDECREF(errorHandler); |
| 4284 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4285 | Py_XDECREF(v); |
| 4286 | return NULL; |
| 4287 | } |
| 4288 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4289 | /* Charmap encoding: the lookup table */ |
| 4290 | |
| 4291 | struct encoding_map{ |
| 4292 | PyObject_HEAD |
| 4293 | unsigned char level1[32]; |
| 4294 | int count2, count3; |
| 4295 | unsigned char level23[1]; |
| 4296 | }; |
| 4297 | |
| 4298 | static PyObject* |
| 4299 | encoding_map_size(PyObject *obj, PyObject* args) |
| 4300 | { |
| 4301 | struct encoding_map *map = (struct encoding_map*)obj; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4302 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4303 | 128*map->count3); |
| 4304 | } |
| 4305 | |
| 4306 | static PyMethodDef encoding_map_methods[] = { |
| 4307 | {"size", encoding_map_size, METH_NOARGS, |
| 4308 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 4309 | { 0 } |
| 4310 | }; |
| 4311 | |
| 4312 | static void |
| 4313 | encoding_map_dealloc(PyObject* o) |
| 4314 | { |
| 4315 | PyObject_FREE(o); |
| 4316 | } |
| 4317 | |
| 4318 | static PyTypeObject EncodingMapType = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 4319 | PyVarObject_HEAD_INIT(NULL, 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4320 | "EncodingMap", /*tp_name*/ |
| 4321 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 4322 | 0, /*tp_itemsize*/ |
| 4323 | /* methods */ |
| 4324 | encoding_map_dealloc, /*tp_dealloc*/ |
| 4325 | 0, /*tp_print*/ |
| 4326 | 0, /*tp_getattr*/ |
| 4327 | 0, /*tp_setattr*/ |
| 4328 | 0, /*tp_compare*/ |
| 4329 | 0, /*tp_repr*/ |
| 4330 | 0, /*tp_as_number*/ |
| 4331 | 0, /*tp_as_sequence*/ |
| 4332 | 0, /*tp_as_mapping*/ |
| 4333 | 0, /*tp_hash*/ |
| 4334 | 0, /*tp_call*/ |
| 4335 | 0, /*tp_str*/ |
| 4336 | 0, /*tp_getattro*/ |
| 4337 | 0, /*tp_setattro*/ |
| 4338 | 0, /*tp_as_buffer*/ |
| 4339 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 4340 | 0, /*tp_doc*/ |
| 4341 | 0, /*tp_traverse*/ |
| 4342 | 0, /*tp_clear*/ |
| 4343 | 0, /*tp_richcompare*/ |
| 4344 | 0, /*tp_weaklistoffset*/ |
| 4345 | 0, /*tp_iter*/ |
| 4346 | 0, /*tp_iternext*/ |
| 4347 | encoding_map_methods, /*tp_methods*/ |
| 4348 | 0, /*tp_members*/ |
| 4349 | 0, /*tp_getset*/ |
| 4350 | 0, /*tp_base*/ |
| 4351 | 0, /*tp_dict*/ |
| 4352 | 0, /*tp_descr_get*/ |
| 4353 | 0, /*tp_descr_set*/ |
| 4354 | 0, /*tp_dictoffset*/ |
| 4355 | 0, /*tp_init*/ |
| 4356 | 0, /*tp_alloc*/ |
| 4357 | 0, /*tp_new*/ |
| 4358 | 0, /*tp_free*/ |
| 4359 | 0, /*tp_is_gc*/ |
| 4360 | }; |
| 4361 | |
| 4362 | PyObject* |
| 4363 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 4364 | { |
| 4365 | Py_UNICODE *decode; |
| 4366 | PyObject *result; |
| 4367 | struct encoding_map *mresult; |
| 4368 | int i; |
| 4369 | int need_dict = 0; |
| 4370 | unsigned char level1[32]; |
| 4371 | unsigned char level2[512]; |
| 4372 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 4373 | int count2 = 0, count3 = 0; |
| 4374 | |
| 4375 | if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { |
| 4376 | PyErr_BadArgument(); |
| 4377 | return NULL; |
| 4378 | } |
| 4379 | decode = PyUnicode_AS_UNICODE(string); |
| 4380 | memset(level1, 0xFF, sizeof level1); |
| 4381 | memset(level2, 0xFF, sizeof level2); |
| 4382 | |
| 4383 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 4384 | or if there are non-BMP characters, we need to use |
| 4385 | a mapping dictionary. */ |
| 4386 | if (decode[0] != 0) |
| 4387 | need_dict = 1; |
| 4388 | for (i = 1; i < 256; i++) { |
| 4389 | int l1, l2; |
| 4390 | if (decode[i] == 0 |
| 4391 | #ifdef Py_UNICODE_WIDE |
| 4392 | || decode[i] > 0xFFFF |
| 4393 | #endif |
| 4394 | ) { |
| 4395 | need_dict = 1; |
| 4396 | break; |
| 4397 | } |
| 4398 | if (decode[i] == 0xFFFE) |
| 4399 | /* unmapped character */ |
| 4400 | continue; |
| 4401 | l1 = decode[i] >> 11; |
| 4402 | l2 = decode[i] >> 7; |
| 4403 | if (level1[l1] == 0xFF) |
| 4404 | level1[l1] = count2++; |
| 4405 | if (level2[l2] == 0xFF) |
| 4406 | level2[l2] = count3++; |
| 4407 | } |
| 4408 | |
| 4409 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 4410 | need_dict = 1; |
| 4411 | |
| 4412 | if (need_dict) { |
| 4413 | PyObject *result = PyDict_New(); |
| 4414 | PyObject *key, *value; |
| 4415 | if (!result) |
| 4416 | return NULL; |
| 4417 | for (i = 0; i < 256; i++) { |
| 4418 | key = value = NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4419 | key = PyLong_FromLong(decode[i]); |
| 4420 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4421 | if (!key || !value) |
| 4422 | goto failed1; |
| 4423 | if (PyDict_SetItem(result, key, value) == -1) |
| 4424 | goto failed1; |
| 4425 | Py_DECREF(key); |
| 4426 | Py_DECREF(value); |
| 4427 | } |
| 4428 | return result; |
| 4429 | failed1: |
| 4430 | Py_XDECREF(key); |
| 4431 | Py_XDECREF(value); |
| 4432 | Py_DECREF(result); |
| 4433 | return NULL; |
| 4434 | } |
| 4435 | |
| 4436 | /* Create a three-level trie */ |
| 4437 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 4438 | 16*count2 + 128*count3 - 1); |
| 4439 | if (!result) |
| 4440 | return PyErr_NoMemory(); |
| 4441 | PyObject_Init(result, &EncodingMapType); |
| 4442 | mresult = (struct encoding_map*)result; |
| 4443 | mresult->count2 = count2; |
| 4444 | mresult->count3 = count3; |
| 4445 | mlevel1 = mresult->level1; |
| 4446 | mlevel2 = mresult->level23; |
| 4447 | mlevel3 = mresult->level23 + 16*count2; |
| 4448 | memcpy(mlevel1, level1, 32); |
| 4449 | memset(mlevel2, 0xFF, 16*count2); |
| 4450 | memset(mlevel3, 0, 128*count3); |
| 4451 | count3 = 0; |
| 4452 | for (i = 1; i < 256; i++) { |
| 4453 | int o1, o2, o3, i2, i3; |
| 4454 | if (decode[i] == 0xFFFE) |
| 4455 | /* unmapped character */ |
| 4456 | continue; |
| 4457 | o1 = decode[i]>>11; |
| 4458 | o2 = (decode[i]>>7) & 0xF; |
| 4459 | i2 = 16*mlevel1[o1] + o2; |
| 4460 | if (mlevel2[i2] == 0xFF) |
| 4461 | mlevel2[i2] = count3++; |
| 4462 | o3 = decode[i] & 0x7F; |
| 4463 | i3 = 128*mlevel2[i2] + o3; |
| 4464 | mlevel3[i3] = i; |
| 4465 | } |
| 4466 | return result; |
| 4467 | } |
| 4468 | |
| 4469 | static int |
| 4470 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 4471 | { |
| 4472 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 4473 | int l1 = c>>11; |
| 4474 | int l2 = (c>>7) & 0xF; |
| 4475 | int l3 = c & 0x7F; |
| 4476 | int i; |
| 4477 | |
| 4478 | #ifdef Py_UNICODE_WIDE |
| 4479 | if (c > 0xFFFF) { |
| 4480 | return -1; |
| 4481 | } |
| 4482 | #endif |
| 4483 | if (c == 0) |
| 4484 | return 0; |
| 4485 | /* level 1*/ |
| 4486 | i = map->level1[l1]; |
| 4487 | if (i == 0xFF) { |
| 4488 | return -1; |
| 4489 | } |
| 4490 | /* level 2*/ |
| 4491 | i = map->level23[16*i+l2]; |
| 4492 | if (i == 0xFF) { |
| 4493 | return -1; |
| 4494 | } |
| 4495 | /* level 3 */ |
| 4496 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 4497 | if (i == 0) { |
| 4498 | return -1; |
| 4499 | } |
| 4500 | return i; |
| 4501 | } |
| 4502 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4503 | /* Lookup the character ch in the mapping. If the character |
| 4504 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 4505 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4506 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4507 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4508 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4509 | PyObject *x; |
| 4510 | |
| 4511 | if (w == NULL) |
| 4512 | return NULL; |
| 4513 | x = PyObject_GetItem(mapping, w); |
| 4514 | Py_DECREF(w); |
| 4515 | if (x == NULL) { |
| 4516 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4517 | /* No mapping found means: mapping is undefined. */ |
| 4518 | PyErr_Clear(); |
| 4519 | x = Py_None; |
| 4520 | Py_INCREF(x); |
| 4521 | return x; |
| 4522 | } else |
| 4523 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4524 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 4525 | else if (x == Py_None) |
| 4526 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4527 | else if (PyLong_Check(x)) { |
| 4528 | long value = PyLong_AS_LONG(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4529 | if (value < 0 || value > 255) { |
| 4530 | PyErr_SetString(PyExc_TypeError, |
| 4531 | "character mapping must be in range(256)"); |
| 4532 | Py_DECREF(x); |
| 4533 | return NULL; |
| 4534 | } |
| 4535 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4536 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4537 | else if (PyBytes_Check(x)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4538 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4539 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4540 | /* wrong return value */ |
Walter Dörwald | 580ceed | 2007-05-09 10:39:19 +0000 | [diff] [blame] | 4541 | PyErr_Format(PyExc_TypeError, |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 4542 | "character mapping must return integer, bytes or None, not %.400s", |
Walter Dörwald | 580ceed | 2007-05-09 10:39:19 +0000 | [diff] [blame] | 4543 | x->ob_type->tp_name); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4544 | Py_DECREF(x); |
| 4545 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4546 | } |
| 4547 | } |
| 4548 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4549 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4550 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4551 | { |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4552 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4553 | /* exponentially overallocate to minimize reallocations */ |
| 4554 | if (requiredsize < 2*outsize) |
| 4555 | requiredsize = 2*outsize; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4556 | if (_PyBytes_Resize(outobj, requiredsize)) |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4557 | return -1; |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4558 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4559 | } |
| 4560 | |
| 4561 | typedef enum charmapencode_result { |
| 4562 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
| 4563 | }charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4564 | /* 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] | 4565 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4566 | space is available. Return a new reference to the object that |
| 4567 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 4568 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 4569 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4570 | static |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4571 | charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4572 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4573 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4574 | PyObject *rep; |
| 4575 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4576 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4577 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 4578 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4579 | int res = encoding_map_lookup(c, mapping); |
| 4580 | Py_ssize_t requiredsize = *outpos+1; |
| 4581 | if (res == -1) |
| 4582 | return enc_FAILED; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4583 | if (outsize<requiredsize) |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4584 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4585 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4586 | outstart = PyBytes_AS_STRING(*outobj); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4587 | outstart[(*outpos)++] = (char)res; |
| 4588 | return enc_SUCCESS; |
| 4589 | } |
| 4590 | |
| 4591 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4592 | if (rep==NULL) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4593 | return enc_EXCEPTION; |
| 4594 | else if (rep==Py_None) { |
| 4595 | Py_DECREF(rep); |
| 4596 | return enc_FAILED; |
| 4597 | } else { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4598 | if (PyLong_Check(rep)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4599 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4600 | if (outsize<requiredsize) |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4601 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4602 | Py_DECREF(rep); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4603 | return enc_EXCEPTION; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4604 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4605 | outstart = PyBytes_AS_STRING(*outobj); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4606 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4607 | } |
| 4608 | else { |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4609 | const char *repchars = PyBytes_AS_STRING(rep); |
| 4610 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4611 | Py_ssize_t requiredsize = *outpos+repsize; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4612 | if (outsize<requiredsize) |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4613 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4614 | Py_DECREF(rep); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4615 | return enc_EXCEPTION; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4616 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4617 | outstart = PyBytes_AS_STRING(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4618 | memcpy(outstart + *outpos, repchars, repsize); |
| 4619 | *outpos += repsize; |
| 4620 | } |
| 4621 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4622 | Py_DECREF(rep); |
| 4623 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4624 | } |
| 4625 | |
| 4626 | /* handle an error in PyUnicode_EncodeCharmap |
| 4627 | Return 0 on success, -1 on error */ |
| 4628 | static |
| 4629 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4630 | 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] | 4631 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4632 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4633 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4634 | { |
| 4635 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4636 | Py_ssize_t repsize; |
| 4637 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4638 | Py_UNICODE *uni2; |
| 4639 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4640 | Py_ssize_t collstartpos = *inpos; |
| 4641 | Py_ssize_t collendpos = *inpos+1; |
| 4642 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4643 | char *encoding = "charmap"; |
| 4644 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4645 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4646 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4647 | /* find all unencodable characters */ |
| 4648 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4649 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 4650 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4651 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 4652 | if (res != -1) |
| 4653 | break; |
| 4654 | ++collendpos; |
| 4655 | continue; |
| 4656 | } |
| 4657 | |
| 4658 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 4659 | if (rep==NULL) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4660 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4661 | else if (rep!=Py_None) { |
| 4662 | Py_DECREF(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4663 | break; |
| 4664 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4665 | Py_DECREF(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4666 | ++collendpos; |
| 4667 | } |
| 4668 | /* cache callback name lookup |
| 4669 | * (if not done yet, i.e. it's the first error) */ |
| 4670 | if (*known_errorHandler==-1) { |
| 4671 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4672 | *known_errorHandler = 1; |
| 4673 | else if (!strcmp(errors, "replace")) |
| 4674 | *known_errorHandler = 2; |
| 4675 | else if (!strcmp(errors, "ignore")) |
| 4676 | *known_errorHandler = 3; |
| 4677 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4678 | *known_errorHandler = 4; |
| 4679 | else |
| 4680 | *known_errorHandler = 0; |
| 4681 | } |
| 4682 | switch (*known_errorHandler) { |
| 4683 | case 1: /* strict */ |
| 4684 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4685 | return -1; |
| 4686 | case 2: /* replace */ |
| 4687 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
| 4688 | x = charmapencode_output('?', mapping, res, respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4689 | if (x==enc_EXCEPTION) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4690 | return -1; |
| 4691 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4692 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4693 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4694 | return -1; |
| 4695 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4696 | } |
| 4697 | /* fall through */ |
| 4698 | case 3: /* ignore */ |
| 4699 | *inpos = collendpos; |
| 4700 | break; |
| 4701 | case 4: /* xmlcharrefreplace */ |
| 4702 | /* generate replacement (temporarily (mis)uses p) */ |
| 4703 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
| 4704 | char buffer[2+29+1+1]; |
| 4705 | char *cp; |
| 4706 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 4707 | for (cp = buffer; *cp; ++cp) { |
| 4708 | x = charmapencode_output(*cp, mapping, res, respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4709 | if (x==enc_EXCEPTION) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4710 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4711 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4712 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4713 | return -1; |
| 4714 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4715 | } |
| 4716 | } |
| 4717 | *inpos = collendpos; |
| 4718 | break; |
| 4719 | default: |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4720 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4721 | encoding, reason, p, size, exceptionObject, |
| 4722 | collstartpos, collendpos, &newpos); |
| 4723 | if (repunicode == NULL) |
| 4724 | return -1; |
| 4725 | /* generate replacement */ |
| 4726 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4727 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 4728 | x = charmapencode_output(*uni2, mapping, res, respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4729 | if (x==enc_EXCEPTION) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4730 | return -1; |
| 4731 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4732 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4733 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4734 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4735 | return -1; |
| 4736 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4737 | } |
| 4738 | *inpos = newpos; |
| 4739 | Py_DECREF(repunicode); |
| 4740 | } |
| 4741 | return 0; |
| 4742 | } |
| 4743 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4744 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4745 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4746 | PyObject *mapping, |
| 4747 | const char *errors) |
| 4748 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4749 | /* output object */ |
| 4750 | PyObject *res = NULL; |
| 4751 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4752 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4753 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4754 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4755 | PyObject *errorHandler = NULL; |
| 4756 | PyObject *exc = NULL; |
| 4757 | /* the following variable is used for caching string comparisons |
| 4758 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 4759 | * 3=ignore, 4=xmlcharrefreplace */ |
| 4760 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4761 | |
| 4762 | /* Default to Latin-1 */ |
| 4763 | if (mapping == NULL) |
| 4764 | return PyUnicode_EncodeLatin1(p, size, errors); |
| 4765 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4766 | /* allocate enough for a simple encoding without |
| 4767 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4768 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4769 | if (res == NULL) |
| 4770 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 4771 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4772 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4773 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4774 | while (inpos<size) { |
| 4775 | /* try to encode it */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4776 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4777 | if (x==enc_EXCEPTION) /* error */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4778 | goto onError; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4779 | if (x==enc_FAILED) { /* unencodable character */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4780 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 4781 | &exc, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4782 | &known_errorHandler, &errorHandler, errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4783 | &res, &respos)) { |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 4784 | goto onError; |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 4785 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4786 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4787 | else |
| 4788 | /* done with this character => adjust input position */ |
| 4789 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4790 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4791 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4792 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4793 | if (respos<PyBytes_GET_SIZE(res)) |
| 4794 | _PyBytes_Resize(&res, respos); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4795 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4796 | Py_XDECREF(exc); |
| 4797 | Py_XDECREF(errorHandler); |
| 4798 | return res; |
| 4799 | |
| 4800 | onError: |
| 4801 | Py_XDECREF(res); |
| 4802 | Py_XDECREF(exc); |
| 4803 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4804 | return NULL; |
| 4805 | } |
| 4806 | |
| 4807 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
| 4808 | PyObject *mapping) |
| 4809 | { |
| 4810 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
| 4811 | PyErr_BadArgument(); |
| 4812 | return NULL; |
| 4813 | } |
| 4814 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
| 4815 | PyUnicode_GET_SIZE(unicode), |
| 4816 | mapping, |
| 4817 | NULL); |
| 4818 | } |
| 4819 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4820 | /* create or adjust a UnicodeTranslateError */ |
| 4821 | static void make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4822 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4823 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4824 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4825 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4826 | if (*exceptionObject == NULL) { |
| 4827 | *exceptionObject = PyUnicodeTranslateError_Create( |
| 4828 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4829 | } |
| 4830 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4831 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 4832 | goto onError; |
| 4833 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 4834 | goto onError; |
| 4835 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 4836 | goto onError; |
| 4837 | return; |
| 4838 | onError: |
| 4839 | Py_DECREF(*exceptionObject); |
| 4840 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4841 | } |
| 4842 | } |
| 4843 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4844 | /* raises a UnicodeTranslateError */ |
| 4845 | static void raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4846 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4847 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4848 | const char *reason) |
| 4849 | { |
| 4850 | make_translate_exception(exceptionObject, |
| 4851 | unicode, size, startpos, endpos, reason); |
| 4852 | if (*exceptionObject != NULL) |
| 4853 | PyCodec_StrictErrors(*exceptionObject); |
| 4854 | } |
| 4855 | |
| 4856 | /* error handling callback helper: |
| 4857 | build arguments, call the callback and check the arguments, |
| 4858 | put the result into newpos and return the replacement string, which |
| 4859 | has to be freed by the caller */ |
| 4860 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
| 4861 | PyObject **errorHandler, |
| 4862 | const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4863 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 4864 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4865 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4866 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4867 | 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] | 4868 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4869 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4870 | PyObject *restuple; |
| 4871 | PyObject *resunicode; |
| 4872 | |
| 4873 | if (*errorHandler == NULL) { |
| 4874 | *errorHandler = PyCodec_LookupError(errors); |
| 4875 | if (*errorHandler == NULL) |
| 4876 | return NULL; |
| 4877 | } |
| 4878 | |
| 4879 | make_translate_exception(exceptionObject, |
| 4880 | unicode, size, startpos, endpos, reason); |
| 4881 | if (*exceptionObject == NULL) |
| 4882 | return NULL; |
| 4883 | |
| 4884 | restuple = PyObject_CallFunctionObjArgs( |
| 4885 | *errorHandler, *exceptionObject, NULL); |
| 4886 | if (restuple == NULL) |
| 4887 | return NULL; |
| 4888 | if (!PyTuple_Check(restuple)) { |
| 4889 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 4890 | Py_DECREF(restuple); |
| 4891 | return NULL; |
| 4892 | } |
| 4893 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4894 | &resunicode, &i_newpos)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4895 | Py_DECREF(restuple); |
| 4896 | return NULL; |
| 4897 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4898 | if (i_newpos<0) |
| 4899 | *newpos = size+i_newpos; |
| 4900 | else |
| 4901 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4902 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 4903 | 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] | 4904 | Py_DECREF(restuple); |
| 4905 | return NULL; |
| 4906 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4907 | Py_INCREF(resunicode); |
| 4908 | Py_DECREF(restuple); |
| 4909 | return resunicode; |
| 4910 | } |
| 4911 | |
| 4912 | /* Lookup the character ch in the mapping and put the result in result, |
| 4913 | which must be decrefed by the caller. |
| 4914 | Return 0 on success, -1 on error */ |
| 4915 | static |
| 4916 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 4917 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4918 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4919 | PyObject *x; |
| 4920 | |
| 4921 | if (w == NULL) |
| 4922 | return -1; |
| 4923 | x = PyObject_GetItem(mapping, w); |
| 4924 | Py_DECREF(w); |
| 4925 | if (x == NULL) { |
| 4926 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4927 | /* No mapping found means: use 1:1 mapping. */ |
| 4928 | PyErr_Clear(); |
| 4929 | *result = NULL; |
| 4930 | return 0; |
| 4931 | } else |
| 4932 | return -1; |
| 4933 | } |
| 4934 | else if (x == Py_None) { |
| 4935 | *result = x; |
| 4936 | return 0; |
| 4937 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4938 | else if (PyLong_Check(x)) { |
| 4939 | long value = PyLong_AS_LONG(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4940 | long max = PyUnicode_GetMax(); |
| 4941 | if (value < 0 || value > max) { |
| 4942 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 4943 | "character mapping must be in range(0x%x)", max+1); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4944 | Py_DECREF(x); |
| 4945 | return -1; |
| 4946 | } |
| 4947 | *result = x; |
| 4948 | return 0; |
| 4949 | } |
| 4950 | else if (PyUnicode_Check(x)) { |
| 4951 | *result = x; |
| 4952 | return 0; |
| 4953 | } |
| 4954 | else { |
| 4955 | /* wrong return value */ |
| 4956 | PyErr_SetString(PyExc_TypeError, |
| 4957 | "character mapping must return integer, None or unicode"); |
Walter Dörwald | 150523e | 2003-08-15 16:52:19 +0000 | [diff] [blame] | 4958 | Py_DECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4959 | return -1; |
| 4960 | } |
| 4961 | } |
| 4962 | /* ensure that *outobj is at least requiredsize characters long, |
| 4963 | if not reallocate and adjust various state variables. |
| 4964 | Return 0 on success, -1 on error */ |
| 4965 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4966 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4967 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4968 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4969 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4970 | if (requiredsize > oldsize) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4971 | /* remember old output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4972 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4973 | /* exponentially overallocate to minimize reallocations */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4974 | if (requiredsize < 2 * oldsize) |
| 4975 | requiredsize = 2 * oldsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 4976 | if (_PyUnicode_Resize(outobj, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4977 | return -1; |
| 4978 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4979 | } |
| 4980 | return 0; |
| 4981 | } |
| 4982 | /* lookup the character, put the result in the output string and adjust |
| 4983 | various state variables. Return a new reference to the object that |
| 4984 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 4985 | undefined (in which case no character was written). |
| 4986 | The called must decref result. |
| 4987 | Return 0 on success, -1 on error. */ |
| 4988 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4989 | 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] | 4990 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4991 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4992 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4993 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4994 | return -1; |
| 4995 | if (*res==NULL) { |
| 4996 | /* not found => default to 1:1 mapping */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4997 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4998 | } |
| 4999 | else if (*res==Py_None) |
| 5000 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5001 | else if (PyLong_Check(*res)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5002 | /* no overflow check, because we know that the space is enough */ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5003 | *(*outp)++ = (Py_UNICODE)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5004 | } |
| 5005 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5006 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5007 | if (repsize==1) { |
| 5008 | /* no overflow check, because we know that the space is enough */ |
| 5009 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 5010 | } |
| 5011 | else if (repsize!=0) { |
| 5012 | /* more than one character */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5013 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
Walter Dörwald | cd736e7 | 2004-02-05 17:36:00 +0000 | [diff] [blame] | 5014 | (insize - (curinp-startinp)) + |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5015 | repsize - 1; |
| 5016 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5017 | return -1; |
| 5018 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 5019 | *outp += repsize; |
| 5020 | } |
| 5021 | } |
| 5022 | else |
| 5023 | return -1; |
| 5024 | return 0; |
| 5025 | } |
| 5026 | |
| 5027 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5028 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5029 | PyObject *mapping, |
| 5030 | const char *errors) |
| 5031 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5032 | /* output object */ |
| 5033 | PyObject *res = NULL; |
| 5034 | /* pointers to the beginning and end+1 of input */ |
| 5035 | const Py_UNICODE *startp = p; |
| 5036 | const Py_UNICODE *endp = p + size; |
| 5037 | /* pointer into the output */ |
| 5038 | Py_UNICODE *str; |
| 5039 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5040 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5041 | char *reason = "character maps to <undefined>"; |
| 5042 | PyObject *errorHandler = NULL; |
| 5043 | PyObject *exc = NULL; |
| 5044 | /* the following variable is used for caching string comparisons |
| 5045 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 5046 | * 3=ignore, 4=xmlcharrefreplace */ |
| 5047 | int known_errorHandler = -1; |
| 5048 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5049 | if (mapping == NULL) { |
| 5050 | PyErr_BadArgument(); |
| 5051 | return NULL; |
| 5052 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5053 | |
| 5054 | /* allocate enough for a simple 1:1 translation without |
| 5055 | replacements, if we need more, we'll resize */ |
| 5056 | res = PyUnicode_FromUnicode(NULL, size); |
| 5057 | if (res == NULL) |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5058 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5059 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5060 | return res; |
| 5061 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5062 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5063 | while (p<endp) { |
| 5064 | /* try to encode it */ |
| 5065 | PyObject *x = NULL; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5066 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5067 | Py_XDECREF(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5068 | goto onError; |
| 5069 | } |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 5070 | Py_XDECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5071 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 5072 | ++p; |
| 5073 | else { /* untranslatable character */ |
| 5074 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5075 | Py_ssize_t repsize; |
| 5076 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5077 | Py_UNICODE *uni2; |
| 5078 | /* startpos for collecting untranslatable chars */ |
| 5079 | const Py_UNICODE *collstart = p; |
| 5080 | const Py_UNICODE *collend = p+1; |
| 5081 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5082 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5083 | /* find all untranslatable characters */ |
| 5084 | while (collend < endp) { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5085 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5086 | goto onError; |
| 5087 | Py_XDECREF(x); |
| 5088 | if (x!=Py_None) |
| 5089 | break; |
| 5090 | ++collend; |
| 5091 | } |
| 5092 | /* cache callback name lookup |
| 5093 | * (if not done yet, i.e. it's the first error) */ |
| 5094 | if (known_errorHandler==-1) { |
| 5095 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5096 | known_errorHandler = 1; |
| 5097 | else if (!strcmp(errors, "replace")) |
| 5098 | known_errorHandler = 2; |
| 5099 | else if (!strcmp(errors, "ignore")) |
| 5100 | known_errorHandler = 3; |
| 5101 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5102 | known_errorHandler = 4; |
| 5103 | else |
| 5104 | known_errorHandler = 0; |
| 5105 | } |
| 5106 | switch (known_errorHandler) { |
| 5107 | case 1: /* strict */ |
| 5108 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
| 5109 | goto onError; |
| 5110 | case 2: /* replace */ |
| 5111 | /* No need to check for space, this is a 1:1 replacement */ |
| 5112 | for (coll = collstart; coll<collend; ++coll) |
| 5113 | *str++ = '?'; |
| 5114 | /* fall through */ |
| 5115 | case 3: /* ignore */ |
| 5116 | p = collend; |
| 5117 | break; |
| 5118 | case 4: /* xmlcharrefreplace */ |
| 5119 | /* generate replacement (temporarily (mis)uses p) */ |
| 5120 | for (p = collstart; p < collend; ++p) { |
| 5121 | char buffer[2+29+1+1]; |
| 5122 | char *cp; |
| 5123 | sprintf(buffer, "&#%d;", (int)*p); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5124 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5125 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 5126 | goto onError; |
| 5127 | for (cp = buffer; *cp; ++cp) |
| 5128 | *str++ = *cp; |
| 5129 | } |
| 5130 | p = collend; |
| 5131 | break; |
| 5132 | default: |
| 5133 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 5134 | reason, startp, size, &exc, |
| 5135 | collstart-startp, collend-startp, &newpos); |
| 5136 | if (repunicode == NULL) |
| 5137 | goto onError; |
| 5138 | /* generate replacement */ |
| 5139 | repsize = PyUnicode_GET_SIZE(repunicode); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5140 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5141 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 5142 | Py_DECREF(repunicode); |
| 5143 | goto onError; |
| 5144 | } |
| 5145 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 5146 | *str++ = *uni2; |
| 5147 | p = startp + newpos; |
| 5148 | Py_DECREF(repunicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5149 | } |
| 5150 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5151 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5152 | /* Resize if we allocated to much */ |
| 5153 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5154 | if (respos<PyUnicode_GET_SIZE(res)) { |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 5155 | if (_PyUnicode_Resize(&res, respos) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 5156 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5157 | } |
| 5158 | Py_XDECREF(exc); |
| 5159 | Py_XDECREF(errorHandler); |
| 5160 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5161 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5162 | onError: |
| 5163 | Py_XDECREF(res); |
| 5164 | Py_XDECREF(exc); |
| 5165 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5166 | return NULL; |
| 5167 | } |
| 5168 | |
| 5169 | PyObject *PyUnicode_Translate(PyObject *str, |
| 5170 | PyObject *mapping, |
| 5171 | const char *errors) |
| 5172 | { |
| 5173 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5174 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5175 | str = PyUnicode_FromObject(str); |
| 5176 | if (str == NULL) |
| 5177 | goto onError; |
| 5178 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
| 5179 | PyUnicode_GET_SIZE(str), |
| 5180 | mapping, |
| 5181 | errors); |
| 5182 | Py_DECREF(str); |
| 5183 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5184 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5185 | onError: |
| 5186 | Py_XDECREF(str); |
| 5187 | return NULL; |
| 5188 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5189 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5190 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 5191 | |
| 5192 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5193 | Py_ssize_t length, |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5194 | char *output, |
| 5195 | const char *errors) |
| 5196 | { |
| 5197 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5198 | PyObject *errorHandler = NULL; |
| 5199 | PyObject *exc = NULL; |
| 5200 | const char *encoding = "decimal"; |
| 5201 | const char *reason = "invalid decimal Unicode string"; |
| 5202 | /* the following variable is used for caching string comparisons |
| 5203 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 5204 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5205 | |
| 5206 | if (output == NULL) { |
| 5207 | PyErr_BadArgument(); |
| 5208 | return -1; |
| 5209 | } |
| 5210 | |
| 5211 | p = s; |
| 5212 | end = s + length; |
| 5213 | while (p < end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5214 | register Py_UNICODE ch = *p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5215 | int decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5216 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5217 | Py_ssize_t repsize; |
| 5218 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5219 | Py_UNICODE *uni2; |
| 5220 | Py_UNICODE *collstart; |
| 5221 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5222 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5223 | if (Py_UNICODE_ISSPACE(ch)) { |
| 5224 | *output++ = ' '; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5225 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5226 | continue; |
| 5227 | } |
| 5228 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5229 | if (decimal >= 0) { |
| 5230 | *output++ = '0' + decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5231 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5232 | continue; |
| 5233 | } |
Guido van Rossum | ba47704 | 2000-04-06 18:18:10 +0000 | [diff] [blame] | 5234 | if (0 < ch && ch < 256) { |
Guido van Rossum | 42c29aa | 2000-05-03 23:58:29 +0000 | [diff] [blame] | 5235 | *output++ = (char)ch; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5236 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5237 | continue; |
| 5238 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5239 | /* All other characters are considered unencodable */ |
| 5240 | collstart = p; |
| 5241 | collend = p+1; |
| 5242 | while (collend < end) { |
| 5243 | if ((0 < *collend && *collend < 256) || |
| 5244 | !Py_UNICODE_ISSPACE(*collend) || |
| 5245 | Py_UNICODE_TODECIMAL(*collend)) |
| 5246 | break; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5247 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5248 | /* cache callback name lookup |
| 5249 | * (if not done yet, i.e. it's the first error) */ |
| 5250 | if (known_errorHandler==-1) { |
| 5251 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5252 | known_errorHandler = 1; |
| 5253 | else if (!strcmp(errors, "replace")) |
| 5254 | known_errorHandler = 2; |
| 5255 | else if (!strcmp(errors, "ignore")) |
| 5256 | known_errorHandler = 3; |
| 5257 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5258 | known_errorHandler = 4; |
| 5259 | else |
| 5260 | known_errorHandler = 0; |
| 5261 | } |
| 5262 | switch (known_errorHandler) { |
| 5263 | case 1: /* strict */ |
| 5264 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 5265 | goto onError; |
| 5266 | case 2: /* replace */ |
| 5267 | for (p = collstart; p < collend; ++p) |
| 5268 | *output++ = '?'; |
| 5269 | /* fall through */ |
| 5270 | case 3: /* ignore */ |
| 5271 | p = collend; |
| 5272 | break; |
| 5273 | case 4: /* xmlcharrefreplace */ |
| 5274 | /* generate replacement (temporarily (mis)uses p) */ |
| 5275 | for (p = collstart; p < collend; ++p) |
| 5276 | output += sprintf(output, "&#%d;", (int)*p); |
| 5277 | p = collend; |
| 5278 | break; |
| 5279 | default: |
| 5280 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 5281 | encoding, reason, s, length, &exc, |
| 5282 | collstart-s, collend-s, &newpos); |
| 5283 | if (repunicode == NULL) |
| 5284 | goto onError; |
| 5285 | /* generate replacement */ |
| 5286 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5287 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 5288 | Py_UNICODE ch = *uni2; |
| 5289 | if (Py_UNICODE_ISSPACE(ch)) |
| 5290 | *output++ = ' '; |
| 5291 | else { |
| 5292 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5293 | if (decimal >= 0) |
| 5294 | *output++ = '0' + decimal; |
| 5295 | else if (0 < ch && ch < 256) |
| 5296 | *output++ = (char)ch; |
| 5297 | else { |
| 5298 | Py_DECREF(repunicode); |
| 5299 | raise_encode_exception(&exc, encoding, |
| 5300 | s, length, collstart-s, collend-s, reason); |
| 5301 | goto onError; |
| 5302 | } |
| 5303 | } |
| 5304 | } |
| 5305 | p = s + newpos; |
| 5306 | Py_DECREF(repunicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5307 | } |
| 5308 | } |
| 5309 | /* 0-terminate the output string */ |
| 5310 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5311 | Py_XDECREF(exc); |
| 5312 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5313 | return 0; |
| 5314 | |
| 5315 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5316 | Py_XDECREF(exc); |
| 5317 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5318 | return -1; |
| 5319 | } |
| 5320 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5321 | /* --- Helpers ------------------------------------------------------------ */ |
| 5322 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 5323 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5324 | #include "stringlib/fastsearch.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5325 | #include "stringlib/count.h" |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 5326 | /* Include _ParseTupleFinds from find.h */ |
| 5327 | #define FROM_UNICODE |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5328 | #include "stringlib/find.h" |
| 5329 | #include "stringlib/partition.h" |
| 5330 | |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 5331 | #define _Py_InsertThousandsGrouping _PyUnicode_InsertThousandsGrouping |
| 5332 | #include "stringlib/localeutil.h" |
| 5333 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5334 | /* helper macro to fixup start/end slice values */ |
| 5335 | #define FIX_START_END(obj) \ |
| 5336 | if (start < 0) \ |
| 5337 | start += (obj)->length; \ |
| 5338 | if (start < 0) \ |
| 5339 | start = 0; \ |
| 5340 | if (end > (obj)->length) \ |
| 5341 | end = (obj)->length; \ |
| 5342 | if (end < 0) \ |
| 5343 | end += (obj)->length; \ |
| 5344 | if (end < 0) \ |
| 5345 | end = 0; |
| 5346 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5347 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5348 | PyObject *substr, |
| 5349 | Py_ssize_t start, |
| 5350 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5351 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5352 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5353 | PyUnicodeObject* str_obj; |
| 5354 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5355 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5356 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 5357 | if (!str_obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5358 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5359 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 5360 | if (!sub_obj) { |
| 5361 | Py_DECREF(str_obj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5362 | return -1; |
| 5363 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5364 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5365 | FIX_START_END(str_obj); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5366 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5367 | result = stringlib_count( |
| 5368 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length |
| 5369 | ); |
| 5370 | |
| 5371 | Py_DECREF(sub_obj); |
| 5372 | Py_DECREF(str_obj); |
| 5373 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5374 | return result; |
| 5375 | } |
| 5376 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5377 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5378 | PyObject *sub, |
| 5379 | Py_ssize_t start, |
| 5380 | Py_ssize_t end, |
| 5381 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5382 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5383 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5384 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5385 | str = PyUnicode_FromObject(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5386 | if (!str) |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 5387 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5388 | sub = PyUnicode_FromObject(sub); |
| 5389 | if (!sub) { |
Marc-André Lemburg | 4164439 | 2002-05-29 13:46:29 +0000 | [diff] [blame] | 5390 | Py_DECREF(str); |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 5391 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5392 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5393 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5394 | if (direction > 0) |
| 5395 | result = stringlib_find_slice( |
| 5396 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5397 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5398 | start, end |
| 5399 | ); |
| 5400 | else |
| 5401 | result = stringlib_rfind_slice( |
| 5402 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5403 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5404 | start, end |
| 5405 | ); |
| 5406 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5407 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5408 | Py_DECREF(sub); |
| 5409 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5410 | return result; |
| 5411 | } |
| 5412 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5413 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5414 | int tailmatch(PyUnicodeObject *self, |
| 5415 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5416 | Py_ssize_t start, |
| 5417 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5418 | int direction) |
| 5419 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5420 | if (substring->length == 0) |
| 5421 | return 1; |
| 5422 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5423 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5424 | |
| 5425 | end -= substring->length; |
| 5426 | if (end < start) |
| 5427 | return 0; |
| 5428 | |
| 5429 | if (direction > 0) { |
| 5430 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 5431 | return 1; |
| 5432 | } else { |
| 5433 | if (Py_UNICODE_MATCH(self, start, substring)) |
| 5434 | return 1; |
| 5435 | } |
| 5436 | |
| 5437 | return 0; |
| 5438 | } |
| 5439 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5440 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5441 | PyObject *substr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5442 | Py_ssize_t start, |
| 5443 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5444 | int direction) |
| 5445 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5446 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5447 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5448 | str = PyUnicode_FromObject(str); |
| 5449 | if (str == NULL) |
| 5450 | return -1; |
| 5451 | substr = PyUnicode_FromObject(substr); |
| 5452 | if (substr == NULL) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5453 | Py_DECREF(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5454 | return -1; |
| 5455 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5456 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5457 | result = tailmatch((PyUnicodeObject *)str, |
| 5458 | (PyUnicodeObject *)substr, |
| 5459 | start, end, direction); |
| 5460 | Py_DECREF(str); |
| 5461 | Py_DECREF(substr); |
| 5462 | return result; |
| 5463 | } |
| 5464 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5465 | /* Apply fixfct filter to the Unicode object self and return a |
| 5466 | reference to the modified object */ |
| 5467 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5468 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5469 | PyObject *fixup(PyUnicodeObject *self, |
| 5470 | int (*fixfct)(PyUnicodeObject *s)) |
| 5471 | { |
| 5472 | |
| 5473 | PyUnicodeObject *u; |
| 5474 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5475 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5476 | if (u == NULL) |
| 5477 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5478 | |
| 5479 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5480 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5481 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5482 | /* fixfct should return TRUE if it modified the buffer. If |
| 5483 | FALSE, return a reference to the original buffer instead |
| 5484 | (to save space, not time) */ |
| 5485 | Py_INCREF(self); |
| 5486 | Py_DECREF(u); |
| 5487 | return (PyObject*) self; |
| 5488 | } |
| 5489 | return (PyObject*) u; |
| 5490 | } |
| 5491 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5492 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5493 | int fixupper(PyUnicodeObject *self) |
| 5494 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5495 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5496 | Py_UNICODE *s = self->str; |
| 5497 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5498 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5499 | while (len-- > 0) { |
| 5500 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5501 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5502 | ch = Py_UNICODE_TOUPPER(*s); |
| 5503 | if (ch != *s) { |
| 5504 | status = 1; |
| 5505 | *s = ch; |
| 5506 | } |
| 5507 | s++; |
| 5508 | } |
| 5509 | |
| 5510 | return status; |
| 5511 | } |
| 5512 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5513 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5514 | int fixlower(PyUnicodeObject *self) |
| 5515 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5516 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5517 | Py_UNICODE *s = self->str; |
| 5518 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5519 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5520 | while (len-- > 0) { |
| 5521 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5522 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5523 | ch = Py_UNICODE_TOLOWER(*s); |
| 5524 | if (ch != *s) { |
| 5525 | status = 1; |
| 5526 | *s = ch; |
| 5527 | } |
| 5528 | s++; |
| 5529 | } |
| 5530 | |
| 5531 | return status; |
| 5532 | } |
| 5533 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5534 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5535 | int fixswapcase(PyUnicodeObject *self) |
| 5536 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5537 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5538 | Py_UNICODE *s = self->str; |
| 5539 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5540 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5541 | while (len-- > 0) { |
| 5542 | if (Py_UNICODE_ISUPPER(*s)) { |
| 5543 | *s = Py_UNICODE_TOLOWER(*s); |
| 5544 | status = 1; |
| 5545 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 5546 | *s = Py_UNICODE_TOUPPER(*s); |
| 5547 | status = 1; |
| 5548 | } |
| 5549 | s++; |
| 5550 | } |
| 5551 | |
| 5552 | return status; |
| 5553 | } |
| 5554 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5555 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5556 | int fixcapitalize(PyUnicodeObject *self) |
| 5557 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5558 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5559 | Py_UNICODE *s = self->str; |
| 5560 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5561 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5562 | if (len == 0) |
| 5563 | return 0; |
| 5564 | if (Py_UNICODE_ISLOWER(*s)) { |
| 5565 | *s = Py_UNICODE_TOUPPER(*s); |
| 5566 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5567 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5568 | s++; |
| 5569 | while (--len > 0) { |
| 5570 | if (Py_UNICODE_ISUPPER(*s)) { |
| 5571 | *s = Py_UNICODE_TOLOWER(*s); |
| 5572 | status = 1; |
| 5573 | } |
| 5574 | s++; |
| 5575 | } |
| 5576 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5577 | } |
| 5578 | |
| 5579 | static |
| 5580 | int fixtitle(PyUnicodeObject *self) |
| 5581 | { |
| 5582 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5583 | register Py_UNICODE *e; |
| 5584 | int previous_is_cased; |
| 5585 | |
| 5586 | /* Shortcut for single character strings */ |
| 5587 | if (PyUnicode_GET_SIZE(self) == 1) { |
| 5588 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 5589 | if (*p != ch) { |
| 5590 | *p = ch; |
| 5591 | return 1; |
| 5592 | } |
| 5593 | else |
| 5594 | return 0; |
| 5595 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5596 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5597 | e = p + PyUnicode_GET_SIZE(self); |
| 5598 | previous_is_cased = 0; |
| 5599 | for (; p < e; p++) { |
| 5600 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5601 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5602 | if (previous_is_cased) |
| 5603 | *p = Py_UNICODE_TOLOWER(ch); |
| 5604 | else |
| 5605 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5606 | |
| 5607 | if (Py_UNICODE_ISLOWER(ch) || |
| 5608 | Py_UNICODE_ISUPPER(ch) || |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5609 | Py_UNICODE_ISTITLE(ch)) |
| 5610 | previous_is_cased = 1; |
| 5611 | else |
| 5612 | previous_is_cased = 0; |
| 5613 | } |
| 5614 | return 1; |
| 5615 | } |
| 5616 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5617 | PyObject * |
| 5618 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5619 | { |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5620 | PyObject *internal_separator = NULL; |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 5621 | const Py_UNICODE blank = ' '; |
| 5622 | const Py_UNICODE *sep = ␣ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5623 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5624 | PyUnicodeObject *res = NULL; /* the result */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5625 | Py_ssize_t res_alloc = 100; /* # allocated bytes for string in res */ |
| 5626 | Py_ssize_t res_used; /* # used bytes */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5627 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 5628 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5629 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5630 | PyObject *item; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5631 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5632 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5633 | fseq = PySequence_Fast(seq, ""); |
| 5634 | if (fseq == NULL) { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5635 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5636 | } |
| 5637 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5638 | /* Grrrr. A codec may be invoked to convert str objects to |
| 5639 | * Unicode, and so it's possible to call back into Python code |
| 5640 | * during PyUnicode_FromObject(), and so it's possible for a sick |
| 5641 | * codec to change the size of fseq (if seq is a list). Therefore |
| 5642 | * we have to keep refetching the size -- can't assume seqlen |
| 5643 | * is invariant. |
| 5644 | */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5645 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 5646 | /* If empty sequence, return u"". */ |
| 5647 | if (seqlen == 0) { |
| 5648 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 5649 | goto Done; |
| 5650 | } |
| 5651 | /* If singleton sequence with an exact Unicode, return that. */ |
| 5652 | if (seqlen == 1) { |
| 5653 | item = PySequence_Fast_GET_ITEM(fseq, 0); |
| 5654 | if (PyUnicode_CheckExact(item)) { |
| 5655 | Py_INCREF(item); |
| 5656 | res = (PyUnicodeObject *)item; |
| 5657 | goto Done; |
| 5658 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5659 | } |
| 5660 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5661 | /* At least two items to join, or one that isn't exact Unicode. */ |
| 5662 | if (seqlen > 1) { |
| 5663 | /* Set up sep and seplen -- they're needed. */ |
| 5664 | if (separator == NULL) { |
| 5665 | sep = ␣ |
| 5666 | seplen = 1; |
| 5667 | } |
| 5668 | else { |
| 5669 | internal_separator = PyUnicode_FromObject(separator); |
| 5670 | if (internal_separator == NULL) |
| 5671 | goto onError; |
| 5672 | sep = PyUnicode_AS_UNICODE(internal_separator); |
| 5673 | seplen = PyUnicode_GET_SIZE(internal_separator); |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5674 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 5675 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5676 | } |
| 5677 | } |
| 5678 | |
| 5679 | /* Get space. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5680 | res = _PyUnicode_New(res_alloc); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5681 | if (res == NULL) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5682 | goto onError; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5683 | res_p = PyUnicode_AS_UNICODE(res); |
| 5684 | res_used = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5685 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5686 | for (i = 0; i < seqlen; ++i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5687 | Py_ssize_t itemlen; |
| 5688 | Py_ssize_t new_res_used; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5689 | |
| 5690 | item = PySequence_Fast_GET_ITEM(fseq, i); |
| 5691 | /* Convert item to Unicode. */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5692 | if (!PyUnicode_Check(item)) { |
| 5693 | PyErr_Format(PyExc_TypeError, |
| 5694 | "sequence item %zd: expected str instance," |
| 5695 | " %.80s found", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5696 | i, Py_TYPE(item)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5697 | goto onError; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5698 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5699 | item = PyUnicode_FromObject(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5700 | if (item == NULL) |
| 5701 | goto onError; |
| 5702 | /* We own a reference to item from here on. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5703 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5704 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 5705 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 5706 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5707 | /* 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] | 5708 | itemlen = PyUnicode_GET_SIZE(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5709 | new_res_used = res_used + itemlen; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5710 | if (new_res_used < 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5711 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5712 | if (i < seqlen - 1) { |
| 5713 | new_res_used += seplen; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5714 | if (new_res_used < 0) |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5715 | goto Overflow; |
| 5716 | } |
| 5717 | if (new_res_used > res_alloc) { |
| 5718 | /* double allocated size until it's big enough */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5719 | do { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5720 | res_alloc += res_alloc; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5721 | if (res_alloc <= 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5722 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5723 | } while (new_res_used > res_alloc); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5724 | if (_PyUnicode_Resize(&res, res_alloc) < 0) { |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 5725 | Py_DECREF(item); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5726 | goto onError; |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 5727 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5728 | res_p = PyUnicode_AS_UNICODE(res) + res_used; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5729 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5730 | |
| 5731 | /* Copy item, and maybe the separator. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5732 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5733 | res_p += itemlen; |
| 5734 | if (i < seqlen - 1) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5735 | Py_UNICODE_COPY(res_p, sep, seplen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5736 | res_p += seplen; |
| 5737 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5738 | Py_DECREF(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5739 | res_used = new_res_used; |
| 5740 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5741 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5742 | /* Shrink res to match the used area; this probably can't fail, |
| 5743 | * but it's cheap to check. |
| 5744 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5745 | if (_PyUnicode_Resize(&res, res_used) < 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5746 | goto onError; |
| 5747 | |
| 5748 | Done: |
| 5749 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5750 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5751 | return (PyObject *)res; |
| 5752 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5753 | Overflow: |
| 5754 | PyErr_SetString(PyExc_OverflowError, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5755 | "join() result is too long for a Python string"); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5756 | Py_DECREF(item); |
| 5757 | /* fall through */ |
| 5758 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5759 | onError: |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5760 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5761 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5762 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5763 | return NULL; |
| 5764 | } |
| 5765 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5766 | static |
| 5767 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5768 | Py_ssize_t left, |
| 5769 | Py_ssize_t right, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5770 | Py_UNICODE fill) |
| 5771 | { |
| 5772 | PyUnicodeObject *u; |
| 5773 | |
| 5774 | if (left < 0) |
| 5775 | left = 0; |
| 5776 | if (right < 0) |
| 5777 | right = 0; |
| 5778 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5779 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5780 | Py_INCREF(self); |
| 5781 | return self; |
| 5782 | } |
| 5783 | |
| 5784 | u = _PyUnicode_New(left + self->length + right); |
| 5785 | if (u) { |
| 5786 | if (left) |
| 5787 | Py_UNICODE_FILL(u->str, fill, left); |
| 5788 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 5789 | if (right) |
| 5790 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 5791 | } |
| 5792 | |
| 5793 | return u; |
| 5794 | } |
| 5795 | |
| 5796 | #define SPLIT_APPEND(data, left, right) \ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5797 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5798 | if (!str) \ |
| 5799 | goto onError; \ |
| 5800 | if (PyList_Append(list, str)) { \ |
| 5801 | Py_DECREF(str); \ |
| 5802 | goto onError; \ |
| 5803 | } \ |
| 5804 | else \ |
| 5805 | Py_DECREF(str); |
| 5806 | |
| 5807 | static |
| 5808 | PyObject *split_whitespace(PyUnicodeObject *self, |
| 5809 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5810 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5811 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5812 | register Py_ssize_t i; |
| 5813 | register Py_ssize_t j; |
| 5814 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5815 | PyObject *str; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5816 | register const Py_UNICODE *buf = self->str; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5817 | |
| 5818 | for (i = j = 0; i < len; ) { |
| 5819 | /* find a token */ |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5820 | while (i < len && Py_UNICODE_ISSPACE(buf[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5821 | i++; |
| 5822 | j = i; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5823 | while (i < len && !Py_UNICODE_ISSPACE(buf[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5824 | i++; |
| 5825 | if (j < i) { |
| 5826 | if (maxcount-- <= 0) |
| 5827 | break; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5828 | SPLIT_APPEND(buf, j, i); |
| 5829 | while (i < len && Py_UNICODE_ISSPACE(buf[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5830 | i++; |
| 5831 | j = i; |
| 5832 | } |
| 5833 | } |
| 5834 | if (j < len) { |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5835 | SPLIT_APPEND(buf, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5836 | } |
| 5837 | return list; |
| 5838 | |
| 5839 | onError: |
| 5840 | Py_DECREF(list); |
| 5841 | return NULL; |
| 5842 | } |
| 5843 | |
| 5844 | PyObject *PyUnicode_Splitlines(PyObject *string, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5845 | int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5846 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5847 | register Py_ssize_t i; |
| 5848 | register Py_ssize_t j; |
| 5849 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5850 | PyObject *list; |
| 5851 | PyObject *str; |
| 5852 | Py_UNICODE *data; |
| 5853 | |
| 5854 | string = PyUnicode_FromObject(string); |
| 5855 | if (string == NULL) |
| 5856 | return NULL; |
| 5857 | data = PyUnicode_AS_UNICODE(string); |
| 5858 | len = PyUnicode_GET_SIZE(string); |
| 5859 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5860 | list = PyList_New(0); |
| 5861 | if (!list) |
| 5862 | goto onError; |
| 5863 | |
| 5864 | for (i = j = 0; i < len; ) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5865 | Py_ssize_t eol; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5866 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5867 | /* Find a line and append it */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5868 | while (i < len && !BLOOM_LINEBREAK(data[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5869 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5870 | |
| 5871 | /* Skip the line break reading CRLF as one line break */ |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5872 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5873 | if (i < len) { |
| 5874 | if (data[i] == '\r' && i + 1 < len && |
| 5875 | data[i+1] == '\n') |
| 5876 | i += 2; |
| 5877 | else |
| 5878 | i++; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5879 | if (keepends) |
| 5880 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5881 | } |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5882 | SPLIT_APPEND(data, j, eol); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5883 | j = i; |
| 5884 | } |
| 5885 | if (j < len) { |
| 5886 | SPLIT_APPEND(data, j, len); |
| 5887 | } |
| 5888 | |
| 5889 | Py_DECREF(string); |
| 5890 | return list; |
| 5891 | |
| 5892 | onError: |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5893 | Py_XDECREF(list); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5894 | Py_DECREF(string); |
| 5895 | return NULL; |
| 5896 | } |
| 5897 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5898 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5899 | PyObject *split_char(PyUnicodeObject *self, |
| 5900 | PyObject *list, |
| 5901 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5902 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5903 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5904 | register Py_ssize_t i; |
| 5905 | register Py_ssize_t j; |
| 5906 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5907 | PyObject *str; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5908 | register const Py_UNICODE *buf = self->str; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5909 | |
| 5910 | for (i = j = 0; i < len; ) { |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5911 | if (buf[i] == ch) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5912 | if (maxcount-- <= 0) |
| 5913 | break; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5914 | SPLIT_APPEND(buf, j, i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5915 | i = j = i + 1; |
| 5916 | } else |
| 5917 | i++; |
| 5918 | } |
| 5919 | if (j <= len) { |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5920 | SPLIT_APPEND(buf, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5921 | } |
| 5922 | return list; |
| 5923 | |
| 5924 | onError: |
| 5925 | Py_DECREF(list); |
| 5926 | return NULL; |
| 5927 | } |
| 5928 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5929 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5930 | PyObject *split_substring(PyUnicodeObject *self, |
| 5931 | PyObject *list, |
| 5932 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5933 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5934 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5935 | register Py_ssize_t i; |
| 5936 | register Py_ssize_t j; |
| 5937 | Py_ssize_t len = self->length; |
| 5938 | Py_ssize_t sublen = substring->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5939 | PyObject *str; |
| 5940 | |
Guido van Rossum | cda4f9a | 2000-12-19 02:23:19 +0000 | [diff] [blame] | 5941 | for (i = j = 0; i <= len - sublen; ) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5942 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 5943 | if (maxcount-- <= 0) |
| 5944 | break; |
| 5945 | SPLIT_APPEND(self->str, j, i); |
| 5946 | i = j = i + sublen; |
| 5947 | } else |
| 5948 | i++; |
| 5949 | } |
| 5950 | if (j <= len) { |
| 5951 | SPLIT_APPEND(self->str, j, len); |
| 5952 | } |
| 5953 | return list; |
| 5954 | |
| 5955 | onError: |
| 5956 | Py_DECREF(list); |
| 5957 | return NULL; |
| 5958 | } |
| 5959 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5960 | static |
| 5961 | PyObject *rsplit_whitespace(PyUnicodeObject *self, |
| 5962 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5963 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5964 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5965 | register Py_ssize_t i; |
| 5966 | register Py_ssize_t j; |
| 5967 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5968 | PyObject *str; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5969 | register const Py_UNICODE *buf = self->str; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5970 | |
| 5971 | for (i = j = len - 1; i >= 0; ) { |
| 5972 | /* find a token */ |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5973 | while (i >= 0 && Py_UNICODE_ISSPACE(buf[i])) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5974 | i--; |
| 5975 | j = i; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5976 | while (i >= 0 && !Py_UNICODE_ISSPACE(buf[i])) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5977 | i--; |
| 5978 | if (j > i) { |
| 5979 | if (maxcount-- <= 0) |
| 5980 | break; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5981 | SPLIT_APPEND(buf, i + 1, j + 1); |
| 5982 | while (i >= 0 && Py_UNICODE_ISSPACE(buf[i])) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5983 | i--; |
| 5984 | j = i; |
| 5985 | } |
| 5986 | } |
| 5987 | if (j >= 0) { |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5988 | SPLIT_APPEND(buf, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5989 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5990 | if (PyList_Reverse(list) < 0) |
| 5991 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5992 | return list; |
| 5993 | |
| 5994 | onError: |
| 5995 | Py_DECREF(list); |
| 5996 | return NULL; |
| 5997 | } |
| 5998 | |
| 5999 | static |
| 6000 | PyObject *rsplit_char(PyUnicodeObject *self, |
| 6001 | PyObject *list, |
| 6002 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6003 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6004 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6005 | register Py_ssize_t i; |
| 6006 | register Py_ssize_t j; |
| 6007 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6008 | PyObject *str; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6009 | register const Py_UNICODE *buf = self->str; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6010 | |
| 6011 | for (i = j = len - 1; i >= 0; ) { |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6012 | if (buf[i] == ch) { |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6013 | if (maxcount-- <= 0) |
| 6014 | break; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6015 | SPLIT_APPEND(buf, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6016 | j = i = i - 1; |
| 6017 | } else |
| 6018 | i--; |
| 6019 | } |
Hye-Shik Chang | 7fc4cf5 | 2003-12-23 09:10:16 +0000 | [diff] [blame] | 6020 | if (j >= -1) { |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6021 | SPLIT_APPEND(buf, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6022 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6023 | if (PyList_Reverse(list) < 0) |
| 6024 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6025 | return list; |
| 6026 | |
| 6027 | onError: |
| 6028 | Py_DECREF(list); |
| 6029 | return NULL; |
| 6030 | } |
| 6031 | |
| 6032 | static |
| 6033 | PyObject *rsplit_substring(PyUnicodeObject *self, |
| 6034 | PyObject *list, |
| 6035 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6036 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6037 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6038 | register Py_ssize_t i; |
| 6039 | register Py_ssize_t j; |
| 6040 | Py_ssize_t len = self->length; |
| 6041 | Py_ssize_t sublen = substring->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6042 | PyObject *str; |
| 6043 | |
| 6044 | for (i = len - sublen, j = len; i >= 0; ) { |
| 6045 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 6046 | if (maxcount-- <= 0) |
| 6047 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6048 | SPLIT_APPEND(self->str, i + sublen, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6049 | j = i; |
| 6050 | i -= sublen; |
| 6051 | } else |
| 6052 | i--; |
| 6053 | } |
| 6054 | if (j >= 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6055 | SPLIT_APPEND(self->str, 0, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6056 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6057 | if (PyList_Reverse(list) < 0) |
| 6058 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6059 | return list; |
| 6060 | |
| 6061 | onError: |
| 6062 | Py_DECREF(list); |
| 6063 | return NULL; |
| 6064 | } |
| 6065 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6066 | #undef SPLIT_APPEND |
| 6067 | |
| 6068 | static |
| 6069 | PyObject *split(PyUnicodeObject *self, |
| 6070 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6071 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6072 | { |
| 6073 | PyObject *list; |
| 6074 | |
| 6075 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6076 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6077 | |
| 6078 | list = PyList_New(0); |
| 6079 | if (!list) |
| 6080 | return NULL; |
| 6081 | |
| 6082 | if (substring == NULL) |
| 6083 | return split_whitespace(self,list,maxcount); |
| 6084 | |
| 6085 | else if (substring->length == 1) |
| 6086 | return split_char(self,list,substring->str[0],maxcount); |
| 6087 | |
| 6088 | else if (substring->length == 0) { |
| 6089 | Py_DECREF(list); |
| 6090 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 6091 | return NULL; |
| 6092 | } |
| 6093 | else |
| 6094 | return split_substring(self,list,substring,maxcount); |
| 6095 | } |
| 6096 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6097 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6098 | PyObject *rsplit(PyUnicodeObject *self, |
| 6099 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6100 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6101 | { |
| 6102 | PyObject *list; |
| 6103 | |
| 6104 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6105 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6106 | |
| 6107 | list = PyList_New(0); |
| 6108 | if (!list) |
| 6109 | return NULL; |
| 6110 | |
| 6111 | if (substring == NULL) |
| 6112 | return rsplit_whitespace(self,list,maxcount); |
| 6113 | |
| 6114 | else if (substring->length == 1) |
| 6115 | return rsplit_char(self,list,substring->str[0],maxcount); |
| 6116 | |
| 6117 | else if (substring->length == 0) { |
| 6118 | Py_DECREF(list); |
| 6119 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 6120 | return NULL; |
| 6121 | } |
| 6122 | else |
| 6123 | return rsplit_substring(self,list,substring,maxcount); |
| 6124 | } |
| 6125 | |
| 6126 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6127 | PyObject *replace(PyUnicodeObject *self, |
| 6128 | PyUnicodeObject *str1, |
| 6129 | PyUnicodeObject *str2, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6130 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6131 | { |
| 6132 | PyUnicodeObject *u; |
| 6133 | |
| 6134 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6135 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6136 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6137 | if (str1->length == str2->length) { |
| 6138 | /* same length */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6139 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6140 | if (str1->length == 1) { |
| 6141 | /* replace characters */ |
| 6142 | Py_UNICODE u1, u2; |
| 6143 | if (!findchar(self->str, self->length, str1->str[0])) |
| 6144 | goto nothing; |
| 6145 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6146 | if (!u) |
| 6147 | return NULL; |
| 6148 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6149 | u1 = str1->str[0]; |
| 6150 | u2 = str2->str[0]; |
| 6151 | for (i = 0; i < u->length; i++) |
| 6152 | if (u->str[i] == u1) { |
| 6153 | if (--maxcount < 0) |
| 6154 | break; |
| 6155 | u->str[i] = u2; |
| 6156 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6157 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6158 | i = fastsearch( |
| 6159 | self->str, self->length, str1->str, str1->length, FAST_SEARCH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6160 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6161 | if (i < 0) |
| 6162 | goto nothing; |
| 6163 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6164 | if (!u) |
| 6165 | return NULL; |
| 6166 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6167 | while (i <= self->length - str1->length) |
| 6168 | if (Py_UNICODE_MATCH(self, i, str1)) { |
| 6169 | if (--maxcount < 0) |
| 6170 | break; |
| 6171 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 6172 | i += str1->length; |
| 6173 | } else |
| 6174 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6175 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6176 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6177 | |
| 6178 | Py_ssize_t n, i, j, e; |
| 6179 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6180 | Py_UNICODE *p; |
| 6181 | |
| 6182 | /* replace strings */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6183 | n = stringlib_count(self->str, self->length, str1->str, str1->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6184 | if (n > maxcount) |
| 6185 | n = maxcount; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6186 | if (n == 0) |
| 6187 | goto nothing; |
| 6188 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 6189 | delta = (str2->length - str1->length); |
| 6190 | if (delta == 0) { |
| 6191 | new_size = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6192 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6193 | product = n * (str2->length - str1->length); |
| 6194 | if ((product / (str2->length - str1->length)) != n) { |
| 6195 | PyErr_SetString(PyExc_OverflowError, |
| 6196 | "replace string is too long"); |
| 6197 | return NULL; |
| 6198 | } |
| 6199 | new_size = self->length + product; |
| 6200 | if (new_size < 0) { |
| 6201 | PyErr_SetString(PyExc_OverflowError, |
| 6202 | "replace string is too long"); |
| 6203 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6204 | } |
| 6205 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6206 | u = _PyUnicode_New(new_size); |
| 6207 | if (!u) |
| 6208 | return NULL; |
| 6209 | i = 0; |
| 6210 | p = u->str; |
| 6211 | e = self->length - str1->length; |
| 6212 | if (str1->length > 0) { |
| 6213 | while (n-- > 0) { |
| 6214 | /* look for next match */ |
| 6215 | j = i; |
| 6216 | while (j <= e) { |
| 6217 | if (Py_UNICODE_MATCH(self, j, str1)) |
| 6218 | break; |
| 6219 | j++; |
| 6220 | } |
| 6221 | if (j > i) { |
| 6222 | if (j > e) |
| 6223 | break; |
| 6224 | /* copy unchanged part [i:j] */ |
| 6225 | Py_UNICODE_COPY(p, self->str+i, j-i); |
| 6226 | p += j - i; |
| 6227 | } |
| 6228 | /* copy substitution string */ |
| 6229 | if (str2->length > 0) { |
| 6230 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6231 | p += str2->length; |
| 6232 | } |
| 6233 | i = j + str1->length; |
| 6234 | } |
| 6235 | if (i < self->length) |
| 6236 | /* copy tail [i:] */ |
| 6237 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6238 | } else { |
| 6239 | /* interleave */ |
| 6240 | while (n > 0) { |
| 6241 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6242 | p += str2->length; |
| 6243 | if (--n <= 0) |
| 6244 | break; |
| 6245 | *p++ = self->str[i++]; |
| 6246 | } |
| 6247 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6248 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6249 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6250 | return (PyObject *) u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6251 | |
| 6252 | nothing: |
| 6253 | /* nothing to replace; return original string (when possible) */ |
| 6254 | if (PyUnicode_CheckExact(self)) { |
| 6255 | Py_INCREF(self); |
| 6256 | return (PyObject *) self; |
| 6257 | } |
| 6258 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6259 | } |
| 6260 | |
| 6261 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 6262 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6263 | PyDoc_STRVAR(title__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 6264 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6265 | \n\ |
| 6266 | 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] | 6267 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6268 | |
| 6269 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6270 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6271 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6272 | return fixup(self, fixtitle); |
| 6273 | } |
| 6274 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6275 | PyDoc_STRVAR(capitalize__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 6276 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6277 | \n\ |
| 6278 | 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] | 6279 | have upper case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6280 | |
| 6281 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6282 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6283 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6284 | return fixup(self, fixcapitalize); |
| 6285 | } |
| 6286 | |
| 6287 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6288 | PyDoc_STRVAR(capwords__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 6289 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6290 | \n\ |
| 6291 | 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] | 6292 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6293 | |
| 6294 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6295 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6296 | { |
| 6297 | PyObject *list; |
| 6298 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6299 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6300 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6301 | /* Split into words */ |
| 6302 | list = split(self, NULL, -1); |
| 6303 | if (!list) |
| 6304 | return NULL; |
| 6305 | |
| 6306 | /* Capitalize each word */ |
| 6307 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 6308 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
| 6309 | fixcapitalize); |
| 6310 | if (item == NULL) |
| 6311 | goto onError; |
| 6312 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 6313 | PyList_SET_ITEM(list, i, item); |
| 6314 | } |
| 6315 | |
| 6316 | /* Join the words to form a new string */ |
| 6317 | item = PyUnicode_Join(NULL, list); |
| 6318 | |
| 6319 | onError: |
| 6320 | Py_DECREF(list); |
| 6321 | return (PyObject *)item; |
| 6322 | } |
| 6323 | #endif |
| 6324 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6325 | /* Argument converter. Coerces to a single unicode character */ |
| 6326 | |
| 6327 | static int |
| 6328 | convert_uc(PyObject *obj, void *addr) |
| 6329 | { |
| 6330 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 6331 | PyObject *uniobj; |
| 6332 | Py_UNICODE *unistr; |
| 6333 | |
| 6334 | uniobj = PyUnicode_FromObject(obj); |
| 6335 | if (uniobj == NULL) { |
| 6336 | PyErr_SetString(PyExc_TypeError, |
| 6337 | "The fill character cannot be converted to Unicode"); |
| 6338 | return 0; |
| 6339 | } |
| 6340 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 6341 | PyErr_SetString(PyExc_TypeError, |
| 6342 | "The fill character must be exactly one character long"); |
| 6343 | Py_DECREF(uniobj); |
| 6344 | return 0; |
| 6345 | } |
| 6346 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 6347 | *fillcharloc = unistr[0]; |
| 6348 | Py_DECREF(uniobj); |
| 6349 | return 1; |
| 6350 | } |
| 6351 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6352 | PyDoc_STRVAR(center__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 6353 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6354 | \n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6355 | Return S centered in a Unicode string of length width. Padding is\n\ |
| 6356 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6357 | |
| 6358 | static PyObject * |
| 6359 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 6360 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6361 | Py_ssize_t marg, left; |
| 6362 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6363 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6364 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 6365 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6366 | return NULL; |
| 6367 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6368 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6369 | Py_INCREF(self); |
| 6370 | return (PyObject*) self; |
| 6371 | } |
| 6372 | |
| 6373 | marg = width - self->length; |
| 6374 | left = marg / 2 + (marg & width & 1); |
| 6375 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6376 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6377 | } |
| 6378 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6379 | #if 0 |
| 6380 | |
| 6381 | /* This code should go into some future Unicode collation support |
| 6382 | module. The basic comparison should compare ordinals on a naive |
Trent Mick | 20abf57 | 2000-08-12 22:14:34 +0000 | [diff] [blame] | 6383 | basis (this is what Java does and thus JPython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6384 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6385 | /* speedy UTF-16 code point order comparison */ |
| 6386 | /* gleaned from: */ |
| 6387 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 6388 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6389 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6390 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6391 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6392 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 6393 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6394 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6395 | }; |
| 6396 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6397 | static int |
| 6398 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6399 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6400 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6401 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6402 | Py_UNICODE *s1 = str1->str; |
| 6403 | Py_UNICODE *s2 = str2->str; |
| 6404 | |
| 6405 | len1 = str1->length; |
| 6406 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6407 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6408 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6409 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6410 | |
| 6411 | c1 = *s1++; |
| 6412 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6413 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6414 | if (c1 > (1<<11) * 26) |
| 6415 | c1 += utf16Fixup[c1>>11]; |
| 6416 | if (c2 > (1<<11) * 26) |
| 6417 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6418 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6419 | |
| 6420 | if (c1 != c2) |
| 6421 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6422 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6423 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6424 | } |
| 6425 | |
| 6426 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6427 | } |
| 6428 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6429 | #else |
| 6430 | |
| 6431 | static int |
| 6432 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6433 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6434 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6435 | |
| 6436 | Py_UNICODE *s1 = str1->str; |
| 6437 | Py_UNICODE *s2 = str2->str; |
| 6438 | |
| 6439 | len1 = str1->length; |
| 6440 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6441 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6442 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6443 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6444 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6445 | c1 = *s1++; |
| 6446 | c2 = *s2++; |
| 6447 | |
| 6448 | if (c1 != c2) |
| 6449 | return (c1 < c2) ? -1 : 1; |
| 6450 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6451 | len1--; len2--; |
| 6452 | } |
| 6453 | |
| 6454 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6455 | } |
| 6456 | |
| 6457 | #endif |
| 6458 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6459 | int PyUnicode_Compare(PyObject *left, |
| 6460 | PyObject *right) |
| 6461 | { |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6462 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) |
| 6463 | return unicode_compare((PyUnicodeObject *)left, |
| 6464 | (PyUnicodeObject *)right); |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6465 | PyErr_Format(PyExc_TypeError, |
| 6466 | "Can't compare %.100s and %.100s", |
| 6467 | left->ob_type->tp_name, |
| 6468 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6469 | return -1; |
| 6470 | } |
| 6471 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6472 | int |
| 6473 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 6474 | { |
| 6475 | int i; |
| 6476 | Py_UNICODE *id; |
| 6477 | assert(PyUnicode_Check(uni)); |
| 6478 | id = PyUnicode_AS_UNICODE(uni); |
| 6479 | /* Compare Unicode string and source character set string */ |
| 6480 | for (i = 0; id[i] && str[i]; i++) |
| 6481 | if (id[i] != str[i]) |
| 6482 | return ((int)id[i] < (int)str[i]) ? -1 : 1; |
| 6483 | if (id[i]) |
| 6484 | return 1; /* uni is longer */ |
| 6485 | if (str[i]) |
| 6486 | return -1; /* str is longer */ |
| 6487 | return 0; |
| 6488 | } |
| 6489 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6490 | PyObject *PyUnicode_RichCompare(PyObject *left, |
| 6491 | PyObject *right, |
| 6492 | int op) |
| 6493 | { |
| 6494 | int result; |
| 6495 | |
| 6496 | result = PyUnicode_Compare(left, right); |
| 6497 | if (result == -1 && PyErr_Occurred()) |
| 6498 | goto onError; |
| 6499 | |
| 6500 | /* Convert the return value to a Boolean */ |
| 6501 | switch (op) { |
| 6502 | case Py_EQ: |
| 6503 | result = (result == 0); |
| 6504 | break; |
| 6505 | case Py_NE: |
| 6506 | result = (result != 0); |
| 6507 | break; |
| 6508 | case Py_LE: |
| 6509 | result = (result <= 0); |
| 6510 | break; |
| 6511 | case Py_GE: |
| 6512 | result = (result >= 0); |
| 6513 | break; |
| 6514 | case Py_LT: |
| 6515 | result = (result == -1); |
| 6516 | break; |
| 6517 | case Py_GT: |
| 6518 | result = (result == 1); |
| 6519 | break; |
| 6520 | } |
| 6521 | return PyBool_FromLong(result); |
| 6522 | |
| 6523 | onError: |
| 6524 | |
| 6525 | /* Standard case |
| 6526 | |
| 6527 | Type errors mean that PyUnicode_FromObject() could not convert |
| 6528 | one of the arguments (usually the right hand side) to Unicode, |
| 6529 | ie. we can't handle the comparison request. However, it is |
| 6530 | possible that the other object knows a comparison method, which |
| 6531 | is why we return Py_NotImplemented to give the other object a |
| 6532 | chance. |
| 6533 | |
| 6534 | */ |
| 6535 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 6536 | PyErr_Clear(); |
| 6537 | Py_INCREF(Py_NotImplemented); |
| 6538 | return Py_NotImplemented; |
| 6539 | } |
| 6540 | if (op != Py_EQ && op != Py_NE) |
| 6541 | return NULL; |
| 6542 | |
| 6543 | /* Equality comparison. |
| 6544 | |
| 6545 | This is a special case: we silence any PyExc_UnicodeDecodeError |
| 6546 | and instead turn it into a PyErr_UnicodeWarning. |
| 6547 | |
| 6548 | */ |
| 6549 | if (!PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) |
| 6550 | return NULL; |
| 6551 | PyErr_Clear(); |
Skip Montanaro | 46fc337 | 2007-08-12 11:44:53 +0000 | [diff] [blame] | 6552 | if (PyErr_WarnEx(PyExc_UnicodeWarning, |
| 6553 | (op == Py_EQ) ? |
| 6554 | "Unicode equal comparison " |
| 6555 | "failed to convert both arguments to Unicode - " |
| 6556 | "interpreting them as being unequal" |
| 6557 | : |
| 6558 | "Unicode unequal comparison " |
| 6559 | "failed to convert both arguments to Unicode - " |
| 6560 | "interpreting them as being unequal", |
| 6561 | 1) < 0) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6562 | return NULL; |
| 6563 | result = (op == Py_NE); |
| 6564 | return PyBool_FromLong(result); |
| 6565 | } |
| 6566 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6567 | int PyUnicode_Contains(PyObject *container, |
| 6568 | PyObject *element) |
| 6569 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6570 | PyObject *str, *sub; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6571 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6572 | |
| 6573 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6574 | sub = PyUnicode_FromObject(element); |
| 6575 | if (!sub) { |
Walter Dörwald | 26e0f51 | 2007-06-12 16:51:31 +0000 | [diff] [blame] | 6576 | PyErr_Format(PyExc_TypeError, |
| 6577 | "'in <string>' requires string as left operand, not %s", |
| 6578 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6579 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6580 | } |
| 6581 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6582 | str = PyUnicode_FromObject(container); |
| 6583 | if (!str) { |
| 6584 | Py_DECREF(sub); |
| 6585 | return -1; |
| 6586 | } |
| 6587 | |
| 6588 | result = stringlib_contains_obj(str, sub); |
| 6589 | |
| 6590 | Py_DECREF(str); |
| 6591 | Py_DECREF(sub); |
| 6592 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6593 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6594 | } |
| 6595 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6596 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 6597 | |
| 6598 | PyObject *PyUnicode_Concat(PyObject *left, |
| 6599 | PyObject *right) |
| 6600 | { |
| 6601 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 6602 | |
| 6603 | /* Coerce the two arguments */ |
| 6604 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 6605 | if (u == NULL) |
| 6606 | goto onError; |
| 6607 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 6608 | if (v == NULL) |
| 6609 | goto onError; |
| 6610 | |
| 6611 | /* Shortcuts */ |
| 6612 | if (v == unicode_empty) { |
| 6613 | Py_DECREF(v); |
| 6614 | return (PyObject *)u; |
| 6615 | } |
| 6616 | if (u == unicode_empty) { |
| 6617 | Py_DECREF(u); |
| 6618 | return (PyObject *)v; |
| 6619 | } |
| 6620 | |
| 6621 | /* Concat the two Unicode strings */ |
| 6622 | w = _PyUnicode_New(u->length + v->length); |
| 6623 | if (w == NULL) |
| 6624 | goto onError; |
| 6625 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 6626 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 6627 | |
| 6628 | Py_DECREF(u); |
| 6629 | Py_DECREF(v); |
| 6630 | return (PyObject *)w; |
| 6631 | |
| 6632 | onError: |
| 6633 | Py_XDECREF(u); |
| 6634 | Py_XDECREF(v); |
| 6635 | return NULL; |
| 6636 | } |
| 6637 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 6638 | void |
| 6639 | PyUnicode_Append(PyObject **pleft, PyObject *right) |
| 6640 | { |
| 6641 | PyObject *new; |
| 6642 | if (*pleft == NULL) |
| 6643 | return; |
| 6644 | if (right == NULL || !PyUnicode_Check(*pleft)) { |
| 6645 | Py_DECREF(*pleft); |
| 6646 | *pleft = NULL; |
| 6647 | return; |
| 6648 | } |
| 6649 | new = PyUnicode_Concat(*pleft, right); |
| 6650 | Py_DECREF(*pleft); |
| 6651 | *pleft = new; |
| 6652 | } |
| 6653 | |
| 6654 | void |
| 6655 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 6656 | { |
| 6657 | PyUnicode_Append(pleft, right); |
| 6658 | Py_XDECREF(right); |
| 6659 | } |
| 6660 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6661 | PyDoc_STRVAR(count__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6662 | "S.count(sub[, start[, end]]) -> int\n\ |
| 6663 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6664 | Return the number of non-overlapping occurrences of substring sub in\n\ |
| 6665 | 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] | 6666 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6667 | |
| 6668 | static PyObject * |
| 6669 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 6670 | { |
| 6671 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6672 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6673 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6674 | PyObject *result; |
| 6675 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6676 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
| 6677 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6678 | return NULL; |
| 6679 | |
| 6680 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6681 | (PyObject *)substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6682 | if (substring == NULL) |
| 6683 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6684 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6685 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6686 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6687 | result = PyLong_FromSsize_t( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6688 | stringlib_count(self->str + start, end - start, |
| 6689 | substring->str, substring->length) |
| 6690 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6691 | |
| 6692 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6693 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6694 | return result; |
| 6695 | } |
| 6696 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6697 | PyDoc_STRVAR(encode__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 6698 | "S.encode([encoding[, errors]]) -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6699 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 6700 | Encode S using the codec registered for encoding. encoding defaults\n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6701 | 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] | 6702 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6703 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 6704 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 6705 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6706 | |
| 6707 | static PyObject * |
| 6708 | unicode_encode(PyUnicodeObject *self, PyObject *args) |
| 6709 | { |
| 6710 | char *encoding = NULL; |
| 6711 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6712 | PyObject *v; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 6713 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6714 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 6715 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 6716 | v = PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6717 | if (v == NULL) |
| 6718 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6719 | if (!PyBytes_Check(v)) { |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6720 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 6721 | "encoder did not return a bytes object " |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6722 | "(type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 6723 | Py_TYPE(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6724 | Py_DECREF(v); |
| 6725 | return NULL; |
| 6726 | } |
| 6727 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6728 | |
| 6729 | onError: |
| 6730 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6731 | } |
| 6732 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6733 | PyDoc_STRVAR(expandtabs__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 6734 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6735 | \n\ |
| 6736 | 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] | 6737 | 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] | 6738 | |
| 6739 | static PyObject* |
| 6740 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 6741 | { |
| 6742 | Py_UNICODE *e; |
| 6743 | Py_UNICODE *p; |
| 6744 | Py_UNICODE *q; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6745 | Py_UNICODE *qe; |
| 6746 | Py_ssize_t i, j, incr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6747 | PyUnicodeObject *u; |
| 6748 | int tabsize = 8; |
| 6749 | |
| 6750 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
| 6751 | return NULL; |
| 6752 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6753 | /* First pass: determine size of output string */ |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6754 | i = 0; /* chars up to and including most recent \n or \r */ |
| 6755 | j = 0; /* chars since most recent \n or \r (use in tab calculations) */ |
| 6756 | e = self->str + self->length; /* end of input */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6757 | for (p = self->str; p < e; p++) |
| 6758 | if (*p == '\t') { |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6759 | if (tabsize > 0) { |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6760 | incr = tabsize - (j % tabsize); /* cannot overflow */ |
| 6761 | if (j > PY_SSIZE_T_MAX - incr) |
| 6762 | goto overflow1; |
| 6763 | j += incr; |
| 6764 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6765 | } |
| 6766 | else { |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6767 | if (j > PY_SSIZE_T_MAX - 1) |
| 6768 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6769 | j++; |
| 6770 | if (*p == '\n' || *p == '\r') { |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6771 | if (i > PY_SSIZE_T_MAX - j) |
| 6772 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6773 | i += j; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6774 | j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6775 | } |
| 6776 | } |
| 6777 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6778 | if (i > PY_SSIZE_T_MAX - j) |
| 6779 | goto overflow1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6780 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6781 | /* Second pass: create output string and fill it */ |
| 6782 | u = _PyUnicode_New(i + j); |
| 6783 | if (!u) |
| 6784 | return NULL; |
| 6785 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6786 | j = 0; /* same as in first pass */ |
| 6787 | q = u->str; /* next output char */ |
| 6788 | qe = u->str + u->length; /* end of output */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6789 | |
| 6790 | for (p = self->str; p < e; p++) |
| 6791 | if (*p == '\t') { |
| 6792 | if (tabsize > 0) { |
| 6793 | i = tabsize - (j % tabsize); |
| 6794 | j += i; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6795 | while (i--) { |
| 6796 | if (q >= qe) |
| 6797 | goto overflow2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6798 | *q++ = ' '; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6799 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6800 | } |
| 6801 | } |
| 6802 | else { |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6803 | if (q >= qe) |
| 6804 | goto overflow2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6805 | *q++ = *p; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6806 | j++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6807 | if (*p == '\n' || *p == '\r') |
| 6808 | j = 0; |
| 6809 | } |
| 6810 | |
| 6811 | return (PyObject*) u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6812 | |
| 6813 | overflow2: |
| 6814 | Py_DECREF(u); |
| 6815 | overflow1: |
| 6816 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 6817 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6818 | } |
| 6819 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6820 | PyDoc_STRVAR(find__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 6821 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6822 | \n\ |
| 6823 | 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] | 6824 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6825 | arguments start and end are interpreted as in slice notation.\n\ |
| 6826 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6827 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6828 | |
| 6829 | static PyObject * |
| 6830 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 6831 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6832 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 6833 | Py_ssize_t start; |
| 6834 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6835 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6836 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 6837 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6838 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6839 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6840 | result = stringlib_find_slice( |
| 6841 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6842 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6843 | start, end |
| 6844 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6845 | |
| 6846 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6847 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6848 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6849 | } |
| 6850 | |
| 6851 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6852 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6853 | { |
| 6854 | if (index < 0 || index >= self->length) { |
| 6855 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 6856 | return NULL; |
| 6857 | } |
| 6858 | |
| 6859 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 6860 | } |
| 6861 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 6862 | /* Believe it or not, this produces the same value for ASCII strings |
| 6863 | as string_hash(). */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6864 | static long |
Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 6865 | unicode_hash(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6866 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 6867 | Py_ssize_t len; |
| 6868 | Py_UNICODE *p; |
| 6869 | long x; |
| 6870 | |
| 6871 | if (self->hash != -1) |
| 6872 | return self->hash; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 6873 | len = Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 6874 | p = self->str; |
| 6875 | x = *p << 7; |
| 6876 | while (--len >= 0) |
| 6877 | x = (1000003*x) ^ *p++; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 6878 | x ^= Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 6879 | if (x == -1) |
| 6880 | x = -2; |
| 6881 | self->hash = x; |
| 6882 | return x; |
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(index__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 6886 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6887 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6888 | 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] | 6889 | |
| 6890 | static PyObject * |
| 6891 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 6892 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6893 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6894 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 6895 | Py_ssize_t start; |
| 6896 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6897 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 6898 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6899 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6900 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6901 | result = stringlib_find_slice( |
| 6902 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6903 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6904 | start, end |
| 6905 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6906 | |
| 6907 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6908 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6909 | if (result < 0) { |
| 6910 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 6911 | return NULL; |
| 6912 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6913 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6914 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6915 | } |
| 6916 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6917 | PyDoc_STRVAR(islower__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6918 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6919 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6920 | 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] | 6921 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6922 | |
| 6923 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6924 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6925 | { |
| 6926 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6927 | register const Py_UNICODE *e; |
| 6928 | int cased; |
| 6929 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6930 | /* Shortcut for single character strings */ |
| 6931 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6932 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6933 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6934 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6935 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6936 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6937 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6938 | e = p + PyUnicode_GET_SIZE(self); |
| 6939 | cased = 0; |
| 6940 | for (; p < e; p++) { |
| 6941 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6942 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6943 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6944 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6945 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 6946 | cased = 1; |
| 6947 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6948 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6949 | } |
| 6950 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6951 | PyDoc_STRVAR(isupper__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6952 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6953 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6954 | 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] | 6955 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6956 | |
| 6957 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6958 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6959 | { |
| 6960 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6961 | register const Py_UNICODE *e; |
| 6962 | int cased; |
| 6963 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6964 | /* Shortcut for single character strings */ |
| 6965 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6966 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6967 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6968 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6969 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6970 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6971 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6972 | e = p + PyUnicode_GET_SIZE(self); |
| 6973 | cased = 0; |
| 6974 | for (; p < e; p++) { |
| 6975 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6976 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6977 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6978 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6979 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 6980 | cased = 1; |
| 6981 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6982 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6983 | } |
| 6984 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6985 | PyDoc_STRVAR(istitle__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6986 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6987 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6988 | Return True if S is a titlecased string and there is at least one\n\ |
| 6989 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 6990 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 6991 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6992 | |
| 6993 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6994 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6995 | { |
| 6996 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6997 | register const Py_UNICODE *e; |
| 6998 | int cased, previous_is_cased; |
| 6999 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7000 | /* Shortcut for single character strings */ |
| 7001 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7002 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 7003 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7004 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7005 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7006 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7007 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7008 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7009 | e = p + PyUnicode_GET_SIZE(self); |
| 7010 | cased = 0; |
| 7011 | previous_is_cased = 0; |
| 7012 | for (; p < e; p++) { |
| 7013 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7014 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7015 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 7016 | if (previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7017 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7018 | previous_is_cased = 1; |
| 7019 | cased = 1; |
| 7020 | } |
| 7021 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 7022 | if (!previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7023 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7024 | previous_is_cased = 1; |
| 7025 | cased = 1; |
| 7026 | } |
| 7027 | else |
| 7028 | previous_is_cased = 0; |
| 7029 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7030 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7031 | } |
| 7032 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7033 | PyDoc_STRVAR(isspace__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7034 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7035 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7036 | Return True if all characters in S are whitespace\n\ |
| 7037 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7038 | |
| 7039 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7040 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7041 | { |
| 7042 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7043 | register const Py_UNICODE *e; |
| 7044 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7045 | /* Shortcut for single character strings */ |
| 7046 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 7047 | Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7048 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7049 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7050 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7051 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7052 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7053 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7054 | e = p + PyUnicode_GET_SIZE(self); |
| 7055 | for (; p < e; p++) { |
| 7056 | if (!Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7057 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7058 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7059 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7060 | } |
| 7061 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7062 | PyDoc_STRVAR(isalpha__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7063 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7064 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7065 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7066 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7067 | |
| 7068 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7069 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7070 | { |
| 7071 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7072 | register const Py_UNICODE *e; |
| 7073 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7074 | /* Shortcut for single character strings */ |
| 7075 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 7076 | Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7077 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7078 | |
| 7079 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7080 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7081 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7082 | |
| 7083 | e = p + PyUnicode_GET_SIZE(self); |
| 7084 | for (; p < e; p++) { |
| 7085 | if (!Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7086 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7087 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7088 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7089 | } |
| 7090 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7091 | PyDoc_STRVAR(isalnum__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7092 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7093 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7094 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7095 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7096 | |
| 7097 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7098 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7099 | { |
| 7100 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7101 | register const Py_UNICODE *e; |
| 7102 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7103 | /* Shortcut for single character strings */ |
| 7104 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 7105 | Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7106 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7107 | |
| 7108 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7109 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7110 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7111 | |
| 7112 | e = p + PyUnicode_GET_SIZE(self); |
| 7113 | for (; p < e; p++) { |
| 7114 | if (!Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7115 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7116 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7117 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7118 | } |
| 7119 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7120 | PyDoc_STRVAR(isdecimal__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7121 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7122 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7123 | 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] | 7124 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7125 | |
| 7126 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7127 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7128 | { |
| 7129 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7130 | register const Py_UNICODE *e; |
| 7131 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7132 | /* Shortcut for single character strings */ |
| 7133 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 7134 | Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7135 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7136 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7137 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7138 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7139 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7140 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7141 | e = p + PyUnicode_GET_SIZE(self); |
| 7142 | for (; p < e; p++) { |
| 7143 | if (!Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7144 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7145 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7146 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7147 | } |
| 7148 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7149 | PyDoc_STRVAR(isdigit__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7150 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7151 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7152 | Return True if all characters in S are digits\n\ |
| 7153 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7154 | |
| 7155 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7156 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7157 | { |
| 7158 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7159 | register const Py_UNICODE *e; |
| 7160 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7161 | /* Shortcut for single character strings */ |
| 7162 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 7163 | Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7164 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7165 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7166 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7167 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7168 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7169 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7170 | e = p + PyUnicode_GET_SIZE(self); |
| 7171 | for (; p < e; p++) { |
| 7172 | if (!Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7173 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7174 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7175 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7176 | } |
| 7177 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7178 | PyDoc_STRVAR(isnumeric__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7179 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7180 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7181 | 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] | 7182 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7183 | |
| 7184 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7185 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7186 | { |
| 7187 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7188 | register const Py_UNICODE *e; |
| 7189 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7190 | /* Shortcut for single character strings */ |
| 7191 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 7192 | Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7193 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7194 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7195 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7196 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7197 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7198 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7199 | e = p + PyUnicode_GET_SIZE(self); |
| 7200 | for (; p < e; p++) { |
| 7201 | if (!Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7202 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7203 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7204 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7205 | } |
| 7206 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7207 | int |
| 7208 | PyUnicode_IsIdentifier(PyObject *self) |
| 7209 | { |
| 7210 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE((PyUnicodeObject*)self); |
| 7211 | register const Py_UNICODE *e; |
| 7212 | |
| 7213 | /* Special case for empty strings */ |
| 7214 | if (PyUnicode_GET_SIZE(self) == 0) |
| 7215 | return 0; |
| 7216 | |
| 7217 | /* PEP 3131 says that the first character must be in |
| 7218 | XID_Start and subsequent characters in XID_Continue, |
| 7219 | and for the ASCII range, the 2.x rules apply (i.e |
| 7220 | start with letters and underscore, continue with |
| 7221 | letters, digits, underscore). However, given the current |
| 7222 | definition of XID_Start and XID_Continue, it is sufficient |
| 7223 | to check just for these, except that _ must be allowed |
| 7224 | as starting an identifier. */ |
| 7225 | if (!_PyUnicode_IsXidStart(*p) && *p != 0x5F /* LOW LINE */) |
| 7226 | return 0; |
| 7227 | |
| 7228 | e = p + PyUnicode_GET_SIZE(self); |
| 7229 | for (p++; p < e; p++) { |
| 7230 | if (!_PyUnicode_IsXidContinue(*p)) |
| 7231 | return 0; |
| 7232 | } |
| 7233 | return 1; |
| 7234 | } |
| 7235 | |
| 7236 | PyDoc_STRVAR(isidentifier__doc__, |
| 7237 | "S.isidentifier() -> bool\n\ |
| 7238 | \n\ |
| 7239 | Return True if S is a valid identifier according\n\ |
| 7240 | to the language definition."); |
| 7241 | |
| 7242 | static PyObject* |
| 7243 | unicode_isidentifier(PyObject *self) |
| 7244 | { |
| 7245 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 7246 | } |
| 7247 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7248 | PyDoc_STRVAR(isprintable__doc__, |
| 7249 | "S.isprintable() -> bool\n\ |
| 7250 | \n\ |
| 7251 | Return True if all characters in S are considered\n\ |
| 7252 | printable in repr() or S is empty, False otherwise."); |
| 7253 | |
| 7254 | static PyObject* |
| 7255 | unicode_isprintable(PyObject *self) |
| 7256 | { |
| 7257 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7258 | register const Py_UNICODE *e; |
| 7259 | |
| 7260 | /* Shortcut for single character strings */ |
| 7261 | if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISPRINTABLE(*p)) { |
| 7262 | Py_RETURN_TRUE; |
| 7263 | } |
| 7264 | |
| 7265 | e = p + PyUnicode_GET_SIZE(self); |
| 7266 | for (; p < e; p++) { |
| 7267 | if (!Py_UNICODE_ISPRINTABLE(*p)) { |
| 7268 | Py_RETURN_FALSE; |
| 7269 | } |
| 7270 | } |
| 7271 | Py_RETURN_TRUE; |
| 7272 | } |
| 7273 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7274 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7275 | "S.join(sequence) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7276 | \n\ |
| 7277 | 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] | 7278 | sequence. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7279 | |
| 7280 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7281 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7282 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7283 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7284 | } |
| 7285 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7286 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7287 | unicode_length(PyUnicodeObject *self) |
| 7288 | { |
| 7289 | return self->length; |
| 7290 | } |
| 7291 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7292 | PyDoc_STRVAR(ljust__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7293 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7294 | \n\ |
| 7295 | 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] | 7296 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7297 | |
| 7298 | static PyObject * |
| 7299 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 7300 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7301 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7302 | Py_UNICODE fillchar = ' '; |
| 7303 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7304 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7305 | return NULL; |
| 7306 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7307 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7308 | Py_INCREF(self); |
| 7309 | return (PyObject*) self; |
| 7310 | } |
| 7311 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7312 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7313 | } |
| 7314 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7315 | PyDoc_STRVAR(lower__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7316 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7317 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7318 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7319 | |
| 7320 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7321 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7322 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7323 | return fixup(self, fixlower); |
| 7324 | } |
| 7325 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7326 | #define LEFTSTRIP 0 |
| 7327 | #define RIGHTSTRIP 1 |
| 7328 | #define BOTHSTRIP 2 |
| 7329 | |
| 7330 | /* Arrays indexed by above */ |
| 7331 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 7332 | |
| 7333 | #define STRIPNAME(i) (stripformat[i]+3) |
| 7334 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7335 | /* externally visible for str.strip(unicode) */ |
| 7336 | PyObject * |
| 7337 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 7338 | { |
| 7339 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7340 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7341 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7342 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 7343 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7344 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7345 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
| 7346 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7347 | i = 0; |
| 7348 | if (striptype != RIGHTSTRIP) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7349 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 7350 | i++; |
| 7351 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7352 | } |
| 7353 | |
| 7354 | j = len; |
| 7355 | if (striptype != LEFTSTRIP) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7356 | do { |
| 7357 | j--; |
| 7358 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 7359 | j++; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7360 | } |
| 7361 | |
| 7362 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7363 | Py_INCREF(self); |
| 7364 | return (PyObject*)self; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7365 | } |
| 7366 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7367 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7368 | } |
| 7369 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7370 | |
| 7371 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7372 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7373 | { |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7374 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7375 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7376 | |
| 7377 | i = 0; |
| 7378 | if (striptype != RIGHTSTRIP) { |
| 7379 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 7380 | i++; |
| 7381 | } |
| 7382 | } |
| 7383 | |
| 7384 | j = len; |
| 7385 | if (striptype != LEFTSTRIP) { |
| 7386 | do { |
| 7387 | j--; |
| 7388 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 7389 | j++; |
| 7390 | } |
| 7391 | |
| 7392 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 7393 | Py_INCREF(self); |
| 7394 | return (PyObject*)self; |
| 7395 | } |
| 7396 | else |
| 7397 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7398 | } |
| 7399 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7400 | |
| 7401 | static PyObject * |
| 7402 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 7403 | { |
| 7404 | PyObject *sep = NULL; |
| 7405 | |
| 7406 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 7407 | return NULL; |
| 7408 | |
| 7409 | if (sep != NULL && sep != Py_None) { |
| 7410 | if (PyUnicode_Check(sep)) |
| 7411 | return _PyUnicode_XStrip(self, striptype, sep); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7412 | else { |
| 7413 | PyErr_Format(PyExc_TypeError, |
| 7414 | "%s arg must be None, unicode or str", |
| 7415 | STRIPNAME(striptype)); |
| 7416 | return NULL; |
| 7417 | } |
| 7418 | } |
| 7419 | |
| 7420 | return do_strip(self, striptype); |
| 7421 | } |
| 7422 | |
| 7423 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7424 | PyDoc_STRVAR(strip__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7425 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7426 | \n\ |
| 7427 | Return a copy of the string S with leading and trailing\n\ |
| 7428 | whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7429 | If chars is given and not None, remove characters in chars instead.\n\ |
| 7430 | 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] | 7431 | |
| 7432 | static PyObject * |
| 7433 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 7434 | { |
| 7435 | if (PyTuple_GET_SIZE(args) == 0) |
| 7436 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 7437 | else |
| 7438 | return do_argstrip(self, BOTHSTRIP, args); |
| 7439 | } |
| 7440 | |
| 7441 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7442 | PyDoc_STRVAR(lstrip__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7443 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7444 | \n\ |
| 7445 | Return a copy of the string S with leading whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7446 | If chars is given and not None, remove characters in chars instead.\n\ |
| 7447 | 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] | 7448 | |
| 7449 | static PyObject * |
| 7450 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 7451 | { |
| 7452 | if (PyTuple_GET_SIZE(args) == 0) |
| 7453 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 7454 | else |
| 7455 | return do_argstrip(self, LEFTSTRIP, args); |
| 7456 | } |
| 7457 | |
| 7458 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7459 | PyDoc_STRVAR(rstrip__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7460 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7461 | \n\ |
| 7462 | Return a copy of the string S with trailing whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7463 | If chars is given and not None, remove characters in chars instead.\n\ |
| 7464 | 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] | 7465 | |
| 7466 | static PyObject * |
| 7467 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 7468 | { |
| 7469 | if (PyTuple_GET_SIZE(args) == 0) |
| 7470 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 7471 | else |
| 7472 | return do_argstrip(self, RIGHTSTRIP, args); |
| 7473 | } |
| 7474 | |
| 7475 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7476 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7477 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7478 | { |
| 7479 | PyUnicodeObject *u; |
| 7480 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7481 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7482 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7483 | |
| 7484 | if (len < 0) |
| 7485 | len = 0; |
| 7486 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7487 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7488 | /* no repeat, return original string */ |
| 7489 | Py_INCREF(str); |
| 7490 | return (PyObject*) str; |
| 7491 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7492 | |
| 7493 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 7494 | * needed doesn't overflow size_t |
| 7495 | */ |
| 7496 | nchars = len * str->length; |
| 7497 | if (len && nchars / len != str->length) { |
| 7498 | PyErr_SetString(PyExc_OverflowError, |
| 7499 | "repeated string is too long"); |
| 7500 | return NULL; |
| 7501 | } |
| 7502 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 7503 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 7504 | PyErr_SetString(PyExc_OverflowError, |
| 7505 | "repeated string is too long"); |
| 7506 | return NULL; |
| 7507 | } |
| 7508 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7509 | if (!u) |
| 7510 | return NULL; |
| 7511 | |
| 7512 | p = u->str; |
| 7513 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7514 | if (str->length == 1 && len > 0) { |
| 7515 | Py_UNICODE_FILL(p, str->str[0], len); |
| 7516 | } else { |
| 7517 | Py_ssize_t done = 0; /* number of characters copied this far */ |
| 7518 | if (done < nchars) { |
| 7519 | Py_UNICODE_COPY(p, str->str, str->length); |
| 7520 | done = str->length; |
| 7521 | } |
| 7522 | while (done < nchars) { |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 7523 | Py_ssize_t n = (done <= nchars-done) ? done : nchars-done; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7524 | Py_UNICODE_COPY(p+done, p, n); |
| 7525 | done += n; |
| 7526 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7527 | } |
| 7528 | |
| 7529 | return (PyObject*) u; |
| 7530 | } |
| 7531 | |
| 7532 | PyObject *PyUnicode_Replace(PyObject *obj, |
| 7533 | PyObject *subobj, |
| 7534 | PyObject *replobj, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7535 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7536 | { |
| 7537 | PyObject *self; |
| 7538 | PyObject *str1; |
| 7539 | PyObject *str2; |
| 7540 | PyObject *result; |
| 7541 | |
| 7542 | self = PyUnicode_FromObject(obj); |
| 7543 | if (self == NULL) |
| 7544 | return NULL; |
| 7545 | str1 = PyUnicode_FromObject(subobj); |
| 7546 | if (str1 == NULL) { |
| 7547 | Py_DECREF(self); |
| 7548 | return NULL; |
| 7549 | } |
| 7550 | str2 = PyUnicode_FromObject(replobj); |
| 7551 | if (str2 == NULL) { |
| 7552 | Py_DECREF(self); |
| 7553 | Py_DECREF(str1); |
| 7554 | return NULL; |
| 7555 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7556 | result = replace((PyUnicodeObject *)self, |
| 7557 | (PyUnicodeObject *)str1, |
| 7558 | (PyUnicodeObject *)str2, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7559 | maxcount); |
| 7560 | Py_DECREF(self); |
| 7561 | Py_DECREF(str1); |
| 7562 | Py_DECREF(str2); |
| 7563 | return result; |
| 7564 | } |
| 7565 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7566 | PyDoc_STRVAR(replace__doc__, |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 7567 | "S.replace (old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7568 | \n\ |
| 7569 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 7570 | old replaced by new. If the optional argument count is\n\ |
| 7571 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7572 | |
| 7573 | static PyObject* |
| 7574 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 7575 | { |
| 7576 | PyUnicodeObject *str1; |
| 7577 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7578 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7579 | PyObject *result; |
| 7580 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7581 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7582 | return NULL; |
| 7583 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 7584 | if (str1 == NULL) |
| 7585 | return NULL; |
| 7586 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7587 | if (str2 == NULL) { |
| 7588 | Py_DECREF(str1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7589 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7590 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7591 | |
| 7592 | result = replace(self, str1, str2, maxcount); |
| 7593 | |
| 7594 | Py_DECREF(str1); |
| 7595 | Py_DECREF(str2); |
| 7596 | return result; |
| 7597 | } |
| 7598 | |
| 7599 | static |
| 7600 | PyObject *unicode_repr(PyObject *unicode) |
| 7601 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7602 | PyObject *repr; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7603 | Py_UNICODE *p; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7604 | Py_UNICODE *s = PyUnicode_AS_UNICODE(unicode); |
| 7605 | Py_ssize_t size = PyUnicode_GET_SIZE(unicode); |
| 7606 | |
| 7607 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 7608 | better to choose a different scheme. Perhaps scan the |
| 7609 | first N-chars of the string and allocate based on that size. |
| 7610 | */ |
| 7611 | /* Initial allocation is based on the longest-possible unichr |
| 7612 | escape. |
| 7613 | |
| 7614 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 7615 | unichr, so in this case it's the longest unichr escape. In |
| 7616 | narrow (UTF-16) builds this is five chars per source unichr |
| 7617 | since there are two unichrs in the surrogate pair, so in narrow |
| 7618 | (UTF-16) builds it's not the longest unichr escape. |
| 7619 | |
| 7620 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 7621 | so in the narrow (UTF-16) build case it's the longest unichr |
| 7622 | escape. |
| 7623 | */ |
| 7624 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7625 | repr = PyUnicode_FromUnicode(NULL, |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7626 | 2 /* quotes */ |
| 7627 | #ifdef Py_UNICODE_WIDE |
| 7628 | + 10*size |
| 7629 | #else |
| 7630 | + 6*size |
| 7631 | #endif |
| 7632 | + 1); |
| 7633 | if (repr == NULL) |
| 7634 | return NULL; |
| 7635 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7636 | p = PyUnicode_AS_UNICODE(repr); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7637 | |
| 7638 | /* Add quote */ |
| 7639 | *p++ = (findchar(s, size, '\'') && |
| 7640 | !findchar(s, size, '"')) ? '"' : '\''; |
| 7641 | while (size-- > 0) { |
| 7642 | Py_UNICODE ch = *s++; |
| 7643 | |
| 7644 | /* Escape quotes and backslashes */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7645 | if ((ch == PyUnicode_AS_UNICODE(repr)[0]) || (ch == '\\')) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7646 | *p++ = '\\'; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7647 | *p++ = ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7648 | continue; |
| 7649 | } |
| 7650 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7651 | /* Map special whitespace to '\t', \n', '\r' */ |
| 7652 | if (ch == '\t') { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7653 | *p++ = '\\'; |
| 7654 | *p++ = 't'; |
| 7655 | } |
| 7656 | else if (ch == '\n') { |
| 7657 | *p++ = '\\'; |
| 7658 | *p++ = 'n'; |
| 7659 | } |
| 7660 | else if (ch == '\r') { |
| 7661 | *p++ = '\\'; |
| 7662 | *p++ = 'r'; |
| 7663 | } |
| 7664 | |
| 7665 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7666 | else if (ch < ' ' || ch == 0x7F) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7667 | *p++ = '\\'; |
| 7668 | *p++ = 'x'; |
| 7669 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 7670 | *p++ = hexdigits[ch & 0x000F]; |
| 7671 | } |
| 7672 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7673 | /* Copy ASCII characters as-is */ |
| 7674 | else if (ch < 0x7F) { |
| 7675 | *p++ = ch; |
| 7676 | } |
| 7677 | |
| 7678 | /* Non-ASCII characters */ |
| 7679 | else { |
| 7680 | Py_UCS4 ucs = ch; |
| 7681 | |
| 7682 | #ifndef Py_UNICODE_WIDE |
| 7683 | Py_UNICODE ch2 = 0; |
| 7684 | /* Get code point from surrogate pair */ |
| 7685 | if (size > 0) { |
| 7686 | ch2 = *s; |
| 7687 | if (ch >= 0xD800 && ch < 0xDC00 && ch2 >= 0xDC00 |
| 7688 | && ch2 <= 0xDFFF) { |
| 7689 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) |
| 7690 | + 0x00010000; |
| 7691 | s++; |
| 7692 | size--; |
| 7693 | } |
| 7694 | } |
| 7695 | #endif |
| 7696 | /* Map Unicode whitespace and control characters |
| 7697 | (categories Z* and C* except ASCII space) |
| 7698 | */ |
| 7699 | if (!Py_UNICODE_ISPRINTABLE(ucs)) { |
| 7700 | /* Map 8-bit characters to '\xhh' */ |
| 7701 | if (ucs <= 0xff) { |
| 7702 | *p++ = '\\'; |
| 7703 | *p++ = 'x'; |
| 7704 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 7705 | *p++ = hexdigits[ch & 0x000F]; |
| 7706 | } |
| 7707 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 7708 | else if (ucs >= 0x10000) { |
| 7709 | *p++ = '\\'; |
| 7710 | *p++ = 'U'; |
| 7711 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 7712 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 7713 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 7714 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 7715 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 7716 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 7717 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 7718 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 7719 | } |
| 7720 | /* Map 16-bit characters to '\uxxxx' */ |
| 7721 | else { |
| 7722 | *p++ = '\\'; |
| 7723 | *p++ = 'u'; |
| 7724 | *p++ = hexdigits[(ucs >> 12) & 0x000F]; |
| 7725 | *p++ = hexdigits[(ucs >> 8) & 0x000F]; |
| 7726 | *p++ = hexdigits[(ucs >> 4) & 0x000F]; |
| 7727 | *p++ = hexdigits[ucs & 0x000F]; |
| 7728 | } |
| 7729 | } |
| 7730 | /* Copy characters as-is */ |
| 7731 | else { |
| 7732 | *p++ = ch; |
| 7733 | #ifndef Py_UNICODE_WIDE |
| 7734 | if (ucs >= 0x10000) |
| 7735 | *p++ = ch2; |
| 7736 | #endif |
| 7737 | } |
| 7738 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7739 | } |
| 7740 | /* Add quote */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7741 | *p++ = PyUnicode_AS_UNICODE(repr)[0]; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7742 | |
| 7743 | *p = '\0'; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7744 | _PyUnicode_Resize(&repr, p - PyUnicode_AS_UNICODE(repr)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7745 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7746 | } |
| 7747 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7748 | PyDoc_STRVAR(rfind__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7749 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7750 | \n\ |
| 7751 | 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] | 7752 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7753 | arguments start and end are interpreted as in slice notation.\n\ |
| 7754 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7755 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7756 | |
| 7757 | static PyObject * |
| 7758 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 7759 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7760 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7761 | Py_ssize_t start; |
| 7762 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7763 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7764 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7765 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
| 7766 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7767 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7768 | result = stringlib_rfind_slice( |
| 7769 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7770 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7771 | start, end |
| 7772 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7773 | |
| 7774 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7775 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7776 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7777 | } |
| 7778 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7779 | PyDoc_STRVAR(rindex__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7780 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7781 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7782 | 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] | 7783 | |
| 7784 | static PyObject * |
| 7785 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 7786 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7787 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7788 | Py_ssize_t start; |
| 7789 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7790 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7791 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7792 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
| 7793 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7794 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7795 | result = stringlib_rfind_slice( |
| 7796 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7797 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7798 | start, end |
| 7799 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7800 | |
| 7801 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7802 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7803 | if (result < 0) { |
| 7804 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 7805 | return NULL; |
| 7806 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7807 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7808 | } |
| 7809 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7810 | PyDoc_STRVAR(rjust__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7811 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7812 | \n\ |
| 7813 | 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] | 7814 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7815 | |
| 7816 | static PyObject * |
| 7817 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 7818 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7819 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7820 | Py_UNICODE fillchar = ' '; |
| 7821 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7822 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7823 | return NULL; |
| 7824 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7825 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7826 | Py_INCREF(self); |
| 7827 | return (PyObject*) self; |
| 7828 | } |
| 7829 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7830 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7831 | } |
| 7832 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7833 | PyObject *PyUnicode_Split(PyObject *s, |
| 7834 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7835 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7836 | { |
| 7837 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7838 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7839 | s = PyUnicode_FromObject(s); |
| 7840 | if (s == NULL) |
| 7841 | return NULL; |
| 7842 | if (sep != NULL) { |
| 7843 | sep = PyUnicode_FromObject(sep); |
| 7844 | if (sep == NULL) { |
| 7845 | Py_DECREF(s); |
| 7846 | return NULL; |
| 7847 | } |
| 7848 | } |
| 7849 | |
| 7850 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 7851 | |
| 7852 | Py_DECREF(s); |
| 7853 | Py_XDECREF(sep); |
| 7854 | return result; |
| 7855 | } |
| 7856 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7857 | PyDoc_STRVAR(split__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7858 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7859 | \n\ |
| 7860 | Return a list of the words in S, using sep as the\n\ |
| 7861 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 7862 | splits are done. If sep is not specified or is None, any\n\ |
Alexandre Vassalotti | 8ae3e05 | 2008-05-16 00:41:41 +0000 | [diff] [blame] | 7863 | whitespace string is a separator and empty strings are\n\ |
| 7864 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7865 | |
| 7866 | static PyObject* |
| 7867 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 7868 | { |
| 7869 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7870 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7871 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7872 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7873 | return NULL; |
| 7874 | |
| 7875 | if (substring == Py_None) |
| 7876 | return split(self, NULL, maxcount); |
| 7877 | else if (PyUnicode_Check(substring)) |
| 7878 | return split(self, (PyUnicodeObject *)substring, maxcount); |
| 7879 | else |
| 7880 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
| 7881 | } |
| 7882 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7883 | PyObject * |
| 7884 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 7885 | { |
| 7886 | PyObject* str_obj; |
| 7887 | PyObject* sep_obj; |
| 7888 | PyObject* out; |
| 7889 | |
| 7890 | str_obj = PyUnicode_FromObject(str_in); |
| 7891 | if (!str_obj) |
| 7892 | return NULL; |
| 7893 | sep_obj = PyUnicode_FromObject(sep_in); |
| 7894 | if (!sep_obj) { |
| 7895 | Py_DECREF(str_obj); |
| 7896 | return NULL; |
| 7897 | } |
| 7898 | |
| 7899 | out = stringlib_partition( |
| 7900 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 7901 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 7902 | ); |
| 7903 | |
| 7904 | Py_DECREF(sep_obj); |
| 7905 | Py_DECREF(str_obj); |
| 7906 | |
| 7907 | return out; |
| 7908 | } |
| 7909 | |
| 7910 | |
| 7911 | PyObject * |
| 7912 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 7913 | { |
| 7914 | PyObject* str_obj; |
| 7915 | PyObject* sep_obj; |
| 7916 | PyObject* out; |
| 7917 | |
| 7918 | str_obj = PyUnicode_FromObject(str_in); |
| 7919 | if (!str_obj) |
| 7920 | return NULL; |
| 7921 | sep_obj = PyUnicode_FromObject(sep_in); |
| 7922 | if (!sep_obj) { |
| 7923 | Py_DECREF(str_obj); |
| 7924 | return NULL; |
| 7925 | } |
| 7926 | |
| 7927 | out = stringlib_rpartition( |
| 7928 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 7929 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 7930 | ); |
| 7931 | |
| 7932 | Py_DECREF(sep_obj); |
| 7933 | Py_DECREF(str_obj); |
| 7934 | |
| 7935 | return out; |
| 7936 | } |
| 7937 | |
| 7938 | PyDoc_STRVAR(partition__doc__, |
| 7939 | "S.partition(sep) -> (head, sep, tail)\n\ |
| 7940 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7941 | Search for the separator sep in S, and return the part before it,\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7942 | the separator itself, and the part after it. If the separator is not\n\ |
| 7943 | found, returns S and two empty strings."); |
| 7944 | |
| 7945 | static PyObject* |
| 7946 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
| 7947 | { |
| 7948 | return PyUnicode_Partition((PyObject *)self, separator); |
| 7949 | } |
| 7950 | |
| 7951 | PyDoc_STRVAR(rpartition__doc__, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7952 | "S.rpartition(sep) -> (tail, sep, head)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7953 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7954 | Search for the separator sep in S, starting at the end of S, and return\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7955 | 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] | 7956 | separator is not found, returns two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7957 | |
| 7958 | static PyObject* |
| 7959 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 7960 | { |
| 7961 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 7962 | } |
| 7963 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7964 | PyObject *PyUnicode_RSplit(PyObject *s, |
| 7965 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7966 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7967 | { |
| 7968 | PyObject *result; |
| 7969 | |
| 7970 | s = PyUnicode_FromObject(s); |
| 7971 | if (s == NULL) |
| 7972 | return NULL; |
| 7973 | if (sep != NULL) { |
| 7974 | sep = PyUnicode_FromObject(sep); |
| 7975 | if (sep == NULL) { |
| 7976 | Py_DECREF(s); |
| 7977 | return NULL; |
| 7978 | } |
| 7979 | } |
| 7980 | |
| 7981 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 7982 | |
| 7983 | Py_DECREF(s); |
| 7984 | Py_XDECREF(sep); |
| 7985 | return result; |
| 7986 | } |
| 7987 | |
| 7988 | PyDoc_STRVAR(rsplit__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7989 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7990 | \n\ |
| 7991 | Return a list of the words in S, using sep as the\n\ |
| 7992 | delimiter string, starting at the end of the string and\n\ |
| 7993 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 7994 | splits are done. If sep is not specified, any whitespace string\n\ |
| 7995 | is a separator."); |
| 7996 | |
| 7997 | static PyObject* |
| 7998 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 7999 | { |
| 8000 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8001 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8002 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8003 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8004 | return NULL; |
| 8005 | |
| 8006 | if (substring == Py_None) |
| 8007 | return rsplit(self, NULL, maxcount); |
| 8008 | else if (PyUnicode_Check(substring)) |
| 8009 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
| 8010 | else |
| 8011 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
| 8012 | } |
| 8013 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8014 | PyDoc_STRVAR(splitlines__doc__, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8015 | "S.splitlines([keepends]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8016 | \n\ |
| 8017 | 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] | 8018 | 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] | 8019 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8020 | |
| 8021 | static PyObject* |
| 8022 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 8023 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8024 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8025 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8026 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8027 | return NULL; |
| 8028 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8029 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8030 | } |
| 8031 | |
| 8032 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 8033 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8034 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 8035 | if (PyUnicode_CheckExact(self)) { |
| 8036 | Py_INCREF(self); |
| 8037 | return self; |
| 8038 | } else |
| 8039 | /* Subtype -- return genuine unicode string with the same value. */ |
| 8040 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(self), |
| 8041 | PyUnicode_GET_SIZE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8042 | } |
| 8043 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8044 | PyDoc_STRVAR(swapcase__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8045 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8046 | \n\ |
| 8047 | 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] | 8048 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8049 | |
| 8050 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8051 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8052 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8053 | return fixup(self, fixswapcase); |
| 8054 | } |
| 8055 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8056 | PyDoc_STRVAR(maketrans__doc__, |
| 8057 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
| 8058 | \n\ |
| 8059 | Return a translation table usable for str.translate().\n\ |
| 8060 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 8061 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
| 8062 | Character keys will then be converted to ordinals.\n\ |
| 8063 | If there are two arguments, they must be strings of equal length, and\n\ |
| 8064 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 8065 | character at the same position in y. If there is a third argument, it\n\ |
| 8066 | must be a string, whose characters will be mapped to None in the result."); |
| 8067 | |
| 8068 | static PyObject* |
| 8069 | unicode_maketrans(PyUnicodeObject *null, PyObject *args) |
| 8070 | { |
| 8071 | PyObject *x, *y = NULL, *z = NULL; |
| 8072 | PyObject *new = NULL, *key, *value; |
| 8073 | Py_ssize_t i = 0; |
| 8074 | int res; |
| 8075 | |
| 8076 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 8077 | return NULL; |
| 8078 | new = PyDict_New(); |
| 8079 | if (!new) |
| 8080 | return NULL; |
| 8081 | if (y != NULL) { |
| 8082 | /* x must be a string too, of equal length */ |
| 8083 | Py_ssize_t ylen = PyUnicode_GET_SIZE(y); |
| 8084 | if (!PyUnicode_Check(x)) { |
| 8085 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 8086 | "be a string if there is a second argument"); |
| 8087 | goto err; |
| 8088 | } |
| 8089 | if (PyUnicode_GET_SIZE(x) != ylen) { |
| 8090 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 8091 | "arguments must have equal length"); |
| 8092 | goto err; |
| 8093 | } |
| 8094 | /* create entries for translating chars in x to those in y */ |
| 8095 | for (i = 0; i < PyUnicode_GET_SIZE(x); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8096 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(x)[i]); |
| 8097 | value = PyLong_FromLong(PyUnicode_AS_UNICODE(y)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8098 | if (!key || !value) |
| 8099 | goto err; |
| 8100 | res = PyDict_SetItem(new, key, value); |
| 8101 | Py_DECREF(key); |
| 8102 | Py_DECREF(value); |
| 8103 | if (res < 0) |
| 8104 | goto err; |
| 8105 | } |
| 8106 | /* create entries for deleting chars in z */ |
| 8107 | if (z != NULL) { |
| 8108 | for (i = 0; i < PyUnicode_GET_SIZE(z); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8109 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(z)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8110 | if (!key) |
| 8111 | goto err; |
| 8112 | res = PyDict_SetItem(new, key, Py_None); |
| 8113 | Py_DECREF(key); |
| 8114 | if (res < 0) |
| 8115 | goto err; |
| 8116 | } |
| 8117 | } |
| 8118 | } else { |
| 8119 | /* x must be a dict */ |
| 8120 | if (!PyDict_Check(x)) { |
| 8121 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 8122 | "to maketrans it must be a dict"); |
| 8123 | goto err; |
| 8124 | } |
| 8125 | /* copy entries into the new dict, converting string keys to int keys */ |
| 8126 | while (PyDict_Next(x, &i, &key, &value)) { |
| 8127 | if (PyUnicode_Check(key)) { |
| 8128 | /* convert string keys to integer keys */ |
| 8129 | PyObject *newkey; |
| 8130 | if (PyUnicode_GET_SIZE(key) != 1) { |
| 8131 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 8132 | "table must be of length 1"); |
| 8133 | goto err; |
| 8134 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8135 | newkey = PyLong_FromLong(PyUnicode_AS_UNICODE(key)[0]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8136 | if (!newkey) |
| 8137 | goto err; |
| 8138 | res = PyDict_SetItem(new, newkey, value); |
| 8139 | Py_DECREF(newkey); |
| 8140 | if (res < 0) |
| 8141 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8142 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8143 | /* just keep integer keys */ |
| 8144 | if (PyDict_SetItem(new, key, value) < 0) |
| 8145 | goto err; |
| 8146 | } else { |
| 8147 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 8148 | "be strings or integers"); |
| 8149 | goto err; |
| 8150 | } |
| 8151 | } |
| 8152 | } |
| 8153 | return new; |
| 8154 | err: |
| 8155 | Py_DECREF(new); |
| 8156 | return NULL; |
| 8157 | } |
| 8158 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8159 | PyDoc_STRVAR(translate__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8160 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8161 | \n\ |
| 8162 | Return a copy of the string S, where all characters have been mapped\n\ |
| 8163 | 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] | 8164 | Unicode ordinals to Unicode ordinals, Unicode strings or None.\n\ |
| 8165 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 8166 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8167 | |
| 8168 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8169 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8170 | { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8171 | return PyUnicode_TranslateCharmap(self->str, self->length, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8172 | } |
| 8173 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8174 | PyDoc_STRVAR(upper__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8175 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8176 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8177 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8178 | |
| 8179 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8180 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8181 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8182 | return fixup(self, fixupper); |
| 8183 | } |
| 8184 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8185 | PyDoc_STRVAR(zfill__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8186 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8187 | \n\ |
| 8188 | 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] | 8189 | of the specified width. The string x is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8190 | |
| 8191 | static PyObject * |
| 8192 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 8193 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8194 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8195 | PyUnicodeObject *u; |
| 8196 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8197 | Py_ssize_t width; |
| 8198 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8199 | return NULL; |
| 8200 | |
| 8201 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 8202 | if (PyUnicode_CheckExact(self)) { |
| 8203 | Py_INCREF(self); |
| 8204 | return (PyObject*) self; |
| 8205 | } |
| 8206 | else |
| 8207 | return PyUnicode_FromUnicode( |
| 8208 | PyUnicode_AS_UNICODE(self), |
| 8209 | PyUnicode_GET_SIZE(self) |
| 8210 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8211 | } |
| 8212 | |
| 8213 | fill = width - self->length; |
| 8214 | |
| 8215 | u = pad(self, fill, 0, '0'); |
| 8216 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8217 | if (u == NULL) |
| 8218 | return NULL; |
| 8219 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8220 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 8221 | /* move sign to beginning of string */ |
| 8222 | u->str[0] = u->str[fill]; |
| 8223 | u->str[fill] = '0'; |
| 8224 | } |
| 8225 | |
| 8226 | return (PyObject*) u; |
| 8227 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8228 | |
| 8229 | #if 0 |
| 8230 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8231 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8232 | { |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 8233 | return PyLong_FromLong(numfree); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8234 | } |
| 8235 | #endif |
| 8236 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8237 | PyDoc_STRVAR(startswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 8238 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8239 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 8240 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 8241 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8242 | With optional end, stop comparing S at that position.\n\ |
| 8243 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8244 | |
| 8245 | static PyObject * |
| 8246 | unicode_startswith(PyUnicodeObject *self, |
| 8247 | PyObject *args) |
| 8248 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8249 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8250 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8251 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8252 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8253 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8254 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8255 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 8256 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8257 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8258 | if (PyTuple_Check(subobj)) { |
| 8259 | Py_ssize_t i; |
| 8260 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 8261 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 8262 | PyTuple_GET_ITEM(subobj, i)); |
| 8263 | if (substring == NULL) |
| 8264 | return NULL; |
| 8265 | result = tailmatch(self, substring, start, end, -1); |
| 8266 | Py_DECREF(substring); |
| 8267 | if (result) { |
| 8268 | Py_RETURN_TRUE; |
| 8269 | } |
| 8270 | } |
| 8271 | /* nothing matched */ |
| 8272 | Py_RETURN_FALSE; |
| 8273 | } |
| 8274 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8275 | if (substring == NULL) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8276 | return NULL; |
| 8277 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8278 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8279 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8280 | } |
| 8281 | |
| 8282 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8283 | PyDoc_STRVAR(endswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 8284 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8285 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 8286 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 8287 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8288 | With optional end, stop comparing S at that position.\n\ |
| 8289 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8290 | |
| 8291 | static PyObject * |
| 8292 | unicode_endswith(PyUnicodeObject *self, |
| 8293 | PyObject *args) |
| 8294 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8295 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8296 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8297 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8298 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8299 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8300 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8301 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
| 8302 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8303 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8304 | if (PyTuple_Check(subobj)) { |
| 8305 | Py_ssize_t i; |
| 8306 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 8307 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 8308 | PyTuple_GET_ITEM(subobj, i)); |
| 8309 | if (substring == NULL) |
| 8310 | return NULL; |
| 8311 | result = tailmatch(self, substring, start, end, +1); |
| 8312 | Py_DECREF(substring); |
| 8313 | if (result) { |
| 8314 | Py_RETURN_TRUE; |
| 8315 | } |
| 8316 | } |
| 8317 | Py_RETURN_FALSE; |
| 8318 | } |
| 8319 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8320 | if (substring == NULL) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8321 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8322 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8323 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8324 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8325 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8326 | } |
| 8327 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8328 | #include "stringlib/string_format.h" |
| 8329 | |
| 8330 | PyDoc_STRVAR(format__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8331 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8332 | \n\ |
| 8333 | "); |
| 8334 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 8335 | static PyObject * |
| 8336 | unicode__format__(PyObject* self, PyObject* args) |
| 8337 | { |
| 8338 | PyObject *format_spec; |
| 8339 | |
| 8340 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 8341 | return NULL; |
| 8342 | |
| 8343 | return _PyUnicode_FormatAdvanced(self, |
| 8344 | PyUnicode_AS_UNICODE(format_spec), |
| 8345 | PyUnicode_GET_SIZE(format_spec)); |
| 8346 | } |
| 8347 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8348 | PyDoc_STRVAR(p_format__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8349 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8350 | \n\ |
| 8351 | "); |
| 8352 | |
| 8353 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8354 | unicode__sizeof__(PyUnicodeObject *v) |
| 8355 | { |
| 8356 | PyObject *res = NULL, *defsize = NULL; |
| 8357 | |
| 8358 | res = PyLong_FromSsize_t(sizeof(PyUnicodeObject) + |
| 8359 | sizeof(Py_UNICODE) * (v->length + 1)); |
| 8360 | if (v->defenc) { |
| 8361 | defsize = PyObject_CallMethod(v->defenc, "__sizeof__", NULL); |
| 8362 | if (defsize == NULL) { |
| 8363 | Py_DECREF(res); |
| 8364 | return NULL; |
| 8365 | } |
| 8366 | res = PyNumber_Add(res, defsize); |
| 8367 | Py_DECREF(defsize); |
| 8368 | } |
| 8369 | return res; |
| 8370 | } |
| 8371 | |
| 8372 | PyDoc_STRVAR(sizeof__doc__, |
| 8373 | "S.__sizeof__() -> size of S in memory, in bytes"); |
| 8374 | |
| 8375 | static PyObject * |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8376 | unicode_getnewargs(PyUnicodeObject *v) |
| 8377 | { |
| 8378 | return Py_BuildValue("(u#)", v->str, v->length); |
| 8379 | } |
| 8380 | |
| 8381 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8382 | static PyMethodDef unicode_methods[] = { |
| 8383 | |
| 8384 | /* Order is according to common usage: often used methods should |
| 8385 | appear first, since lookup is done sequentially. */ |
| 8386 | |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8387 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS, encode__doc__}, |
| 8388 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 8389 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8390 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8391 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 8392 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 8393 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 8394 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 8395 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 8396 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 8397 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8398 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8399 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 8400 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 8401 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8402 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8403 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 8404 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 8405 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8406 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8407 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8408 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8409 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8410 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 8411 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 8412 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 8413 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 8414 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 8415 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 8416 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 8417 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 8418 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 8419 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 8420 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 8421 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 8422 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 8423 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 8424 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8425 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8426 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 8427 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 8428 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Eric Smith | f6db409 | 2007-08-27 23:52:26 +0000 | [diff] [blame] | 8429 | {"_formatter_field_name_split", (PyCFunction) formatter_field_name_split, METH_NOARGS}, |
| 8430 | {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8431 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 8432 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8433 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8434 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8435 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8436 | #endif |
| 8437 | |
| 8438 | #if 0 |
| 8439 | /* This one is just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8440 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8441 | #endif |
| 8442 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8443 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8444 | {NULL, NULL} |
| 8445 | }; |
| 8446 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8447 | static PyObject * |
| 8448 | unicode_mod(PyObject *v, PyObject *w) |
| 8449 | { |
| 8450 | if (!PyUnicode_Check(v)) { |
| 8451 | Py_INCREF(Py_NotImplemented); |
| 8452 | return Py_NotImplemented; |
| 8453 | } |
| 8454 | return PyUnicode_Format(v, w); |
| 8455 | } |
| 8456 | |
| 8457 | static PyNumberMethods unicode_as_number = { |
| 8458 | 0, /*nb_add*/ |
| 8459 | 0, /*nb_subtract*/ |
| 8460 | 0, /*nb_multiply*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8461 | unicode_mod, /*nb_remainder*/ |
| 8462 | }; |
| 8463 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8464 | static PySequenceMethods unicode_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8465 | (lenfunc) unicode_length, /* sq_length */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8466 | PyUnicode_Concat, /* sq_concat */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8467 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 8468 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
Thomas Wouters | d2cf20e | 2007-08-30 22:57:53 +0000 | [diff] [blame] | 8469 | 0, /* sq_slice */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8470 | 0, /* sq_ass_item */ |
| 8471 | 0, /* sq_ass_slice */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8472 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8473 | }; |
| 8474 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8475 | static PyObject* |
| 8476 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 8477 | { |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 8478 | if (PyIndex_Check(item)) { |
| 8479 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8480 | if (i == -1 && PyErr_Occurred()) |
| 8481 | return NULL; |
| 8482 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8483 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8484 | return unicode_getitem(self, i); |
| 8485 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8486 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8487 | Py_UNICODE* source_buf; |
| 8488 | Py_UNICODE* result_buf; |
| 8489 | PyObject* result; |
| 8490 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8491 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8492 | &start, &stop, &step, &slicelength) < 0) { |
| 8493 | return NULL; |
| 8494 | } |
| 8495 | |
| 8496 | if (slicelength <= 0) { |
| 8497 | return PyUnicode_FromUnicode(NULL, 0); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 8498 | } else if (start == 0 && step == 1 && slicelength == self->length && |
| 8499 | PyUnicode_CheckExact(self)) { |
| 8500 | Py_INCREF(self); |
| 8501 | return (PyObject *)self; |
| 8502 | } else if (step == 1) { |
| 8503 | return PyUnicode_FromUnicode(self->str + start, slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8504 | } else { |
| 8505 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 8506 | result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* |
| 8507 | sizeof(Py_UNICODE)); |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8508 | |
| 8509 | if (result_buf == NULL) |
| 8510 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8511 | |
| 8512 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 8513 | result_buf[i] = source_buf[cur]; |
| 8514 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8515 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8516 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 8517 | PyObject_FREE(result_buf); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8518 | return result; |
| 8519 | } |
| 8520 | } else { |
| 8521 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 8522 | return NULL; |
| 8523 | } |
| 8524 | } |
| 8525 | |
| 8526 | static PyMappingMethods unicode_as_mapping = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8527 | (lenfunc)unicode_length, /* mp_length */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8528 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 8529 | (objobjargproc)0, /* mp_ass_subscript */ |
| 8530 | }; |
| 8531 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8532 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8533 | /* Helpers for PyUnicode_Format() */ |
| 8534 | |
| 8535 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8536 | 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] | 8537 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8538 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8539 | if (argidx < arglen) { |
| 8540 | (*p_argidx)++; |
| 8541 | if (arglen < 0) |
| 8542 | return args; |
| 8543 | else |
| 8544 | return PyTuple_GetItem(args, argidx); |
| 8545 | } |
| 8546 | PyErr_SetString(PyExc_TypeError, |
| 8547 | "not enough arguments for format string"); |
| 8548 | return NULL; |
| 8549 | } |
| 8550 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8551 | static Py_ssize_t |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8552 | strtounicode(Py_UNICODE *buffer, const char *charbuffer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8553 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8554 | register Py_ssize_t i; |
| 8555 | Py_ssize_t len = strlen(charbuffer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8556 | for (i = len - 1; i >= 0; i--) |
| 8557 | buffer[i] = (Py_UNICODE) charbuffer[i]; |
| 8558 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8559 | return len; |
| 8560 | } |
| 8561 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8562 | static int |
| 8563 | doubletounicode(Py_UNICODE *buffer, size_t len, const char *format, double x) |
| 8564 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8565 | Py_ssize_t result; |
| 8566 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8567 | PyOS_ascii_formatd((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8568 | result = strtounicode(buffer, (char *)buffer); |
| 8569 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8570 | } |
| 8571 | |
Christian Heimes | 3fd1399 | 2008-03-21 01:05:49 +0000 | [diff] [blame] | 8572 | #if 0 |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8573 | static int |
| 8574 | longtounicode(Py_UNICODE *buffer, size_t len, const char *format, long x) |
| 8575 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8576 | Py_ssize_t result; |
| 8577 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8578 | PyOS_snprintf((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8579 | result = strtounicode(buffer, (char *)buffer); |
| 8580 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8581 | } |
Christian Heimes | 3fd1399 | 2008-03-21 01:05:49 +0000 | [diff] [blame] | 8582 | #endif |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8583 | |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 8584 | /* XXX To save some code duplication, formatfloat/long/int could have been |
| 8585 | shared with stringobject.c, converting from 8-bit to Unicode after the |
| 8586 | formatting is done. */ |
| 8587 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8588 | static int |
| 8589 | formatfloat(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8590 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8591 | int flags, |
| 8592 | int prec, |
| 8593 | int type, |
| 8594 | PyObject *v) |
| 8595 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8596 | /* fmt = '%#.' + `prec` + `type` |
| 8597 | 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] | 8598 | char fmt[20]; |
| 8599 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8600 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8601 | x = PyFloat_AsDouble(v); |
| 8602 | if (x == -1.0 && PyErr_Occurred()) |
| 8603 | return -1; |
| 8604 | if (prec < 0) |
| 8605 | prec = 6; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8606 | if (type == 'f' && (fabs(x) / 1e25) >= 1e25) |
| 8607 | type = 'g'; |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8608 | /* Worst case length calc to ensure no buffer overrun: |
| 8609 | |
| 8610 | 'g' formats: |
| 8611 | fmt = %#.<prec>g |
| 8612 | buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
| 8613 | for any double rep.) |
| 8614 | len = 1 + prec + 1 + 2 + 5 = 9 + prec |
| 8615 | |
| 8616 | 'f' formats: |
| 8617 | buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
| 8618 | len = 1 + 50 + 1 + prec = 52 + prec |
| 8619 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8620 | If prec=0 the effective precision is 1 (the leading digit is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8621 | always given), therefore increase the length by one. |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8622 | |
| 8623 | */ |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 8624 | if (((type == 'g' || type == 'G') && |
| 8625 | buflen <= (size_t)10 + (size_t)prec) || |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8626 | (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8627 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8628 | "formatted float is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8629 | return -1; |
| 8630 | } |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8631 | PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
| 8632 | (flags&F_ALT) ? "#" : "", |
| 8633 | prec, type); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8634 | return doubletounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8635 | } |
| 8636 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8637 | static PyObject* |
| 8638 | formatlong(PyObject *val, int flags, int prec, int type) |
| 8639 | { |
| 8640 | char *buf; |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8641 | int len; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8642 | PyObject *str; /* temporary string object. */ |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8643 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8644 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8645 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8646 | if (!str) |
| 8647 | return NULL; |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8648 | result = PyUnicode_FromStringAndSize(buf, len); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8649 | Py_DECREF(str); |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8650 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8651 | } |
| 8652 | |
Christian Heimes | 3fd1399 | 2008-03-21 01:05:49 +0000 | [diff] [blame] | 8653 | #if 0 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8654 | static int |
| 8655 | formatint(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8656 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8657 | int flags, |
| 8658 | int prec, |
| 8659 | int type, |
| 8660 | PyObject *v) |
| 8661 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8662 | /* fmt = '%#.' + `prec` + 'l' + `type` |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8663 | * worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
| 8664 | * + 1 + 1 |
| 8665 | * = 24 |
| 8666 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8667 | char fmt[64]; /* plenty big enough! */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8668 | char *sign; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8669 | long x; |
| 8670 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8671 | x = PyLong_AsLong(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8672 | if (x == -1 && PyErr_Occurred()) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8673 | return -1; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8674 | if (x < 0 && type == 'u') { |
| 8675 | type = 'd'; |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 8676 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8677 | if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) |
| 8678 | sign = "-"; |
| 8679 | else |
| 8680 | sign = ""; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8681 | if (prec < 0) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8682 | prec = 1; |
| 8683 | |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8684 | /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) |
| 8685 | * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8686 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8687 | if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8688 | PyErr_SetString(PyExc_OverflowError, |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8689 | "formatted integer is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8690 | return -1; |
| 8691 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8692 | |
| 8693 | if ((flags & F_ALT) && |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 8694 | (type == 'x' || type == 'X' || type == 'o')) { |
| 8695 | /* When converting under %#o, %#x or %#X, there are a number |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8696 | * of issues that cause pain: |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 8697 | * - for %#o, we want a different base marker than C |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8698 | * - when 0 is being converted, the C standard leaves off |
| 8699 | * the '0x' or '0X', which is inconsistent with other |
| 8700 | * %#x/%#X conversions and inconsistent with Python's |
| 8701 | * hex() function |
| 8702 | * - there are platforms that violate the standard and |
| 8703 | * convert 0 with the '0x' or '0X' |
| 8704 | * (Metrowerks, Compaq Tru64) |
| 8705 | * - there are platforms that give '0x' when converting |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8706 | * under %#X, but convert 0 in accordance with the |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8707 | * standard (OS/2 EMX) |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8708 | * |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8709 | * We can achieve the desired consistency by inserting our |
| 8710 | * own '0x' or '0X' prefix, and substituting %x/%X in place |
| 8711 | * of %#x/%#X. |
| 8712 | * |
| 8713 | * Note that this is the same approach as used in |
| 8714 | * formatint() in stringobject.c |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 8715 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8716 | PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", |
| 8717 | sign, type, prec, type); |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 8718 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8719 | else { |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8720 | PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", |
| 8721 | sign, (flags&F_ALT) ? "#" : "", |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8722 | prec, type); |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 8723 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8724 | if (sign[0]) |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8725 | return longtounicode(buf, buflen, fmt, -x); |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8726 | else |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8727 | return longtounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8728 | } |
Christian Heimes | 3fd1399 | 2008-03-21 01:05:49 +0000 | [diff] [blame] | 8729 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8730 | |
| 8731 | static int |
| 8732 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8733 | size_t buflen, |
| 8734 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8735 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8736 | /* presume that the buffer is at least 2 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8737 | if (PyUnicode_Check(v)) { |
| 8738 | if (PyUnicode_GET_SIZE(v) != 1) |
| 8739 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8740 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8741 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8742 | else { |
| 8743 | /* Integer input truncated to a character */ |
| 8744 | long x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8745 | x = PyLong_AsLong(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8746 | if (x == -1 && PyErr_Occurred()) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8747 | goto onError; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8748 | #ifdef Py_UNICODE_WIDE |
| 8749 | if (x < 0 || x > 0x10ffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 8750 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8751 | "%c arg not in range(0x110000) " |
| 8752 | "(wide Python build)"); |
| 8753 | return -1; |
| 8754 | } |
| 8755 | #else |
| 8756 | if (x < 0 || x > 0xffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 8757 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8758 | "%c arg not in range(0x10000) " |
| 8759 | "(narrow Python build)"); |
| 8760 | return -1; |
| 8761 | } |
| 8762 | #endif |
| 8763 | buf[0] = (Py_UNICODE) x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8764 | } |
| 8765 | buf[1] = '\0'; |
| 8766 | return 1; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8767 | |
| 8768 | onError: |
| 8769 | PyErr_SetString(PyExc_TypeError, |
| 8770 | "%c requires int or char"); |
| 8771 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8772 | } |
| 8773 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8774 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 8775 | |
| 8776 | FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
| 8777 | chars are formatted. XXX This is a magic number. Each formatting |
| 8778 | routine does bounds checking to ensure no overflow, but a better |
| 8779 | solution may be to malloc a buffer of appropriate size for each |
| 8780 | format. For now, the current solution is sufficient. |
| 8781 | */ |
| 8782 | #define FORMATBUFLEN (size_t)120 |
| 8783 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8784 | PyObject *PyUnicode_Format(PyObject *format, |
| 8785 | PyObject *args) |
| 8786 | { |
| 8787 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8788 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8789 | int args_owned = 0; |
| 8790 | PyUnicodeObject *result = NULL; |
| 8791 | PyObject *dict = NULL; |
| 8792 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8793 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8794 | if (format == NULL || args == NULL) { |
| 8795 | PyErr_BadInternalCall(); |
| 8796 | return NULL; |
| 8797 | } |
| 8798 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8799 | if (uformat == NULL) |
| 8800 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8801 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 8802 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 8803 | |
| 8804 | reslen = rescnt = fmtcnt + 100; |
| 8805 | result = _PyUnicode_New(reslen); |
| 8806 | if (result == NULL) |
| 8807 | goto onError; |
| 8808 | res = PyUnicode_AS_UNICODE(result); |
| 8809 | |
| 8810 | if (PyTuple_Check(args)) { |
| 8811 | arglen = PyTuple_Size(args); |
| 8812 | argidx = 0; |
| 8813 | } |
| 8814 | else { |
| 8815 | arglen = -1; |
| 8816 | argidx = -2; |
| 8817 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8818 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 8819 | !PyUnicode_Check(args)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8820 | dict = args; |
| 8821 | |
| 8822 | while (--fmtcnt >= 0) { |
| 8823 | if (*fmt != '%') { |
| 8824 | if (--rescnt < 0) { |
| 8825 | rescnt = fmtcnt + 100; |
| 8826 | reslen += rescnt; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8827 | if (_PyUnicode_Resize(&result, reslen) < 0) |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 8828 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8829 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 8830 | --rescnt; |
| 8831 | } |
| 8832 | *res++ = *fmt++; |
| 8833 | } |
| 8834 | else { |
| 8835 | /* Got a format specifier */ |
| 8836 | int flags = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8837 | Py_ssize_t width = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8838 | int prec = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8839 | Py_UNICODE c = '\0'; |
| 8840 | Py_UNICODE fill; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 8841 | int isnumok; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8842 | PyObject *v = NULL; |
| 8843 | PyObject *temp = NULL; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8844 | Py_UNICODE *pbuf; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8845 | Py_UNICODE sign; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8846 | Py_ssize_t len; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8847 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8848 | |
| 8849 | fmt++; |
| 8850 | if (*fmt == '(') { |
| 8851 | Py_UNICODE *keystart; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8852 | Py_ssize_t keylen; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8853 | PyObject *key; |
| 8854 | int pcount = 1; |
| 8855 | |
| 8856 | if (dict == NULL) { |
| 8857 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8858 | "format requires a mapping"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8859 | goto onError; |
| 8860 | } |
| 8861 | ++fmt; |
| 8862 | --fmtcnt; |
| 8863 | keystart = fmt; |
| 8864 | /* Skip over balanced parentheses */ |
| 8865 | while (pcount > 0 && --fmtcnt >= 0) { |
| 8866 | if (*fmt == ')') |
| 8867 | --pcount; |
| 8868 | else if (*fmt == '(') |
| 8869 | ++pcount; |
| 8870 | fmt++; |
| 8871 | } |
| 8872 | keylen = fmt - keystart - 1; |
| 8873 | if (fmtcnt < 0 || pcount > 0) { |
| 8874 | PyErr_SetString(PyExc_ValueError, |
| 8875 | "incomplete format key"); |
| 8876 | goto onError; |
| 8877 | } |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 8878 | #if 0 |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8879 | /* keys are converted to strings using UTF-8 and |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8880 | then looked up since Python uses strings to hold |
| 8881 | variables names etc. in its namespaces and we |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8882 | wouldn't want to break common idioms. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8883 | key = PyUnicode_EncodeUTF8(keystart, |
| 8884 | keylen, |
| 8885 | NULL); |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 8886 | #else |
| 8887 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 8888 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8889 | if (key == NULL) |
| 8890 | goto onError; |
| 8891 | if (args_owned) { |
| 8892 | Py_DECREF(args); |
| 8893 | args_owned = 0; |
| 8894 | } |
| 8895 | args = PyObject_GetItem(dict, key); |
| 8896 | Py_DECREF(key); |
| 8897 | if (args == NULL) { |
| 8898 | goto onError; |
| 8899 | } |
| 8900 | args_owned = 1; |
| 8901 | arglen = -1; |
| 8902 | argidx = -2; |
| 8903 | } |
| 8904 | while (--fmtcnt >= 0) { |
| 8905 | switch (c = *fmt++) { |
| 8906 | case '-': flags |= F_LJUST; continue; |
| 8907 | case '+': flags |= F_SIGN; continue; |
| 8908 | case ' ': flags |= F_BLANK; continue; |
| 8909 | case '#': flags |= F_ALT; continue; |
| 8910 | case '0': flags |= F_ZERO; continue; |
| 8911 | } |
| 8912 | break; |
| 8913 | } |
| 8914 | if (c == '*') { |
| 8915 | v = getnextarg(args, arglen, &argidx); |
| 8916 | if (v == NULL) |
| 8917 | goto onError; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8918 | if (!PyLong_Check(v)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8919 | PyErr_SetString(PyExc_TypeError, |
| 8920 | "* wants int"); |
| 8921 | goto onError; |
| 8922 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8923 | width = PyLong_AsLong(v); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 8924 | if (width == -1 && PyErr_Occurred()) |
| 8925 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8926 | if (width < 0) { |
| 8927 | flags |= F_LJUST; |
| 8928 | width = -width; |
| 8929 | } |
| 8930 | if (--fmtcnt >= 0) |
| 8931 | c = *fmt++; |
| 8932 | } |
| 8933 | else if (c >= '0' && c <= '9') { |
| 8934 | width = c - '0'; |
| 8935 | while (--fmtcnt >= 0) { |
| 8936 | c = *fmt++; |
| 8937 | if (c < '0' || c > '9') |
| 8938 | break; |
| 8939 | if ((width*10) / 10 != width) { |
| 8940 | PyErr_SetString(PyExc_ValueError, |
| 8941 | "width too big"); |
| 8942 | goto onError; |
| 8943 | } |
| 8944 | width = width*10 + (c - '0'); |
| 8945 | } |
| 8946 | } |
| 8947 | if (c == '.') { |
| 8948 | prec = 0; |
| 8949 | if (--fmtcnt >= 0) |
| 8950 | c = *fmt++; |
| 8951 | if (c == '*') { |
| 8952 | v = getnextarg(args, arglen, &argidx); |
| 8953 | if (v == NULL) |
| 8954 | goto onError; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8955 | if (!PyLong_Check(v)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8956 | PyErr_SetString(PyExc_TypeError, |
| 8957 | "* wants int"); |
| 8958 | goto onError; |
| 8959 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8960 | prec = PyLong_AsLong(v); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 8961 | if (prec == -1 && PyErr_Occurred()) |
| 8962 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8963 | if (prec < 0) |
| 8964 | prec = 0; |
| 8965 | if (--fmtcnt >= 0) |
| 8966 | c = *fmt++; |
| 8967 | } |
| 8968 | else if (c >= '0' && c <= '9') { |
| 8969 | prec = c - '0'; |
| 8970 | while (--fmtcnt >= 0) { |
| 8971 | c = Py_CHARMASK(*fmt++); |
| 8972 | if (c < '0' || c > '9') |
| 8973 | break; |
| 8974 | if ((prec*10) / 10 != prec) { |
| 8975 | PyErr_SetString(PyExc_ValueError, |
| 8976 | "prec too big"); |
| 8977 | goto onError; |
| 8978 | } |
| 8979 | prec = prec*10 + (c - '0'); |
| 8980 | } |
| 8981 | } |
| 8982 | } /* prec */ |
| 8983 | if (fmtcnt >= 0) { |
| 8984 | if (c == 'h' || c == 'l' || c == 'L') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8985 | if (--fmtcnt >= 0) |
| 8986 | c = *fmt++; |
| 8987 | } |
| 8988 | } |
| 8989 | if (fmtcnt < 0) { |
| 8990 | PyErr_SetString(PyExc_ValueError, |
| 8991 | "incomplete format"); |
| 8992 | goto onError; |
| 8993 | } |
| 8994 | if (c != '%') { |
| 8995 | v = getnextarg(args, arglen, &argidx); |
| 8996 | if (v == NULL) |
| 8997 | goto onError; |
| 8998 | } |
| 8999 | sign = 0; |
| 9000 | fill = ' '; |
| 9001 | switch (c) { |
| 9002 | |
| 9003 | case '%': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9004 | pbuf = formatbuf; |
| 9005 | /* presume that buffer length is at least 1 */ |
| 9006 | pbuf[0] = '%'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9007 | len = 1; |
| 9008 | break; |
| 9009 | |
| 9010 | case 's': |
| 9011 | case 'r': |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 9012 | case 'a': |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9013 | if (PyUnicode_Check(v) && c == 's') { |
| 9014 | temp = v; |
| 9015 | Py_INCREF(temp); |
| 9016 | } |
| 9017 | else { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9018 | if (c == 's') |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 9019 | temp = PyObject_Str(v); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 9020 | else if (c == 'r') |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9021 | temp = PyObject_Repr(v); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 9022 | else |
| 9023 | temp = PyObject_ASCII(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9024 | if (temp == NULL) |
| 9025 | goto onError; |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 9026 | if (PyUnicode_Check(temp)) |
| 9027 | /* nothing to do */; |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 9028 | else { |
| 9029 | Py_DECREF(temp); |
| 9030 | PyErr_SetString(PyExc_TypeError, |
| 9031 | "%s argument has non-string str()"); |
| 9032 | goto onError; |
| 9033 | } |
| 9034 | } |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9035 | pbuf = PyUnicode_AS_UNICODE(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9036 | len = PyUnicode_GET_SIZE(temp); |
| 9037 | if (prec >= 0 && len > prec) |
| 9038 | len = prec; |
| 9039 | break; |
| 9040 | |
| 9041 | case 'i': |
| 9042 | case 'd': |
| 9043 | case 'u': |
| 9044 | case 'o': |
| 9045 | case 'x': |
| 9046 | case 'X': |
| 9047 | if (c == 'i') |
| 9048 | c = 'd'; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 9049 | isnumok = 0; |
| 9050 | if (PyNumber_Check(v)) { |
| 9051 | PyObject *iobj=NULL; |
| 9052 | |
| 9053 | if (PyLong_Check(v)) { |
| 9054 | iobj = v; |
| 9055 | Py_INCREF(iobj); |
| 9056 | } |
| 9057 | else { |
| 9058 | iobj = PyNumber_Long(v); |
| 9059 | } |
| 9060 | if (iobj!=NULL) { |
| 9061 | if (PyLong_Check(iobj)) { |
| 9062 | isnumok = 1; |
| 9063 | temp = formatlong(iobj, flags, prec, c); |
| 9064 | Py_DECREF(iobj); |
| 9065 | if (!temp) |
| 9066 | goto onError; |
| 9067 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9068 | len = PyUnicode_GET_SIZE(temp); |
| 9069 | sign = 1; |
| 9070 | } |
| 9071 | else { |
| 9072 | Py_DECREF(iobj); |
| 9073 | } |
| 9074 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9075 | } |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 9076 | if (!isnumok) { |
| 9077 | PyErr_Format(PyExc_TypeError, |
| 9078 | "%%%c format: a number is required, " |
Martin v. Löwis | 5a6f458 | 2008-04-07 03:22:07 +0000 | [diff] [blame] | 9079 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9080 | goto onError; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9081 | } |
| 9082 | if (flags & F_ZERO) |
| 9083 | fill = '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9084 | break; |
| 9085 | |
| 9086 | case 'e': |
| 9087 | case 'E': |
| 9088 | case 'f': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 9089 | case 'F': |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9090 | case 'g': |
| 9091 | case 'G': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 9092 | if (c == 'F') |
| 9093 | c = 'f'; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9094 | pbuf = formatbuf; |
| 9095 | len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 9096 | flags, prec, c, v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9097 | if (len < 0) |
| 9098 | goto onError; |
| 9099 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9100 | if (flags & F_ZERO) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9101 | fill = '0'; |
| 9102 | break; |
| 9103 | |
| 9104 | case 'c': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9105 | pbuf = formatbuf; |
| 9106 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9107 | if (len < 0) |
| 9108 | goto onError; |
| 9109 | break; |
| 9110 | |
| 9111 | default: |
| 9112 | PyErr_Format(PyExc_ValueError, |
Andrew M. Kuchling | 6ca8917 | 2000-12-15 13:07:46 +0000 | [diff] [blame] | 9113 | "unsupported format character '%c' (0x%x) " |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 9114 | "at index %zd", |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9115 | (31<=c && c<=126) ? (char)c : '?', |
Marc-André Lemburg | 24e53b6 | 2002-09-24 09:32:14 +0000 | [diff] [blame] | 9116 | (int)c, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 9117 | (Py_ssize_t)(fmt - 1 - |
| 9118 | PyUnicode_AS_UNICODE(uformat))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9119 | goto onError; |
| 9120 | } |
| 9121 | if (sign) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9122 | if (*pbuf == '-' || *pbuf == '+') { |
| 9123 | sign = *pbuf++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9124 | len--; |
| 9125 | } |
| 9126 | else if (flags & F_SIGN) |
| 9127 | sign = '+'; |
| 9128 | else if (flags & F_BLANK) |
| 9129 | sign = ' '; |
| 9130 | else |
| 9131 | sign = 0; |
| 9132 | } |
| 9133 | if (width < len) |
| 9134 | width = len; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 9135 | if (rescnt - (sign != 0) < width) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9136 | reslen -= rescnt; |
| 9137 | rescnt = width + fmtcnt + 100; |
| 9138 | reslen += rescnt; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 9139 | if (reslen < 0) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 9140 | Py_XDECREF(temp); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9141 | PyErr_NoMemory(); |
| 9142 | goto onError; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 9143 | } |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9144 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 9145 | Py_XDECREF(temp); |
| 9146 | goto onError; |
| 9147 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9148 | res = PyUnicode_AS_UNICODE(result) |
| 9149 | + reslen - rescnt; |
| 9150 | } |
| 9151 | if (sign) { |
| 9152 | if (fill != ' ') |
| 9153 | *res++ = sign; |
| 9154 | rescnt--; |
| 9155 | if (width > len) |
| 9156 | width--; |
| 9157 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 9158 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9159 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 9160 | assert(pbuf[1] == c); |
| 9161 | if (fill != ' ') { |
| 9162 | *res++ = *pbuf++; |
| 9163 | *res++ = *pbuf++; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9164 | } |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 9165 | rescnt -= 2; |
| 9166 | width -= 2; |
| 9167 | if (width < 0) |
| 9168 | width = 0; |
| 9169 | len -= 2; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9170 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9171 | if (width > len && !(flags & F_LJUST)) { |
| 9172 | do { |
| 9173 | --rescnt; |
| 9174 | *res++ = fill; |
| 9175 | } while (--width > len); |
| 9176 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9177 | if (fill == ' ') { |
| 9178 | if (sign) |
| 9179 | *res++ = sign; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 9180 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9181 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 9182 | assert(pbuf[1] == c); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9183 | *res++ = *pbuf++; |
| 9184 | *res++ = *pbuf++; |
| 9185 | } |
| 9186 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9187 | Py_UNICODE_COPY(res, pbuf, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9188 | res += len; |
| 9189 | rescnt -= len; |
| 9190 | while (--width >= len) { |
| 9191 | --rescnt; |
| 9192 | *res++ = ' '; |
| 9193 | } |
| 9194 | if (dict && (argidx < arglen) && c != '%') { |
| 9195 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 9196 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9197 | Py_XDECREF(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9198 | goto onError; |
| 9199 | } |
| 9200 | Py_XDECREF(temp); |
| 9201 | } /* '%' */ |
| 9202 | } /* until end */ |
| 9203 | if (argidx < arglen && !dict) { |
| 9204 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 9205 | "not all arguments converted during string formatting"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9206 | goto onError; |
| 9207 | } |
| 9208 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9209 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
| 9210 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9211 | if (args_owned) { |
| 9212 | Py_DECREF(args); |
| 9213 | } |
| 9214 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9215 | return (PyObject *)result; |
| 9216 | |
| 9217 | onError: |
| 9218 | Py_XDECREF(result); |
| 9219 | Py_DECREF(uformat); |
| 9220 | if (args_owned) { |
| 9221 | Py_DECREF(args); |
| 9222 | } |
| 9223 | return NULL; |
| 9224 | } |
| 9225 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 9226 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9227 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 9228 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9229 | static PyObject * |
| 9230 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9231 | { |
| 9232 | PyObject *x = NULL; |
Guido van Rossum | 55b4a7b | 2007-07-11 09:28:11 +0000 | [diff] [blame] | 9233 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9234 | char *encoding = NULL; |
| 9235 | char *errors = NULL; |
| 9236 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9237 | if (type != &PyUnicode_Type) |
| 9238 | return unicode_subtype_new(type, args, kwds); |
Alexandre Vassalotti | 999679a | 2008-05-03 04:42:16 +0000 | [diff] [blame] | 9239 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9240 | kwlist, &x, &encoding, &errors)) |
| 9241 | return NULL; |
| 9242 | if (x == NULL) |
| 9243 | return (PyObject *)_PyUnicode_New(0); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 9244 | if (encoding == NULL && errors == NULL) |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 9245 | return PyObject_Str(x); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 9246 | else |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9247 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
| 9248 | } |
| 9249 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9250 | static PyObject * |
| 9251 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9252 | { |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 9253 | PyUnicodeObject *tmp, *pnew; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9254 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9255 | |
| 9256 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 9257 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 9258 | if (tmp == NULL) |
| 9259 | return NULL; |
| 9260 | assert(PyUnicode_Check(tmp)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 9261 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 9262 | if (pnew == NULL) { |
| 9263 | Py_DECREF(tmp); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9264 | return NULL; |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 9265 | } |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 9266 | pnew->str = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 9267 | if (pnew->str == NULL) { |
| 9268 | _Py_ForgetReference((PyObject *)pnew); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 9269 | PyObject_Del(pnew); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 9270 | Py_DECREF(tmp); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 9271 | return PyErr_NoMemory(); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9272 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 9273 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 9274 | pnew->length = n; |
| 9275 | pnew->hash = tmp->hash; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9276 | Py_DECREF(tmp); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 9277 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9278 | } |
| 9279 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9280 | PyDoc_STRVAR(unicode_doc, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 9281 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9282 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 9283 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 9284 | encoding defaults to the current default string encoding.\n\ |
| 9285 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9286 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9287 | static PyObject *unicode_iter(PyObject *seq); |
| 9288 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9289 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 9290 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | 84fc66d | 2007-05-03 17:18:26 +0000 | [diff] [blame] | 9291 | "str", /* tp_name */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9292 | sizeof(PyUnicodeObject), /* tp_size */ |
| 9293 | 0, /* tp_itemsize */ |
| 9294 | /* Slots */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 9295 | (destructor)unicode_dealloc, /* tp_dealloc */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9296 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9297 | 0, /* tp_getattr */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9298 | 0, /* tp_setattr */ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9299 | 0, /* tp_compare */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9300 | unicode_repr, /* tp_repr */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 9301 | &unicode_as_number, /* tp_as_number */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9302 | &unicode_as_sequence, /* tp_as_sequence */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9303 | &unicode_as_mapping, /* tp_as_mapping */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9304 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 9305 | 0, /* tp_call*/ |
| 9306 | (reprfunc) unicode_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9307 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9308 | 0, /* tp_setattro */ |
Alexandre Vassalotti | 70a2371 | 2007-10-14 02:05:51 +0000 | [diff] [blame] | 9309 | 0, /* tp_as_buffer */ |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 9310 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 9311 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9312 | unicode_doc, /* tp_doc */ |
| 9313 | 0, /* tp_traverse */ |
| 9314 | 0, /* tp_clear */ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9315 | PyUnicode_RichCompare, /* tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9316 | 0, /* tp_weaklistoffset */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9317 | unicode_iter, /* tp_iter */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9318 | 0, /* tp_iternext */ |
| 9319 | unicode_methods, /* tp_methods */ |
| 9320 | 0, /* tp_members */ |
| 9321 | 0, /* tp_getset */ |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 9322 | &PyBaseObject_Type, /* tp_base */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9323 | 0, /* tp_dict */ |
| 9324 | 0, /* tp_descr_get */ |
| 9325 | 0, /* tp_descr_set */ |
| 9326 | 0, /* tp_dictoffset */ |
| 9327 | 0, /* tp_init */ |
| 9328 | 0, /* tp_alloc */ |
| 9329 | unicode_new, /* tp_new */ |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 9330 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9331 | }; |
| 9332 | |
| 9333 | /* Initialize the Unicode implementation */ |
| 9334 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9335 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9336 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9337 | int i; |
| 9338 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9339 | /* XXX - move this array to unicodectype.c ? */ |
| 9340 | Py_UNICODE linebreak[] = { |
| 9341 | 0x000A, /* LINE FEED */ |
| 9342 | 0x000D, /* CARRIAGE RETURN */ |
| 9343 | 0x001C, /* FILE SEPARATOR */ |
| 9344 | 0x001D, /* GROUP SEPARATOR */ |
| 9345 | 0x001E, /* RECORD SEPARATOR */ |
| 9346 | 0x0085, /* NEXT LINE */ |
| 9347 | 0x2028, /* LINE SEPARATOR */ |
| 9348 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 9349 | }; |
| 9350 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 9351 | /* Init the implementation */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 9352 | free_list = NULL; |
| 9353 | numfree = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9354 | unicode_empty = _PyUnicode_New(0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9355 | if (!unicode_empty) |
| 9356 | return; |
| 9357 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9358 | for (i = 0; i < 256; i++) |
| 9359 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 9360 | if (PyType_Ready(&PyUnicode_Type) < 0) |
| 9361 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9362 | |
| 9363 | /* initialize the linebreak bloom filter */ |
| 9364 | bloom_linebreak = make_bloom_mask( |
| 9365 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 9366 | ); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9367 | |
| 9368 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9369 | } |
| 9370 | |
| 9371 | /* Finalize the Unicode implementation */ |
| 9372 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9373 | int |
| 9374 | PyUnicode_ClearFreeList(void) |
| 9375 | { |
| 9376 | int freelist_size = numfree; |
| 9377 | PyUnicodeObject *u; |
| 9378 | |
| 9379 | for (u = free_list; u != NULL;) { |
| 9380 | PyUnicodeObject *v = u; |
| 9381 | u = *(PyUnicodeObject **)u; |
| 9382 | if (v->str) |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 9383 | PyObject_DEL(v->str); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9384 | Py_XDECREF(v->defenc); |
| 9385 | PyObject_Del(v); |
| 9386 | numfree--; |
| 9387 | } |
| 9388 | free_list = NULL; |
| 9389 | assert(numfree == 0); |
| 9390 | return freelist_size; |
| 9391 | } |
| 9392 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9393 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9394 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9395 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9396 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9397 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 9398 | Py_XDECREF(unicode_empty); |
| 9399 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 9400 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9401 | for (i = 0; i < 256; i++) { |
| 9402 | if (unicode_latin1[i]) { |
| 9403 | Py_DECREF(unicode_latin1[i]); |
| 9404 | unicode_latin1[i] = NULL; |
| 9405 | } |
| 9406 | } |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9407 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9408 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9409 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9410 | void |
| 9411 | PyUnicode_InternInPlace(PyObject **p) |
| 9412 | { |
| 9413 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 9414 | PyObject *t; |
| 9415 | if (s == NULL || !PyUnicode_Check(s)) |
| 9416 | Py_FatalError( |
| 9417 | "PyUnicode_InternInPlace: unicode strings only please!"); |
| 9418 | /* If it's a subclass, we don't really know what putting |
| 9419 | it in the interned dict might do. */ |
| 9420 | if (!PyUnicode_CheckExact(s)) |
| 9421 | return; |
| 9422 | if (PyUnicode_CHECK_INTERNED(s)) |
| 9423 | return; |
| 9424 | if (interned == NULL) { |
| 9425 | interned = PyDict_New(); |
| 9426 | if (interned == NULL) { |
| 9427 | PyErr_Clear(); /* Don't leave an exception */ |
| 9428 | return; |
| 9429 | } |
| 9430 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9431 | /* It might be that the GetItem call fails even |
| 9432 | though the key is present in the dictionary, |
| 9433 | namely when this happens during a stack overflow. */ |
| 9434 | Py_ALLOW_RECURSION |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9435 | t = PyDict_GetItem(interned, (PyObject *)s); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9436 | Py_END_ALLOW_RECURSION |
| 9437 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9438 | if (t) { |
| 9439 | Py_INCREF(t); |
| 9440 | Py_DECREF(*p); |
| 9441 | *p = t; |
| 9442 | return; |
| 9443 | } |
| 9444 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9445 | PyThreadState_GET()->recursion_critical = 1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9446 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 9447 | PyErr_Clear(); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9448 | PyThreadState_GET()->recursion_critical = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9449 | return; |
| 9450 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9451 | PyThreadState_GET()->recursion_critical = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9452 | /* The two references in interned are not counted by refcnt. |
| 9453 | The deallocator will take care of this */ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 9454 | Py_REFCNT(s) -= 2; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9455 | PyUnicode_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL; |
| 9456 | } |
| 9457 | |
| 9458 | void |
| 9459 | PyUnicode_InternImmortal(PyObject **p) |
| 9460 | { |
| 9461 | PyUnicode_InternInPlace(p); |
| 9462 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
| 9463 | PyUnicode_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL; |
| 9464 | Py_INCREF(*p); |
| 9465 | } |
| 9466 | } |
| 9467 | |
| 9468 | PyObject * |
| 9469 | PyUnicode_InternFromString(const char *cp) |
| 9470 | { |
| 9471 | PyObject *s = PyUnicode_FromString(cp); |
| 9472 | if (s == NULL) |
| 9473 | return NULL; |
| 9474 | PyUnicode_InternInPlace(&s); |
| 9475 | return s; |
| 9476 | } |
| 9477 | |
| 9478 | void _Py_ReleaseInternedUnicodeStrings(void) |
| 9479 | { |
| 9480 | PyObject *keys; |
| 9481 | PyUnicodeObject *s; |
| 9482 | Py_ssize_t i, n; |
| 9483 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
| 9484 | |
| 9485 | if (interned == NULL || !PyDict_Check(interned)) |
| 9486 | return; |
| 9487 | keys = PyDict_Keys(interned); |
| 9488 | if (keys == NULL || !PyList_Check(keys)) { |
| 9489 | PyErr_Clear(); |
| 9490 | return; |
| 9491 | } |
| 9492 | |
| 9493 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 9494 | detector, interned unicode strings are not forcibly deallocated; |
| 9495 | rather, we give them their stolen references back, and then clear |
| 9496 | and DECREF the interned dict. */ |
| 9497 | |
| 9498 | n = PyList_GET_SIZE(keys); |
| 9499 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
| 9500 | n); |
| 9501 | for (i = 0; i < n; i++) { |
| 9502 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
| 9503 | switch (s->state) { |
| 9504 | case SSTATE_NOT_INTERNED: |
| 9505 | /* XXX Shouldn't happen */ |
| 9506 | break; |
| 9507 | case SSTATE_INTERNED_IMMORTAL: |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 9508 | Py_REFCNT(s) += 1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9509 | immortal_size += s->length; |
| 9510 | break; |
| 9511 | case SSTATE_INTERNED_MORTAL: |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 9512 | Py_REFCNT(s) += 2; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9513 | mortal_size += s->length; |
| 9514 | break; |
| 9515 | default: |
| 9516 | Py_FatalError("Inconsistent interned string state."); |
| 9517 | } |
| 9518 | s->state = SSTATE_NOT_INTERNED; |
| 9519 | } |
| 9520 | fprintf(stderr, "total size of all interned strings: " |
| 9521 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 9522 | "mortal/immortal\n", mortal_size, immortal_size); |
| 9523 | Py_DECREF(keys); |
| 9524 | PyDict_Clear(interned); |
| 9525 | Py_DECREF(interned); |
| 9526 | interned = NULL; |
| 9527 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9528 | |
| 9529 | |
| 9530 | /********************* Unicode Iterator **************************/ |
| 9531 | |
| 9532 | typedef struct { |
| 9533 | PyObject_HEAD |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9534 | Py_ssize_t it_index; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9535 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
| 9536 | } unicodeiterobject; |
| 9537 | |
| 9538 | static void |
| 9539 | unicodeiter_dealloc(unicodeiterobject *it) |
| 9540 | { |
| 9541 | _PyObject_GC_UNTRACK(it); |
| 9542 | Py_XDECREF(it->it_seq); |
| 9543 | PyObject_GC_Del(it); |
| 9544 | } |
| 9545 | |
| 9546 | static int |
| 9547 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 9548 | { |
| 9549 | Py_VISIT(it->it_seq); |
| 9550 | return 0; |
| 9551 | } |
| 9552 | |
| 9553 | static PyObject * |
| 9554 | unicodeiter_next(unicodeiterobject *it) |
| 9555 | { |
| 9556 | PyUnicodeObject *seq; |
| 9557 | PyObject *item; |
| 9558 | |
| 9559 | assert(it != NULL); |
| 9560 | seq = it->it_seq; |
| 9561 | if (seq == NULL) |
| 9562 | return NULL; |
| 9563 | assert(PyUnicode_Check(seq)); |
| 9564 | |
| 9565 | if (it->it_index < PyUnicode_GET_SIZE(seq)) { |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9566 | item = PyUnicode_FromUnicode( |
| 9567 | PyUnicode_AS_UNICODE(seq)+it->it_index, 1); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9568 | if (item != NULL) |
| 9569 | ++it->it_index; |
| 9570 | return item; |
| 9571 | } |
| 9572 | |
| 9573 | Py_DECREF(seq); |
| 9574 | it->it_seq = NULL; |
| 9575 | return NULL; |
| 9576 | } |
| 9577 | |
| 9578 | static PyObject * |
| 9579 | unicodeiter_len(unicodeiterobject *it) |
| 9580 | { |
| 9581 | Py_ssize_t len = 0; |
| 9582 | if (it->it_seq) |
| 9583 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9584 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9585 | } |
| 9586 | |
| 9587 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 9588 | |
| 9589 | static PyMethodDef unicodeiter_methods[] = { |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9590 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
| 9591 | length_hint_doc}, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9592 | {NULL, NULL} /* sentinel */ |
| 9593 | }; |
| 9594 | |
| 9595 | PyTypeObject PyUnicodeIter_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 9596 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Christian Heimes | f83be4e | 2007-11-28 09:44:38 +0000 | [diff] [blame] | 9597 | "str_iterator", /* tp_name */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9598 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 9599 | 0, /* tp_itemsize */ |
| 9600 | /* methods */ |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9601 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9602 | 0, /* tp_print */ |
| 9603 | 0, /* tp_getattr */ |
| 9604 | 0, /* tp_setattr */ |
| 9605 | 0, /* tp_compare */ |
| 9606 | 0, /* tp_repr */ |
| 9607 | 0, /* tp_as_number */ |
| 9608 | 0, /* tp_as_sequence */ |
| 9609 | 0, /* tp_as_mapping */ |
| 9610 | 0, /* tp_hash */ |
| 9611 | 0, /* tp_call */ |
| 9612 | 0, /* tp_str */ |
| 9613 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9614 | 0, /* tp_setattro */ |
| 9615 | 0, /* tp_as_buffer */ |
| 9616 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 9617 | 0, /* tp_doc */ |
| 9618 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 9619 | 0, /* tp_clear */ |
| 9620 | 0, /* tp_richcompare */ |
| 9621 | 0, /* tp_weaklistoffset */ |
| 9622 | PyObject_SelfIter, /* tp_iter */ |
| 9623 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 9624 | unicodeiter_methods, /* tp_methods */ |
| 9625 | 0, |
| 9626 | }; |
| 9627 | |
| 9628 | static PyObject * |
| 9629 | unicode_iter(PyObject *seq) |
| 9630 | { |
| 9631 | unicodeiterobject *it; |
| 9632 | |
| 9633 | if (!PyUnicode_Check(seq)) { |
| 9634 | PyErr_BadInternalCall(); |
| 9635 | return NULL; |
| 9636 | } |
| 9637 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 9638 | if (it == NULL) |
| 9639 | return NULL; |
| 9640 | it->it_index = 0; |
| 9641 | Py_INCREF(seq); |
| 9642 | it->it_seq = (PyUnicodeObject *)seq; |
| 9643 | _PyObject_GC_TRACK(it); |
| 9644 | return (PyObject *)it; |
| 9645 | } |
| 9646 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9647 | size_t |
| 9648 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 9649 | { |
| 9650 | int res = 0; |
| 9651 | while(*u++) |
| 9652 | res++; |
| 9653 | return res; |
| 9654 | } |
| 9655 | |
| 9656 | Py_UNICODE* |
| 9657 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9658 | { |
| 9659 | Py_UNICODE *u = s1; |
| 9660 | while ((*u++ = *s2++)); |
| 9661 | return s1; |
| 9662 | } |
| 9663 | |
| 9664 | Py_UNICODE* |
| 9665 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 9666 | { |
| 9667 | Py_UNICODE *u = s1; |
| 9668 | while ((*u++ = *s2++)) |
| 9669 | if (n-- == 0) |
| 9670 | break; |
| 9671 | return s1; |
| 9672 | } |
| 9673 | |
| 9674 | int |
| 9675 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9676 | { |
| 9677 | while (*s1 && *s2 && *s1 == *s2) |
| 9678 | s1++, s2++; |
| 9679 | if (*s1 && *s2) |
| 9680 | return (*s1 < *s2) ? -1 : +1; |
| 9681 | if (*s1) |
| 9682 | return 1; |
| 9683 | if (*s2) |
| 9684 | return -1; |
| 9685 | return 0; |
| 9686 | } |
| 9687 | |
| 9688 | Py_UNICODE* |
| 9689 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 9690 | { |
| 9691 | const Py_UNICODE *p; |
| 9692 | for (p = s; *p; p++) |
| 9693 | if (*p == c) |
| 9694 | return (Py_UNICODE*)p; |
| 9695 | return NULL; |
| 9696 | } |
| 9697 | |
| 9698 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9699 | #ifdef __cplusplus |
| 9700 | } |
| 9701 | #endif |
| 9702 | |
| 9703 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9704 | /* |
| 9705 | Local variables: |
| 9706 | c-basic-offset: 4 |
| 9707 | indent-tabs-mode: nil |
| 9708 | End: |
| 9709 | */ |