Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1 | /* |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2 | |
| 3 | Unicode implementation based on original code by Fredrik Lundh, |
Fred Drake | 785d14f | 2000-05-09 19:54:43 +0000 | [diff] [blame] | 4 | modified by Marc-Andre Lemburg <mal@lemburg.com> according to the |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5 | Unicode Integration Proposal (see file Misc/unicode.txt). |
| 6 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7 | Major speed upgrades to the method implementations at the Reykjavik |
| 8 | NeedForSpeed sprint, by Fredrik Lundh and Andrew Dalke. |
| 9 | |
Guido van Rossum | 16b1ad9 | 2000-08-03 16:24:25 +0000 | [diff] [blame] | 10 | Copyright (c) Corporation for National Research Initiatives. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 12 | -------------------------------------------------------------------- |
| 13 | The original string type implementation is: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 15 | Copyright (c) 1999 by Secret Labs AB |
| 16 | Copyright (c) 1999 by Fredrik Lundh |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 17 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 18 | By obtaining, using, and/or copying this software and/or its |
| 19 | associated documentation, you agree that you have read, understood, |
| 20 | and will comply with the following terms and conditions: |
| 21 | |
| 22 | Permission to use, copy, modify, and distribute this software and its |
| 23 | associated documentation for any purpose and without fee is hereby |
| 24 | granted, provided that the above copyright notice appears in all |
| 25 | copies, and that both that copyright notice and this permission notice |
| 26 | appear in supporting documentation, and that the name of Secret Labs |
| 27 | AB or the author not be used in advertising or publicity pertaining to |
| 28 | distribution of the software without specific, written prior |
| 29 | permission. |
| 30 | |
| 31 | SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 32 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 33 | FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR |
| 34 | ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 35 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 36 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 37 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 38 | -------------------------------------------------------------------- |
| 39 | |
| 40 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 41 | |
Martin v. Löwis | 5cb6936 | 2006-04-14 09:08:42 +0000 | [diff] [blame] | 42 | #define PY_SSIZE_T_CLEAN |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 43 | #include "Python.h" |
| 44 | |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 45 | #include "formatter_unicode.h" |
| 46 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 47 | #include "unicodeobject.h" |
Marc-André Lemburg | d49e5b4 | 2000-06-30 14:58:20 +0000 | [diff] [blame] | 48 | #include "ucnhash.h" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 49 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 50 | #ifdef MS_WINDOWS |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 51 | #include <windows.h> |
| 52 | #endif |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 53 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 54 | /* Limit for the Unicode object free list */ |
| 55 | |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 56 | #define PyUnicode_MAXFREELIST 1024 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 57 | |
| 58 | /* Limit for the Unicode object free list stay alive optimization. |
| 59 | |
| 60 | The implementation will keep allocated Unicode memory intact for |
| 61 | all objects on the free list having a size less than this |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 62 | limit. This reduces malloc() overhead for small Unicode objects. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 63 | |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 64 | At worst this will result in PyUnicode_MAXFREELIST * |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 65 | (sizeof(PyUnicodeObject) + KEEPALIVE_SIZE_LIMIT + |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 66 | malloc()-overhead) bytes of unused garbage. |
| 67 | |
| 68 | Setting the limit to 0 effectively turns the feature off. |
| 69 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 70 | Note: This is an experimental feature ! If you get core dumps when |
| 71 | using Unicode objects, turn this feature off. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 72 | |
| 73 | */ |
| 74 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 75 | #define KEEPALIVE_SIZE_LIMIT 9 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 76 | |
| 77 | /* Endianness switches; defaults to little endian */ |
| 78 | |
| 79 | #ifdef WORDS_BIGENDIAN |
| 80 | # define BYTEORDER_IS_BIG_ENDIAN |
| 81 | #else |
| 82 | # define BYTEORDER_IS_LITTLE_ENDIAN |
| 83 | #endif |
| 84 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 85 | /* --- Globals ------------------------------------------------------------ |
| 86 | |
| 87 | The globals are initialized by the _PyUnicode_Init() API and should |
| 88 | not be used before calling that API. |
| 89 | |
| 90 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 91 | |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 92 | |
| 93 | #ifdef __cplusplus |
| 94 | extern "C" { |
| 95 | #endif |
| 96 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 97 | /* Free list for Unicode objects */ |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 98 | static PyUnicodeObject *free_list; |
| 99 | static int numfree; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 100 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 101 | /* The empty Unicode object is shared to improve performance. */ |
| 102 | static PyUnicodeObject *unicode_empty; |
| 103 | |
| 104 | /* Single character Unicode strings in the Latin-1 range are being |
| 105 | shared as well. */ |
| 106 | static PyUnicodeObject *unicode_latin1[256]; |
| 107 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 108 | /* Default encoding to use and assume when NULL is passed as encoding |
| 109 | parameter; it is initialized by _PyUnicode_Init(). |
| 110 | |
| 111 | Always use the PyUnicode_SetDefaultEncoding() and |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 112 | PyUnicode_GetDefaultEncoding() APIs to access this global. |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 113 | |
| 114 | */ |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 115 | static char unicode_default_encoding[100]; |
| 116 | |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 117 | /* Fast detection of the most frequent whitespace characters */ |
| 118 | const unsigned char _Py_ascii_whitespace[] = { |
| 119 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 120 | // case 0x0009: /* HORIZONTAL TABULATION */ |
| 121 | // case 0x000A: /* LINE FEED */ |
| 122 | // case 0x000B: /* VERTICAL TABULATION */ |
| 123 | // case 0x000C: /* FORM FEED */ |
| 124 | // case 0x000D: /* CARRIAGE RETURN */ |
| 125 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 126 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 127 | // case 0x001C: /* FILE SEPARATOR */ |
| 128 | // case 0x001D: /* GROUP SEPARATOR */ |
| 129 | // case 0x001E: /* RECORD SEPARATOR */ |
| 130 | // case 0x001F: /* UNIT SEPARATOR */ |
| 131 | 0, 0, 0, 0, 1, 1, 1, 1, |
| 132 | // case 0x0020: /* SPACE */ |
| 133 | 1, 0, 0, 0, 0, 0, 0, 0, |
| 134 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 135 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 136 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 137 | |
| 138 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 139 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 140 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 141 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 142 | 0, 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 | |
| 148 | /* Same for linebreaks */ |
| 149 | static unsigned char ascii_linebreak[] = { |
| 150 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 151 | // 0x000A, /* LINE FEED */ |
| 152 | // 0x000D, /* CARRIAGE RETURN */ |
| 153 | 0, 0, 1, 0, 0, 1, 0, 0, |
| 154 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 155 | // 0x001C, /* FILE SEPARATOR */ |
| 156 | // 0x001D, /* GROUP SEPARATOR */ |
| 157 | // 0x001E, /* RECORD SEPARATOR */ |
| 158 | 0, 0, 0, 0, 1, 1, 1, 0, |
| 159 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 160 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 161 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 162 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 163 | |
| 164 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 165 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 166 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 167 | 0, 0, 0, 0, 0, 0, 0, 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 | |
| 174 | |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 175 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 176 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 177 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 178 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 179 | return 0x10FFFF; |
| 180 | #else |
| 181 | /* This is actually an illegal character, so it should |
| 182 | not be passed to unichr. */ |
| 183 | return 0xFFFF; |
| 184 | #endif |
| 185 | } |
| 186 | |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 187 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 188 | |
| 189 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 190 | to keep things simple, we use a single bitmask, using the least 5 |
| 191 | bits from each unicode characters as the bit index. */ |
| 192 | |
| 193 | /* the linebreak mask is set up by Unicode_Init below */ |
| 194 | |
| 195 | #define BLOOM_MASK unsigned long |
| 196 | |
| 197 | static BLOOM_MASK bloom_linebreak; |
| 198 | |
| 199 | #define BLOOM(mask, ch) ((mask & (1 << ((ch) & 0x1F)))) |
| 200 | |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 201 | #define BLOOM_LINEBREAK(ch) \ |
| 202 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 203 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 204 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 205 | Py_LOCAL_INLINE(BLOOM_MASK) make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len) |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 206 | { |
| 207 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 208 | |
| 209 | long mask; |
| 210 | Py_ssize_t i; |
| 211 | |
| 212 | mask = 0; |
| 213 | for (i = 0; i < len; i++) |
| 214 | mask |= (1 << (ptr[i] & 0x1F)); |
| 215 | |
| 216 | return mask; |
| 217 | } |
| 218 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 219 | Py_LOCAL_INLINE(int) unicode_member(Py_UNICODE chr, Py_UNICODE* set, Py_ssize_t setlen) |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 220 | { |
| 221 | Py_ssize_t i; |
| 222 | |
| 223 | for (i = 0; i < setlen; i++) |
| 224 | if (set[i] == chr) |
| 225 | return 1; |
| 226 | |
Fredrik Lundh | 7763351 | 2006-05-23 19:47:35 +0000 | [diff] [blame] | 227 | return 0; |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | #define BLOOM_MEMBER(mask, chr, set, setlen)\ |
| 231 | BLOOM(mask, chr) && unicode_member(chr, set, setlen) |
| 232 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 233 | /* --- Unicode Object ----------------------------------------------------- */ |
| 234 | |
| 235 | static |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 236 | int unicode_resize(register PyUnicodeObject *unicode, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 237 | Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 238 | { |
| 239 | void *oldstr; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 240 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 241 | /* Shortcut if there's nothing much to do. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 242 | if (unicode->length == length) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 243 | goto reset; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 244 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 245 | /* Resizing shared object (unicode_empty or single character |
| 246 | objects) in-place is not allowed. Use PyUnicode_Resize() |
| 247 | instead ! */ |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 248 | |
Martin v. Löwis | 80d2e59 | 2006-04-13 06:06:08 +0000 | [diff] [blame] | 249 | if (unicode == unicode_empty || |
| 250 | (unicode->length == 1 && |
| 251 | unicode->str[0] < 256U && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 252 | unicode_latin1[unicode->str[0]] == unicode)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 253 | PyErr_SetString(PyExc_SystemError, |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 254 | "can't resize shared unicode objects"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 255 | return -1; |
| 256 | } |
| 257 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 258 | /* We allocate one more byte to make sure the string is Ux0000 terminated. |
| 259 | The overallocation is also used by fastsearch, which assumes that it's |
Andrew M. Kuchling | 07bbfc6 | 2006-05-26 19:51:10 +0000 | [diff] [blame] | 260 | safe to look at str[length] (without making any assumptions about what |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 261 | it contains). */ |
| 262 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 263 | oldstr = unicode->str; |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 264 | unicode->str = PyObject_REALLOC(unicode->str, |
| 265 | sizeof(Py_UNICODE) * (length + 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 266 | if (!unicode->str) { |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 267 | unicode->str = (Py_UNICODE *)oldstr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 268 | PyErr_NoMemory(); |
| 269 | return -1; |
| 270 | } |
| 271 | unicode->str[length] = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 272 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 273 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 274 | reset: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 275 | /* Reset the object caches */ |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 276 | if (unicode->defenc) { |
| 277 | Py_DECREF(unicode->defenc); |
| 278 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 279 | } |
| 280 | unicode->hash = -1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 281 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | /* We allocate one more byte to make sure the string is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 286 | Ux0000 terminated -- XXX is this needed ? |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 287 | |
| 288 | XXX This allocator could further be enhanced by assuring that the |
| 289 | free list never reduces its size below 1. |
| 290 | |
| 291 | */ |
| 292 | |
| 293 | static |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 294 | PyUnicodeObject *_PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 295 | { |
| 296 | register PyUnicodeObject *unicode; |
| 297 | |
Andrew Dalke | e0df762 | 2006-05-27 11:04:36 +0000 | [diff] [blame] | 298 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 299 | if (length == 0 && unicode_empty != NULL) { |
| 300 | Py_INCREF(unicode_empty); |
| 301 | return unicode_empty; |
| 302 | } |
| 303 | |
| 304 | /* Unicode freelist & memory allocation */ |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 305 | if (free_list) { |
| 306 | unicode = free_list; |
| 307 | free_list = *(PyUnicodeObject **)unicode; |
| 308 | numfree--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 309 | if (unicode->str) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 310 | /* Keep-Alive optimization: we only upsize the buffer, |
| 311 | never downsize it. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 312 | if ((unicode->length < length) && |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 313 | unicode_resize(unicode, length) < 0) { |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 314 | PyObject_DEL(unicode->str); |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 315 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 316 | } |
| 317 | } |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 318 | else { |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 319 | size_t new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 320 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 321 | } |
| 322 | PyObject_INIT(unicode, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 323 | } |
| 324 | else { |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 325 | size_t new_size; |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 326 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 327 | if (unicode == NULL) |
| 328 | return NULL; |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 329 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 330 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 333 | if (!unicode->str) { |
| 334 | PyErr_NoMemory(); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 335 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 336 | } |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 337 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 338 | * the caller fails before initializing str -- unicode_resize() |
| 339 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 340 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 341 | * We don't want unicode_resize to read uninitialized memory in |
| 342 | * that case. |
| 343 | */ |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 344 | unicode->str[0] = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 345 | unicode->str[length] = 0; |
Martin v. Löwis | f15da69 | 2006-04-13 07:24:50 +0000 | [diff] [blame] | 346 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 347 | unicode->hash = -1; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 348 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 349 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 350 | |
| 351 | onError: |
| 352 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 353 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 354 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | static |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 358 | void unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 359 | { |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 360 | if (PyUnicode_CheckExact(unicode) && |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 361 | numfree < PyUnicode_MAXFREELIST) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 362 | /* Keep-Alive optimization */ |
| 363 | if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 364 | PyObject_DEL(unicode->str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 365 | unicode->str = NULL; |
| 366 | unicode->length = 0; |
| 367 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 368 | if (unicode->defenc) { |
| 369 | Py_DECREF(unicode->defenc); |
| 370 | unicode->defenc = NULL; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 371 | } |
| 372 | /* Add to free list */ |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 373 | *(PyUnicodeObject **)unicode = free_list; |
| 374 | free_list = unicode; |
| 375 | numfree++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 376 | } |
| 377 | else { |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 378 | PyObject_DEL(unicode->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 379 | Py_XDECREF(unicode->defenc); |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 380 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 384 | int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 385 | { |
| 386 | register PyUnicodeObject *v; |
| 387 | |
| 388 | /* Argument checks */ |
| 389 | if (unicode == NULL) { |
| 390 | PyErr_BadInternalCall(); |
| 391 | return -1; |
| 392 | } |
| 393 | v = (PyUnicodeObject *)*unicode; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 394 | 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] | 395 | PyErr_BadInternalCall(); |
| 396 | return -1; |
| 397 | } |
| 398 | |
| 399 | /* Resizing unicode_empty and single character objects is not |
| 400 | possible since these are being shared. We simply return a fresh |
| 401 | copy with the same Unicode content. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 402 | if (v->length != length && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 403 | (v == unicode_empty || v->length == 1)) { |
| 404 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 405 | if (w == NULL) |
| 406 | return -1; |
| 407 | Py_UNICODE_COPY(w->str, v->str, |
| 408 | length < v->length ? length : v->length); |
Raymond Hettinger | c8df578 | 2003-03-09 07:30:43 +0000 | [diff] [blame] | 409 | Py_DECREF(*unicode); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 410 | *unicode = (PyObject *)w; |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 415 | objects, since we can modify them in-place. */ |
| 416 | return unicode_resize(v, length); |
| 417 | } |
| 418 | |
| 419 | /* Internal API for use in unicodeobject.c only ! */ |
| 420 | #define _PyUnicode_Resize(unicodevar, length) \ |
| 421 | PyUnicode_Resize(((PyObject **)(unicodevar)), length) |
| 422 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 423 | PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 424 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 425 | { |
| 426 | PyUnicodeObject *unicode; |
| 427 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 428 | /* If the Unicode data is known at construction time, we can apply |
| 429 | some optimizations which share commonly used objects. */ |
| 430 | if (u != NULL) { |
| 431 | |
| 432 | /* Optimization for empty strings */ |
| 433 | if (size == 0 && unicode_empty != NULL) { |
| 434 | Py_INCREF(unicode_empty); |
| 435 | return (PyObject *)unicode_empty; |
| 436 | } |
| 437 | |
| 438 | /* Single character Unicode objects in the Latin-1 range are |
| 439 | shared when using this constructor */ |
| 440 | if (size == 1 && *u < 256) { |
| 441 | unicode = unicode_latin1[*u]; |
| 442 | if (!unicode) { |
| 443 | unicode = _PyUnicode_New(1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 444 | if (!unicode) |
| 445 | return NULL; |
Marc-André Lemburg | 8879a33 | 2001-06-07 12:26:56 +0000 | [diff] [blame] | 446 | unicode->str[0] = *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 447 | unicode_latin1[*u] = unicode; |
| 448 | } |
| 449 | Py_INCREF(unicode); |
| 450 | return (PyObject *)unicode; |
| 451 | } |
| 452 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 453 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 454 | unicode = _PyUnicode_New(size); |
| 455 | if (!unicode) |
| 456 | return NULL; |
| 457 | |
| 458 | /* Copy the Unicode data into the new object */ |
| 459 | if (u != NULL) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 460 | Py_UNICODE_COPY(unicode->str, u, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 461 | |
| 462 | return (PyObject *)unicode; |
| 463 | } |
| 464 | |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 465 | PyObject *PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
| 466 | { |
| 467 | PyUnicodeObject *unicode; |
Gregory P. Smith | c00eb73 | 2008-04-09 23:16:37 +0000 | [diff] [blame] | 468 | |
Gregory P. Smith | c00eb73 | 2008-04-09 23:16:37 +0000 | [diff] [blame] | 469 | if (size < 0) { |
| 470 | PyErr_SetString(PyExc_SystemError, |
| 471 | "Negative size passed to PyUnicode_FromStringAndSize"); |
| 472 | return NULL; |
| 473 | } |
| 474 | |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 475 | /* If the Unicode data is known at construction time, we can apply |
| 476 | some optimizations which share commonly used objects. |
| 477 | Also, this means the input must be UTF-8, so fall back to the |
| 478 | UTF-8 decoder at the end. */ |
| 479 | if (u != NULL) { |
| 480 | |
| 481 | /* Optimization for empty strings */ |
| 482 | if (size == 0 && unicode_empty != NULL) { |
| 483 | Py_INCREF(unicode_empty); |
| 484 | return (PyObject *)unicode_empty; |
| 485 | } |
| 486 | |
| 487 | /* Single characters are shared when using this constructor. |
| 488 | Restrict to ASCII, since the input must be UTF-8. */ |
| 489 | if (size == 1 && Py_CHARMASK(*u) < 128) { |
Neal Norwitz | d183bdd | 2008-03-28 04:58:51 +0000 | [diff] [blame] | 490 | unicode = unicode_latin1[Py_CHARMASK(*u)]; |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 491 | if (!unicode) { |
| 492 | unicode = _PyUnicode_New(1); |
| 493 | if (!unicode) |
| 494 | return NULL; |
| 495 | unicode->str[0] = Py_CHARMASK(*u); |
Neal Norwitz | d183bdd | 2008-03-28 04:58:51 +0000 | [diff] [blame] | 496 | unicode_latin1[Py_CHARMASK(*u)] = unicode; |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 497 | } |
| 498 | Py_INCREF(unicode); |
| 499 | return (PyObject *)unicode; |
| 500 | } |
| 501 | |
| 502 | return PyUnicode_DecodeUTF8(u, size, NULL); |
| 503 | } |
| 504 | |
| 505 | unicode = _PyUnicode_New(size); |
| 506 | if (!unicode) |
| 507 | return NULL; |
| 508 | |
| 509 | return (PyObject *)unicode; |
| 510 | } |
| 511 | |
| 512 | PyObject *PyUnicode_FromString(const char *u) |
| 513 | { |
| 514 | size_t size = strlen(u); |
| 515 | if (size > PY_SSIZE_T_MAX) { |
| 516 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 517 | return NULL; |
| 518 | } |
| 519 | |
| 520 | return PyUnicode_FromStringAndSize(u, size); |
| 521 | } |
| 522 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 523 | #ifdef HAVE_WCHAR_H |
| 524 | |
| 525 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 526 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 527 | { |
| 528 | PyUnicodeObject *unicode; |
| 529 | |
| 530 | if (w == NULL) { |
| 531 | PyErr_BadInternalCall(); |
| 532 | return NULL; |
| 533 | } |
| 534 | |
| 535 | unicode = _PyUnicode_New(size); |
| 536 | if (!unicode) |
| 537 | return NULL; |
| 538 | |
| 539 | /* Copy the wchar_t data into the new object */ |
| 540 | #ifdef HAVE_USABLE_WCHAR_T |
| 541 | memcpy(unicode->str, w, size * sizeof(wchar_t)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 542 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 543 | { |
| 544 | register Py_UNICODE *u; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 545 | register Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 546 | u = PyUnicode_AS_UNICODE(unicode); |
Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 547 | for (i = size; i > 0; i--) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 548 | *u++ = *w++; |
| 549 | } |
| 550 | #endif |
| 551 | |
| 552 | return (PyObject *)unicode; |
| 553 | } |
| 554 | |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 555 | static void |
| 556 | makefmt(char *fmt, int longflag, int size_tflag, int zeropad, int width, int precision, char c) |
| 557 | { |
| 558 | *fmt++ = '%'; |
| 559 | if (width) { |
| 560 | if (zeropad) |
| 561 | *fmt++ = '0'; |
| 562 | fmt += sprintf(fmt, "%d", width); |
| 563 | } |
| 564 | if (precision) |
| 565 | fmt += sprintf(fmt, ".%d", precision); |
| 566 | if (longflag) |
| 567 | *fmt++ = 'l'; |
| 568 | else if (size_tflag) { |
| 569 | char *f = PY_FORMAT_SIZE_T; |
| 570 | while (*f) |
| 571 | *fmt++ = *f++; |
| 572 | } |
| 573 | *fmt++ = c; |
| 574 | *fmt = '\0'; |
| 575 | } |
| 576 | |
| 577 | #define appendstring(string) {for (copy = string;*copy;) *s++ = *copy++;} |
| 578 | |
| 579 | PyObject * |
| 580 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 581 | { |
| 582 | va_list count; |
| 583 | Py_ssize_t callcount = 0; |
| 584 | PyObject **callresults = NULL; |
| 585 | PyObject **callresult = NULL; |
| 586 | Py_ssize_t n = 0; |
| 587 | int width = 0; |
| 588 | int precision = 0; |
| 589 | int zeropad; |
| 590 | const char* f; |
| 591 | Py_UNICODE *s; |
| 592 | PyObject *string; |
| 593 | /* used by sprintf */ |
| 594 | char buffer[21]; |
| 595 | /* use abuffer instead of buffer, if we need more space |
| 596 | * (which can happen if there's a format specifier with width). */ |
| 597 | char *abuffer = NULL; |
| 598 | char *realbuffer; |
| 599 | Py_ssize_t abuffersize = 0; |
| 600 | char fmt[60]; /* should be enough for %0width.precisionld */ |
| 601 | const char *copy; |
| 602 | |
| 603 | #ifdef VA_LIST_IS_ARRAY |
| 604 | Py_MEMCPY(count, vargs, sizeof(va_list)); |
| 605 | #else |
| 606 | #ifdef __va_copy |
| 607 | __va_copy(count, vargs); |
| 608 | #else |
| 609 | count = vargs; |
| 610 | #endif |
| 611 | #endif |
| 612 | /* step 1: count the number of %S/%R format specifications |
| 613 | * (we call PyObject_Str()/PyObject_Repr() for these objects |
| 614 | * once during step 3 and put the result in an array) */ |
| 615 | for (f = format; *f; f++) { |
| 616 | if (*f == '%' && (*(f+1)=='S' || *(f+1)=='R')) |
| 617 | ++callcount; |
| 618 | } |
| 619 | /* step 2: allocate memory for the results of |
| 620 | * PyObject_Str()/PyObject_Repr() calls */ |
| 621 | if (callcount) { |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 622 | callresults = PyObject_Malloc(sizeof(PyObject *)*callcount); |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 623 | if (!callresults) { |
| 624 | PyErr_NoMemory(); |
| 625 | return NULL; |
| 626 | } |
| 627 | callresult = callresults; |
| 628 | } |
| 629 | /* step 3: figure out how large a buffer we need */ |
| 630 | for (f = format; *f; f++) { |
| 631 | if (*f == '%') { |
| 632 | const char* p = f; |
| 633 | width = 0; |
Neal Norwitz | ade57d0 | 2008-03-23 06:19:57 +0000 | [diff] [blame] | 634 | while (isdigit((unsigned)*f)) |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 635 | width = (width*10) + *f++ - '0'; |
Neal Norwitz | ade57d0 | 2008-03-23 06:19:57 +0000 | [diff] [blame] | 636 | while (*++f && *f != '%' && !isalpha((unsigned)*f)) |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 637 | ; |
| 638 | |
| 639 | /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
| 640 | * they don't affect the amount of space we reserve. |
| 641 | */ |
| 642 | if ((*f == 'l' || *f == 'z') && |
| 643 | (f[1] == 'd' || f[1] == 'u')) |
| 644 | ++f; |
| 645 | |
| 646 | switch (*f) { |
| 647 | case 'c': |
| 648 | (void)va_arg(count, int); |
| 649 | /* fall through... */ |
| 650 | case '%': |
| 651 | n++; |
| 652 | break; |
| 653 | case 'd': case 'u': case 'i': case 'x': |
| 654 | (void) va_arg(count, int); |
| 655 | /* 20 bytes is enough to hold a 64-bit |
| 656 | integer. Decimal takes the most space. |
| 657 | This isn't enough for octal. |
| 658 | If a width is specified we need more |
| 659 | (which we allocate later). */ |
| 660 | if (width < 20) |
| 661 | width = 20; |
| 662 | n += width; |
| 663 | if (abuffersize < width) |
| 664 | abuffersize = width; |
| 665 | break; |
| 666 | case 's': |
| 667 | { |
| 668 | /* UTF-8 */ |
| 669 | unsigned char*s; |
| 670 | s = va_arg(count, unsigned char*); |
| 671 | while (*s) { |
| 672 | if (*s < 128) { |
| 673 | n++; s++; |
| 674 | } else if (*s < 0xc0) { |
| 675 | /* invalid UTF-8 */ |
| 676 | n++; s++; |
| 677 | } else if (*s < 0xc0) { |
| 678 | n++; |
| 679 | s++; if(!*s)break; |
| 680 | s++; |
| 681 | } else if (*s < 0xe0) { |
| 682 | n++; |
| 683 | s++; if(!*s)break; |
| 684 | s++; if(!*s)break; |
| 685 | s++; |
| 686 | } else { |
| 687 | #ifdef Py_UNICODE_WIDE |
| 688 | n++; |
| 689 | #else |
| 690 | n+=2; |
| 691 | #endif |
| 692 | s++; if(!*s)break; |
| 693 | s++; if(!*s)break; |
| 694 | s++; if(!*s)break; |
| 695 | s++; |
| 696 | } |
| 697 | } |
| 698 | break; |
| 699 | } |
| 700 | case 'U': |
| 701 | { |
| 702 | PyObject *obj = va_arg(count, PyObject *); |
| 703 | assert(obj && PyUnicode_Check(obj)); |
| 704 | n += PyUnicode_GET_SIZE(obj); |
| 705 | break; |
| 706 | } |
| 707 | case 'V': |
| 708 | { |
| 709 | PyObject *obj = va_arg(count, PyObject *); |
| 710 | const char *str = va_arg(count, const char *); |
| 711 | assert(obj || str); |
| 712 | assert(!obj || PyUnicode_Check(obj)); |
| 713 | if (obj) |
| 714 | n += PyUnicode_GET_SIZE(obj); |
| 715 | else |
| 716 | n += strlen(str); |
| 717 | break; |
| 718 | } |
| 719 | case 'S': |
| 720 | { |
| 721 | PyObject *obj = va_arg(count, PyObject *); |
| 722 | PyObject *str; |
| 723 | assert(obj); |
| 724 | str = PyObject_Str(obj); |
| 725 | if (!str) |
| 726 | goto fail; |
| 727 | n += PyUnicode_GET_SIZE(str); |
| 728 | /* Remember the str and switch to the next slot */ |
| 729 | *callresult++ = str; |
| 730 | break; |
| 731 | } |
| 732 | case 'R': |
| 733 | { |
| 734 | PyObject *obj = va_arg(count, PyObject *); |
| 735 | PyObject *repr; |
| 736 | assert(obj); |
| 737 | repr = PyObject_Repr(obj); |
| 738 | if (!repr) |
| 739 | goto fail; |
| 740 | n += PyUnicode_GET_SIZE(repr); |
| 741 | /* Remember the repr and switch to the next slot */ |
| 742 | *callresult++ = repr; |
| 743 | break; |
| 744 | } |
| 745 | case 'p': |
| 746 | (void) va_arg(count, int); |
| 747 | /* maximum 64-bit pointer representation: |
| 748 | * 0xffffffffffffffff |
| 749 | * so 19 characters is enough. |
| 750 | * XXX I count 18 -- what's the extra for? |
| 751 | */ |
| 752 | n += 19; |
| 753 | break; |
| 754 | default: |
| 755 | /* if we stumble upon an unknown |
| 756 | formatting code, copy the rest of |
| 757 | the format string to the output |
| 758 | string. (we cannot just skip the |
| 759 | code, since there's no way to know |
| 760 | what's in the argument list) */ |
| 761 | n += strlen(p); |
| 762 | goto expand; |
| 763 | } |
| 764 | } else |
| 765 | n++; |
| 766 | } |
| 767 | expand: |
| 768 | if (abuffersize > 20) { |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 769 | abuffer = PyObject_Malloc(abuffersize); |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 770 | if (!abuffer) { |
| 771 | PyErr_NoMemory(); |
| 772 | goto fail; |
| 773 | } |
| 774 | realbuffer = abuffer; |
| 775 | } |
| 776 | else |
| 777 | realbuffer = buffer; |
| 778 | /* step 4: fill the buffer */ |
| 779 | /* Since we've analyzed how much space we need for the worst case, |
| 780 | we don't have to resize the string. |
| 781 | There can be no errors beyond this point. */ |
| 782 | string = PyUnicode_FromUnicode(NULL, n); |
| 783 | if (!string) |
| 784 | goto fail; |
| 785 | |
| 786 | s = PyUnicode_AS_UNICODE(string); |
| 787 | callresult = callresults; |
| 788 | |
| 789 | for (f = format; *f; f++) { |
| 790 | if (*f == '%') { |
| 791 | const char* p = f++; |
| 792 | int longflag = 0; |
| 793 | int size_tflag = 0; |
| 794 | zeropad = (*f == '0'); |
| 795 | /* parse the width.precision part */ |
| 796 | width = 0; |
Neal Norwitz | ade57d0 | 2008-03-23 06:19:57 +0000 | [diff] [blame] | 797 | while (isdigit((unsigned)*f)) |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 798 | width = (width*10) + *f++ - '0'; |
| 799 | precision = 0; |
| 800 | if (*f == '.') { |
| 801 | f++; |
Neal Norwitz | ade57d0 | 2008-03-23 06:19:57 +0000 | [diff] [blame] | 802 | while (isdigit((unsigned)*f)) |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 803 | precision = (precision*10) + *f++ - '0'; |
| 804 | } |
| 805 | /* handle the long flag, but only for %ld and %lu. |
| 806 | others can be added when necessary. */ |
| 807 | if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) { |
| 808 | longflag = 1; |
| 809 | ++f; |
| 810 | } |
| 811 | /* handle the size_t flag. */ |
| 812 | if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
| 813 | size_tflag = 1; |
| 814 | ++f; |
| 815 | } |
| 816 | |
| 817 | switch (*f) { |
| 818 | case 'c': |
| 819 | *s++ = va_arg(vargs, int); |
| 820 | break; |
| 821 | case 'd': |
| 822 | makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'd'); |
| 823 | if (longflag) |
| 824 | sprintf(realbuffer, fmt, va_arg(vargs, long)); |
| 825 | else if (size_tflag) |
| 826 | sprintf(realbuffer, fmt, va_arg(vargs, Py_ssize_t)); |
| 827 | else |
| 828 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 829 | appendstring(realbuffer); |
| 830 | break; |
| 831 | case 'u': |
| 832 | makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'u'); |
| 833 | if (longflag) |
| 834 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned long)); |
| 835 | else if (size_tflag) |
| 836 | sprintf(realbuffer, fmt, va_arg(vargs, size_t)); |
| 837 | else |
| 838 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned int)); |
| 839 | appendstring(realbuffer); |
| 840 | break; |
| 841 | case 'i': |
| 842 | makefmt(fmt, 0, 0, zeropad, width, precision, 'i'); |
| 843 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 844 | appendstring(realbuffer); |
| 845 | break; |
| 846 | case 'x': |
| 847 | makefmt(fmt, 0, 0, zeropad, width, precision, 'x'); |
| 848 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 849 | appendstring(realbuffer); |
| 850 | break; |
| 851 | case 's': |
| 852 | { |
| 853 | /* Parameter must be UTF-8 encoded. |
| 854 | In case of encoding errors, use |
| 855 | the replacement character. */ |
| 856 | PyObject *u; |
| 857 | p = va_arg(vargs, char*); |
| 858 | u = PyUnicode_DecodeUTF8(p, strlen(p), |
| 859 | "replace"); |
| 860 | if (!u) |
| 861 | goto fail; |
| 862 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(u), |
| 863 | PyUnicode_GET_SIZE(u)); |
| 864 | s += PyUnicode_GET_SIZE(u); |
| 865 | Py_DECREF(u); |
| 866 | break; |
| 867 | } |
| 868 | case 'U': |
| 869 | { |
| 870 | PyObject *obj = va_arg(vargs, PyObject *); |
| 871 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 872 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 873 | s += size; |
| 874 | break; |
| 875 | } |
| 876 | case 'V': |
| 877 | { |
| 878 | PyObject *obj = va_arg(vargs, PyObject *); |
| 879 | const char *str = va_arg(vargs, const char *); |
| 880 | if (obj) { |
| 881 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 882 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 883 | s += size; |
| 884 | } else { |
| 885 | appendstring(str); |
| 886 | } |
| 887 | break; |
| 888 | } |
| 889 | case 'S': |
| 890 | case 'R': |
| 891 | { |
| 892 | Py_UNICODE *ucopy; |
| 893 | Py_ssize_t usize; |
| 894 | Py_ssize_t upos; |
| 895 | /* unused, since we already have the result */ |
| 896 | (void) va_arg(vargs, PyObject *); |
| 897 | ucopy = PyUnicode_AS_UNICODE(*callresult); |
| 898 | usize = PyUnicode_GET_SIZE(*callresult); |
| 899 | for (upos = 0; upos<usize;) |
| 900 | *s++ = ucopy[upos++]; |
| 901 | /* We're done with the unicode()/repr() => forget it */ |
| 902 | Py_DECREF(*callresult); |
| 903 | /* switch to next unicode()/repr() result */ |
| 904 | ++callresult; |
| 905 | break; |
| 906 | } |
| 907 | case 'p': |
| 908 | sprintf(buffer, "%p", va_arg(vargs, void*)); |
| 909 | /* %p is ill-defined: ensure leading 0x. */ |
| 910 | if (buffer[1] == 'X') |
| 911 | buffer[1] = 'x'; |
| 912 | else if (buffer[1] != 'x') { |
| 913 | memmove(buffer+2, buffer, strlen(buffer)+1); |
| 914 | buffer[0] = '0'; |
| 915 | buffer[1] = 'x'; |
| 916 | } |
| 917 | appendstring(buffer); |
| 918 | break; |
| 919 | case '%': |
| 920 | *s++ = '%'; |
| 921 | break; |
| 922 | default: |
| 923 | appendstring(p); |
| 924 | goto end; |
| 925 | } |
| 926 | } else |
| 927 | *s++ = *f; |
| 928 | } |
| 929 | |
| 930 | end: |
| 931 | if (callresults) |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 932 | PyObject_Free(callresults); |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 933 | if (abuffer) |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 934 | PyObject_Free(abuffer); |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 935 | _PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string)); |
| 936 | return string; |
| 937 | fail: |
| 938 | if (callresults) { |
| 939 | PyObject **callresult2 = callresults; |
| 940 | while (callresult2 < callresult) { |
| 941 | Py_DECREF(*callresult2); |
| 942 | ++callresult2; |
| 943 | } |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 944 | PyObject_Free(callresults); |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 945 | } |
| 946 | if (abuffer) |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 947 | PyObject_Free(abuffer); |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 948 | return NULL; |
| 949 | } |
| 950 | |
| 951 | #undef appendstring |
| 952 | |
| 953 | PyObject * |
| 954 | PyUnicode_FromFormat(const char *format, ...) |
| 955 | { |
| 956 | PyObject* ret; |
| 957 | va_list vargs; |
| 958 | |
| 959 | #ifdef HAVE_STDARG_PROTOTYPES |
| 960 | va_start(vargs, format); |
| 961 | #else |
| 962 | va_start(vargs); |
| 963 | #endif |
| 964 | ret = PyUnicode_FromFormatV(format, vargs); |
| 965 | va_end(vargs); |
| 966 | return ret; |
| 967 | } |
| 968 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 969 | Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
| 970 | wchar_t *w, |
| 971 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 972 | { |
| 973 | if (unicode == NULL) { |
| 974 | PyErr_BadInternalCall(); |
| 975 | return -1; |
| 976 | } |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 977 | |
| 978 | /* If possible, try to copy the 0-termination as well */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 979 | if (size > PyUnicode_GET_SIZE(unicode)) |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 980 | size = PyUnicode_GET_SIZE(unicode) + 1; |
| 981 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 982 | #ifdef HAVE_USABLE_WCHAR_T |
| 983 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 984 | #else |
| 985 | { |
| 986 | register Py_UNICODE *u; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 987 | register Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 988 | u = PyUnicode_AS_UNICODE(unicode); |
Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 989 | for (i = size; i > 0; i--) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 990 | *w++ = *u++; |
| 991 | } |
| 992 | #endif |
| 993 | |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 994 | if (size > PyUnicode_GET_SIZE(unicode)) |
| 995 | return PyUnicode_GET_SIZE(unicode); |
| 996 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 997 | return size; |
| 998 | } |
| 999 | |
| 1000 | #endif |
| 1001 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1002 | PyObject *PyUnicode_FromOrdinal(int ordinal) |
| 1003 | { |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 1004 | Py_UNICODE s[1]; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1005 | |
| 1006 | #ifdef Py_UNICODE_WIDE |
| 1007 | if (ordinal < 0 || ordinal > 0x10ffff) { |
| 1008 | PyErr_SetString(PyExc_ValueError, |
| 1009 | "unichr() arg not in range(0x110000) " |
| 1010 | "(wide Python build)"); |
| 1011 | return NULL; |
| 1012 | } |
| 1013 | #else |
| 1014 | if (ordinal < 0 || ordinal > 0xffff) { |
| 1015 | PyErr_SetString(PyExc_ValueError, |
| 1016 | "unichr() arg not in range(0x10000) " |
| 1017 | "(narrow Python build)"); |
| 1018 | return NULL; |
| 1019 | } |
| 1020 | #endif |
| 1021 | |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 1022 | s[0] = (Py_UNICODE)ordinal; |
| 1023 | return PyUnicode_FromUnicode(s, 1); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1026 | PyObject *PyUnicode_FromObject(register PyObject *obj) |
| 1027 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1028 | /* XXX Perhaps we should make this API an alias of |
| 1029 | PyObject_Unicode() instead ?! */ |
| 1030 | if (PyUnicode_CheckExact(obj)) { |
| 1031 | Py_INCREF(obj); |
| 1032 | return obj; |
| 1033 | } |
| 1034 | if (PyUnicode_Check(obj)) { |
| 1035 | /* For a Unicode subtype that's not a Unicode object, |
| 1036 | return a true Unicode object with the same data. */ |
| 1037 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 1038 | PyUnicode_GET_SIZE(obj)); |
| 1039 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1040 | return PyUnicode_FromEncodedObject(obj, NULL, "strict"); |
| 1041 | } |
| 1042 | |
| 1043 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
| 1044 | const char *encoding, |
| 1045 | const char *errors) |
| 1046 | { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1047 | const char *s = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1048 | Py_ssize_t len; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1049 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1050 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1051 | if (obj == NULL) { |
| 1052 | PyErr_BadInternalCall(); |
| 1053 | return NULL; |
| 1054 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1055 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1056 | #if 0 |
| 1057 | /* For b/w compatibility we also accept Unicode objects provided |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 1058 | that no encodings is given and then redirect to |
| 1059 | PyObject_Unicode() which then applies the additional logic for |
| 1060 | Unicode subclasses. |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1061 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1062 | NOTE: This API should really only be used for object which |
| 1063 | represent *encoded* Unicode ! |
| 1064 | |
| 1065 | */ |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1066 | if (PyUnicode_Check(obj)) { |
| 1067 | if (encoding) { |
| 1068 | PyErr_SetString(PyExc_TypeError, |
| 1069 | "decoding Unicode is not supported"); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1070 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1071 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1072 | return PyObject_Unicode(obj); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1073 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1074 | #else |
| 1075 | if (PyUnicode_Check(obj)) { |
| 1076 | PyErr_SetString(PyExc_TypeError, |
| 1077 | "decoding Unicode is not supported"); |
| 1078 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1079 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1080 | #endif |
| 1081 | |
| 1082 | /* Coerce object */ |
| 1083 | if (PyString_Check(obj)) { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1084 | s = PyString_AS_STRING(obj); |
| 1085 | len = PyString_GET_SIZE(obj); |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 1086 | } |
Christian Heimes | 3497f94 | 2008-05-26 12:29:14 +0000 | [diff] [blame] | 1087 | else if (PyByteArray_Check(obj)) { |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 1088 | /* Python 2.x specific */ |
| 1089 | PyErr_Format(PyExc_TypeError, |
| 1090 | "decoding bytearray is not supported"); |
| 1091 | return NULL; |
| 1092 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1093 | else if (PyObject_AsCharBuffer(obj, &s, &len)) { |
| 1094 | /* Overwrite the error message with something more useful in |
| 1095 | case of a TypeError. */ |
| 1096 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1097 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1098 | "coercing to Unicode: need string or buffer, " |
| 1099 | "%.80s found", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1100 | Py_TYPE(obj)->tp_name); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1101 | goto onError; |
| 1102 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1103 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1104 | /* Convert to Unicode */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1105 | if (len == 0) { |
| 1106 | Py_INCREF(unicode_empty); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1107 | v = (PyObject *)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1108 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1109 | else |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1110 | v = PyUnicode_Decode(s, len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 1111 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1112 | return v; |
| 1113 | |
| 1114 | onError: |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1115 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
| 1118 | PyObject *PyUnicode_Decode(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1119 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1120 | const char *encoding, |
| 1121 | const char *errors) |
| 1122 | { |
| 1123 | PyObject *buffer = NULL, *unicode; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1124 | |
| 1125 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1126 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1127 | |
| 1128 | /* Shortcuts for common default encodings */ |
| 1129 | if (strcmp(encoding, "utf-8") == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1130 | return PyUnicode_DecodeUTF8(s, size, errors); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1131 | else if (strcmp(encoding, "latin-1") == 0) |
| 1132 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1133 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 1134 | else if (strcmp(encoding, "mbcs") == 0) |
| 1135 | return PyUnicode_DecodeMBCS(s, size, errors); |
| 1136 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1137 | else if (strcmp(encoding, "ascii") == 0) |
| 1138 | return PyUnicode_DecodeASCII(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1139 | |
| 1140 | /* Decode via the codec registry */ |
| 1141 | buffer = PyBuffer_FromMemory((void *)s, size); |
| 1142 | if (buffer == NULL) |
| 1143 | goto onError; |
| 1144 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 1145 | if (unicode == NULL) |
| 1146 | goto onError; |
| 1147 | if (!PyUnicode_Check(unicode)) { |
| 1148 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 1149 | "decoder did not return an unicode object (type=%.400s)", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1150 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1151 | Py_DECREF(unicode); |
| 1152 | goto onError; |
| 1153 | } |
| 1154 | Py_DECREF(buffer); |
| 1155 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1156 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1157 | onError: |
| 1158 | Py_XDECREF(buffer); |
| 1159 | return NULL; |
| 1160 | } |
| 1161 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1162 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 1163 | const char *encoding, |
| 1164 | const char *errors) |
| 1165 | { |
| 1166 | PyObject *v; |
| 1167 | |
| 1168 | if (!PyUnicode_Check(unicode)) { |
| 1169 | PyErr_BadArgument(); |
| 1170 | goto onError; |
| 1171 | } |
| 1172 | |
| 1173 | if (encoding == NULL) |
| 1174 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1175 | |
| 1176 | /* Decode via the codec registry */ |
| 1177 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1178 | if (v == NULL) |
| 1179 | goto onError; |
| 1180 | return v; |
| 1181 | |
| 1182 | onError: |
| 1183 | return NULL; |
| 1184 | } |
| 1185 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1186 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1187 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1188 | const char *encoding, |
| 1189 | const char *errors) |
| 1190 | { |
| 1191 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1192 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1193 | unicode = PyUnicode_FromUnicode(s, size); |
| 1194 | if (unicode == NULL) |
| 1195 | return NULL; |
| 1196 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 1197 | Py_DECREF(unicode); |
| 1198 | return v; |
| 1199 | } |
| 1200 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1201 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 1202 | const char *encoding, |
| 1203 | const char *errors) |
| 1204 | { |
| 1205 | PyObject *v; |
| 1206 | |
| 1207 | if (!PyUnicode_Check(unicode)) { |
| 1208 | PyErr_BadArgument(); |
| 1209 | goto onError; |
| 1210 | } |
| 1211 | |
| 1212 | if (encoding == NULL) |
| 1213 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1214 | |
| 1215 | /* Encode via the codec registry */ |
| 1216 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1217 | if (v == NULL) |
| 1218 | goto onError; |
| 1219 | return v; |
| 1220 | |
| 1221 | onError: |
| 1222 | return NULL; |
| 1223 | } |
| 1224 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1225 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 1226 | const char *encoding, |
| 1227 | const char *errors) |
| 1228 | { |
| 1229 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1230 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1231 | if (!PyUnicode_Check(unicode)) { |
| 1232 | PyErr_BadArgument(); |
| 1233 | goto onError; |
| 1234 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1235 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1236 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1237 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1238 | |
| 1239 | /* Shortcuts for common default encodings */ |
| 1240 | if (errors == NULL) { |
| 1241 | if (strcmp(encoding, "utf-8") == 0) |
Jeremy Hylton | 9cea41c | 2001-05-29 17:13:15 +0000 | [diff] [blame] | 1242 | return PyUnicode_AsUTF8String(unicode); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1243 | else if (strcmp(encoding, "latin-1") == 0) |
| 1244 | return PyUnicode_AsLatin1String(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1245 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 1246 | else if (strcmp(encoding, "mbcs") == 0) |
| 1247 | return PyUnicode_AsMBCSString(unicode); |
| 1248 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1249 | else if (strcmp(encoding, "ascii") == 0) |
| 1250 | return PyUnicode_AsASCIIString(unicode); |
| 1251 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1252 | |
| 1253 | /* Encode via the codec registry */ |
| 1254 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1255 | if (v == NULL) |
| 1256 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1257 | if (!PyString_Check(v)) { |
| 1258 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 1259 | "encoder did not return a string object (type=%.400s)", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1260 | Py_TYPE(v)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1261 | Py_DECREF(v); |
| 1262 | goto onError; |
| 1263 | } |
| 1264 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1265 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1266 | onError: |
| 1267 | return NULL; |
| 1268 | } |
| 1269 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1270 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
| 1271 | const char *errors) |
| 1272 | { |
| 1273 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
| 1274 | |
| 1275 | if (v) |
| 1276 | return v; |
| 1277 | v = PyUnicode_AsEncodedString(unicode, NULL, errors); |
| 1278 | if (v && errors == NULL) |
| 1279 | ((PyUnicodeObject *)unicode)->defenc = v; |
| 1280 | return v; |
| 1281 | } |
| 1282 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1283 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 1284 | { |
| 1285 | if (!PyUnicode_Check(unicode)) { |
| 1286 | PyErr_BadArgument(); |
| 1287 | goto onError; |
| 1288 | } |
| 1289 | return PyUnicode_AS_UNICODE(unicode); |
| 1290 | |
| 1291 | onError: |
| 1292 | return NULL; |
| 1293 | } |
| 1294 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1295 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1296 | { |
| 1297 | if (!PyUnicode_Check(unicode)) { |
| 1298 | PyErr_BadArgument(); |
| 1299 | goto onError; |
| 1300 | } |
| 1301 | return PyUnicode_GET_SIZE(unicode); |
| 1302 | |
| 1303 | onError: |
| 1304 | return -1; |
| 1305 | } |
| 1306 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 1307 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1308 | { |
| 1309 | return unicode_default_encoding; |
| 1310 | } |
| 1311 | |
| 1312 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 1313 | { |
| 1314 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1315 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1316 | /* Make sure the encoding is valid. As side effect, this also |
| 1317 | loads the encoding into the codec registry cache. */ |
| 1318 | v = _PyCodec_Lookup(encoding); |
| 1319 | if (v == NULL) |
| 1320 | goto onError; |
| 1321 | Py_DECREF(v); |
| 1322 | strncpy(unicode_default_encoding, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1323 | encoding, |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1324 | sizeof(unicode_default_encoding)); |
| 1325 | return 0; |
| 1326 | |
| 1327 | onError: |
| 1328 | return -1; |
| 1329 | } |
| 1330 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1331 | /* error handling callback helper: |
| 1332 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 1333 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1334 | and adjust various state variables. |
| 1335 | return 0 on success, -1 on error |
| 1336 | */ |
| 1337 | |
| 1338 | static |
| 1339 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
| 1340 | const char *encoding, const char *reason, |
Walter Dörwald | 8757878 | 2007-08-30 15:30:09 +0000 | [diff] [blame] | 1341 | const char *input, Py_ssize_t insize, Py_ssize_t *startinpos, |
| 1342 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1343 | PyObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1344 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1345 | 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] | 1346 | |
| 1347 | PyObject *restuple = NULL; |
| 1348 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1349 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
| 1350 | Py_ssize_t requiredsize; |
| 1351 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1352 | Py_UNICODE *repptr; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1353 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1354 | int res = -1; |
| 1355 | |
| 1356 | if (*errorHandler == NULL) { |
| 1357 | *errorHandler = PyCodec_LookupError(errors); |
| 1358 | if (*errorHandler == NULL) |
| 1359 | goto onError; |
| 1360 | } |
| 1361 | |
| 1362 | if (*exceptionObject == NULL) { |
| 1363 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 1364 | encoding, input, insize, *startinpos, *endinpos, reason); |
| 1365 | if (*exceptionObject == NULL) |
| 1366 | goto onError; |
| 1367 | } |
| 1368 | else { |
| 1369 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 1370 | goto onError; |
| 1371 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
| 1372 | goto onError; |
| 1373 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 1374 | goto onError; |
| 1375 | } |
| 1376 | |
| 1377 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 1378 | if (restuple == NULL) |
| 1379 | goto onError; |
| 1380 | if (!PyTuple_Check(restuple)) { |
| 1381 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 1382 | goto onError; |
| 1383 | } |
| 1384 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
| 1385 | goto onError; |
| 1386 | if (newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1387 | newpos = insize+newpos; |
| 1388 | if (newpos<0 || newpos>insize) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 1389 | 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] | 1390 | goto onError; |
| 1391 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1392 | |
| 1393 | /* need more space? (at least enough for what we |
| 1394 | have+the replacement+the rest of the string (starting |
| 1395 | at the new input position), so we won't have to check space |
| 1396 | when there are no errors in the rest of the string) */ |
| 1397 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 1398 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 1399 | requiredsize = *outpos + repsize + insize-newpos; |
| 1400 | if (requiredsize > outsize) { |
| 1401 | if (requiredsize<2*outsize) |
| 1402 | requiredsize = 2*outsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1403 | if (PyUnicode_Resize(output, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1404 | goto onError; |
| 1405 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
| 1406 | } |
| 1407 | *endinpos = newpos; |
| 1408 | *inptr = input + newpos; |
| 1409 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 1410 | *outptr += repsize; |
| 1411 | *outpos += repsize; |
| 1412 | /* we made it! */ |
| 1413 | res = 0; |
| 1414 | |
| 1415 | onError: |
| 1416 | Py_XDECREF(restuple); |
| 1417 | return res; |
| 1418 | } |
| 1419 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1420 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 1421 | |
| 1422 | /* see RFC2152 for details */ |
| 1423 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1424 | static |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1425 | char utf7_special[128] = { |
| 1426 | /* indicate whether a UTF-7 character is special i.e. cannot be directly |
| 1427 | encoded: |
| 1428 | 0 - not special |
| 1429 | 1 - special |
| 1430 | 2 - whitespace (optional) |
| 1431 | 3 - RFC2152 Set O (optional) */ |
| 1432 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, |
| 1433 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1434 | 2, 3, 3, 3, 3, 3, 3, 0, 0, 0, 3, 1, 0, 0, 0, 1, |
| 1435 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, |
| 1436 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1437 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, |
| 1438 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1439 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 1, 1, |
| 1440 | |
| 1441 | }; |
| 1442 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1443 | /* Note: The comparison (c) <= 0 is a trick to work-around gcc |
| 1444 | warnings about the comparison always being false; since |
| 1445 | utf7_special[0] is 1, we can safely make that one comparison |
| 1446 | true */ |
| 1447 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1448 | #define SPECIAL(c, encodeO, encodeWS) \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1449 | ((c) > 127 || (c) <= 0 || utf7_special[(c)] == 1 || \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1450 | (encodeWS && (utf7_special[(c)] == 2)) || \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1451 | (encodeO && (utf7_special[(c)] == 3))) |
| 1452 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1453 | #define B64(n) \ |
| 1454 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 1455 | #define B64CHAR(c) \ |
| 1456 | (isalnum(c) || (c) == '+' || (c) == '/') |
| 1457 | #define UB64(c) \ |
| 1458 | ((c) == '+' ? 62 : (c) == '/' ? 63 : (c) >= 'a' ? \ |
| 1459 | (c) - 71 : (c) >= 'A' ? (c) - 65 : (c) + 4 ) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1460 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1461 | #define ENCODE(out, ch, bits) \ |
| 1462 | while (bits >= 6) { \ |
| 1463 | *out++ = B64(ch >> (bits-6)); \ |
| 1464 | bits -= 6; \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1467 | #define DECODE(out, ch, bits, surrogate) \ |
| 1468 | while (bits >= 16) { \ |
| 1469 | Py_UNICODE outCh = (Py_UNICODE) ((ch >> (bits-16)) & 0xffff); \ |
| 1470 | bits -= 16; \ |
| 1471 | if (surrogate) { \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1472 | /* We have already generated an error for the high surrogate \ |
| 1473 | 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] | 1474 | surrogate = 0; \ |
| 1475 | } else if (0xDC00 <= outCh && outCh <= 0xDFFF) { \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1476 | /* This is a surrogate pair. Unfortunately we can't represent \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1477 | it in a 16-bit character */ \ |
| 1478 | surrogate = 1; \ |
| 1479 | errmsg = "code pairs are not supported"; \ |
| 1480 | goto utf7Error; \ |
| 1481 | } else { \ |
| 1482 | *out++ = outCh; \ |
| 1483 | } \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1484 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1485 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1486 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1487 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1488 | const char *errors) |
| 1489 | { |
Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1490 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 1491 | } |
| 1492 | |
| 1493 | PyObject *PyUnicode_DecodeUTF7Stateful(const char *s, |
| 1494 | Py_ssize_t size, |
| 1495 | const char *errors, |
| 1496 | Py_ssize_t *consumed) |
| 1497 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1498 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1499 | Py_ssize_t startinpos; |
| 1500 | Py_ssize_t endinpos; |
| 1501 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1502 | const char *e; |
| 1503 | PyUnicodeObject *unicode; |
| 1504 | Py_UNICODE *p; |
| 1505 | const char *errmsg = ""; |
| 1506 | int inShift = 0; |
| 1507 | unsigned int bitsleft = 0; |
| 1508 | unsigned long charsleft = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1509 | int surrogate = 0; |
| 1510 | PyObject *errorHandler = NULL; |
| 1511 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1512 | |
| 1513 | unicode = _PyUnicode_New(size); |
| 1514 | if (!unicode) |
| 1515 | return NULL; |
Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1516 | if (size == 0) { |
| 1517 | if (consumed) |
| 1518 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1519 | return (PyObject *)unicode; |
Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1520 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1521 | |
| 1522 | p = unicode->str; |
| 1523 | e = s + size; |
| 1524 | |
| 1525 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1526 | Py_UNICODE ch; |
| 1527 | restart: |
| 1528 | ch = *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1529 | |
| 1530 | if (inShift) { |
| 1531 | if ((ch == '-') || !B64CHAR(ch)) { |
| 1532 | inShift = 0; |
| 1533 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1534 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1535 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1536 | if (bitsleft >= 6) { |
| 1537 | /* The shift sequence has a partial character in it. If |
| 1538 | bitsleft < 6 then we could just classify it as padding |
| 1539 | but that is not the case here */ |
| 1540 | |
| 1541 | errmsg = "partial character in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1542 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1543 | } |
| 1544 | /* According to RFC2152 the remaining bits should be zero. We |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1545 | choose to signal an error/insert a replacement character |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1546 | here so indicate the potential of a misencoded character. */ |
| 1547 | |
| 1548 | /* On x86, a << b == a << (b%32) so make sure that bitsleft != 0 */ |
| 1549 | if (bitsleft && charsleft << (sizeof(charsleft) * 8 - bitsleft)) { |
| 1550 | errmsg = "non-zero padding bits in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1551 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
| 1554 | if (ch == '-') { |
| 1555 | if ((s < e) && (*(s) == '-')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1556 | *p++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1557 | inShift = 1; |
| 1558 | } |
| 1559 | } else if (SPECIAL(ch,0,0)) { |
| 1560 | errmsg = "unexpected special character"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1561 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1562 | } else { |
| 1563 | *p++ = ch; |
| 1564 | } |
| 1565 | } else { |
| 1566 | charsleft = (charsleft << 6) | UB64(ch); |
| 1567 | bitsleft += 6; |
| 1568 | s++; |
| 1569 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1570 | } |
| 1571 | } |
| 1572 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1573 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1574 | s++; |
| 1575 | if (s < e && *s == '-') { |
| 1576 | s++; |
| 1577 | *p++ = '+'; |
| 1578 | } else |
| 1579 | { |
| 1580 | inShift = 1; |
| 1581 | bitsleft = 0; |
| 1582 | } |
| 1583 | } |
| 1584 | else if (SPECIAL(ch,0,0)) { |
Walter Dörwald | 9d04542 | 2007-08-30 15:34:55 +0000 | [diff] [blame] | 1585 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1586 | errmsg = "unexpected special character"; |
| 1587 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1588 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1589 | } |
| 1590 | else { |
| 1591 | *p++ = ch; |
| 1592 | s++; |
| 1593 | } |
| 1594 | continue; |
| 1595 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1596 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1597 | endinpos = s-starts; |
| 1598 | if (unicode_decode_call_errorhandler( |
| 1599 | errors, &errorHandler, |
| 1600 | "utf7", errmsg, |
| 1601 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1602 | (PyObject **)&unicode, &outpos, &p)) |
| 1603 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1606 | if (inShift && !consumed) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1607 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1608 | endinpos = size; |
| 1609 | if (unicode_decode_call_errorhandler( |
| 1610 | errors, &errorHandler, |
| 1611 | "utf7", "unterminated shift sequence", |
| 1612 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1613 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1614 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1615 | if (s < e) |
| 1616 | goto restart; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1617 | } |
Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1618 | if (consumed) { |
| 1619 | if(inShift) |
| 1620 | *consumed = startinpos; |
| 1621 | else |
| 1622 | *consumed = s-starts; |
| 1623 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1624 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1625 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1626 | goto onError; |
| 1627 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1628 | Py_XDECREF(errorHandler); |
| 1629 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1630 | return (PyObject *)unicode; |
| 1631 | |
| 1632 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1633 | Py_XDECREF(errorHandler); |
| 1634 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1635 | Py_DECREF(unicode); |
| 1636 | return NULL; |
| 1637 | } |
| 1638 | |
| 1639 | |
| 1640 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1641 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1642 | int encodeSetO, |
| 1643 | int encodeWhiteSpace, |
| 1644 | const char *errors) |
| 1645 | { |
| 1646 | PyObject *v; |
| 1647 | /* It might be possible to tighten this worst case */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1648 | Py_ssize_t cbAllocated = 5 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1649 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1650 | Py_ssize_t i = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1651 | unsigned int bitsleft = 0; |
| 1652 | unsigned long charsleft = 0; |
| 1653 | char * out; |
| 1654 | char * start; |
| 1655 | |
| 1656 | if (size == 0) |
| 1657 | return PyString_FromStringAndSize(NULL, 0); |
| 1658 | |
| 1659 | v = PyString_FromStringAndSize(NULL, cbAllocated); |
| 1660 | if (v == NULL) |
| 1661 | return NULL; |
| 1662 | |
| 1663 | start = out = PyString_AS_STRING(v); |
| 1664 | for (;i < size; ++i) { |
| 1665 | Py_UNICODE ch = s[i]; |
| 1666 | |
| 1667 | if (!inShift) { |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1668 | if (ch == '+') { |
| 1669 | *out++ = '+'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1670 | *out++ = '-'; |
| 1671 | } else if (SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1672 | charsleft = ch; |
| 1673 | bitsleft = 16; |
| 1674 | *out++ = '+'; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1675 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1676 | inShift = bitsleft > 0; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1677 | } else { |
| 1678 | *out++ = (char) ch; |
| 1679 | } |
| 1680 | } else { |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1681 | if (!SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1682 | *out++ = B64(charsleft << (6-bitsleft)); |
| 1683 | charsleft = 0; |
| 1684 | bitsleft = 0; |
| 1685 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 1686 | so no '-' is required, except if the character is itself a '-' */ |
| 1687 | if (B64CHAR(ch) || ch == '-') { |
| 1688 | *out++ = '-'; |
| 1689 | } |
| 1690 | inShift = 0; |
| 1691 | *out++ = (char) ch; |
| 1692 | } else { |
| 1693 | bitsleft += 16; |
| 1694 | charsleft = (charsleft << 16) | ch; |
| 1695 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
| 1696 | |
| 1697 | /* If the next character is special then we dont' need to terminate |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1698 | 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] | 1699 | or '-' then the shift sequence will be terminated implicitly and we |
| 1700 | don't have to insert a '-'. */ |
| 1701 | |
| 1702 | if (bitsleft == 0) { |
| 1703 | if (i + 1 < size) { |
| 1704 | Py_UNICODE ch2 = s[i+1]; |
| 1705 | |
| 1706 | if (SPECIAL(ch2, encodeSetO, encodeWhiteSpace)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1707 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1708 | } else if (B64CHAR(ch2) || ch2 == '-') { |
| 1709 | *out++ = '-'; |
| 1710 | inShift = 0; |
| 1711 | } else { |
| 1712 | inShift = 0; |
| 1713 | } |
| 1714 | |
| 1715 | } |
| 1716 | else { |
| 1717 | *out++ = '-'; |
| 1718 | inShift = 0; |
| 1719 | } |
| 1720 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1721 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1722 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1723 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1724 | if (bitsleft) { |
| 1725 | *out++= B64(charsleft << (6-bitsleft) ); |
| 1726 | *out++ = '-'; |
| 1727 | } |
| 1728 | |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 1729 | _PyString_Resize(&v, out - start); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1730 | return v; |
| 1731 | } |
| 1732 | |
| 1733 | #undef SPECIAL |
| 1734 | #undef B64 |
| 1735 | #undef B64CHAR |
| 1736 | #undef UB64 |
| 1737 | #undef ENCODE |
| 1738 | #undef DECODE |
| 1739 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1740 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 1741 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1742 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1743 | char utf8_code_length[256] = { |
| 1744 | /* Map UTF-8 encoded prefix byte to sequence length. zero means |
| 1745 | illegal prefix. see RFC 2279 for details */ |
| 1746 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1747 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1748 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1749 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1750 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1751 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1752 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1753 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1754 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1755 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1756 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1757 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1758 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1759 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1760 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 1761 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 |
| 1762 | }; |
| 1763 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1764 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1765 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1766 | const char *errors) |
| 1767 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1768 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 1769 | } |
| 1770 | |
| 1771 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1772 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1773 | const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1774 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1775 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1776 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1777 | int n; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1778 | Py_ssize_t startinpos; |
| 1779 | Py_ssize_t endinpos; |
| 1780 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1781 | const char *e; |
| 1782 | PyUnicodeObject *unicode; |
| 1783 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1784 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1785 | PyObject *errorHandler = NULL; |
| 1786 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1787 | |
| 1788 | /* Note: size will always be longer than the resulting Unicode |
| 1789 | character count */ |
| 1790 | unicode = _PyUnicode_New(size); |
| 1791 | if (!unicode) |
| 1792 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1793 | if (size == 0) { |
| 1794 | if (consumed) |
| 1795 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1796 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1797 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1798 | |
| 1799 | /* Unpack UTF-8 encoded data */ |
| 1800 | p = unicode->str; |
| 1801 | e = s + size; |
| 1802 | |
| 1803 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1804 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1805 | |
| 1806 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1807 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1808 | s++; |
| 1809 | continue; |
| 1810 | } |
| 1811 | |
| 1812 | n = utf8_code_length[ch]; |
| 1813 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1814 | if (s + n > e) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1815 | if (consumed) |
| 1816 | break; |
| 1817 | else { |
| 1818 | errmsg = "unexpected end of data"; |
| 1819 | startinpos = s-starts; |
| 1820 | endinpos = size; |
| 1821 | goto utf8Error; |
| 1822 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1823 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1824 | |
| 1825 | switch (n) { |
| 1826 | |
| 1827 | case 0: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1828 | errmsg = "unexpected code byte"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1829 | startinpos = s-starts; |
| 1830 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1831 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1832 | |
| 1833 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1834 | errmsg = "internal error"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1835 | startinpos = s-starts; |
| 1836 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1837 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1838 | |
| 1839 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1840 | if ((s[1] & 0xc0) != 0x80) { |
| 1841 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1842 | startinpos = s-starts; |
| 1843 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1844 | goto utf8Error; |
| 1845 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1846 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1847 | if (ch < 0x80) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1848 | startinpos = s-starts; |
| 1849 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1850 | errmsg = "illegal encoding"; |
| 1851 | goto utf8Error; |
| 1852 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1853 | else |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1854 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1855 | break; |
| 1856 | |
| 1857 | case 3: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1858 | if ((s[1] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1859 | (s[2] & 0xc0) != 0x80) { |
| 1860 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1861 | startinpos = s-starts; |
| 1862 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1863 | goto utf8Error; |
| 1864 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1865 | 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] | 1866 | if (ch < 0x0800) { |
| 1867 | /* Note: UTF-8 encodings of surrogates are considered |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1868 | legal UTF-8 sequences; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1869 | |
| 1870 | XXX For wide builds (UCS-4) we should probably try |
| 1871 | to recombine the surrogates into a single code |
| 1872 | unit. |
| 1873 | */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1874 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1875 | startinpos = s-starts; |
| 1876 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1877 | goto utf8Error; |
| 1878 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1879 | else |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1880 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1881 | break; |
| 1882 | |
| 1883 | case 4: |
| 1884 | if ((s[1] & 0xc0) != 0x80 || |
| 1885 | (s[2] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1886 | (s[3] & 0xc0) != 0x80) { |
| 1887 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1888 | startinpos = s-starts; |
| 1889 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1890 | goto utf8Error; |
| 1891 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1892 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 1893 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 1894 | /* validate and convert to UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1895 | if ((ch < 0x10000) /* minimum value allowed for 4 |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1896 | byte encoding */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1897 | || (ch > 0x10ffff)) /* maximum value allowed for |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1898 | UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1899 | { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1900 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1901 | startinpos = s-starts; |
| 1902 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1903 | goto utf8Error; |
| 1904 | } |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1905 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1906 | *p++ = (Py_UNICODE)ch; |
| 1907 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1908 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1909 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1910 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 1911 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1912 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1913 | /* high surrogate = top 10 bits added to D800 */ |
| 1914 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1915 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1916 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 1917 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1918 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1919 | break; |
| 1920 | |
| 1921 | default: |
| 1922 | /* Other sizes are only needed for UCS-4 */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1923 | errmsg = "unsupported Unicode code range"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1924 | startinpos = s-starts; |
| 1925 | endinpos = startinpos+n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1926 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1927 | } |
| 1928 | s += n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1929 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1930 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1931 | utf8Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1932 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1933 | if (unicode_decode_call_errorhandler( |
| 1934 | errors, &errorHandler, |
| 1935 | "utf8", errmsg, |
| 1936 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 1937 | (PyObject **)&unicode, &outpos, &p)) |
| 1938 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1939 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1940 | if (consumed) |
| 1941 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1942 | |
| 1943 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1944 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1945 | goto onError; |
| 1946 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1947 | Py_XDECREF(errorHandler); |
| 1948 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1949 | return (PyObject *)unicode; |
| 1950 | |
| 1951 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1952 | Py_XDECREF(errorHandler); |
| 1953 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1954 | Py_DECREF(unicode); |
| 1955 | return NULL; |
| 1956 | } |
| 1957 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1958 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 1959 | and allocate exactly as much space needed at the end. Else allocate the |
| 1960 | maximum possible needed (4 result bytes per Unicode character), and return |
| 1961 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1962 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1963 | PyObject * |
| 1964 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1965 | Py_ssize_t size, |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1966 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1967 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1968 | #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] | 1969 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1970 | Py_ssize_t i; /* index into s of next input byte */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1971 | PyObject *v; /* result string object */ |
| 1972 | char *p; /* next free byte in output buffer */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1973 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 1974 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1975 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1976 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1977 | assert(s != NULL); |
| 1978 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1979 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1980 | if (size <= MAX_SHORT_UNICHARS) { |
| 1981 | /* Write into the stack buffer; nallocated can't overflow. |
| 1982 | * At the end, we'll allocate exactly as much heap space as it |
| 1983 | * turns out we need. |
| 1984 | */ |
| 1985 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
| 1986 | v = NULL; /* will allocate after we're done */ |
| 1987 | p = stackbuf; |
| 1988 | } |
| 1989 | else { |
| 1990 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 1991 | nallocated = size * 4; |
| 1992 | if (nallocated / 4 != size) /* overflow! */ |
| 1993 | return PyErr_NoMemory(); |
| 1994 | v = PyString_FromStringAndSize(NULL, nallocated); |
| 1995 | if (v == NULL) |
| 1996 | return NULL; |
| 1997 | p = PyString_AS_STRING(v); |
| 1998 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1999 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2000 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2001 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2002 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2003 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2004 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2005 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2006 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2007 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2008 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 2009 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 2010 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2011 | } |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2012 | else { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2013 | /* Encode UCS2 Unicode ordinals */ |
| 2014 | if (ch < 0x10000) { |
| 2015 | /* Special case: check for high surrogate */ |
| 2016 | if (0xD800 <= ch && ch <= 0xDBFF && i != size) { |
| 2017 | Py_UCS4 ch2 = s[i]; |
| 2018 | /* Check for low surrogate and combine the two to |
| 2019 | form a UCS4 value */ |
| 2020 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2021 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2022 | i++; |
| 2023 | goto encodeUCS4; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2024 | } |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2025 | /* Fall through: handles isolated high surrogates */ |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2026 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2027 | *p++ = (char)(0xe0 | (ch >> 12)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2028 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2029 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2030 | continue; |
| 2031 | } |
| 2032 | encodeUCS4: |
| 2033 | /* Encode UCS4 Unicode ordinals */ |
| 2034 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2035 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 2036 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2037 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2038 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2039 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 2040 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2041 | if (v == NULL) { |
| 2042 | /* This was stack allocated. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2043 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2044 | assert(nneeded <= nallocated); |
| 2045 | v = PyString_FromStringAndSize(stackbuf, nneeded); |
| 2046 | } |
| 2047 | else { |
| 2048 | /* Cut back to size actually needed. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2049 | nneeded = p - PyString_AS_STRING(v); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2050 | assert(nneeded <= nallocated); |
| 2051 | _PyString_Resize(&v, nneeded); |
| 2052 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2053 | return v; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2054 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2055 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2056 | } |
| 2057 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2058 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 2059 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2060 | if (!PyUnicode_Check(unicode)) { |
| 2061 | PyErr_BadArgument(); |
| 2062 | return NULL; |
| 2063 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 2064 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 2065 | PyUnicode_GET_SIZE(unicode), |
| 2066 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2067 | } |
| 2068 | |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2069 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 2070 | |
| 2071 | PyObject * |
| 2072 | PyUnicode_DecodeUTF32(const char *s, |
| 2073 | Py_ssize_t size, |
| 2074 | const char *errors, |
| 2075 | int *byteorder) |
| 2076 | { |
| 2077 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 2078 | } |
| 2079 | |
| 2080 | PyObject * |
| 2081 | PyUnicode_DecodeUTF32Stateful(const char *s, |
| 2082 | Py_ssize_t size, |
| 2083 | const char *errors, |
| 2084 | int *byteorder, |
| 2085 | Py_ssize_t *consumed) |
| 2086 | { |
| 2087 | const char *starts = s; |
| 2088 | Py_ssize_t startinpos; |
| 2089 | Py_ssize_t endinpos; |
| 2090 | Py_ssize_t outpos; |
| 2091 | PyUnicodeObject *unicode; |
| 2092 | Py_UNICODE *p; |
| 2093 | #ifndef Py_UNICODE_WIDE |
| 2094 | int i, pairs; |
| 2095 | #else |
| 2096 | const int pairs = 0; |
| 2097 | #endif |
| 2098 | const unsigned char *q, *e; |
| 2099 | int bo = 0; /* assume native ordering by default */ |
| 2100 | const char *errmsg = ""; |
Walter Dörwald | 20b40d3 | 2007-08-17 16:52:50 +0000 | [diff] [blame] | 2101 | /* Offsets from q for retrieving bytes in the right order. */ |
| 2102 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2103 | int iorder[] = {0, 1, 2, 3}; |
| 2104 | #else |
| 2105 | int iorder[] = {3, 2, 1, 0}; |
| 2106 | #endif |
Walter Dörwald | 9ab80a9 | 2007-08-17 16:58:43 +0000 | [diff] [blame] | 2107 | PyObject *errorHandler = NULL; |
| 2108 | PyObject *exc = NULL; |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2109 | /* On narrow builds we split characters outside the BMP into two |
| 2110 | codepoints => count how much extra space we need. */ |
| 2111 | #ifndef Py_UNICODE_WIDE |
| 2112 | for (i = pairs = 0; i < size/4; i++) |
| 2113 | if (((Py_UCS4 *)s)[i] >= 0x10000) |
| 2114 | pairs++; |
| 2115 | #endif |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2116 | |
| 2117 | /* This might be one to much, because of a BOM */ |
| 2118 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 2119 | if (!unicode) |
| 2120 | return NULL; |
| 2121 | if (size == 0) |
| 2122 | return (PyObject *)unicode; |
| 2123 | |
| 2124 | /* Unpack UTF-32 encoded data */ |
| 2125 | p = unicode->str; |
| 2126 | q = (unsigned char *)s; |
| 2127 | e = q + size; |
| 2128 | |
| 2129 | if (byteorder) |
| 2130 | bo = *byteorder; |
| 2131 | |
| 2132 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2133 | byte order setting accordingly. In native mode, the leading BOM |
| 2134 | mark is skipped, in all other modes, it is copied to the output |
| 2135 | stream as-is (giving a ZWNBSP character). */ |
| 2136 | if (bo == 0) { |
| 2137 | if (size >= 4) { |
| 2138 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2139 | (q[iorder[1]] << 8) | q[iorder[0]]; |
| 2140 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2141 | if (bom == 0x0000FEFF) { |
| 2142 | q += 4; |
| 2143 | bo = -1; |
| 2144 | } |
| 2145 | else if (bom == 0xFFFE0000) { |
| 2146 | q += 4; |
| 2147 | bo = 1; |
| 2148 | } |
| 2149 | #else |
| 2150 | if (bom == 0x0000FEFF) { |
| 2151 | q += 4; |
| 2152 | bo = 1; |
| 2153 | } |
| 2154 | else if (bom == 0xFFFE0000) { |
| 2155 | q += 4; |
| 2156 | bo = -1; |
| 2157 | } |
| 2158 | #endif |
| 2159 | } |
| 2160 | } |
| 2161 | |
| 2162 | if (bo == -1) { |
| 2163 | /* force LE */ |
| 2164 | iorder[0] = 0; |
| 2165 | iorder[1] = 1; |
| 2166 | iorder[2] = 2; |
| 2167 | iorder[3] = 3; |
| 2168 | } |
| 2169 | else if (bo == 1) { |
| 2170 | /* force BE */ |
| 2171 | iorder[0] = 3; |
| 2172 | iorder[1] = 2; |
| 2173 | iorder[2] = 1; |
| 2174 | iorder[3] = 0; |
| 2175 | } |
| 2176 | |
| 2177 | while (q < e) { |
| 2178 | Py_UCS4 ch; |
| 2179 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 2180 | if (e-q<4) { |
| 2181 | if (consumed) |
| 2182 | break; |
| 2183 | errmsg = "truncated data"; |
| 2184 | startinpos = ((const char *)q)-starts; |
| 2185 | endinpos = ((const char *)e)-starts; |
| 2186 | goto utf32Error; |
| 2187 | /* The remaining input chars are ignored if the callback |
| 2188 | chooses to skip the input */ |
| 2189 | } |
| 2190 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2191 | (q[iorder[1]] << 8) | q[iorder[0]]; |
| 2192 | |
| 2193 | if (ch >= 0x110000) |
| 2194 | { |
| 2195 | errmsg = "codepoint not in range(0x110000)"; |
| 2196 | startinpos = ((const char *)q)-starts; |
| 2197 | endinpos = startinpos+4; |
| 2198 | goto utf32Error; |
| 2199 | } |
| 2200 | #ifndef Py_UNICODE_WIDE |
| 2201 | if (ch >= 0x10000) |
| 2202 | { |
| 2203 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 2204 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2205 | } |
| 2206 | else |
| 2207 | #endif |
| 2208 | *p++ = ch; |
| 2209 | q += 4; |
| 2210 | continue; |
| 2211 | utf32Error: |
| 2212 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2213 | if (unicode_decode_call_errorhandler( |
| 2214 | errors, &errorHandler, |
| 2215 | "utf32", errmsg, |
| 2216 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2217 | (PyObject **)&unicode, &outpos, &p)) |
| 2218 | goto onError; |
| 2219 | } |
| 2220 | |
| 2221 | if (byteorder) |
| 2222 | *byteorder = bo; |
| 2223 | |
| 2224 | if (consumed) |
| 2225 | *consumed = (const char *)q-starts; |
| 2226 | |
| 2227 | /* Adjust length */ |
| 2228 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
| 2229 | goto onError; |
| 2230 | |
| 2231 | Py_XDECREF(errorHandler); |
| 2232 | Py_XDECREF(exc); |
| 2233 | return (PyObject *)unicode; |
| 2234 | |
| 2235 | onError: |
| 2236 | Py_DECREF(unicode); |
| 2237 | Py_XDECREF(errorHandler); |
| 2238 | Py_XDECREF(exc); |
| 2239 | return NULL; |
| 2240 | } |
| 2241 | |
| 2242 | PyObject * |
| 2243 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 2244 | Py_ssize_t size, |
| 2245 | const char *errors, |
| 2246 | int byteorder) |
| 2247 | { |
| 2248 | PyObject *v; |
| 2249 | unsigned char *p; |
| 2250 | #ifndef Py_UNICODE_WIDE |
| 2251 | int i, pairs; |
| 2252 | #else |
| 2253 | const int pairs = 0; |
| 2254 | #endif |
| 2255 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2256 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2257 | int iorder[] = {0, 1, 2, 3}; |
| 2258 | #else |
| 2259 | int iorder[] = {3, 2, 1, 0}; |
| 2260 | #endif |
| 2261 | |
| 2262 | #define STORECHAR(CH) \ |
| 2263 | do { \ |
| 2264 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 2265 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 2266 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 2267 | p[iorder[0]] = (CH) & 0xff; \ |
| 2268 | p += 4; \ |
| 2269 | } while(0) |
| 2270 | |
| 2271 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 2272 | so we need less space. */ |
| 2273 | #ifndef Py_UNICODE_WIDE |
| 2274 | for (i = pairs = 0; i < size-1; i++) |
| 2275 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 2276 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 2277 | pairs++; |
| 2278 | #endif |
| 2279 | v = PyString_FromStringAndSize(NULL, |
| 2280 | 4 * (size - pairs + (byteorder == 0))); |
| 2281 | if (v == NULL) |
| 2282 | return NULL; |
| 2283 | |
| 2284 | p = (unsigned char *)PyString_AS_STRING(v); |
| 2285 | if (byteorder == 0) |
| 2286 | STORECHAR(0xFEFF); |
| 2287 | if (size == 0) |
| 2288 | return v; |
| 2289 | |
| 2290 | if (byteorder == -1) { |
| 2291 | /* force LE */ |
| 2292 | iorder[0] = 0; |
| 2293 | iorder[1] = 1; |
| 2294 | iorder[2] = 2; |
| 2295 | iorder[3] = 3; |
| 2296 | } |
| 2297 | else if (byteorder == 1) { |
| 2298 | /* force BE */ |
| 2299 | iorder[0] = 3; |
| 2300 | iorder[1] = 2; |
| 2301 | iorder[2] = 1; |
| 2302 | iorder[3] = 0; |
| 2303 | } |
| 2304 | |
| 2305 | while (size-- > 0) { |
| 2306 | Py_UCS4 ch = *s++; |
| 2307 | #ifndef Py_UNICODE_WIDE |
| 2308 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 2309 | Py_UCS4 ch2 = *s; |
| 2310 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 2311 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 2312 | s++; |
| 2313 | size--; |
| 2314 | } |
| 2315 | } |
| 2316 | #endif |
| 2317 | STORECHAR(ch); |
| 2318 | } |
| 2319 | return v; |
| 2320 | #undef STORECHAR |
| 2321 | } |
| 2322 | |
| 2323 | PyObject *PyUnicode_AsUTF32String(PyObject *unicode) |
| 2324 | { |
| 2325 | if (!PyUnicode_Check(unicode)) { |
| 2326 | PyErr_BadArgument(); |
| 2327 | return NULL; |
| 2328 | } |
| 2329 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
| 2330 | PyUnicode_GET_SIZE(unicode), |
| 2331 | NULL, |
| 2332 | 0); |
| 2333 | } |
| 2334 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2335 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 2336 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2337 | PyObject * |
| 2338 | PyUnicode_DecodeUTF16(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2339 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2340 | const char *errors, |
| 2341 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2342 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2343 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 2344 | } |
| 2345 | |
| 2346 | PyObject * |
| 2347 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2348 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2349 | const char *errors, |
| 2350 | int *byteorder, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2351 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2352 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2353 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2354 | Py_ssize_t startinpos; |
| 2355 | Py_ssize_t endinpos; |
| 2356 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2357 | PyUnicodeObject *unicode; |
| 2358 | Py_UNICODE *p; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2359 | const unsigned char *q, *e; |
| 2360 | int bo = 0; /* assume native ordering by default */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2361 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2362 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 2363 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2364 | int ihi = 1, ilo = 0; |
| 2365 | #else |
| 2366 | int ihi = 0, ilo = 1; |
| 2367 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2368 | PyObject *errorHandler = NULL; |
| 2369 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2370 | |
| 2371 | /* Note: size will always be longer than the resulting Unicode |
| 2372 | character count */ |
| 2373 | unicode = _PyUnicode_New(size); |
| 2374 | if (!unicode) |
| 2375 | return NULL; |
| 2376 | if (size == 0) |
| 2377 | return (PyObject *)unicode; |
| 2378 | |
| 2379 | /* Unpack UTF-16 encoded data */ |
| 2380 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2381 | q = (unsigned char *)s; |
| 2382 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2383 | |
| 2384 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2385 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2386 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2387 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2388 | byte order setting accordingly. In native mode, the leading BOM |
| 2389 | mark is skipped, in all other modes, it is copied to the output |
| 2390 | stream as-is (giving a ZWNBSP character). */ |
| 2391 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2392 | if (size >= 2) { |
| 2393 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2394 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2395 | if (bom == 0xFEFF) { |
| 2396 | q += 2; |
| 2397 | bo = -1; |
| 2398 | } |
| 2399 | else if (bom == 0xFFFE) { |
| 2400 | q += 2; |
| 2401 | bo = 1; |
| 2402 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2403 | #else |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2404 | if (bom == 0xFEFF) { |
| 2405 | q += 2; |
| 2406 | bo = 1; |
| 2407 | } |
| 2408 | else if (bom == 0xFFFE) { |
| 2409 | q += 2; |
| 2410 | bo = -1; |
| 2411 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2412 | #endif |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2413 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2414 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2415 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2416 | if (bo == -1) { |
| 2417 | /* force LE */ |
| 2418 | ihi = 1; |
| 2419 | ilo = 0; |
| 2420 | } |
| 2421 | else if (bo == 1) { |
| 2422 | /* force BE */ |
| 2423 | ihi = 0; |
| 2424 | ilo = 1; |
| 2425 | } |
| 2426 | |
| 2427 | while (q < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2428 | Py_UNICODE ch; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2429 | /* remaining bytes at the end? (size should be even) */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2430 | if (e-q<2) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2431 | if (consumed) |
| 2432 | break; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2433 | errmsg = "truncated data"; |
| 2434 | startinpos = ((const char *)q)-starts; |
| 2435 | endinpos = ((const char *)e)-starts; |
| 2436 | goto utf16Error; |
| 2437 | /* The remaining input chars are ignored if the callback |
| 2438 | chooses to skip the input */ |
| 2439 | } |
| 2440 | ch = (q[ihi] << 8) | q[ilo]; |
| 2441 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2442 | q += 2; |
| 2443 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2444 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 2445 | *p++ = ch; |
| 2446 | continue; |
| 2447 | } |
| 2448 | |
| 2449 | /* UTF-16 code pair: */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2450 | if (q >= e) { |
| 2451 | errmsg = "unexpected end of data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2452 | startinpos = (((const char *)q)-2)-starts; |
| 2453 | endinpos = ((const char *)e)-starts; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2454 | goto utf16Error; |
| 2455 | } |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 2456 | if (0xD800 <= ch && ch <= 0xDBFF) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2457 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 2458 | q += 2; |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 2459 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2460 | #ifndef Py_UNICODE_WIDE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2461 | *p++ = ch; |
| 2462 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2463 | #else |
| 2464 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2465 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2466 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2467 | } |
| 2468 | else { |
| 2469 | errmsg = "illegal UTF-16 surrogate"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2470 | startinpos = (((const char *)q)-4)-starts; |
| 2471 | endinpos = startinpos+2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2472 | goto utf16Error; |
| 2473 | } |
| 2474 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2475 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2476 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2477 | startinpos = (((const char *)q)-2)-starts; |
| 2478 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2479 | /* Fall through to report the error */ |
| 2480 | |
| 2481 | utf16Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2482 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2483 | if (unicode_decode_call_errorhandler( |
| 2484 | errors, &errorHandler, |
| 2485 | "utf16", errmsg, |
| 2486 | starts, size, &startinpos, &endinpos, &exc, (const char **)&q, |
| 2487 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2488 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2489 | } |
| 2490 | |
| 2491 | if (byteorder) |
| 2492 | *byteorder = bo; |
| 2493 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2494 | if (consumed) |
| 2495 | *consumed = (const char *)q-starts; |
| 2496 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2497 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2498 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2499 | goto onError; |
| 2500 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2501 | Py_XDECREF(errorHandler); |
| 2502 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2503 | return (PyObject *)unicode; |
| 2504 | |
| 2505 | onError: |
| 2506 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2507 | Py_XDECREF(errorHandler); |
| 2508 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2509 | return NULL; |
| 2510 | } |
| 2511 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2512 | PyObject * |
| 2513 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2514 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2515 | const char *errors, |
| 2516 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2517 | { |
| 2518 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2519 | unsigned char *p; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2520 | #ifdef Py_UNICODE_WIDE |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2521 | int i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2522 | #else |
| 2523 | const int pairs = 0; |
| 2524 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2525 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2526 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2527 | int ihi = 1, ilo = 0; |
| 2528 | #else |
| 2529 | int ihi = 0, ilo = 1; |
| 2530 | #endif |
| 2531 | |
| 2532 | #define STORECHAR(CH) \ |
| 2533 | do { \ |
| 2534 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 2535 | p[ilo] = (CH) & 0xff; \ |
| 2536 | p += 2; \ |
| 2537 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2538 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2539 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2540 | for (i = pairs = 0; i < size; i++) |
| 2541 | if (s[i] >= 0x10000) |
| 2542 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2543 | #endif |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2544 | v = PyString_FromStringAndSize(NULL, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2545 | 2 * (size + pairs + (byteorder == 0))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2546 | if (v == NULL) |
| 2547 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2548 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2549 | p = (unsigned char *)PyString_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2550 | if (byteorder == 0) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2551 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2552 | if (size == 0) |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 2553 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2554 | |
| 2555 | if (byteorder == -1) { |
| 2556 | /* force LE */ |
| 2557 | ihi = 1; |
| 2558 | ilo = 0; |
| 2559 | } |
| 2560 | else if (byteorder == 1) { |
| 2561 | /* force BE */ |
| 2562 | ihi = 0; |
| 2563 | ilo = 1; |
| 2564 | } |
| 2565 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2566 | while (size-- > 0) { |
| 2567 | Py_UNICODE ch = *s++; |
| 2568 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2569 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2570 | if (ch >= 0x10000) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2571 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2572 | ch = 0xD800 | ((ch-0x10000) >> 10); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2573 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2574 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2575 | STORECHAR(ch); |
| 2576 | if (ch2) |
| 2577 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2578 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2579 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2580 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2581 | } |
| 2582 | |
| 2583 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 2584 | { |
| 2585 | if (!PyUnicode_Check(unicode)) { |
| 2586 | PyErr_BadArgument(); |
| 2587 | return NULL; |
| 2588 | } |
| 2589 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
| 2590 | PyUnicode_GET_SIZE(unicode), |
| 2591 | NULL, |
| 2592 | 0); |
| 2593 | } |
| 2594 | |
| 2595 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 2596 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2597 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 2598 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2599 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2600 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2601 | const char *errors) |
| 2602 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2603 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2604 | Py_ssize_t startinpos; |
| 2605 | Py_ssize_t endinpos; |
| 2606 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2607 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2608 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2609 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2610 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2611 | char* message; |
| 2612 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2613 | PyObject *errorHandler = NULL; |
| 2614 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2615 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2616 | /* Escaped strings will always be longer than the resulting |
| 2617 | 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] | 2618 | length after conversion to the true value. |
| 2619 | (but if the error callback returns a long replacement string |
| 2620 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2621 | v = _PyUnicode_New(size); |
| 2622 | if (v == NULL) |
| 2623 | goto onError; |
| 2624 | if (size == 0) |
| 2625 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2626 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2627 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2628 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2629 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2630 | while (s < end) { |
| 2631 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2632 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2633 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2634 | |
| 2635 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 2636 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2637 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2638 | continue; |
| 2639 | } |
| 2640 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2641 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2642 | /* \ - Escapes */ |
| 2643 | s++; |
Guido van Rossum | 1c1ac38 | 2007-10-29 22:15:05 +0000 | [diff] [blame] | 2644 | c = *s++; |
| 2645 | if (s > end) |
| 2646 | c = '\0'; /* Invalid after \ */ |
| 2647 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2648 | |
| 2649 | /* \x escapes */ |
| 2650 | case '\n': break; |
| 2651 | case '\\': *p++ = '\\'; break; |
| 2652 | case '\'': *p++ = '\''; break; |
| 2653 | case '\"': *p++ = '\"'; break; |
| 2654 | case 'b': *p++ = '\b'; break; |
| 2655 | case 'f': *p++ = '\014'; break; /* FF */ |
| 2656 | case 't': *p++ = '\t'; break; |
| 2657 | case 'n': *p++ = '\n'; break; |
| 2658 | case 'r': *p++ = '\r'; break; |
| 2659 | case 'v': *p++ = '\013'; break; /* VT */ |
| 2660 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 2661 | |
| 2662 | /* \OOO (octal) escapes */ |
| 2663 | case '0': case '1': case '2': case '3': |
| 2664 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2665 | x = s[-1] - '0'; |
Guido van Rossum | 1c1ac38 | 2007-10-29 22:15:05 +0000 | [diff] [blame] | 2666 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2667 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 1c1ac38 | 2007-10-29 22:15:05 +0000 | [diff] [blame] | 2668 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2669 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2670 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2671 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2672 | break; |
| 2673 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2674 | /* hex escapes */ |
| 2675 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2676 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2677 | digits = 2; |
| 2678 | message = "truncated \\xXX escape"; |
| 2679 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2680 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2681 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2682 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2683 | digits = 4; |
| 2684 | message = "truncated \\uXXXX escape"; |
| 2685 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2686 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2687 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2688 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2689 | digits = 8; |
| 2690 | message = "truncated \\UXXXXXXXX escape"; |
| 2691 | hexescape: |
| 2692 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2693 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2694 | if (s+digits>end) { |
| 2695 | endinpos = size; |
| 2696 | if (unicode_decode_call_errorhandler( |
| 2697 | errors, &errorHandler, |
| 2698 | "unicodeescape", "end of string in escape sequence", |
| 2699 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2700 | (PyObject **)&v, &outpos, &p)) |
| 2701 | goto onError; |
| 2702 | goto nextByte; |
| 2703 | } |
| 2704 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2705 | c = (unsigned char) s[i]; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2706 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2707 | endinpos = (s+i+1)-starts; |
| 2708 | if (unicode_decode_call_errorhandler( |
| 2709 | errors, &errorHandler, |
| 2710 | "unicodeescape", message, |
| 2711 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2712 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2713 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2714 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2715 | } |
| 2716 | chr = (chr<<4) & ~0xF; |
| 2717 | if (c >= '0' && c <= '9') |
| 2718 | chr += c - '0'; |
| 2719 | else if (c >= 'a' && c <= 'f') |
| 2720 | chr += 10 + c - 'a'; |
| 2721 | else |
| 2722 | chr += 10 + c - 'A'; |
| 2723 | } |
| 2724 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 2725 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2726 | /* _decoding_error will have already written into the |
| 2727 | target buffer. */ |
| 2728 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2729 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2730 | /* when we get here, chr is a 32-bit unicode character */ |
| 2731 | if (chr <= 0xffff) |
| 2732 | /* UCS-2 character */ |
| 2733 | *p++ = (Py_UNICODE) chr; |
| 2734 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2735 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2736 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2737 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2738 | *p++ = chr; |
| 2739 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2740 | chr -= 0x10000L; |
| 2741 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2742 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2743 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2744 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2745 | endinpos = s-starts; |
| 2746 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2747 | if (unicode_decode_call_errorhandler( |
| 2748 | errors, &errorHandler, |
| 2749 | "unicodeescape", "illegal Unicode character", |
| 2750 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2751 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2752 | goto onError; |
| 2753 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2754 | break; |
| 2755 | |
| 2756 | /* \N{name} */ |
| 2757 | case 'N': |
| 2758 | message = "malformed \\N character escape"; |
| 2759 | if (ucnhash_CAPI == NULL) { |
| 2760 | /* load the unicode data module */ |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2761 | PyObject *m, *api; |
Christian Heimes | 000a074 | 2008-01-03 22:16:32 +0000 | [diff] [blame] | 2762 | m = PyImport_ImportModuleNoBlock("unicodedata"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2763 | if (m == NULL) |
| 2764 | goto ucnhashError; |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2765 | api = PyObject_GetAttrString(m, "ucnhash_CAPI"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2766 | Py_DECREF(m); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2767 | if (api == NULL) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2768 | goto ucnhashError; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2769 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCObject_AsVoidPtr(api); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2770 | Py_DECREF(api); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2771 | if (ucnhash_CAPI == NULL) |
| 2772 | goto ucnhashError; |
| 2773 | } |
| 2774 | if (*s == '{') { |
| 2775 | const char *start = s+1; |
| 2776 | /* look for the closing brace */ |
| 2777 | while (*s != '}' && s < end) |
| 2778 | s++; |
| 2779 | if (s > start && s < end && *s == '}') { |
| 2780 | /* found a name. look it up in the unicode database */ |
| 2781 | message = "unknown Unicode character name"; |
| 2782 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 2783 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2784 | goto store; |
| 2785 | } |
| 2786 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2787 | endinpos = s-starts; |
| 2788 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2789 | if (unicode_decode_call_errorhandler( |
| 2790 | errors, &errorHandler, |
| 2791 | "unicodeescape", message, |
| 2792 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2793 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2794 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2795 | break; |
| 2796 | |
| 2797 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2798 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2799 | message = "\\ at end of string"; |
| 2800 | s--; |
| 2801 | endinpos = s-starts; |
| 2802 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2803 | if (unicode_decode_call_errorhandler( |
| 2804 | errors, &errorHandler, |
| 2805 | "unicodeescape", message, |
| 2806 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 2807 | (PyObject **)&v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2808 | goto onError; |
| 2809 | } |
| 2810 | else { |
| 2811 | *p++ = '\\'; |
| 2812 | *p++ = (unsigned char)s[-1]; |
| 2813 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2814 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2815 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2816 | nextByte: |
| 2817 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2818 | } |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2819 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2820 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 2821 | Py_XDECREF(errorHandler); |
| 2822 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2823 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2824 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2825 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2826 | PyErr_SetString( |
| 2827 | PyExc_UnicodeError, |
| 2828 | "\\N escapes not supported (can't load unicodedata module)" |
| 2829 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2830 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2831 | Py_XDECREF(errorHandler); |
| 2832 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 2833 | return NULL; |
| 2834 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2835 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2836 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2837 | Py_XDECREF(errorHandler); |
| 2838 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2839 | return NULL; |
| 2840 | } |
| 2841 | |
| 2842 | /* Return a Unicode-Escape string version of the Unicode object. |
| 2843 | |
| 2844 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 2845 | appropriate. |
| 2846 | |
| 2847 | */ |
| 2848 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 2849 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
Fredrik Lundh | 95e2a91 | 2006-05-26 11:38:15 +0000 | [diff] [blame] | 2850 | Py_ssize_t size, |
| 2851 | Py_UNICODE ch) |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 2852 | { |
| 2853 | /* like wcschr, but doesn't stop at NULL characters */ |
| 2854 | |
| 2855 | while (size-- > 0) { |
| 2856 | if (*s == ch) |
| 2857 | return s; |
| 2858 | s++; |
| 2859 | } |
| 2860 | |
| 2861 | return NULL; |
| 2862 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 2863 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2864 | static |
| 2865 | PyObject *unicodeescape_string(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2866 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2867 | int quotes) |
| 2868 | { |
| 2869 | PyObject *repr; |
| 2870 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2871 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2872 | static const char *hexdigit = "0123456789abcdef"; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2873 | |
Neal Norwitz | 17753ec | 2006-08-21 22:21:19 +0000 | [diff] [blame] | 2874 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 2875 | better to choose a different scheme. Perhaps scan the |
| 2876 | first N-chars of the string and allocate based on that size. |
| 2877 | */ |
| 2878 | /* Initial allocation is based on the longest-possible unichr |
| 2879 | escape. |
| 2880 | |
| 2881 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 2882 | unichr, so in this case it's the longest unichr escape. In |
| 2883 | narrow (UTF-16) builds this is five chars per source unichr |
| 2884 | since there are two unichrs in the surrogate pair, so in narrow |
| 2885 | (UTF-16) builds it's not the longest unichr escape. |
| 2886 | |
| 2887 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 2888 | so in the narrow (UTF-16) build case it's the longest unichr |
| 2889 | escape. |
| 2890 | */ |
| 2891 | |
| 2892 | repr = PyString_FromStringAndSize(NULL, |
| 2893 | 2 |
| 2894 | #ifdef Py_UNICODE_WIDE |
| 2895 | + 10*size |
| 2896 | #else |
| 2897 | + 6*size |
| 2898 | #endif |
| 2899 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2900 | if (repr == NULL) |
| 2901 | return NULL; |
| 2902 | |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2903 | p = PyString_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2904 | |
| 2905 | if (quotes) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2906 | *p++ = 'u'; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2907 | *p++ = (findchar(s, size, '\'') && |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2908 | !findchar(s, size, '"')) ? '"' : '\''; |
| 2909 | } |
| 2910 | while (size-- > 0) { |
| 2911 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2912 | |
Hye-Shik Chang | 835b243 | 2005-12-17 04:38:31 +0000 | [diff] [blame] | 2913 | /* Escape quotes and backslashes */ |
| 2914 | if ((quotes && |
| 2915 | ch == (Py_UNICODE) PyString_AS_STRING(repr)[1]) || ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2916 | *p++ = '\\'; |
| 2917 | *p++ = (char) ch; |
Guido van Rossum | ad9744a | 2001-09-21 15:38:17 +0000 | [diff] [blame] | 2918 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2919 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2920 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 2921 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2922 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 2923 | else if (ch >= 0x10000) { |
| 2924 | *p++ = '\\'; |
| 2925 | *p++ = 'U'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2926 | *p++ = hexdigit[(ch >> 28) & 0x0000000F]; |
| 2927 | *p++ = hexdigit[(ch >> 24) & 0x0000000F]; |
| 2928 | *p++ = hexdigit[(ch >> 20) & 0x0000000F]; |
| 2929 | *p++ = hexdigit[(ch >> 16) & 0x0000000F]; |
| 2930 | *p++ = hexdigit[(ch >> 12) & 0x0000000F]; |
| 2931 | *p++ = hexdigit[(ch >> 8) & 0x0000000F]; |
| 2932 | *p++ = hexdigit[(ch >> 4) & 0x0000000F]; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2933 | *p++ = hexdigit[ch & 0x0000000F]; |
| 2934 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2935 | } |
Neal Norwitz | 17753ec | 2006-08-21 22:21:19 +0000 | [diff] [blame] | 2936 | #else |
| 2937 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2938 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 2939 | Py_UNICODE ch2; |
| 2940 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2941 | |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2942 | ch2 = *s++; |
| 2943 | size--; |
| 2944 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 2945 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 2946 | *p++ = '\\'; |
| 2947 | *p++ = 'U'; |
| 2948 | *p++ = hexdigit[(ucs >> 28) & 0x0000000F]; |
| 2949 | *p++ = hexdigit[(ucs >> 24) & 0x0000000F]; |
| 2950 | *p++ = hexdigit[(ucs >> 20) & 0x0000000F]; |
| 2951 | *p++ = hexdigit[(ucs >> 16) & 0x0000000F]; |
| 2952 | *p++ = hexdigit[(ucs >> 12) & 0x0000000F]; |
| 2953 | *p++ = hexdigit[(ucs >> 8) & 0x0000000F]; |
| 2954 | *p++ = hexdigit[(ucs >> 4) & 0x0000000F]; |
| 2955 | *p++ = hexdigit[ucs & 0x0000000F]; |
| 2956 | continue; |
| 2957 | } |
| 2958 | /* Fall through: isolated surrogates are copied as-is */ |
| 2959 | s--; |
| 2960 | size++; |
| 2961 | } |
Neal Norwitz | 17753ec | 2006-08-21 22:21:19 +0000 | [diff] [blame] | 2962 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2963 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2964 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2965 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2966 | *p++ = '\\'; |
| 2967 | *p++ = 'u'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2968 | *p++ = hexdigit[(ch >> 12) & 0x000F]; |
| 2969 | *p++ = hexdigit[(ch >> 8) & 0x000F]; |
| 2970 | *p++ = hexdigit[(ch >> 4) & 0x000F]; |
| 2971 | *p++ = hexdigit[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2972 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2973 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2974 | /* Map special whitespace to '\t', \n', '\r' */ |
| 2975 | else if (ch == '\t') { |
| 2976 | *p++ = '\\'; |
| 2977 | *p++ = 't'; |
| 2978 | } |
| 2979 | else if (ch == '\n') { |
| 2980 | *p++ = '\\'; |
| 2981 | *p++ = 'n'; |
| 2982 | } |
| 2983 | else if (ch == '\r') { |
| 2984 | *p++ = '\\'; |
| 2985 | *p++ = 'r'; |
| 2986 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2987 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2988 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 2989 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2990 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2991 | *p++ = 'x'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2992 | *p++ = hexdigit[(ch >> 4) & 0x000F]; |
| 2993 | *p++ = hexdigit[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2994 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2995 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2996 | /* Copy everything else as-is */ |
| 2997 | else |
| 2998 | *p++ = (char) ch; |
| 2999 | } |
| 3000 | if (quotes) |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3001 | *p++ = PyString_AS_STRING(repr)[1]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3002 | |
| 3003 | *p = '\0'; |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 3004 | _PyString_Resize(&repr, p - PyString_AS_STRING(repr)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3005 | return repr; |
| 3006 | } |
| 3007 | |
| 3008 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3009 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3010 | { |
| 3011 | return unicodeescape_string(s, size, 0); |
| 3012 | } |
| 3013 | |
| 3014 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
| 3015 | { |
| 3016 | if (!PyUnicode_Check(unicode)) { |
| 3017 | PyErr_BadArgument(); |
| 3018 | return NULL; |
| 3019 | } |
| 3020 | return PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3021 | PyUnicode_GET_SIZE(unicode)); |
| 3022 | } |
| 3023 | |
| 3024 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 3025 | |
| 3026 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3027 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3028 | const char *errors) |
| 3029 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3030 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3031 | Py_ssize_t startinpos; |
| 3032 | Py_ssize_t endinpos; |
| 3033 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3034 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3035 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3036 | const char *end; |
| 3037 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3038 | PyObject *errorHandler = NULL; |
| 3039 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3040 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3041 | /* Escaped strings will always be longer than the resulting |
| 3042 | 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] | 3043 | length after conversion to the true value. (But decoding error |
| 3044 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3045 | v = _PyUnicode_New(size); |
| 3046 | if (v == NULL) |
| 3047 | goto onError; |
| 3048 | if (size == 0) |
| 3049 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3050 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3051 | end = s + size; |
| 3052 | while (s < end) { |
| 3053 | unsigned char c; |
Martin v. Löwis | 047c05e | 2002-03-21 08:55:28 +0000 | [diff] [blame] | 3054 | Py_UCS4 x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3055 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3056 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3057 | |
| 3058 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3059 | if (*s != '\\') { |
| 3060 | *p++ = (unsigned char)*s++; |
| 3061 | continue; |
| 3062 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3063 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3064 | |
| 3065 | /* \u-escapes are only interpreted iff the number of leading |
| 3066 | backslashes if odd */ |
| 3067 | bs = s; |
| 3068 | for (;s < end;) { |
| 3069 | if (*s != '\\') |
| 3070 | break; |
| 3071 | *p++ = (unsigned char)*s++; |
| 3072 | } |
| 3073 | if (((s - bs) & 1) == 0 || |
| 3074 | s >= end || |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3075 | (*s != 'u' && *s != 'U')) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3076 | continue; |
| 3077 | } |
| 3078 | p--; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3079 | count = *s=='u' ? 4 : 8; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3080 | s++; |
| 3081 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3082 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3083 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3084 | for (x = 0, i = 0; i < count; ++i, ++s) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3085 | c = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3086 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3087 | endinpos = s-starts; |
| 3088 | if (unicode_decode_call_errorhandler( |
| 3089 | errors, &errorHandler, |
| 3090 | "rawunicodeescape", "truncated \\uXXXX", |
| 3091 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 3092 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3093 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3094 | goto nextByte; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3095 | } |
| 3096 | x = (x<<4) & ~0xF; |
| 3097 | if (c >= '0' && c <= '9') |
| 3098 | x += c - '0'; |
| 3099 | else if (c >= 'a' && c <= 'f') |
| 3100 | x += 10 + c - 'a'; |
| 3101 | else |
| 3102 | x += 10 + c - 'A'; |
| 3103 | } |
Amaury Forgeot d'Arc | 9a0d346 | 2008-03-23 09:55:29 +0000 | [diff] [blame] | 3104 | if (x <= 0xffff) |
| 3105 | /* UCS-2 character */ |
| 3106 | *p++ = (Py_UNICODE) x; |
| 3107 | else if (x <= 0x10ffff) { |
| 3108 | /* UCS-4 character. Either store directly, or as |
| 3109 | surrogate pair. */ |
| 3110 | #ifdef Py_UNICODE_WIDE |
Amaury Forgeot d'Arc | fac02fa | 2008-03-24 21:04:10 +0000 | [diff] [blame] | 3111 | *p++ = (Py_UNICODE) x; |
Amaury Forgeot d'Arc | 9a0d346 | 2008-03-23 09:55:29 +0000 | [diff] [blame] | 3112 | #else |
| 3113 | x -= 0x10000L; |
| 3114 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 3115 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
| 3116 | #endif |
| 3117 | } else { |
| 3118 | endinpos = s-starts; |
| 3119 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3120 | if (unicode_decode_call_errorhandler( |
| 3121 | errors, &errorHandler, |
| 3122 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
| 3123 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 3124 | (PyObject **)&v, &outpos, &p)) |
| 3125 | goto onError; |
| 3126 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3127 | nextByte: |
| 3128 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3129 | } |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 3130 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3131 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3132 | Py_XDECREF(errorHandler); |
| 3133 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3134 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3135 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3136 | onError: |
| 3137 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3138 | Py_XDECREF(errorHandler); |
| 3139 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3140 | return NULL; |
| 3141 | } |
| 3142 | |
| 3143 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3144 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3145 | { |
| 3146 | PyObject *repr; |
| 3147 | char *p; |
| 3148 | char *q; |
| 3149 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3150 | static const char *hexdigit = "0123456789abcdef"; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3151 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3152 | #ifdef Py_UNICODE_WIDE |
| 3153 | repr = PyString_FromStringAndSize(NULL, 10 * size); |
| 3154 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3155 | repr = PyString_FromStringAndSize(NULL, 6 * size); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3156 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3157 | if (repr == NULL) |
| 3158 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3159 | if (size == 0) |
| 3160 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3161 | |
| 3162 | p = q = PyString_AS_STRING(repr); |
| 3163 | while (size-- > 0) { |
| 3164 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3165 | #ifdef Py_UNICODE_WIDE |
| 3166 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 3167 | if (ch >= 0x10000) { |
| 3168 | *p++ = '\\'; |
| 3169 | *p++ = 'U'; |
| 3170 | *p++ = hexdigit[(ch >> 28) & 0xf]; |
| 3171 | *p++ = hexdigit[(ch >> 24) & 0xf]; |
| 3172 | *p++ = hexdigit[(ch >> 20) & 0xf]; |
| 3173 | *p++ = hexdigit[(ch >> 16) & 0xf]; |
| 3174 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 3175 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 3176 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 3177 | *p++ = hexdigit[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3178 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3179 | else |
Amaury Forgeot d'Arc | 9a0d346 | 2008-03-23 09:55:29 +0000 | [diff] [blame] | 3180 | #else |
| 3181 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 3182 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 3183 | Py_UNICODE ch2; |
| 3184 | Py_UCS4 ucs; |
| 3185 | |
| 3186 | ch2 = *s++; |
| 3187 | size--; |
| 3188 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 3189 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 3190 | *p++ = '\\'; |
| 3191 | *p++ = 'U'; |
| 3192 | *p++ = hexdigit[(ucs >> 28) & 0xf]; |
| 3193 | *p++ = hexdigit[(ucs >> 24) & 0xf]; |
| 3194 | *p++ = hexdigit[(ucs >> 20) & 0xf]; |
| 3195 | *p++ = hexdigit[(ucs >> 16) & 0xf]; |
| 3196 | *p++ = hexdigit[(ucs >> 12) & 0xf]; |
| 3197 | *p++ = hexdigit[(ucs >> 8) & 0xf]; |
| 3198 | *p++ = hexdigit[(ucs >> 4) & 0xf]; |
| 3199 | *p++ = hexdigit[ucs & 0xf]; |
| 3200 | continue; |
| 3201 | } |
| 3202 | /* Fall through: isolated surrogates are copied as-is */ |
| 3203 | s--; |
| 3204 | size++; |
| 3205 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3206 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3207 | /* Map 16-bit characters to '\uxxxx' */ |
| 3208 | if (ch >= 256) { |
| 3209 | *p++ = '\\'; |
| 3210 | *p++ = 'u'; |
| 3211 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 3212 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 3213 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 3214 | *p++ = hexdigit[ch & 15]; |
| 3215 | } |
| 3216 | /* Copy everything else as-is */ |
| 3217 | else |
| 3218 | *p++ = (char) ch; |
| 3219 | } |
| 3220 | *p = '\0'; |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 3221 | _PyString_Resize(&repr, p - q); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3222 | return repr; |
| 3223 | } |
| 3224 | |
| 3225 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 3226 | { |
| 3227 | if (!PyUnicode_Check(unicode)) { |
| 3228 | PyErr_BadArgument(); |
| 3229 | return NULL; |
| 3230 | } |
| 3231 | return PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3232 | PyUnicode_GET_SIZE(unicode)); |
| 3233 | } |
| 3234 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3235 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 3236 | |
| 3237 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3238 | Py_ssize_t size, |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3239 | const char *errors) |
| 3240 | { |
| 3241 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3242 | Py_ssize_t startinpos; |
| 3243 | Py_ssize_t endinpos; |
| 3244 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3245 | PyUnicodeObject *v; |
| 3246 | Py_UNICODE *p; |
| 3247 | const char *end; |
| 3248 | const char *reason; |
| 3249 | PyObject *errorHandler = NULL; |
| 3250 | PyObject *exc = NULL; |
| 3251 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 3252 | #ifdef Py_UNICODE_WIDE |
| 3253 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 3254 | #endif |
| 3255 | |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 3256 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3257 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 3258 | if (v == NULL) |
| 3259 | goto onError; |
| 3260 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
| 3261 | return (PyObject *)v; |
| 3262 | p = PyUnicode_AS_UNICODE(v); |
| 3263 | end = s + size; |
| 3264 | |
| 3265 | while (s < end) { |
Neal Norwitz | 1004a53 | 2006-05-15 07:17:23 +0000 | [diff] [blame] | 3266 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3267 | /* We have to sanity check the raw data, otherwise doom looms for |
| 3268 | some malformed UCS-4 data. */ |
| 3269 | if ( |
| 3270 | #ifdef Py_UNICODE_WIDE |
| 3271 | *p > unimax || *p < 0 || |
| 3272 | #endif |
| 3273 | end-s < Py_UNICODE_SIZE |
| 3274 | ) |
| 3275 | { |
| 3276 | startinpos = s - starts; |
| 3277 | if (end-s < Py_UNICODE_SIZE) { |
| 3278 | endinpos = end-starts; |
| 3279 | reason = "truncated input"; |
| 3280 | } |
| 3281 | else { |
| 3282 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 3283 | reason = "illegal code point (> 0x10FFFF)"; |
| 3284 | } |
| 3285 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 3286 | if (unicode_decode_call_errorhandler( |
| 3287 | errors, &errorHandler, |
| 3288 | "unicode_internal", reason, |
| 3289 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 3290 | (PyObject **)&v, &outpos, &p)) { |
| 3291 | goto onError; |
| 3292 | } |
| 3293 | } |
| 3294 | else { |
| 3295 | p++; |
| 3296 | s += Py_UNICODE_SIZE; |
| 3297 | } |
| 3298 | } |
| 3299 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 3300 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3301 | goto onError; |
| 3302 | Py_XDECREF(errorHandler); |
| 3303 | Py_XDECREF(exc); |
| 3304 | return (PyObject *)v; |
| 3305 | |
| 3306 | onError: |
| 3307 | Py_XDECREF(v); |
| 3308 | Py_XDECREF(errorHandler); |
| 3309 | Py_XDECREF(exc); |
| 3310 | return NULL; |
| 3311 | } |
| 3312 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3313 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 3314 | |
| 3315 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3316 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3317 | const char *errors) |
| 3318 | { |
| 3319 | PyUnicodeObject *v; |
| 3320 | Py_UNICODE *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3321 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3322 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3323 | if (size == 1) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3324 | Py_UNICODE r = *(unsigned char*)s; |
| 3325 | return PyUnicode_FromUnicode(&r, 1); |
| 3326 | } |
| 3327 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3328 | v = _PyUnicode_New(size); |
| 3329 | if (v == NULL) |
| 3330 | goto onError; |
| 3331 | if (size == 0) |
| 3332 | return (PyObject *)v; |
| 3333 | p = PyUnicode_AS_UNICODE(v); |
| 3334 | while (size-- > 0) |
| 3335 | *p++ = (unsigned char)*s++; |
| 3336 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3337 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3338 | onError: |
| 3339 | Py_XDECREF(v); |
| 3340 | return NULL; |
| 3341 | } |
| 3342 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3343 | /* create or adjust a UnicodeEncodeError */ |
| 3344 | static void make_encode_exception(PyObject **exceptionObject, |
| 3345 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3346 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3347 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3348 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3349 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3350 | if (*exceptionObject == NULL) { |
| 3351 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 3352 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3353 | } |
| 3354 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3355 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 3356 | goto onError; |
| 3357 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 3358 | goto onError; |
| 3359 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 3360 | goto onError; |
| 3361 | return; |
| 3362 | onError: |
| 3363 | Py_DECREF(*exceptionObject); |
| 3364 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3365 | } |
| 3366 | } |
| 3367 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3368 | /* raises a UnicodeEncodeError */ |
| 3369 | static void raise_encode_exception(PyObject **exceptionObject, |
| 3370 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3371 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3372 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3373 | const char *reason) |
| 3374 | { |
| 3375 | make_encode_exception(exceptionObject, |
| 3376 | encoding, unicode, size, startpos, endpos, reason); |
| 3377 | if (*exceptionObject != NULL) |
| 3378 | PyCodec_StrictErrors(*exceptionObject); |
| 3379 | } |
| 3380 | |
| 3381 | /* error handling callback helper: |
| 3382 | build arguments, call the callback and check the arguments, |
| 3383 | put the result into newpos and return the replacement string, which |
| 3384 | has to be freed by the caller */ |
| 3385 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
| 3386 | PyObject **errorHandler, |
| 3387 | const char *encoding, const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3388 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 3389 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3390 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3391 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3392 | 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] | 3393 | |
| 3394 | PyObject *restuple; |
| 3395 | PyObject *resunicode; |
| 3396 | |
| 3397 | if (*errorHandler == NULL) { |
| 3398 | *errorHandler = PyCodec_LookupError(errors); |
| 3399 | if (*errorHandler == NULL) |
| 3400 | return NULL; |
| 3401 | } |
| 3402 | |
| 3403 | make_encode_exception(exceptionObject, |
| 3404 | encoding, unicode, size, startpos, endpos, reason); |
| 3405 | if (*exceptionObject == NULL) |
| 3406 | return NULL; |
| 3407 | |
| 3408 | restuple = PyObject_CallFunctionObjArgs( |
| 3409 | *errorHandler, *exceptionObject, NULL); |
| 3410 | if (restuple == NULL) |
| 3411 | return NULL; |
| 3412 | if (!PyTuple_Check(restuple)) { |
| 3413 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 3414 | Py_DECREF(restuple); |
| 3415 | return NULL; |
| 3416 | } |
| 3417 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
| 3418 | &resunicode, newpos)) { |
| 3419 | Py_DECREF(restuple); |
| 3420 | return NULL; |
| 3421 | } |
| 3422 | if (*newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3423 | *newpos = size+*newpos; |
| 3424 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 3425 | 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] | 3426 | Py_DECREF(restuple); |
| 3427 | return NULL; |
| 3428 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3429 | Py_INCREF(resunicode); |
| 3430 | Py_DECREF(restuple); |
| 3431 | return resunicode; |
| 3432 | } |
| 3433 | |
| 3434 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3435 | Py_ssize_t size, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3436 | const char *errors, |
| 3437 | int limit) |
| 3438 | { |
| 3439 | /* output object */ |
| 3440 | PyObject *res; |
| 3441 | /* pointers to the beginning and end+1 of input */ |
| 3442 | const Py_UNICODE *startp = p; |
| 3443 | const Py_UNICODE *endp = p + size; |
| 3444 | /* pointer to the beginning of the unencodable characters */ |
| 3445 | /* const Py_UNICODE *badp = NULL; */ |
| 3446 | /* pointer into the output */ |
| 3447 | char *str; |
| 3448 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3449 | Py_ssize_t respos = 0; |
| 3450 | Py_ssize_t ressize; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 3451 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 3452 | 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] | 3453 | PyObject *errorHandler = NULL; |
| 3454 | PyObject *exc = NULL; |
| 3455 | /* the following variable is used for caching string comparisons |
| 3456 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 3457 | int known_errorHandler = -1; |
| 3458 | |
| 3459 | /* allocate enough for a simple encoding without |
| 3460 | replacements, if we need more, we'll resize */ |
| 3461 | res = PyString_FromStringAndSize(NULL, size); |
| 3462 | if (res == NULL) |
| 3463 | goto onError; |
| 3464 | if (size == 0) |
| 3465 | return res; |
| 3466 | str = PyString_AS_STRING(res); |
| 3467 | ressize = size; |
| 3468 | |
| 3469 | while (p<endp) { |
| 3470 | Py_UNICODE c = *p; |
| 3471 | |
| 3472 | /* can we encode this? */ |
| 3473 | if (c<limit) { |
| 3474 | /* no overflow check, because we know that the space is enough */ |
| 3475 | *str++ = (char)c; |
| 3476 | ++p; |
| 3477 | } |
| 3478 | else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3479 | Py_ssize_t unicodepos = p-startp; |
| 3480 | Py_ssize_t requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3481 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3482 | Py_ssize_t repsize; |
| 3483 | Py_ssize_t newpos; |
| 3484 | Py_ssize_t respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3485 | Py_UNICODE *uni2; |
| 3486 | /* startpos for collecting unencodable chars */ |
| 3487 | const Py_UNICODE *collstart = p; |
| 3488 | const Py_UNICODE *collend = p; |
| 3489 | /* find all unecodable characters */ |
| 3490 | while ((collend < endp) && ((*collend)>=limit)) |
| 3491 | ++collend; |
| 3492 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 3493 | if (known_errorHandler==-1) { |
| 3494 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3495 | known_errorHandler = 1; |
| 3496 | else if (!strcmp(errors, "replace")) |
| 3497 | known_errorHandler = 2; |
| 3498 | else if (!strcmp(errors, "ignore")) |
| 3499 | known_errorHandler = 3; |
| 3500 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3501 | known_errorHandler = 4; |
| 3502 | else |
| 3503 | known_errorHandler = 0; |
| 3504 | } |
| 3505 | switch (known_errorHandler) { |
| 3506 | case 1: /* strict */ |
| 3507 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 3508 | goto onError; |
| 3509 | case 2: /* replace */ |
| 3510 | while (collstart++<collend) |
| 3511 | *str++ = '?'; /* fall through */ |
| 3512 | case 3: /* ignore */ |
| 3513 | p = collend; |
| 3514 | break; |
| 3515 | case 4: /* xmlcharrefreplace */ |
| 3516 | respos = str-PyString_AS_STRING(res); |
| 3517 | /* determine replacement size (temporarily (mis)uses p) */ |
| 3518 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 3519 | if (*p<10) |
| 3520 | repsize += 2+1+1; |
| 3521 | else if (*p<100) |
| 3522 | repsize += 2+2+1; |
| 3523 | else if (*p<1000) |
| 3524 | repsize += 2+3+1; |
| 3525 | else if (*p<10000) |
| 3526 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 3527 | #ifndef Py_UNICODE_WIDE |
| 3528 | else |
| 3529 | repsize += 2+5+1; |
| 3530 | #else |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3531 | else if (*p<100000) |
| 3532 | repsize += 2+5+1; |
| 3533 | else if (*p<1000000) |
| 3534 | repsize += 2+6+1; |
| 3535 | else |
| 3536 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3537 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3538 | } |
| 3539 | requiredsize = respos+repsize+(endp-collend); |
| 3540 | if (requiredsize > ressize) { |
| 3541 | if (requiredsize<2*ressize) |
| 3542 | requiredsize = 2*ressize; |
| 3543 | if (_PyString_Resize(&res, requiredsize)) |
| 3544 | goto onError; |
| 3545 | str = PyString_AS_STRING(res) + respos; |
| 3546 | ressize = requiredsize; |
| 3547 | } |
| 3548 | /* generate replacement (temporarily (mis)uses p) */ |
| 3549 | for (p = collstart; p < collend; ++p) { |
| 3550 | str += sprintf(str, "&#%d;", (int)*p); |
| 3551 | } |
| 3552 | p = collend; |
| 3553 | break; |
| 3554 | default: |
| 3555 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 3556 | encoding, reason, startp, size, &exc, |
| 3557 | collstart-startp, collend-startp, &newpos); |
| 3558 | if (repunicode == NULL) |
| 3559 | goto onError; |
| 3560 | /* need more space? (at least enough for what we |
| 3561 | have+the replacement+the rest of the string, so |
| 3562 | we won't have to check space for encodable characters) */ |
| 3563 | respos = str-PyString_AS_STRING(res); |
| 3564 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3565 | requiredsize = respos+repsize+(endp-collend); |
| 3566 | if (requiredsize > ressize) { |
| 3567 | if (requiredsize<2*ressize) |
| 3568 | requiredsize = 2*ressize; |
| 3569 | if (_PyString_Resize(&res, requiredsize)) { |
| 3570 | Py_DECREF(repunicode); |
| 3571 | goto onError; |
| 3572 | } |
| 3573 | str = PyString_AS_STRING(res) + respos; |
| 3574 | ressize = requiredsize; |
| 3575 | } |
| 3576 | /* check if there is anything unencodable in the replacement |
| 3577 | and copy it to the output */ |
| 3578 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 3579 | c = *uni2; |
| 3580 | if (c >= limit) { |
| 3581 | raise_encode_exception(&exc, encoding, startp, size, |
| 3582 | unicodepos, unicodepos+1, reason); |
| 3583 | Py_DECREF(repunicode); |
| 3584 | goto onError; |
| 3585 | } |
| 3586 | *str = (char)c; |
| 3587 | } |
| 3588 | p = startp + newpos; |
| 3589 | Py_DECREF(repunicode); |
| 3590 | } |
| 3591 | } |
| 3592 | } |
| 3593 | /* Resize if we allocated to much */ |
| 3594 | respos = str-PyString_AS_STRING(res); |
| 3595 | if (respos<ressize) |
| 3596 | /* If this falls res will be NULL */ |
| 3597 | _PyString_Resize(&res, respos); |
| 3598 | Py_XDECREF(errorHandler); |
| 3599 | Py_XDECREF(exc); |
| 3600 | return res; |
| 3601 | |
| 3602 | onError: |
| 3603 | Py_XDECREF(res); |
| 3604 | Py_XDECREF(errorHandler); |
| 3605 | Py_XDECREF(exc); |
| 3606 | return NULL; |
| 3607 | } |
| 3608 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3609 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3610 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3611 | const char *errors) |
| 3612 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3613 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3614 | } |
| 3615 | |
| 3616 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 3617 | { |
| 3618 | if (!PyUnicode_Check(unicode)) { |
| 3619 | PyErr_BadArgument(); |
| 3620 | return NULL; |
| 3621 | } |
| 3622 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 3623 | PyUnicode_GET_SIZE(unicode), |
| 3624 | NULL); |
| 3625 | } |
| 3626 | |
| 3627 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 3628 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3629 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3630 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3631 | const char *errors) |
| 3632 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3633 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3634 | PyUnicodeObject *v; |
| 3635 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3636 | Py_ssize_t startinpos; |
| 3637 | Py_ssize_t endinpos; |
| 3638 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3639 | const char *e; |
| 3640 | PyObject *errorHandler = NULL; |
| 3641 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3642 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3643 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3644 | if (size == 1 && *(unsigned char*)s < 128) { |
| 3645 | Py_UNICODE r = *(unsigned char*)s; |
| 3646 | return PyUnicode_FromUnicode(&r, 1); |
| 3647 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3648 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3649 | v = _PyUnicode_New(size); |
| 3650 | if (v == NULL) |
| 3651 | goto onError; |
| 3652 | if (size == 0) |
| 3653 | return (PyObject *)v; |
| 3654 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3655 | e = s + size; |
| 3656 | while (s < e) { |
| 3657 | register unsigned char c = (unsigned char)*s; |
| 3658 | if (c < 128) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3659 | *p++ = c; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3660 | ++s; |
| 3661 | } |
| 3662 | else { |
| 3663 | startinpos = s-starts; |
| 3664 | endinpos = startinpos + 1; |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 3665 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3666 | if (unicode_decode_call_errorhandler( |
| 3667 | errors, &errorHandler, |
| 3668 | "ascii", "ordinal not in range(128)", |
| 3669 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 3670 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3671 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3672 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3673 | } |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3674 | if (p - PyUnicode_AS_UNICODE(v) < PyString_GET_SIZE(v)) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 3675 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3676 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3677 | Py_XDECREF(errorHandler); |
| 3678 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3679 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3680 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3681 | onError: |
| 3682 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3683 | Py_XDECREF(errorHandler); |
| 3684 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3685 | return NULL; |
| 3686 | } |
| 3687 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3688 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3689 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3690 | const char *errors) |
| 3691 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3692 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3693 | } |
| 3694 | |
| 3695 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 3696 | { |
| 3697 | if (!PyUnicode_Check(unicode)) { |
| 3698 | PyErr_BadArgument(); |
| 3699 | return NULL; |
| 3700 | } |
| 3701 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 3702 | PyUnicode_GET_SIZE(unicode), |
| 3703 | NULL); |
| 3704 | } |
| 3705 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3706 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3707 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3708 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3709 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3710 | #if SIZEOF_INT < SIZEOF_SSIZE_T |
| 3711 | #define NEED_RETRY |
| 3712 | #endif |
| 3713 | |
| 3714 | /* XXX This code is limited to "true" double-byte encodings, as |
| 3715 | a) it assumes an incomplete character consists of a single byte, and |
| 3716 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
| 3717 | encodings, see IsDBCSLeadByteEx documentation. */ |
| 3718 | |
| 3719 | static int is_dbcs_lead_byte(const char *s, int offset) |
| 3720 | { |
| 3721 | const char *curr = s + offset; |
| 3722 | |
| 3723 | if (IsDBCSLeadByte(*curr)) { |
| 3724 | const char *prev = CharPrev(s, curr); |
| 3725 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
| 3726 | } |
| 3727 | return 0; |
| 3728 | } |
| 3729 | |
| 3730 | /* |
| 3731 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 3732 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 3733 | */ |
| 3734 | static int decode_mbcs(PyUnicodeObject **v, |
| 3735 | const char *s, /* MBCS string */ |
| 3736 | int size, /* sizeof MBCS string */ |
| 3737 | int final) |
| 3738 | { |
| 3739 | Py_UNICODE *p; |
| 3740 | Py_ssize_t n = 0; |
| 3741 | int usize = 0; |
| 3742 | |
| 3743 | assert(size >= 0); |
| 3744 | |
| 3745 | /* Skip trailing lead-byte unless 'final' is set */ |
| 3746 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
| 3747 | --size; |
| 3748 | |
| 3749 | /* First get the size of the result */ |
| 3750 | if (size > 0) { |
| 3751 | usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); |
| 3752 | if (usize == 0) { |
| 3753 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3754 | return -1; |
| 3755 | } |
| 3756 | } |
| 3757 | |
| 3758 | if (*v == NULL) { |
| 3759 | /* Create unicode object */ |
| 3760 | *v = _PyUnicode_New(usize); |
| 3761 | if (*v == NULL) |
| 3762 | return -1; |
| 3763 | } |
| 3764 | else { |
| 3765 | /* Extend unicode object */ |
| 3766 | n = PyUnicode_GET_SIZE(*v); |
| 3767 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 3768 | return -1; |
| 3769 | } |
| 3770 | |
| 3771 | /* Do the conversion */ |
| 3772 | if (size > 0) { |
| 3773 | p = PyUnicode_AS_UNICODE(*v) + n; |
| 3774 | if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { |
| 3775 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3776 | return -1; |
| 3777 | } |
| 3778 | } |
| 3779 | |
| 3780 | return size; |
| 3781 | } |
| 3782 | |
| 3783 | PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
| 3784 | Py_ssize_t size, |
| 3785 | const char *errors, |
| 3786 | Py_ssize_t *consumed) |
| 3787 | { |
| 3788 | PyUnicodeObject *v = NULL; |
| 3789 | int done; |
| 3790 | |
| 3791 | if (consumed) |
| 3792 | *consumed = 0; |
| 3793 | |
| 3794 | #ifdef NEED_RETRY |
| 3795 | retry: |
| 3796 | if (size > INT_MAX) |
| 3797 | done = decode_mbcs(&v, s, INT_MAX, 0); |
| 3798 | else |
| 3799 | #endif |
| 3800 | done = decode_mbcs(&v, s, (int)size, !consumed); |
| 3801 | |
| 3802 | if (done < 0) { |
| 3803 | Py_XDECREF(v); |
| 3804 | return NULL; |
| 3805 | } |
| 3806 | |
| 3807 | if (consumed) |
| 3808 | *consumed += done; |
| 3809 | |
| 3810 | #ifdef NEED_RETRY |
| 3811 | if (size > INT_MAX) { |
| 3812 | s += done; |
| 3813 | size -= done; |
| 3814 | goto retry; |
| 3815 | } |
| 3816 | #endif |
| 3817 | |
| 3818 | return (PyObject *)v; |
| 3819 | } |
| 3820 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3821 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3822 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3823 | const char *errors) |
| 3824 | { |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3825 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 3826 | } |
| 3827 | |
| 3828 | /* |
| 3829 | * Convert unicode into string object (MBCS). |
| 3830 | * Returns 0 if succeed, -1 otherwise. |
| 3831 | */ |
| 3832 | static int encode_mbcs(PyObject **repr, |
| 3833 | const Py_UNICODE *p, /* unicode */ |
| 3834 | int size) /* size of unicode */ |
| 3835 | { |
| 3836 | int mbcssize = 0; |
| 3837 | Py_ssize_t n = 0; |
| 3838 | |
| 3839 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3840 | |
| 3841 | /* First get the size of the result */ |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3842 | if (size > 0) { |
| 3843 | mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); |
| 3844 | if (mbcssize == 0) { |
| 3845 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3846 | return -1; |
| 3847 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3848 | } |
| 3849 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3850 | if (*repr == NULL) { |
| 3851 | /* Create string object */ |
| 3852 | *repr = PyString_FromStringAndSize(NULL, mbcssize); |
| 3853 | if (*repr == NULL) |
| 3854 | return -1; |
| 3855 | } |
| 3856 | else { |
| 3857 | /* Extend string object */ |
| 3858 | n = PyString_Size(*repr); |
| 3859 | if (_PyString_Resize(repr, n + mbcssize) < 0) |
| 3860 | return -1; |
| 3861 | } |
| 3862 | |
| 3863 | /* Do the conversion */ |
| 3864 | if (size > 0) { |
| 3865 | char *s = PyString_AS_STRING(*repr) + n; |
| 3866 | if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { |
| 3867 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3868 | return -1; |
| 3869 | } |
| 3870 | } |
| 3871 | |
| 3872 | return 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3873 | } |
| 3874 | |
| 3875 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3876 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3877 | const char *errors) |
| 3878 | { |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3879 | PyObject *repr = NULL; |
| 3880 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 3881 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3882 | #ifdef NEED_RETRY |
| 3883 | retry: |
| 3884 | if (size > INT_MAX) |
| 3885 | ret = encode_mbcs(&repr, p, INT_MAX); |
| 3886 | else |
| 3887 | #endif |
| 3888 | ret = encode_mbcs(&repr, p, (int)size); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3889 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3890 | if (ret < 0) { |
| 3891 | Py_XDECREF(repr); |
| 3892 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3893 | } |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3894 | |
| 3895 | #ifdef NEED_RETRY |
| 3896 | if (size > INT_MAX) { |
| 3897 | p += INT_MAX; |
| 3898 | size -= INT_MAX; |
| 3899 | goto retry; |
| 3900 | } |
| 3901 | #endif |
| 3902 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3903 | return repr; |
| 3904 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3905 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3906 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 3907 | { |
| 3908 | if (!PyUnicode_Check(unicode)) { |
| 3909 | PyErr_BadArgument(); |
| 3910 | return NULL; |
| 3911 | } |
| 3912 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 3913 | PyUnicode_GET_SIZE(unicode), |
| 3914 | NULL); |
| 3915 | } |
| 3916 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3917 | #undef NEED_RETRY |
| 3918 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3919 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3920 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3921 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 3922 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3923 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3924 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3925 | PyObject *mapping, |
| 3926 | const char *errors) |
| 3927 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3928 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3929 | Py_ssize_t startinpos; |
| 3930 | Py_ssize_t endinpos; |
| 3931 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3932 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3933 | PyUnicodeObject *v; |
| 3934 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3935 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3936 | PyObject *errorHandler = NULL; |
| 3937 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3938 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3939 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3940 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3941 | /* Default to Latin-1 */ |
| 3942 | if (mapping == NULL) |
| 3943 | return PyUnicode_DecodeLatin1(s, size, errors); |
| 3944 | |
| 3945 | v = _PyUnicode_New(size); |
| 3946 | if (v == NULL) |
| 3947 | goto onError; |
| 3948 | if (size == 0) |
| 3949 | return (PyObject *)v; |
| 3950 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3951 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3952 | if (PyUnicode_CheckExact(mapping)) { |
| 3953 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 3954 | maplen = PyUnicode_GET_SIZE(mapping); |
| 3955 | while (s < e) { |
| 3956 | unsigned char ch = *s; |
| 3957 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3958 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3959 | if (ch < maplen) |
| 3960 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3961 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3962 | if (x == 0xfffe) { |
| 3963 | /* undefined mapping */ |
| 3964 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3965 | startinpos = s-starts; |
| 3966 | endinpos = startinpos+1; |
| 3967 | if (unicode_decode_call_errorhandler( |
| 3968 | errors, &errorHandler, |
| 3969 | "charmap", "character maps to <undefined>", |
| 3970 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 3971 | (PyObject **)&v, &outpos, &p)) { |
| 3972 | goto onError; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 3973 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3974 | continue; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 3975 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3976 | *p++ = x; |
| 3977 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3978 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3979 | } |
| 3980 | else { |
| 3981 | while (s < e) { |
| 3982 | unsigned char ch = *s; |
| 3983 | PyObject *w, *x; |
| 3984 | |
| 3985 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 3986 | w = PyInt_FromLong((long)ch); |
| 3987 | if (w == NULL) |
| 3988 | goto onError; |
| 3989 | x = PyObject_GetItem(mapping, w); |
| 3990 | Py_DECREF(w); |
| 3991 | if (x == NULL) { |
| 3992 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 3993 | /* No mapping found means: mapping is undefined. */ |
| 3994 | PyErr_Clear(); |
| 3995 | x = Py_None; |
| 3996 | Py_INCREF(x); |
| 3997 | } else |
| 3998 | goto onError; |
| 3999 | } |
| 4000 | |
| 4001 | /* Apply mapping */ |
| 4002 | if (PyInt_Check(x)) { |
| 4003 | long value = PyInt_AS_LONG(x); |
| 4004 | if (value < 0 || value > 65535) { |
| 4005 | PyErr_SetString(PyExc_TypeError, |
| 4006 | "character mapping must be in range(65536)"); |
| 4007 | Py_DECREF(x); |
| 4008 | goto onError; |
| 4009 | } |
| 4010 | *p++ = (Py_UNICODE)value; |
| 4011 | } |
| 4012 | else if (x == Py_None) { |
| 4013 | /* undefined mapping */ |
| 4014 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4015 | startinpos = s-starts; |
| 4016 | endinpos = startinpos+1; |
| 4017 | if (unicode_decode_call_errorhandler( |
| 4018 | errors, &errorHandler, |
| 4019 | "charmap", "character maps to <undefined>", |
| 4020 | starts, size, &startinpos, &endinpos, &exc, &s, |
| 4021 | (PyObject **)&v, &outpos, &p)) { |
| 4022 | Py_DECREF(x); |
| 4023 | goto onError; |
| 4024 | } |
Walter Dörwald | d4fff17 | 2005-11-28 22:15:56 +0000 | [diff] [blame] | 4025 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4026 | continue; |
| 4027 | } |
| 4028 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4029 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4030 | |
| 4031 | if (targetsize == 1) |
| 4032 | /* 1-1 mapping */ |
| 4033 | *p++ = *PyUnicode_AS_UNICODE(x); |
| 4034 | |
| 4035 | else if (targetsize > 1) { |
| 4036 | /* 1-n mapping */ |
| 4037 | if (targetsize > extrachars) { |
| 4038 | /* resize first */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4039 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 4040 | Py_ssize_t needed = (targetsize - extrachars) + \ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4041 | (targetsize << 2); |
| 4042 | extrachars += needed; |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 4043 | /* XXX overflow detection missing */ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4044 | if (_PyUnicode_Resize(&v, |
| 4045 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 4046 | Py_DECREF(x); |
| 4047 | goto onError; |
| 4048 | } |
| 4049 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 4050 | } |
| 4051 | Py_UNICODE_COPY(p, |
| 4052 | PyUnicode_AS_UNICODE(x), |
| 4053 | targetsize); |
| 4054 | p += targetsize; |
| 4055 | extrachars -= targetsize; |
| 4056 | } |
| 4057 | /* 1-0 mapping: skip the character */ |
| 4058 | } |
| 4059 | else { |
| 4060 | /* wrong return value */ |
| 4061 | PyErr_SetString(PyExc_TypeError, |
| 4062 | "character mapping must return integer, None or unicode"); |
| 4063 | Py_DECREF(x); |
| 4064 | goto onError; |
| 4065 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4066 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4067 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4068 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4069 | } |
| 4070 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4071 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4072 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4073 | Py_XDECREF(errorHandler); |
| 4074 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4075 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4076 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4077 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4078 | Py_XDECREF(errorHandler); |
| 4079 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4080 | Py_XDECREF(v); |
| 4081 | return NULL; |
| 4082 | } |
| 4083 | |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4084 | /* Charmap encoding: the lookup table */ |
| 4085 | |
| 4086 | struct encoding_map{ |
| 4087 | PyObject_HEAD |
| 4088 | unsigned char level1[32]; |
| 4089 | int count2, count3; |
| 4090 | unsigned char level23[1]; |
| 4091 | }; |
| 4092 | |
| 4093 | static PyObject* |
| 4094 | encoding_map_size(PyObject *obj, PyObject* args) |
| 4095 | { |
| 4096 | struct encoding_map *map = (struct encoding_map*)obj; |
| 4097 | return PyInt_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
| 4098 | 128*map->count3); |
| 4099 | } |
| 4100 | |
| 4101 | static PyMethodDef encoding_map_methods[] = { |
| 4102 | {"size", encoding_map_size, METH_NOARGS, |
| 4103 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 4104 | { 0 } |
| 4105 | }; |
| 4106 | |
| 4107 | static void |
| 4108 | encoding_map_dealloc(PyObject* o) |
| 4109 | { |
| 4110 | PyObject_FREE(o); |
| 4111 | } |
| 4112 | |
| 4113 | static PyTypeObject EncodingMapType = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 4114 | PyVarObject_HEAD_INIT(NULL, 0) |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4115 | "EncodingMap", /*tp_name*/ |
| 4116 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 4117 | 0, /*tp_itemsize*/ |
| 4118 | /* methods */ |
| 4119 | encoding_map_dealloc, /*tp_dealloc*/ |
| 4120 | 0, /*tp_print*/ |
| 4121 | 0, /*tp_getattr*/ |
| 4122 | 0, /*tp_setattr*/ |
| 4123 | 0, /*tp_compare*/ |
| 4124 | 0, /*tp_repr*/ |
| 4125 | 0, /*tp_as_number*/ |
| 4126 | 0, /*tp_as_sequence*/ |
| 4127 | 0, /*tp_as_mapping*/ |
| 4128 | 0, /*tp_hash*/ |
| 4129 | 0, /*tp_call*/ |
| 4130 | 0, /*tp_str*/ |
| 4131 | 0, /*tp_getattro*/ |
| 4132 | 0, /*tp_setattro*/ |
| 4133 | 0, /*tp_as_buffer*/ |
| 4134 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 4135 | 0, /*tp_doc*/ |
| 4136 | 0, /*tp_traverse*/ |
| 4137 | 0, /*tp_clear*/ |
| 4138 | 0, /*tp_richcompare*/ |
| 4139 | 0, /*tp_weaklistoffset*/ |
| 4140 | 0, /*tp_iter*/ |
| 4141 | 0, /*tp_iternext*/ |
| 4142 | encoding_map_methods, /*tp_methods*/ |
| 4143 | 0, /*tp_members*/ |
| 4144 | 0, /*tp_getset*/ |
| 4145 | 0, /*tp_base*/ |
| 4146 | 0, /*tp_dict*/ |
| 4147 | 0, /*tp_descr_get*/ |
| 4148 | 0, /*tp_descr_set*/ |
| 4149 | 0, /*tp_dictoffset*/ |
| 4150 | 0, /*tp_init*/ |
| 4151 | 0, /*tp_alloc*/ |
| 4152 | 0, /*tp_new*/ |
| 4153 | 0, /*tp_free*/ |
| 4154 | 0, /*tp_is_gc*/ |
| 4155 | }; |
| 4156 | |
| 4157 | PyObject* |
| 4158 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 4159 | { |
| 4160 | Py_UNICODE *decode; |
| 4161 | PyObject *result; |
| 4162 | struct encoding_map *mresult; |
| 4163 | int i; |
| 4164 | int need_dict = 0; |
| 4165 | unsigned char level1[32]; |
| 4166 | unsigned char level2[512]; |
| 4167 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 4168 | int count2 = 0, count3 = 0; |
| 4169 | |
| 4170 | if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { |
| 4171 | PyErr_BadArgument(); |
| 4172 | return NULL; |
| 4173 | } |
| 4174 | decode = PyUnicode_AS_UNICODE(string); |
| 4175 | memset(level1, 0xFF, sizeof level1); |
| 4176 | memset(level2, 0xFF, sizeof level2); |
| 4177 | |
| 4178 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 4179 | or if there are non-BMP characters, we need to use |
| 4180 | a mapping dictionary. */ |
| 4181 | if (decode[0] != 0) |
| 4182 | need_dict = 1; |
| 4183 | for (i = 1; i < 256; i++) { |
| 4184 | int l1, l2; |
| 4185 | if (decode[i] == 0 |
| 4186 | #ifdef Py_UNICODE_WIDE |
| 4187 | || decode[i] > 0xFFFF |
| 4188 | #endif |
| 4189 | ) { |
| 4190 | need_dict = 1; |
| 4191 | break; |
| 4192 | } |
| 4193 | if (decode[i] == 0xFFFE) |
| 4194 | /* unmapped character */ |
| 4195 | continue; |
| 4196 | l1 = decode[i] >> 11; |
| 4197 | l2 = decode[i] >> 7; |
| 4198 | if (level1[l1] == 0xFF) |
| 4199 | level1[l1] = count2++; |
| 4200 | if (level2[l2] == 0xFF) |
| 4201 | level2[l2] = count3++; |
| 4202 | } |
| 4203 | |
| 4204 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 4205 | need_dict = 1; |
| 4206 | |
| 4207 | if (need_dict) { |
| 4208 | PyObject *result = PyDict_New(); |
| 4209 | PyObject *key, *value; |
| 4210 | if (!result) |
| 4211 | return NULL; |
| 4212 | for (i = 0; i < 256; i++) { |
| 4213 | key = value = NULL; |
| 4214 | key = PyInt_FromLong(decode[i]); |
| 4215 | value = PyInt_FromLong(i); |
| 4216 | if (!key || !value) |
| 4217 | goto failed1; |
| 4218 | if (PyDict_SetItem(result, key, value) == -1) |
| 4219 | goto failed1; |
Georg Brandl | 9f16760 | 2006-06-04 21:46:16 +0000 | [diff] [blame] | 4220 | Py_DECREF(key); |
| 4221 | Py_DECREF(value); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4222 | } |
| 4223 | return result; |
| 4224 | failed1: |
| 4225 | Py_XDECREF(key); |
| 4226 | Py_XDECREF(value); |
| 4227 | Py_DECREF(result); |
| 4228 | return NULL; |
| 4229 | } |
| 4230 | |
| 4231 | /* Create a three-level trie */ |
| 4232 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 4233 | 16*count2 + 128*count3 - 1); |
| 4234 | if (!result) |
| 4235 | return PyErr_NoMemory(); |
| 4236 | PyObject_Init(result, &EncodingMapType); |
| 4237 | mresult = (struct encoding_map*)result; |
| 4238 | mresult->count2 = count2; |
| 4239 | mresult->count3 = count3; |
| 4240 | mlevel1 = mresult->level1; |
| 4241 | mlevel2 = mresult->level23; |
| 4242 | mlevel3 = mresult->level23 + 16*count2; |
| 4243 | memcpy(mlevel1, level1, 32); |
| 4244 | memset(mlevel2, 0xFF, 16*count2); |
| 4245 | memset(mlevel3, 0, 128*count3); |
| 4246 | count3 = 0; |
| 4247 | for (i = 1; i < 256; i++) { |
| 4248 | int o1, o2, o3, i2, i3; |
| 4249 | if (decode[i] == 0xFFFE) |
| 4250 | /* unmapped character */ |
| 4251 | continue; |
| 4252 | o1 = decode[i]>>11; |
| 4253 | o2 = (decode[i]>>7) & 0xF; |
| 4254 | i2 = 16*mlevel1[o1] + o2; |
| 4255 | if (mlevel2[i2] == 0xFF) |
| 4256 | mlevel2[i2] = count3++; |
| 4257 | o3 = decode[i] & 0x7F; |
| 4258 | i3 = 128*mlevel2[i2] + o3; |
| 4259 | mlevel3[i3] = i; |
| 4260 | } |
| 4261 | return result; |
| 4262 | } |
| 4263 | |
| 4264 | static int |
| 4265 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 4266 | { |
| 4267 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 4268 | int l1 = c>>11; |
| 4269 | int l2 = (c>>7) & 0xF; |
| 4270 | int l3 = c & 0x7F; |
| 4271 | int i; |
| 4272 | |
| 4273 | #ifdef Py_UNICODE_WIDE |
| 4274 | if (c > 0xFFFF) { |
| 4275 | return -1; |
| 4276 | } |
| 4277 | #endif |
| 4278 | if (c == 0) |
| 4279 | return 0; |
| 4280 | /* level 1*/ |
| 4281 | i = map->level1[l1]; |
| 4282 | if (i == 0xFF) { |
| 4283 | return -1; |
| 4284 | } |
| 4285 | /* level 2*/ |
| 4286 | i = map->level23[16*i+l2]; |
| 4287 | if (i == 0xFF) { |
| 4288 | return -1; |
| 4289 | } |
| 4290 | /* level 3 */ |
| 4291 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 4292 | if (i == 0) { |
| 4293 | return -1; |
| 4294 | } |
| 4295 | return i; |
| 4296 | } |
| 4297 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4298 | /* Lookup the character ch in the mapping. If the character |
| 4299 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 4300 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4301 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4302 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4303 | PyObject *w = PyInt_FromLong((long)c); |
| 4304 | PyObject *x; |
| 4305 | |
| 4306 | if (w == NULL) |
| 4307 | return NULL; |
| 4308 | x = PyObject_GetItem(mapping, w); |
| 4309 | Py_DECREF(w); |
| 4310 | if (x == NULL) { |
| 4311 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4312 | /* No mapping found means: mapping is undefined. */ |
| 4313 | PyErr_Clear(); |
| 4314 | x = Py_None; |
| 4315 | Py_INCREF(x); |
| 4316 | return x; |
| 4317 | } else |
| 4318 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4319 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 4320 | else if (x == Py_None) |
| 4321 | return x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4322 | else if (PyInt_Check(x)) { |
| 4323 | long value = PyInt_AS_LONG(x); |
| 4324 | if (value < 0 || value > 255) { |
| 4325 | PyErr_SetString(PyExc_TypeError, |
| 4326 | "character mapping must be in range(256)"); |
| 4327 | Py_DECREF(x); |
| 4328 | return NULL; |
| 4329 | } |
| 4330 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4331 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4332 | else if (PyString_Check(x)) |
| 4333 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4334 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4335 | /* wrong return value */ |
| 4336 | PyErr_SetString(PyExc_TypeError, |
| 4337 | "character mapping must return integer, None or str"); |
| 4338 | Py_DECREF(x); |
| 4339 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4340 | } |
| 4341 | } |
| 4342 | |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4343 | static int |
| 4344 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
| 4345 | { |
| 4346 | Py_ssize_t outsize = PyString_GET_SIZE(*outobj); |
| 4347 | /* exponentially overallocate to minimize reallocations */ |
| 4348 | if (requiredsize < 2*outsize) |
| 4349 | requiredsize = 2*outsize; |
| 4350 | if (_PyString_Resize(outobj, requiredsize)) { |
| 4351 | return 0; |
| 4352 | } |
| 4353 | return 1; |
| 4354 | } |
| 4355 | |
| 4356 | typedef enum charmapencode_result { |
| 4357 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
| 4358 | }charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4359 | /* lookup the character, put the result in the output string and adjust |
| 4360 | various state variables. Reallocate the output string if not enough |
| 4361 | space is available. Return a new reference to the object that |
| 4362 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 4363 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 4364 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4365 | static |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4366 | charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4367 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4368 | { |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4369 | PyObject *rep; |
| 4370 | char *outstart; |
| 4371 | Py_ssize_t outsize = PyString_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4372 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4373 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4374 | int res = encoding_map_lookup(c, mapping); |
| 4375 | Py_ssize_t requiredsize = *outpos+1; |
| 4376 | if (res == -1) |
| 4377 | return enc_FAILED; |
| 4378 | if (outsize<requiredsize) |
| 4379 | if (!charmapencode_resize(outobj, outpos, requiredsize)) |
| 4380 | return enc_EXCEPTION; |
| 4381 | outstart = PyString_AS_STRING(*outobj); |
| 4382 | outstart[(*outpos)++] = (char)res; |
| 4383 | return enc_SUCCESS; |
| 4384 | } |
| 4385 | |
| 4386 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4387 | if (rep==NULL) |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4388 | return enc_EXCEPTION; |
| 4389 | else if (rep==Py_None) { |
| 4390 | Py_DECREF(rep); |
| 4391 | return enc_FAILED; |
| 4392 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4393 | if (PyInt_Check(rep)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4394 | Py_ssize_t requiredsize = *outpos+1; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4395 | if (outsize<requiredsize) |
| 4396 | if (!charmapencode_resize(outobj, outpos, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4397 | Py_DECREF(rep); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4398 | return enc_EXCEPTION; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4399 | } |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4400 | outstart = PyString_AS_STRING(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4401 | outstart[(*outpos)++] = (char)PyInt_AS_LONG(rep); |
| 4402 | } |
| 4403 | else { |
| 4404 | const char *repchars = PyString_AS_STRING(rep); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4405 | Py_ssize_t repsize = PyString_GET_SIZE(rep); |
| 4406 | Py_ssize_t requiredsize = *outpos+repsize; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4407 | if (outsize<requiredsize) |
| 4408 | if (!charmapencode_resize(outobj, outpos, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4409 | Py_DECREF(rep); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4410 | return enc_EXCEPTION; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4411 | } |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4412 | outstart = PyString_AS_STRING(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4413 | memcpy(outstart + *outpos, repchars, repsize); |
| 4414 | *outpos += repsize; |
| 4415 | } |
| 4416 | } |
Georg Brandl | 9f16760 | 2006-06-04 21:46:16 +0000 | [diff] [blame] | 4417 | Py_DECREF(rep); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4418 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4419 | } |
| 4420 | |
| 4421 | /* handle an error in PyUnicode_EncodeCharmap |
| 4422 | Return 0 on success, -1 on error */ |
| 4423 | static |
| 4424 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4425 | 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] | 4426 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4427 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4428 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4429 | { |
| 4430 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4431 | Py_ssize_t repsize; |
| 4432 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4433 | Py_UNICODE *uni2; |
| 4434 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4435 | Py_ssize_t collstartpos = *inpos; |
| 4436 | Py_ssize_t collendpos = *inpos+1; |
| 4437 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4438 | char *encoding = "charmap"; |
| 4439 | char *reason = "character maps to <undefined>"; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4440 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4441 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4442 | /* find all unencodable characters */ |
| 4443 | while (collendpos < size) { |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4444 | PyObject *rep; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4445 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4446 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 4447 | if (res != -1) |
| 4448 | break; |
| 4449 | ++collendpos; |
| 4450 | continue; |
| 4451 | } |
| 4452 | |
| 4453 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 4454 | if (rep==NULL) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4455 | return -1; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4456 | else if (rep!=Py_None) { |
| 4457 | Py_DECREF(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4458 | break; |
| 4459 | } |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4460 | Py_DECREF(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4461 | ++collendpos; |
| 4462 | } |
| 4463 | /* cache callback name lookup |
| 4464 | * (if not done yet, i.e. it's the first error) */ |
| 4465 | if (*known_errorHandler==-1) { |
| 4466 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4467 | *known_errorHandler = 1; |
| 4468 | else if (!strcmp(errors, "replace")) |
| 4469 | *known_errorHandler = 2; |
| 4470 | else if (!strcmp(errors, "ignore")) |
| 4471 | *known_errorHandler = 3; |
| 4472 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4473 | *known_errorHandler = 4; |
| 4474 | else |
| 4475 | *known_errorHandler = 0; |
| 4476 | } |
| 4477 | switch (*known_errorHandler) { |
| 4478 | case 1: /* strict */ |
| 4479 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4480 | return -1; |
| 4481 | case 2: /* replace */ |
| 4482 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
| 4483 | x = charmapencode_output('?', mapping, res, respos); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4484 | if (x==enc_EXCEPTION) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4485 | return -1; |
| 4486 | } |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4487 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4488 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4489 | return -1; |
| 4490 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4491 | } |
| 4492 | /* fall through */ |
| 4493 | case 3: /* ignore */ |
| 4494 | *inpos = collendpos; |
| 4495 | break; |
| 4496 | case 4: /* xmlcharrefreplace */ |
| 4497 | /* generate replacement (temporarily (mis)uses p) */ |
| 4498 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
| 4499 | char buffer[2+29+1+1]; |
| 4500 | char *cp; |
| 4501 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 4502 | for (cp = buffer; *cp; ++cp) { |
| 4503 | x = charmapencode_output(*cp, mapping, res, respos); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4504 | if (x==enc_EXCEPTION) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4505 | return -1; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4506 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4507 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4508 | return -1; |
| 4509 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4510 | } |
| 4511 | } |
| 4512 | *inpos = collendpos; |
| 4513 | break; |
| 4514 | default: |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4515 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4516 | encoding, reason, p, size, exceptionObject, |
| 4517 | collstartpos, collendpos, &newpos); |
| 4518 | if (repunicode == NULL) |
| 4519 | return -1; |
| 4520 | /* generate replacement */ |
| 4521 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4522 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 4523 | x = charmapencode_output(*uni2, mapping, res, respos); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4524 | if (x==enc_EXCEPTION) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4525 | return -1; |
| 4526 | } |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4527 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4528 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4529 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4530 | return -1; |
| 4531 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4532 | } |
| 4533 | *inpos = newpos; |
| 4534 | Py_DECREF(repunicode); |
| 4535 | } |
| 4536 | return 0; |
| 4537 | } |
| 4538 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4539 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4540 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4541 | PyObject *mapping, |
| 4542 | const char *errors) |
| 4543 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4544 | /* output object */ |
| 4545 | PyObject *res = NULL; |
| 4546 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4547 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4548 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4549 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4550 | PyObject *errorHandler = NULL; |
| 4551 | PyObject *exc = NULL; |
| 4552 | /* the following variable is used for caching string comparisons |
| 4553 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 4554 | * 3=ignore, 4=xmlcharrefreplace */ |
| 4555 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4556 | |
| 4557 | /* Default to Latin-1 */ |
| 4558 | if (mapping == NULL) |
| 4559 | return PyUnicode_EncodeLatin1(p, size, errors); |
| 4560 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4561 | /* allocate enough for a simple encoding without |
| 4562 | replacements, if we need more, we'll resize */ |
| 4563 | res = PyString_FromStringAndSize(NULL, size); |
| 4564 | if (res == NULL) |
| 4565 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 4566 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4567 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4568 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4569 | while (inpos<size) { |
| 4570 | /* try to encode it */ |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4571 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 4572 | if (x==enc_EXCEPTION) /* error */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4573 | goto onError; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4574 | if (x==enc_FAILED) { /* unencodable character */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4575 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 4576 | &exc, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4577 | &known_errorHandler, &errorHandler, errors, |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 4578 | &res, &respos)) { |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 4579 | goto onError; |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 4580 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4581 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4582 | else |
| 4583 | /* done with this character => adjust input position */ |
| 4584 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4585 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4586 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4587 | /* Resize if we allocated to much */ |
| 4588 | if (respos<PyString_GET_SIZE(res)) { |
| 4589 | if (_PyString_Resize(&res, respos)) |
| 4590 | goto onError; |
| 4591 | } |
| 4592 | Py_XDECREF(exc); |
| 4593 | Py_XDECREF(errorHandler); |
| 4594 | return res; |
| 4595 | |
| 4596 | onError: |
| 4597 | Py_XDECREF(res); |
| 4598 | Py_XDECREF(exc); |
| 4599 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4600 | return NULL; |
| 4601 | } |
| 4602 | |
| 4603 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
| 4604 | PyObject *mapping) |
| 4605 | { |
| 4606 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
| 4607 | PyErr_BadArgument(); |
| 4608 | return NULL; |
| 4609 | } |
| 4610 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
| 4611 | PyUnicode_GET_SIZE(unicode), |
| 4612 | mapping, |
| 4613 | NULL); |
| 4614 | } |
| 4615 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4616 | /* create or adjust a UnicodeTranslateError */ |
| 4617 | static void make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4618 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4619 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4620 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4621 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4622 | if (*exceptionObject == NULL) { |
| 4623 | *exceptionObject = PyUnicodeTranslateError_Create( |
| 4624 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4625 | } |
| 4626 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4627 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 4628 | goto onError; |
| 4629 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 4630 | goto onError; |
| 4631 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 4632 | goto onError; |
| 4633 | return; |
| 4634 | onError: |
| 4635 | Py_DECREF(*exceptionObject); |
| 4636 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4637 | } |
| 4638 | } |
| 4639 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4640 | /* raises a UnicodeTranslateError */ |
| 4641 | static void raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4642 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4643 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4644 | const char *reason) |
| 4645 | { |
| 4646 | make_translate_exception(exceptionObject, |
| 4647 | unicode, size, startpos, endpos, reason); |
| 4648 | if (*exceptionObject != NULL) |
| 4649 | PyCodec_StrictErrors(*exceptionObject); |
| 4650 | } |
| 4651 | |
| 4652 | /* error handling callback helper: |
| 4653 | build arguments, call the callback and check the arguments, |
| 4654 | put the result into newpos and return the replacement string, which |
| 4655 | has to be freed by the caller */ |
| 4656 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
| 4657 | PyObject **errorHandler, |
| 4658 | const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4659 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 4660 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4661 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4662 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4663 | 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] | 4664 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4665 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4666 | PyObject *restuple; |
| 4667 | PyObject *resunicode; |
| 4668 | |
| 4669 | if (*errorHandler == NULL) { |
| 4670 | *errorHandler = PyCodec_LookupError(errors); |
| 4671 | if (*errorHandler == NULL) |
| 4672 | return NULL; |
| 4673 | } |
| 4674 | |
| 4675 | make_translate_exception(exceptionObject, |
| 4676 | unicode, size, startpos, endpos, reason); |
| 4677 | if (*exceptionObject == NULL) |
| 4678 | return NULL; |
| 4679 | |
| 4680 | restuple = PyObject_CallFunctionObjArgs( |
| 4681 | *errorHandler, *exceptionObject, NULL); |
| 4682 | if (restuple == NULL) |
| 4683 | return NULL; |
| 4684 | if (!PyTuple_Check(restuple)) { |
| 4685 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 4686 | Py_DECREF(restuple); |
| 4687 | return NULL; |
| 4688 | } |
| 4689 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4690 | &resunicode, &i_newpos)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4691 | Py_DECREF(restuple); |
| 4692 | return NULL; |
| 4693 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4694 | if (i_newpos<0) |
| 4695 | *newpos = size+i_newpos; |
| 4696 | else |
| 4697 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4698 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 4699 | 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] | 4700 | Py_DECREF(restuple); |
| 4701 | return NULL; |
| 4702 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4703 | Py_INCREF(resunicode); |
| 4704 | Py_DECREF(restuple); |
| 4705 | return resunicode; |
| 4706 | } |
| 4707 | |
| 4708 | /* Lookup the character ch in the mapping and put the result in result, |
| 4709 | which must be decrefed by the caller. |
| 4710 | Return 0 on success, -1 on error */ |
| 4711 | static |
| 4712 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 4713 | { |
| 4714 | PyObject *w = PyInt_FromLong((long)c); |
| 4715 | PyObject *x; |
| 4716 | |
| 4717 | if (w == NULL) |
| 4718 | return -1; |
| 4719 | x = PyObject_GetItem(mapping, w); |
| 4720 | Py_DECREF(w); |
| 4721 | if (x == NULL) { |
| 4722 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4723 | /* No mapping found means: use 1:1 mapping. */ |
| 4724 | PyErr_Clear(); |
| 4725 | *result = NULL; |
| 4726 | return 0; |
| 4727 | } else |
| 4728 | return -1; |
| 4729 | } |
| 4730 | else if (x == Py_None) { |
| 4731 | *result = x; |
| 4732 | return 0; |
| 4733 | } |
| 4734 | else if (PyInt_Check(x)) { |
| 4735 | long value = PyInt_AS_LONG(x); |
| 4736 | long max = PyUnicode_GetMax(); |
| 4737 | if (value < 0 || value > max) { |
| 4738 | PyErr_Format(PyExc_TypeError, |
| 4739 | "character mapping must be in range(0x%lx)", max+1); |
| 4740 | Py_DECREF(x); |
| 4741 | return -1; |
| 4742 | } |
| 4743 | *result = x; |
| 4744 | return 0; |
| 4745 | } |
| 4746 | else if (PyUnicode_Check(x)) { |
| 4747 | *result = x; |
| 4748 | return 0; |
| 4749 | } |
| 4750 | else { |
| 4751 | /* wrong return value */ |
| 4752 | PyErr_SetString(PyExc_TypeError, |
| 4753 | "character mapping must return integer, None or unicode"); |
Walter Dörwald | 150523e | 2003-08-15 16:52:19 +0000 | [diff] [blame] | 4754 | Py_DECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4755 | return -1; |
| 4756 | } |
| 4757 | } |
| 4758 | /* ensure that *outobj is at least requiredsize characters long, |
| 4759 | if not reallocate and adjust various state variables. |
| 4760 | Return 0 on success, -1 on error */ |
| 4761 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4762 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4763 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4764 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4765 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4766 | if (requiredsize > oldsize) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4767 | /* remember old output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4768 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4769 | /* exponentially overallocate to minimize reallocations */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4770 | if (requiredsize < 2 * oldsize) |
| 4771 | requiredsize = 2 * oldsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 4772 | if (_PyUnicode_Resize(outobj, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4773 | return -1; |
| 4774 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4775 | } |
| 4776 | return 0; |
| 4777 | } |
| 4778 | /* lookup the character, put the result in the output string and adjust |
| 4779 | various state variables. Return a new reference to the object that |
| 4780 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 4781 | undefined (in which case no character was written). |
| 4782 | The called must decref result. |
| 4783 | Return 0 on success, -1 on error. */ |
| 4784 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4785 | 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] | 4786 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4787 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4788 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4789 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4790 | return -1; |
| 4791 | if (*res==NULL) { |
| 4792 | /* not found => default to 1:1 mapping */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4793 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4794 | } |
| 4795 | else if (*res==Py_None) |
| 4796 | ; |
| 4797 | else if (PyInt_Check(*res)) { |
| 4798 | /* no overflow check, because we know that the space is enough */ |
| 4799 | *(*outp)++ = (Py_UNICODE)PyInt_AS_LONG(*res); |
| 4800 | } |
| 4801 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4802 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4803 | if (repsize==1) { |
| 4804 | /* no overflow check, because we know that the space is enough */ |
| 4805 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 4806 | } |
| 4807 | else if (repsize!=0) { |
| 4808 | /* more than one character */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4809 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
Walter Dörwald | cd736e7 | 2004-02-05 17:36:00 +0000 | [diff] [blame] | 4810 | (insize - (curinp-startinp)) + |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4811 | repsize - 1; |
| 4812 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4813 | return -1; |
| 4814 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 4815 | *outp += repsize; |
| 4816 | } |
| 4817 | } |
| 4818 | else |
| 4819 | return -1; |
| 4820 | return 0; |
| 4821 | } |
| 4822 | |
| 4823 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4824 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4825 | PyObject *mapping, |
| 4826 | const char *errors) |
| 4827 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4828 | /* output object */ |
| 4829 | PyObject *res = NULL; |
| 4830 | /* pointers to the beginning and end+1 of input */ |
| 4831 | const Py_UNICODE *startp = p; |
| 4832 | const Py_UNICODE *endp = p + size; |
| 4833 | /* pointer into the output */ |
| 4834 | Py_UNICODE *str; |
| 4835 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4836 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4837 | char *reason = "character maps to <undefined>"; |
| 4838 | PyObject *errorHandler = NULL; |
| 4839 | PyObject *exc = NULL; |
| 4840 | /* the following variable is used for caching string comparisons |
| 4841 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 4842 | * 3=ignore, 4=xmlcharrefreplace */ |
| 4843 | int known_errorHandler = -1; |
| 4844 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4845 | if (mapping == NULL) { |
| 4846 | PyErr_BadArgument(); |
| 4847 | return NULL; |
| 4848 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4849 | |
| 4850 | /* allocate enough for a simple 1:1 translation without |
| 4851 | replacements, if we need more, we'll resize */ |
| 4852 | res = PyUnicode_FromUnicode(NULL, size); |
| 4853 | if (res == NULL) |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4854 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4855 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4856 | return res; |
| 4857 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4858 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4859 | while (p<endp) { |
| 4860 | /* try to encode it */ |
| 4861 | PyObject *x = NULL; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4862 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4863 | Py_XDECREF(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4864 | goto onError; |
| 4865 | } |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 4866 | Py_XDECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4867 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 4868 | ++p; |
| 4869 | else { /* untranslatable character */ |
| 4870 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4871 | Py_ssize_t repsize; |
| 4872 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4873 | Py_UNICODE *uni2; |
| 4874 | /* startpos for collecting untranslatable chars */ |
| 4875 | const Py_UNICODE *collstart = p; |
| 4876 | const Py_UNICODE *collend = p+1; |
| 4877 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4878 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4879 | /* find all untranslatable characters */ |
| 4880 | while (collend < endp) { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4881 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4882 | goto onError; |
| 4883 | Py_XDECREF(x); |
| 4884 | if (x!=Py_None) |
| 4885 | break; |
| 4886 | ++collend; |
| 4887 | } |
| 4888 | /* cache callback name lookup |
| 4889 | * (if not done yet, i.e. it's the first error) */ |
| 4890 | if (known_errorHandler==-1) { |
| 4891 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4892 | known_errorHandler = 1; |
| 4893 | else if (!strcmp(errors, "replace")) |
| 4894 | known_errorHandler = 2; |
| 4895 | else if (!strcmp(errors, "ignore")) |
| 4896 | known_errorHandler = 3; |
| 4897 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4898 | known_errorHandler = 4; |
| 4899 | else |
| 4900 | known_errorHandler = 0; |
| 4901 | } |
| 4902 | switch (known_errorHandler) { |
| 4903 | case 1: /* strict */ |
| 4904 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
| 4905 | goto onError; |
| 4906 | case 2: /* replace */ |
| 4907 | /* No need to check for space, this is a 1:1 replacement */ |
| 4908 | for (coll = collstart; coll<collend; ++coll) |
| 4909 | *str++ = '?'; |
| 4910 | /* fall through */ |
| 4911 | case 3: /* ignore */ |
| 4912 | p = collend; |
| 4913 | break; |
| 4914 | case 4: /* xmlcharrefreplace */ |
| 4915 | /* generate replacement (temporarily (mis)uses p) */ |
| 4916 | for (p = collstart; p < collend; ++p) { |
| 4917 | char buffer[2+29+1+1]; |
| 4918 | char *cp; |
| 4919 | sprintf(buffer, "&#%d;", (int)*p); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4920 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4921 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 4922 | goto onError; |
| 4923 | for (cp = buffer; *cp; ++cp) |
| 4924 | *str++ = *cp; |
| 4925 | } |
| 4926 | p = collend; |
| 4927 | break; |
| 4928 | default: |
| 4929 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 4930 | reason, startp, size, &exc, |
| 4931 | collstart-startp, collend-startp, &newpos); |
| 4932 | if (repunicode == NULL) |
| 4933 | goto onError; |
| 4934 | /* generate replacement */ |
| 4935 | repsize = PyUnicode_GET_SIZE(repunicode); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4936 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4937 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 4938 | Py_DECREF(repunicode); |
| 4939 | goto onError; |
| 4940 | } |
| 4941 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 4942 | *str++ = *uni2; |
| 4943 | p = startp + newpos; |
| 4944 | Py_DECREF(repunicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4945 | } |
| 4946 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4947 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4948 | /* Resize if we allocated to much */ |
| 4949 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4950 | if (respos<PyUnicode_GET_SIZE(res)) { |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 4951 | if (_PyUnicode_Resize(&res, respos) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 4952 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4953 | } |
| 4954 | Py_XDECREF(exc); |
| 4955 | Py_XDECREF(errorHandler); |
| 4956 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4957 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4958 | onError: |
| 4959 | Py_XDECREF(res); |
| 4960 | Py_XDECREF(exc); |
| 4961 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4962 | return NULL; |
| 4963 | } |
| 4964 | |
| 4965 | PyObject *PyUnicode_Translate(PyObject *str, |
| 4966 | PyObject *mapping, |
| 4967 | const char *errors) |
| 4968 | { |
| 4969 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4970 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4971 | str = PyUnicode_FromObject(str); |
| 4972 | if (str == NULL) |
| 4973 | goto onError; |
| 4974 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
| 4975 | PyUnicode_GET_SIZE(str), |
| 4976 | mapping, |
| 4977 | errors); |
| 4978 | Py_DECREF(str); |
| 4979 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4980 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4981 | onError: |
| 4982 | Py_XDECREF(str); |
| 4983 | return NULL; |
| 4984 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4985 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4986 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 4987 | |
| 4988 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4989 | Py_ssize_t length, |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 4990 | char *output, |
| 4991 | const char *errors) |
| 4992 | { |
| 4993 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4994 | PyObject *errorHandler = NULL; |
| 4995 | PyObject *exc = NULL; |
| 4996 | const char *encoding = "decimal"; |
| 4997 | const char *reason = "invalid decimal Unicode string"; |
| 4998 | /* the following variable is used for caching string comparisons |
| 4999 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 5000 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5001 | |
| 5002 | if (output == NULL) { |
| 5003 | PyErr_BadArgument(); |
| 5004 | return -1; |
| 5005 | } |
| 5006 | |
| 5007 | p = s; |
| 5008 | end = s + length; |
| 5009 | while (p < end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5010 | register Py_UNICODE ch = *p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5011 | int decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5012 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5013 | Py_ssize_t repsize; |
| 5014 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5015 | Py_UNICODE *uni2; |
| 5016 | Py_UNICODE *collstart; |
| 5017 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5018 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5019 | if (Py_UNICODE_ISSPACE(ch)) { |
| 5020 | *output++ = ' '; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5021 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5022 | continue; |
| 5023 | } |
| 5024 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5025 | if (decimal >= 0) { |
| 5026 | *output++ = '0' + decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5027 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5028 | continue; |
| 5029 | } |
Guido van Rossum | ba47704 | 2000-04-06 18:18:10 +0000 | [diff] [blame] | 5030 | if (0 < ch && ch < 256) { |
Guido van Rossum | 42c29aa | 2000-05-03 23:58:29 +0000 | [diff] [blame] | 5031 | *output++ = (char)ch; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5032 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5033 | continue; |
| 5034 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5035 | /* All other characters are considered unencodable */ |
| 5036 | collstart = p; |
| 5037 | collend = p+1; |
| 5038 | while (collend < end) { |
| 5039 | if ((0 < *collend && *collend < 256) || |
| 5040 | !Py_UNICODE_ISSPACE(*collend) || |
| 5041 | Py_UNICODE_TODECIMAL(*collend)) |
| 5042 | break; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5043 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5044 | /* cache callback name lookup |
| 5045 | * (if not done yet, i.e. it's the first error) */ |
| 5046 | if (known_errorHandler==-1) { |
| 5047 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5048 | known_errorHandler = 1; |
| 5049 | else if (!strcmp(errors, "replace")) |
| 5050 | known_errorHandler = 2; |
| 5051 | else if (!strcmp(errors, "ignore")) |
| 5052 | known_errorHandler = 3; |
| 5053 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5054 | known_errorHandler = 4; |
| 5055 | else |
| 5056 | known_errorHandler = 0; |
| 5057 | } |
| 5058 | switch (known_errorHandler) { |
| 5059 | case 1: /* strict */ |
| 5060 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 5061 | goto onError; |
| 5062 | case 2: /* replace */ |
| 5063 | for (p = collstart; p < collend; ++p) |
| 5064 | *output++ = '?'; |
| 5065 | /* fall through */ |
| 5066 | case 3: /* ignore */ |
| 5067 | p = collend; |
| 5068 | break; |
| 5069 | case 4: /* xmlcharrefreplace */ |
| 5070 | /* generate replacement (temporarily (mis)uses p) */ |
| 5071 | for (p = collstart; p < collend; ++p) |
| 5072 | output += sprintf(output, "&#%d;", (int)*p); |
| 5073 | p = collend; |
| 5074 | break; |
| 5075 | default: |
| 5076 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 5077 | encoding, reason, s, length, &exc, |
| 5078 | collstart-s, collend-s, &newpos); |
| 5079 | if (repunicode == NULL) |
| 5080 | goto onError; |
| 5081 | /* generate replacement */ |
| 5082 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5083 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 5084 | Py_UNICODE ch = *uni2; |
| 5085 | if (Py_UNICODE_ISSPACE(ch)) |
| 5086 | *output++ = ' '; |
| 5087 | else { |
| 5088 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5089 | if (decimal >= 0) |
| 5090 | *output++ = '0' + decimal; |
| 5091 | else if (0 < ch && ch < 256) |
| 5092 | *output++ = (char)ch; |
| 5093 | else { |
| 5094 | Py_DECREF(repunicode); |
| 5095 | raise_encode_exception(&exc, encoding, |
| 5096 | s, length, collstart-s, collend-s, reason); |
| 5097 | goto onError; |
| 5098 | } |
| 5099 | } |
| 5100 | } |
| 5101 | p = s + newpos; |
| 5102 | Py_DECREF(repunicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5103 | } |
| 5104 | } |
| 5105 | /* 0-terminate the output string */ |
| 5106 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5107 | Py_XDECREF(exc); |
| 5108 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5109 | return 0; |
| 5110 | |
| 5111 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5112 | Py_XDECREF(exc); |
| 5113 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5114 | return -1; |
| 5115 | } |
| 5116 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5117 | /* --- Helpers ------------------------------------------------------------ */ |
| 5118 | |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 5119 | #include "stringlib/unicodedefs.h" |
Fredrik Lundh | 6471ee4 | 2006-05-24 14:28:11 +0000 | [diff] [blame] | 5120 | |
Facundo Batista | 6f7e6fb | 2007-11-16 19:16:15 +0000 | [diff] [blame] | 5121 | #define FROM_UNICODE |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 5122 | |
Fredrik Lundh | a50d201 | 2006-05-26 17:04:58 +0000 | [diff] [blame] | 5123 | #include "stringlib/fastsearch.h" |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5124 | |
| 5125 | #include "stringlib/count.h" |
| 5126 | #include "stringlib/find.h" |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 5127 | #include "stringlib/partition.h" |
| 5128 | |
Fredrik Lundh | c816281 | 2006-05-26 19:33:03 +0000 | [diff] [blame] | 5129 | /* helper macro to fixup start/end slice values */ |
| 5130 | #define FIX_START_END(obj) \ |
| 5131 | if (start < 0) \ |
| 5132 | start += (obj)->length; \ |
| 5133 | if (start < 0) \ |
| 5134 | start = 0; \ |
| 5135 | if (end > (obj)->length) \ |
| 5136 | end = (obj)->length; \ |
| 5137 | if (end < 0) \ |
| 5138 | end += (obj)->length; \ |
| 5139 | if (end < 0) \ |
| 5140 | end = 0; |
| 5141 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5142 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5143 | PyObject *substr, |
| 5144 | Py_ssize_t start, |
| 5145 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5146 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5147 | Py_ssize_t result; |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5148 | PyUnicodeObject* str_obj; |
| 5149 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5150 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5151 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 5152 | if (!str_obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5153 | return -1; |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5154 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 5155 | if (!sub_obj) { |
| 5156 | Py_DECREF(str_obj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5157 | return -1; |
| 5158 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5159 | |
Fredrik Lundh | c816281 | 2006-05-26 19:33:03 +0000 | [diff] [blame] | 5160 | FIX_START_END(str_obj); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5161 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5162 | result = stringlib_count( |
| 5163 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length |
| 5164 | ); |
| 5165 | |
| 5166 | Py_DECREF(sub_obj); |
| 5167 | Py_DECREF(str_obj); |
| 5168 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5169 | return result; |
| 5170 | } |
| 5171 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5172 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5173 | PyObject *sub, |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 5174 | Py_ssize_t start, |
| 5175 | Py_ssize_t end, |
| 5176 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5177 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5178 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5179 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5180 | str = PyUnicode_FromObject(str); |
| 5181 | if (!str) |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 5182 | return -2; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5183 | sub = PyUnicode_FromObject(sub); |
| 5184 | if (!sub) { |
| 5185 | Py_DECREF(str); |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 5186 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5187 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5188 | |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 5189 | if (direction > 0) |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 5190 | result = stringlib_find_slice( |
| 5191 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5192 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5193 | start, end |
| 5194 | ); |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 5195 | else |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 5196 | result = stringlib_rfind_slice( |
| 5197 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5198 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5199 | start, end |
| 5200 | ); |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 5201 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5202 | Py_DECREF(str); |
| 5203 | Py_DECREF(sub); |
| 5204 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5205 | return result; |
| 5206 | } |
| 5207 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5208 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5209 | int tailmatch(PyUnicodeObject *self, |
| 5210 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5211 | Py_ssize_t start, |
| 5212 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5213 | int direction) |
| 5214 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5215 | if (substring->length == 0) |
| 5216 | return 1; |
| 5217 | |
Fredrik Lundh | c816281 | 2006-05-26 19:33:03 +0000 | [diff] [blame] | 5218 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5219 | |
| 5220 | end -= substring->length; |
| 5221 | if (end < start) |
| 5222 | return 0; |
| 5223 | |
| 5224 | if (direction > 0) { |
| 5225 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 5226 | return 1; |
| 5227 | } else { |
| 5228 | if (Py_UNICODE_MATCH(self, start, substring)) |
| 5229 | return 1; |
| 5230 | } |
| 5231 | |
| 5232 | return 0; |
| 5233 | } |
| 5234 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5235 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5236 | PyObject *substr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5237 | Py_ssize_t start, |
| 5238 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5239 | int direction) |
| 5240 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5241 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5242 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5243 | str = PyUnicode_FromObject(str); |
| 5244 | if (str == NULL) |
| 5245 | return -1; |
| 5246 | substr = PyUnicode_FromObject(substr); |
| 5247 | if (substr == NULL) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5248 | Py_DECREF(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5249 | return -1; |
| 5250 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5251 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5252 | result = tailmatch((PyUnicodeObject *)str, |
| 5253 | (PyUnicodeObject *)substr, |
| 5254 | start, end, direction); |
| 5255 | Py_DECREF(str); |
| 5256 | Py_DECREF(substr); |
| 5257 | return result; |
| 5258 | } |
| 5259 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5260 | /* Apply fixfct filter to the Unicode object self and return a |
| 5261 | reference to the modified object */ |
| 5262 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5263 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5264 | PyObject *fixup(PyUnicodeObject *self, |
| 5265 | int (*fixfct)(PyUnicodeObject *s)) |
| 5266 | { |
| 5267 | |
| 5268 | PyUnicodeObject *u; |
| 5269 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5270 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5271 | if (u == NULL) |
| 5272 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5273 | |
| 5274 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5275 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5276 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5277 | /* fixfct should return TRUE if it modified the buffer. If |
| 5278 | FALSE, return a reference to the original buffer instead |
| 5279 | (to save space, not time) */ |
| 5280 | Py_INCREF(self); |
| 5281 | Py_DECREF(u); |
| 5282 | return (PyObject*) self; |
| 5283 | } |
| 5284 | return (PyObject*) u; |
| 5285 | } |
| 5286 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5287 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5288 | int fixupper(PyUnicodeObject *self) |
| 5289 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5290 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5291 | Py_UNICODE *s = self->str; |
| 5292 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5293 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5294 | while (len-- > 0) { |
| 5295 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5296 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5297 | ch = Py_UNICODE_TOUPPER(*s); |
| 5298 | if (ch != *s) { |
| 5299 | status = 1; |
| 5300 | *s = ch; |
| 5301 | } |
| 5302 | s++; |
| 5303 | } |
| 5304 | |
| 5305 | return status; |
| 5306 | } |
| 5307 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5308 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5309 | int fixlower(PyUnicodeObject *self) |
| 5310 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5311 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5312 | Py_UNICODE *s = self->str; |
| 5313 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5314 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5315 | while (len-- > 0) { |
| 5316 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5317 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5318 | ch = Py_UNICODE_TOLOWER(*s); |
| 5319 | if (ch != *s) { |
| 5320 | status = 1; |
| 5321 | *s = ch; |
| 5322 | } |
| 5323 | s++; |
| 5324 | } |
| 5325 | |
| 5326 | return status; |
| 5327 | } |
| 5328 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5329 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5330 | int fixswapcase(PyUnicodeObject *self) |
| 5331 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5332 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5333 | Py_UNICODE *s = self->str; |
| 5334 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5335 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5336 | while (len-- > 0) { |
| 5337 | if (Py_UNICODE_ISUPPER(*s)) { |
| 5338 | *s = Py_UNICODE_TOLOWER(*s); |
| 5339 | status = 1; |
| 5340 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 5341 | *s = Py_UNICODE_TOUPPER(*s); |
| 5342 | status = 1; |
| 5343 | } |
| 5344 | s++; |
| 5345 | } |
| 5346 | |
| 5347 | return status; |
| 5348 | } |
| 5349 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5350 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5351 | int fixcapitalize(PyUnicodeObject *self) |
| 5352 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5353 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5354 | Py_UNICODE *s = self->str; |
| 5355 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5356 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5357 | if (len == 0) |
| 5358 | return 0; |
| 5359 | if (Py_UNICODE_ISLOWER(*s)) { |
| 5360 | *s = Py_UNICODE_TOUPPER(*s); |
| 5361 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5362 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5363 | s++; |
| 5364 | while (--len > 0) { |
| 5365 | if (Py_UNICODE_ISUPPER(*s)) { |
| 5366 | *s = Py_UNICODE_TOLOWER(*s); |
| 5367 | status = 1; |
| 5368 | } |
| 5369 | s++; |
| 5370 | } |
| 5371 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5372 | } |
| 5373 | |
| 5374 | static |
| 5375 | int fixtitle(PyUnicodeObject *self) |
| 5376 | { |
| 5377 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5378 | register Py_UNICODE *e; |
| 5379 | int previous_is_cased; |
| 5380 | |
| 5381 | /* Shortcut for single character strings */ |
| 5382 | if (PyUnicode_GET_SIZE(self) == 1) { |
| 5383 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 5384 | if (*p != ch) { |
| 5385 | *p = ch; |
| 5386 | return 1; |
| 5387 | } |
| 5388 | else |
| 5389 | return 0; |
| 5390 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5391 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5392 | e = p + PyUnicode_GET_SIZE(self); |
| 5393 | previous_is_cased = 0; |
| 5394 | for (; p < e; p++) { |
| 5395 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5396 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5397 | if (previous_is_cased) |
| 5398 | *p = Py_UNICODE_TOLOWER(ch); |
| 5399 | else |
| 5400 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5401 | |
| 5402 | if (Py_UNICODE_ISLOWER(ch) || |
| 5403 | Py_UNICODE_ISUPPER(ch) || |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5404 | Py_UNICODE_ISTITLE(ch)) |
| 5405 | previous_is_cased = 1; |
| 5406 | else |
| 5407 | previous_is_cased = 0; |
| 5408 | } |
| 5409 | return 1; |
| 5410 | } |
| 5411 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5412 | PyObject * |
| 5413 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5414 | { |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5415 | PyObject *internal_separator = NULL; |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 5416 | const Py_UNICODE blank = ' '; |
| 5417 | const Py_UNICODE *sep = ␣ |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 5418 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5419 | PyUnicodeObject *res = NULL; /* the result */ |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 5420 | Py_ssize_t res_alloc = 100; /* # allocated bytes for string in res */ |
| 5421 | Py_ssize_t res_used; /* # used bytes */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5422 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 5423 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5424 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5425 | PyObject *item; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5426 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5427 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5428 | fseq = PySequence_Fast(seq, ""); |
| 5429 | if (fseq == NULL) { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5430 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5431 | } |
| 5432 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5433 | /* Grrrr. A codec may be invoked to convert str objects to |
| 5434 | * Unicode, and so it's possible to call back into Python code |
| 5435 | * during PyUnicode_FromObject(), and so it's possible for a sick |
| 5436 | * codec to change the size of fseq (if seq is a list). Therefore |
| 5437 | * we have to keep refetching the size -- can't assume seqlen |
| 5438 | * is invariant. |
| 5439 | */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5440 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 5441 | /* If empty sequence, return u"". */ |
| 5442 | if (seqlen == 0) { |
| 5443 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 5444 | goto Done; |
| 5445 | } |
| 5446 | /* If singleton sequence with an exact Unicode, return that. */ |
| 5447 | if (seqlen == 1) { |
| 5448 | item = PySequence_Fast_GET_ITEM(fseq, 0); |
| 5449 | if (PyUnicode_CheckExact(item)) { |
| 5450 | Py_INCREF(item); |
| 5451 | res = (PyUnicodeObject *)item; |
| 5452 | goto Done; |
| 5453 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5454 | } |
| 5455 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5456 | /* At least two items to join, or one that isn't exact Unicode. */ |
| 5457 | if (seqlen > 1) { |
| 5458 | /* Set up sep and seplen -- they're needed. */ |
| 5459 | if (separator == NULL) { |
| 5460 | sep = ␣ |
| 5461 | seplen = 1; |
| 5462 | } |
| 5463 | else { |
| 5464 | internal_separator = PyUnicode_FromObject(separator); |
| 5465 | if (internal_separator == NULL) |
| 5466 | goto onError; |
| 5467 | sep = PyUnicode_AS_UNICODE(internal_separator); |
| 5468 | seplen = PyUnicode_GET_SIZE(internal_separator); |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5469 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 5470 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5471 | } |
| 5472 | } |
| 5473 | |
| 5474 | /* Get space. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5475 | res = _PyUnicode_New(res_alloc); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5476 | if (res == NULL) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5477 | goto onError; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5478 | res_p = PyUnicode_AS_UNICODE(res); |
| 5479 | res_used = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5480 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5481 | for (i = 0; i < seqlen; ++i) { |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 5482 | Py_ssize_t itemlen; |
| 5483 | Py_ssize_t new_res_used; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5484 | |
| 5485 | item = PySequence_Fast_GET_ITEM(fseq, i); |
| 5486 | /* Convert item to Unicode. */ |
| 5487 | if (! PyUnicode_Check(item) && ! PyString_Check(item)) { |
| 5488 | PyErr_Format(PyExc_TypeError, |
Thomas Wouters | 715a4cd | 2006-04-16 22:04:49 +0000 | [diff] [blame] | 5489 | "sequence item %zd: expected string or Unicode," |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5490 | " %.80s found", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5491 | i, Py_TYPE(item)->tp_name); |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 5492 | goto onError; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5493 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5494 | item = PyUnicode_FromObject(item); |
| 5495 | if (item == NULL) |
| 5496 | goto onError; |
| 5497 | /* We own a reference to item from here on. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5498 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5499 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 5500 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 5501 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5502 | /* 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] | 5503 | itemlen = PyUnicode_GET_SIZE(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5504 | new_res_used = res_used + itemlen; |
Georg Brandl | 90e27d3 | 2006-06-10 06:40:50 +0000 | [diff] [blame] | 5505 | if (new_res_used < 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5506 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5507 | if (i < seqlen - 1) { |
| 5508 | new_res_used += seplen; |
Georg Brandl | 90e27d3 | 2006-06-10 06:40:50 +0000 | [diff] [blame] | 5509 | if (new_res_used < 0) |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5510 | goto Overflow; |
| 5511 | } |
| 5512 | if (new_res_used > res_alloc) { |
| 5513 | /* double allocated size until it's big enough */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5514 | do { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5515 | res_alloc += res_alloc; |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 5516 | if (res_alloc <= 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5517 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5518 | } while (new_res_used > res_alloc); |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5519 | if (_PyUnicode_Resize(&res, res_alloc) < 0) { |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 5520 | Py_DECREF(item); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5521 | goto onError; |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 5522 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5523 | res_p = PyUnicode_AS_UNICODE(res) + res_used; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5524 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5525 | |
| 5526 | /* Copy item, and maybe the separator. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5527 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5528 | res_p += itemlen; |
| 5529 | if (i < seqlen - 1) { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5530 | Py_UNICODE_COPY(res_p, sep, seplen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5531 | res_p += seplen; |
| 5532 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5533 | Py_DECREF(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5534 | res_used = new_res_used; |
| 5535 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5536 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5537 | /* Shrink res to match the used area; this probably can't fail, |
| 5538 | * but it's cheap to check. |
| 5539 | */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5540 | if (_PyUnicode_Resize(&res, res_used) < 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5541 | goto onError; |
| 5542 | |
| 5543 | Done: |
| 5544 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5545 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5546 | return (PyObject *)res; |
| 5547 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5548 | Overflow: |
| 5549 | PyErr_SetString(PyExc_OverflowError, |
Georg Brandl | 90e27d3 | 2006-06-10 06:40:50 +0000 | [diff] [blame] | 5550 | "join() result is too long for a Python string"); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5551 | Py_DECREF(item); |
| 5552 | /* fall through */ |
| 5553 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5554 | onError: |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5555 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5556 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5557 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5558 | return NULL; |
| 5559 | } |
| 5560 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5561 | static |
| 5562 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5563 | Py_ssize_t left, |
| 5564 | Py_ssize_t right, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5565 | Py_UNICODE fill) |
| 5566 | { |
| 5567 | PyUnicodeObject *u; |
| 5568 | |
| 5569 | if (left < 0) |
| 5570 | left = 0; |
| 5571 | if (right < 0) |
| 5572 | right = 0; |
| 5573 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5574 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5575 | Py_INCREF(self); |
| 5576 | return self; |
| 5577 | } |
| 5578 | |
| 5579 | u = _PyUnicode_New(left + self->length + right); |
| 5580 | if (u) { |
| 5581 | if (left) |
| 5582 | Py_UNICODE_FILL(u->str, fill, left); |
| 5583 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 5584 | if (right) |
| 5585 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 5586 | } |
| 5587 | |
| 5588 | return u; |
| 5589 | } |
| 5590 | |
| 5591 | #define SPLIT_APPEND(data, left, right) \ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5592 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5593 | if (!str) \ |
| 5594 | goto onError; \ |
| 5595 | if (PyList_Append(list, str)) { \ |
| 5596 | Py_DECREF(str); \ |
| 5597 | goto onError; \ |
| 5598 | } \ |
| 5599 | else \ |
| 5600 | Py_DECREF(str); |
| 5601 | |
| 5602 | static |
| 5603 | PyObject *split_whitespace(PyUnicodeObject *self, |
| 5604 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5605 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5606 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5607 | register Py_ssize_t i; |
| 5608 | register Py_ssize_t j; |
| 5609 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5610 | PyObject *str; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5611 | register const Py_UNICODE *buf = self->str; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5612 | |
| 5613 | for (i = j = 0; i < len; ) { |
| 5614 | /* find a token */ |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5615 | while (i < len && Py_UNICODE_ISSPACE(buf[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5616 | i++; |
| 5617 | j = i; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5618 | while (i < len && !Py_UNICODE_ISSPACE(buf[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5619 | i++; |
| 5620 | if (j < i) { |
| 5621 | if (maxcount-- <= 0) |
| 5622 | break; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5623 | SPLIT_APPEND(buf, j, i); |
| 5624 | while (i < len && Py_UNICODE_ISSPACE(buf[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5625 | i++; |
| 5626 | j = i; |
| 5627 | } |
| 5628 | } |
| 5629 | if (j < len) { |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5630 | SPLIT_APPEND(buf, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5631 | } |
| 5632 | return list; |
| 5633 | |
| 5634 | onError: |
| 5635 | Py_DECREF(list); |
| 5636 | return NULL; |
| 5637 | } |
| 5638 | |
| 5639 | PyObject *PyUnicode_Splitlines(PyObject *string, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5640 | int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5641 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5642 | register Py_ssize_t i; |
| 5643 | register Py_ssize_t j; |
| 5644 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5645 | PyObject *list; |
| 5646 | PyObject *str; |
| 5647 | Py_UNICODE *data; |
| 5648 | |
| 5649 | string = PyUnicode_FromObject(string); |
| 5650 | if (string == NULL) |
| 5651 | return NULL; |
| 5652 | data = PyUnicode_AS_UNICODE(string); |
| 5653 | len = PyUnicode_GET_SIZE(string); |
| 5654 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5655 | list = PyList_New(0); |
| 5656 | if (!list) |
| 5657 | goto onError; |
| 5658 | |
| 5659 | for (i = j = 0; i < len; ) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5660 | Py_ssize_t eol; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5661 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5662 | /* Find a line and append it */ |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5663 | while (i < len && !BLOOM_LINEBREAK(data[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5664 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5665 | |
| 5666 | /* Skip the line break reading CRLF as one line break */ |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5667 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5668 | if (i < len) { |
| 5669 | if (data[i] == '\r' && i + 1 < len && |
| 5670 | data[i+1] == '\n') |
| 5671 | i += 2; |
| 5672 | else |
| 5673 | i++; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5674 | if (keepends) |
| 5675 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5676 | } |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5677 | SPLIT_APPEND(data, j, eol); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5678 | j = i; |
| 5679 | } |
| 5680 | if (j < len) { |
| 5681 | SPLIT_APPEND(data, j, len); |
| 5682 | } |
| 5683 | |
| 5684 | Py_DECREF(string); |
| 5685 | return list; |
| 5686 | |
| 5687 | onError: |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5688 | Py_XDECREF(list); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5689 | Py_DECREF(string); |
| 5690 | return NULL; |
| 5691 | } |
| 5692 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5693 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5694 | PyObject *split_char(PyUnicodeObject *self, |
| 5695 | PyObject *list, |
| 5696 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5697 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5698 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5699 | register Py_ssize_t i; |
| 5700 | register Py_ssize_t j; |
| 5701 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5702 | PyObject *str; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5703 | register const Py_UNICODE *buf = self->str; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5704 | |
| 5705 | for (i = j = 0; i < len; ) { |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5706 | if (buf[i] == ch) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5707 | if (maxcount-- <= 0) |
| 5708 | break; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5709 | SPLIT_APPEND(buf, j, i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5710 | i = j = i + 1; |
| 5711 | } else |
| 5712 | i++; |
| 5713 | } |
| 5714 | if (j <= len) { |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5715 | SPLIT_APPEND(buf, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5716 | } |
| 5717 | return list; |
| 5718 | |
| 5719 | onError: |
| 5720 | Py_DECREF(list); |
| 5721 | return NULL; |
| 5722 | } |
| 5723 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5724 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5725 | PyObject *split_substring(PyUnicodeObject *self, |
| 5726 | PyObject *list, |
| 5727 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5728 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5729 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5730 | register Py_ssize_t i; |
| 5731 | register Py_ssize_t j; |
| 5732 | Py_ssize_t len = self->length; |
| 5733 | Py_ssize_t sublen = substring->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5734 | PyObject *str; |
| 5735 | |
Guido van Rossum | cda4f9a | 2000-12-19 02:23:19 +0000 | [diff] [blame] | 5736 | for (i = j = 0; i <= len - sublen; ) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5737 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 5738 | if (maxcount-- <= 0) |
| 5739 | break; |
| 5740 | SPLIT_APPEND(self->str, j, i); |
| 5741 | i = j = i + sublen; |
| 5742 | } else |
| 5743 | i++; |
| 5744 | } |
| 5745 | if (j <= len) { |
| 5746 | SPLIT_APPEND(self->str, j, len); |
| 5747 | } |
| 5748 | return list; |
| 5749 | |
| 5750 | onError: |
| 5751 | Py_DECREF(list); |
| 5752 | return NULL; |
| 5753 | } |
| 5754 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5755 | static |
| 5756 | PyObject *rsplit_whitespace(PyUnicodeObject *self, |
| 5757 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5758 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5759 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5760 | register Py_ssize_t i; |
| 5761 | register Py_ssize_t j; |
| 5762 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5763 | PyObject *str; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5764 | register const Py_UNICODE *buf = self->str; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5765 | |
| 5766 | for (i = j = len - 1; i >= 0; ) { |
| 5767 | /* find a token */ |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5768 | while (i >= 0 && Py_UNICODE_ISSPACE(buf[i])) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5769 | i--; |
| 5770 | j = i; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5771 | while (i >= 0 && !Py_UNICODE_ISSPACE(buf[i])) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5772 | i--; |
| 5773 | if (j > i) { |
| 5774 | if (maxcount-- <= 0) |
| 5775 | break; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5776 | SPLIT_APPEND(buf, i + 1, j + 1); |
| 5777 | while (i >= 0 && Py_UNICODE_ISSPACE(buf[i])) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5778 | i--; |
| 5779 | j = i; |
| 5780 | } |
| 5781 | } |
| 5782 | if (j >= 0) { |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5783 | SPLIT_APPEND(buf, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5784 | } |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5785 | if (PyList_Reverse(list) < 0) |
| 5786 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5787 | return list; |
| 5788 | |
| 5789 | onError: |
| 5790 | Py_DECREF(list); |
| 5791 | return NULL; |
| 5792 | } |
| 5793 | |
| 5794 | static |
| 5795 | PyObject *rsplit_char(PyUnicodeObject *self, |
| 5796 | PyObject *list, |
| 5797 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5798 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5799 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5800 | register Py_ssize_t i; |
| 5801 | register Py_ssize_t j; |
| 5802 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5803 | PyObject *str; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5804 | register const Py_UNICODE *buf = self->str; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5805 | |
| 5806 | for (i = j = len - 1; i >= 0; ) { |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5807 | if (buf[i] == ch) { |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5808 | if (maxcount-- <= 0) |
| 5809 | break; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5810 | SPLIT_APPEND(buf, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5811 | j = i = i - 1; |
| 5812 | } else |
| 5813 | i--; |
| 5814 | } |
Hye-Shik Chang | 7fc4cf5 | 2003-12-23 09:10:16 +0000 | [diff] [blame] | 5815 | if (j >= -1) { |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5816 | SPLIT_APPEND(buf, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5817 | } |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5818 | if (PyList_Reverse(list) < 0) |
| 5819 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5820 | return list; |
| 5821 | |
| 5822 | onError: |
| 5823 | Py_DECREF(list); |
| 5824 | return NULL; |
| 5825 | } |
| 5826 | |
| 5827 | static |
| 5828 | PyObject *rsplit_substring(PyUnicodeObject *self, |
| 5829 | PyObject *list, |
| 5830 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5831 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5832 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5833 | register Py_ssize_t i; |
| 5834 | register Py_ssize_t j; |
| 5835 | Py_ssize_t len = self->length; |
| 5836 | Py_ssize_t sublen = substring->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5837 | PyObject *str; |
| 5838 | |
| 5839 | for (i = len - sublen, j = len; i >= 0; ) { |
| 5840 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 5841 | if (maxcount-- <= 0) |
| 5842 | break; |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5843 | SPLIT_APPEND(self->str, i + sublen, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5844 | j = i; |
| 5845 | i -= sublen; |
| 5846 | } else |
| 5847 | i--; |
| 5848 | } |
| 5849 | if (j >= 0) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5850 | SPLIT_APPEND(self->str, 0, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5851 | } |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5852 | if (PyList_Reverse(list) < 0) |
| 5853 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5854 | return list; |
| 5855 | |
| 5856 | onError: |
| 5857 | Py_DECREF(list); |
| 5858 | return NULL; |
| 5859 | } |
| 5860 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5861 | #undef SPLIT_APPEND |
| 5862 | |
| 5863 | static |
| 5864 | PyObject *split(PyUnicodeObject *self, |
| 5865 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5866 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5867 | { |
| 5868 | PyObject *list; |
| 5869 | |
| 5870 | if (maxcount < 0) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5871 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5872 | |
| 5873 | list = PyList_New(0); |
| 5874 | if (!list) |
| 5875 | return NULL; |
| 5876 | |
| 5877 | if (substring == NULL) |
| 5878 | return split_whitespace(self,list,maxcount); |
| 5879 | |
| 5880 | else if (substring->length == 1) |
| 5881 | return split_char(self,list,substring->str[0],maxcount); |
| 5882 | |
| 5883 | else if (substring->length == 0) { |
| 5884 | Py_DECREF(list); |
| 5885 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 5886 | return NULL; |
| 5887 | } |
| 5888 | else |
| 5889 | return split_substring(self,list,substring,maxcount); |
| 5890 | } |
| 5891 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5892 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5893 | PyObject *rsplit(PyUnicodeObject *self, |
| 5894 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5895 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5896 | { |
| 5897 | PyObject *list; |
| 5898 | |
| 5899 | if (maxcount < 0) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5900 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5901 | |
| 5902 | list = PyList_New(0); |
| 5903 | if (!list) |
| 5904 | return NULL; |
| 5905 | |
| 5906 | if (substring == NULL) |
| 5907 | return rsplit_whitespace(self,list,maxcount); |
| 5908 | |
| 5909 | else if (substring->length == 1) |
| 5910 | return rsplit_char(self,list,substring->str[0],maxcount); |
| 5911 | |
| 5912 | else if (substring->length == 0) { |
| 5913 | Py_DECREF(list); |
| 5914 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 5915 | return NULL; |
| 5916 | } |
| 5917 | else |
| 5918 | return rsplit_substring(self,list,substring,maxcount); |
| 5919 | } |
| 5920 | |
| 5921 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5922 | PyObject *replace(PyUnicodeObject *self, |
| 5923 | PyUnicodeObject *str1, |
| 5924 | PyUnicodeObject *str2, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5925 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5926 | { |
| 5927 | PyUnicodeObject *u; |
| 5928 | |
| 5929 | if (maxcount < 0) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5930 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5931 | |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5932 | if (str1->length == str2->length) { |
| 5933 | /* same length */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5934 | Py_ssize_t i; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5935 | if (str1->length == 1) { |
| 5936 | /* replace characters */ |
| 5937 | Py_UNICODE u1, u2; |
| 5938 | if (!findchar(self->str, self->length, str1->str[0])) |
| 5939 | goto nothing; |
| 5940 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 5941 | if (!u) |
| 5942 | return NULL; |
| 5943 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5944 | u1 = str1->str[0]; |
| 5945 | u2 = str2->str[0]; |
| 5946 | for (i = 0; i < u->length; i++) |
| 5947 | if (u->str[i] == u1) { |
| 5948 | if (--maxcount < 0) |
| 5949 | break; |
| 5950 | u->str[i] = u2; |
| 5951 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5952 | } else { |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5953 | i = fastsearch( |
| 5954 | self->str, self->length, str1->str, str1->length, FAST_SEARCH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5955 | ); |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5956 | if (i < 0) |
| 5957 | goto nothing; |
| 5958 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 5959 | if (!u) |
| 5960 | return NULL; |
| 5961 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5962 | while (i <= self->length - str1->length) |
| 5963 | if (Py_UNICODE_MATCH(self, i, str1)) { |
| 5964 | if (--maxcount < 0) |
| 5965 | break; |
| 5966 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 5967 | i += str1->length; |
| 5968 | } else |
| 5969 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5970 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5971 | } else { |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5972 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 5973 | Py_ssize_t n, i, j, e; |
Fredrik Lundh | 0c71f88 | 2006-05-25 16:46:54 +0000 | [diff] [blame] | 5974 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5975 | Py_UNICODE *p; |
| 5976 | |
| 5977 | /* replace strings */ |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5978 | n = stringlib_count(self->str, self->length, str1->str, str1->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5979 | if (n > maxcount) |
| 5980 | n = maxcount; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5981 | if (n == 0) |
| 5982 | goto nothing; |
Fredrik Lundh | 0c71f88 | 2006-05-25 16:46:54 +0000 | [diff] [blame] | 5983 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 5984 | delta = (str2->length - str1->length); |
| 5985 | if (delta == 0) { |
| 5986 | new_size = self->length; |
| 5987 | } else { |
| 5988 | product = n * (str2->length - str1->length); |
| 5989 | if ((product / (str2->length - str1->length)) != n) { |
| 5990 | PyErr_SetString(PyExc_OverflowError, |
| 5991 | "replace string is too long"); |
| 5992 | return NULL; |
| 5993 | } |
| 5994 | new_size = self->length + product; |
| 5995 | if (new_size < 0) { |
| 5996 | PyErr_SetString(PyExc_OverflowError, |
| 5997 | "replace string is too long"); |
| 5998 | return NULL; |
| 5999 | } |
| 6000 | } |
| 6001 | u = _PyUnicode_New(new_size); |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6002 | if (!u) |
| 6003 | return NULL; |
| 6004 | i = 0; |
| 6005 | p = u->str; |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 6006 | e = self->length - str1->length; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6007 | if (str1->length > 0) { |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 6008 | while (n-- > 0) { |
| 6009 | /* look for next match */ |
| 6010 | j = i; |
| 6011 | while (j <= e) { |
| 6012 | if (Py_UNICODE_MATCH(self, j, str1)) |
| 6013 | break; |
| 6014 | j++; |
| 6015 | } |
| 6016 | if (j > i) { |
| 6017 | if (j > e) |
| 6018 | break; |
| 6019 | /* copy unchanged part [i:j] */ |
| 6020 | Py_UNICODE_COPY(p, self->str+i, j-i); |
| 6021 | p += j - i; |
| 6022 | } |
| 6023 | /* copy substitution string */ |
| 6024 | if (str2->length > 0) { |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6025 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6026 | p += str2->length; |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 6027 | } |
| 6028 | i = j + str1->length; |
| 6029 | } |
| 6030 | if (i < self->length) |
| 6031 | /* copy tail [i:] */ |
| 6032 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6033 | } else { |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 6034 | /* interleave */ |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6035 | while (n > 0) { |
| 6036 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6037 | p += str2->length; |
| 6038 | if (--n <= 0) |
| 6039 | break; |
| 6040 | *p++ = self->str[i++]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6041 | } |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6042 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6043 | } |
| 6044 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6045 | return (PyObject *) u; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6046 | |
| 6047 | nothing: |
| 6048 | /* nothing to replace; return original string (when possible) */ |
| 6049 | if (PyUnicode_CheckExact(self)) { |
| 6050 | Py_INCREF(self); |
| 6051 | return (PyObject *) self; |
| 6052 | } |
| 6053 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6054 | } |
| 6055 | |
| 6056 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 6057 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6058 | PyDoc_STRVAR(title__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6059 | "S.title() -> unicode\n\ |
| 6060 | \n\ |
| 6061 | 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] | 6062 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6063 | |
| 6064 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6065 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6066 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6067 | return fixup(self, fixtitle); |
| 6068 | } |
| 6069 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6070 | PyDoc_STRVAR(capitalize__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6071 | "S.capitalize() -> unicode\n\ |
| 6072 | \n\ |
| 6073 | 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] | 6074 | have upper case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6075 | |
| 6076 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6077 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6078 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6079 | return fixup(self, fixcapitalize); |
| 6080 | } |
| 6081 | |
| 6082 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6083 | PyDoc_STRVAR(capwords__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6084 | "S.capwords() -> unicode\n\ |
| 6085 | \n\ |
| 6086 | 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] | 6087 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6088 | |
| 6089 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6090 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6091 | { |
| 6092 | PyObject *list; |
| 6093 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6094 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6095 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6096 | /* Split into words */ |
| 6097 | list = split(self, NULL, -1); |
| 6098 | if (!list) |
| 6099 | return NULL; |
| 6100 | |
| 6101 | /* Capitalize each word */ |
| 6102 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 6103 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
| 6104 | fixcapitalize); |
| 6105 | if (item == NULL) |
| 6106 | goto onError; |
| 6107 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 6108 | PyList_SET_ITEM(list, i, item); |
| 6109 | } |
| 6110 | |
| 6111 | /* Join the words to form a new string */ |
| 6112 | item = PyUnicode_Join(NULL, list); |
| 6113 | |
| 6114 | onError: |
| 6115 | Py_DECREF(list); |
| 6116 | return (PyObject *)item; |
| 6117 | } |
| 6118 | #endif |
| 6119 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6120 | /* Argument converter. Coerces to a single unicode character */ |
| 6121 | |
| 6122 | static int |
| 6123 | convert_uc(PyObject *obj, void *addr) |
| 6124 | { |
| 6125 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 6126 | PyObject *uniobj; |
| 6127 | Py_UNICODE *unistr; |
| 6128 | |
| 6129 | uniobj = PyUnicode_FromObject(obj); |
| 6130 | if (uniobj == NULL) { |
| 6131 | PyErr_SetString(PyExc_TypeError, |
| 6132 | "The fill character cannot be converted to Unicode"); |
| 6133 | return 0; |
| 6134 | } |
| 6135 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 6136 | PyErr_SetString(PyExc_TypeError, |
| 6137 | "The fill character must be exactly one character long"); |
| 6138 | Py_DECREF(uniobj); |
| 6139 | return 0; |
| 6140 | } |
| 6141 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 6142 | *fillcharloc = unistr[0]; |
| 6143 | Py_DECREF(uniobj); |
| 6144 | return 1; |
| 6145 | } |
| 6146 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6147 | PyDoc_STRVAR(center__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6148 | "S.center(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6149 | \n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6150 | Return S centered in a Unicode string of length width. Padding is\n\ |
| 6151 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6152 | |
| 6153 | static PyObject * |
| 6154 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 6155 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6156 | Py_ssize_t marg, left; |
| 6157 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6158 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6159 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 6160 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6161 | return NULL; |
| 6162 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6163 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6164 | Py_INCREF(self); |
| 6165 | return (PyObject*) self; |
| 6166 | } |
| 6167 | |
| 6168 | marg = width - self->length; |
| 6169 | left = marg / 2 + (marg & width & 1); |
| 6170 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6171 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6172 | } |
| 6173 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6174 | #if 0 |
| 6175 | |
| 6176 | /* This code should go into some future Unicode collation support |
| 6177 | module. The basic comparison should compare ordinals on a naive |
Trent Mick | 20abf57 | 2000-08-12 22:14:34 +0000 | [diff] [blame] | 6178 | basis (this is what Java does and thus JPython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6179 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6180 | /* speedy UTF-16 code point order comparison */ |
| 6181 | /* gleaned from: */ |
| 6182 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 6183 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6184 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6185 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6186 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6187 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 6188 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6189 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6190 | }; |
| 6191 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6192 | static int |
| 6193 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6194 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6195 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6196 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6197 | Py_UNICODE *s1 = str1->str; |
| 6198 | Py_UNICODE *s2 = str2->str; |
| 6199 | |
| 6200 | len1 = str1->length; |
| 6201 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6202 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6203 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6204 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6205 | |
| 6206 | c1 = *s1++; |
| 6207 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6208 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6209 | if (c1 > (1<<11) * 26) |
| 6210 | c1 += utf16Fixup[c1>>11]; |
| 6211 | if (c2 > (1<<11) * 26) |
| 6212 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6213 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6214 | |
| 6215 | if (c1 != c2) |
| 6216 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6217 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6218 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6219 | } |
| 6220 | |
| 6221 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6222 | } |
| 6223 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6224 | #else |
| 6225 | |
| 6226 | static int |
| 6227 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6228 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6229 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6230 | |
| 6231 | Py_UNICODE *s1 = str1->str; |
| 6232 | Py_UNICODE *s2 = str2->str; |
| 6233 | |
| 6234 | len1 = str1->length; |
| 6235 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6236 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6237 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6238 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6239 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6240 | c1 = *s1++; |
| 6241 | c2 = *s2++; |
| 6242 | |
| 6243 | if (c1 != c2) |
| 6244 | return (c1 < c2) ? -1 : 1; |
| 6245 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6246 | len1--; len2--; |
| 6247 | } |
| 6248 | |
| 6249 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6250 | } |
| 6251 | |
| 6252 | #endif |
| 6253 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6254 | int PyUnicode_Compare(PyObject *left, |
| 6255 | PyObject *right) |
| 6256 | { |
| 6257 | PyUnicodeObject *u = NULL, *v = NULL; |
| 6258 | int result; |
| 6259 | |
| 6260 | /* Coerce the two arguments */ |
| 6261 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 6262 | if (u == NULL) |
| 6263 | goto onError; |
| 6264 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 6265 | if (v == NULL) |
| 6266 | goto onError; |
| 6267 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6268 | /* Shortcut for empty or interned objects */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6269 | if (v == u) { |
| 6270 | Py_DECREF(u); |
| 6271 | Py_DECREF(v); |
| 6272 | return 0; |
| 6273 | } |
| 6274 | |
| 6275 | result = unicode_compare(u, v); |
| 6276 | |
| 6277 | Py_DECREF(u); |
| 6278 | Py_DECREF(v); |
| 6279 | return result; |
| 6280 | |
| 6281 | onError: |
| 6282 | Py_XDECREF(u); |
| 6283 | Py_XDECREF(v); |
| 6284 | return -1; |
| 6285 | } |
| 6286 | |
Marc-André Lemburg | 040f76b | 2006-08-14 10:55:19 +0000 | [diff] [blame] | 6287 | PyObject *PyUnicode_RichCompare(PyObject *left, |
| 6288 | PyObject *right, |
| 6289 | int op) |
| 6290 | { |
| 6291 | int result; |
| 6292 | |
| 6293 | result = PyUnicode_Compare(left, right); |
| 6294 | if (result == -1 && PyErr_Occurred()) |
| 6295 | goto onError; |
| 6296 | |
| 6297 | /* Convert the return value to a Boolean */ |
| 6298 | switch (op) { |
| 6299 | case Py_EQ: |
| 6300 | result = (result == 0); |
| 6301 | break; |
| 6302 | case Py_NE: |
| 6303 | result = (result != 0); |
| 6304 | break; |
| 6305 | case Py_LE: |
| 6306 | result = (result <= 0); |
| 6307 | break; |
| 6308 | case Py_GE: |
| 6309 | result = (result >= 0); |
| 6310 | break; |
| 6311 | case Py_LT: |
| 6312 | result = (result == -1); |
| 6313 | break; |
| 6314 | case Py_GT: |
| 6315 | result = (result == 1); |
| 6316 | break; |
| 6317 | } |
| 6318 | return PyBool_FromLong(result); |
| 6319 | |
| 6320 | onError: |
| 6321 | |
| 6322 | /* Standard case |
| 6323 | |
| 6324 | Type errors mean that PyUnicode_FromObject() could not convert |
| 6325 | one of the arguments (usually the right hand side) to Unicode, |
| 6326 | ie. we can't handle the comparison request. However, it is |
| 6327 | possible that the other object knows a comparison method, which |
| 6328 | is why we return Py_NotImplemented to give the other object a |
| 6329 | chance. |
| 6330 | |
| 6331 | */ |
| 6332 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 6333 | PyErr_Clear(); |
| 6334 | Py_INCREF(Py_NotImplemented); |
| 6335 | return Py_NotImplemented; |
| 6336 | } |
| 6337 | if (op != Py_EQ && op != Py_NE) |
| 6338 | return NULL; |
| 6339 | |
| 6340 | /* Equality comparison. |
| 6341 | |
| 6342 | This is a special case: we silence any PyExc_UnicodeDecodeError |
| 6343 | and instead turn it into a PyErr_UnicodeWarning. |
| 6344 | |
| 6345 | */ |
| 6346 | if (!PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) |
| 6347 | return NULL; |
| 6348 | PyErr_Clear(); |
| 6349 | if (PyErr_Warn(PyExc_UnicodeWarning, |
| 6350 | (op == Py_EQ) ? |
| 6351 | "Unicode equal comparison " |
| 6352 | "failed to convert both arguments to Unicode - " |
| 6353 | "interpreting them as being unequal" : |
| 6354 | "Unicode unequal comparison " |
| 6355 | "failed to convert both arguments to Unicode - " |
| 6356 | "interpreting them as being unequal" |
| 6357 | ) < 0) |
| 6358 | return NULL; |
| 6359 | result = (op == Py_NE); |
| 6360 | return PyBool_FromLong(result); |
| 6361 | } |
| 6362 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6363 | int PyUnicode_Contains(PyObject *container, |
| 6364 | PyObject *element) |
| 6365 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6366 | PyObject *str, *sub; |
| 6367 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6368 | |
| 6369 | /* Coerce the two arguments */ |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6370 | sub = PyUnicode_FromObject(element); |
| 6371 | if (!sub) { |
Marc-André Lemburg | 7c01468 | 2000-06-28 08:11:47 +0000 | [diff] [blame] | 6372 | PyErr_SetString(PyExc_TypeError, |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 6373 | "'in <string>' requires string as left operand"); |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 6374 | return -1; |
Marc-André Lemburg | 7c01468 | 2000-06-28 08:11:47 +0000 | [diff] [blame] | 6375 | } |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 6376 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6377 | str = PyUnicode_FromObject(container); |
| 6378 | if (!str) { |
| 6379 | Py_DECREF(sub); |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 6380 | return -1; |
| 6381 | } |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6382 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6383 | result = stringlib_contains_obj(str, sub); |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 6384 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6385 | Py_DECREF(str); |
| 6386 | Py_DECREF(sub); |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6387 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6388 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6389 | } |
| 6390 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6391 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 6392 | |
| 6393 | PyObject *PyUnicode_Concat(PyObject *left, |
| 6394 | PyObject *right) |
| 6395 | { |
| 6396 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 6397 | |
| 6398 | /* Coerce the two arguments */ |
| 6399 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 6400 | if (u == NULL) |
| 6401 | goto onError; |
| 6402 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 6403 | if (v == NULL) |
| 6404 | goto onError; |
| 6405 | |
| 6406 | /* Shortcuts */ |
| 6407 | if (v == unicode_empty) { |
| 6408 | Py_DECREF(v); |
| 6409 | return (PyObject *)u; |
| 6410 | } |
| 6411 | if (u == unicode_empty) { |
| 6412 | Py_DECREF(u); |
| 6413 | return (PyObject *)v; |
| 6414 | } |
| 6415 | |
| 6416 | /* Concat the two Unicode strings */ |
| 6417 | w = _PyUnicode_New(u->length + v->length); |
| 6418 | if (w == NULL) |
| 6419 | goto onError; |
| 6420 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 6421 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 6422 | |
| 6423 | Py_DECREF(u); |
| 6424 | Py_DECREF(v); |
| 6425 | return (PyObject *)w; |
| 6426 | |
| 6427 | onError: |
| 6428 | Py_XDECREF(u); |
| 6429 | Py_XDECREF(v); |
| 6430 | return NULL; |
| 6431 | } |
| 6432 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6433 | PyDoc_STRVAR(count__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6434 | "S.count(sub[, start[, end]]) -> int\n\ |
| 6435 | \n\ |
Fredrik Lundh | 763b50f | 2006-05-22 15:35:12 +0000 | [diff] [blame] | 6436 | Return the number of non-overlapping occurrences of substring sub in\n\ |
| 6437 | 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] | 6438 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6439 | |
| 6440 | static PyObject * |
| 6441 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 6442 | { |
| 6443 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6444 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6445 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6446 | PyObject *result; |
| 6447 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6448 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
| 6449 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6450 | return NULL; |
| 6451 | |
| 6452 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 6453 | (PyObject *)substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6454 | if (substring == NULL) |
| 6455 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6456 | |
Fredrik Lundh | c816281 | 2006-05-26 19:33:03 +0000 | [diff] [blame] | 6457 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6458 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 6459 | result = PyInt_FromSsize_t( |
| 6460 | stringlib_count(self->str + start, end - start, |
| 6461 | substring->str, substring->length) |
| 6462 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6463 | |
| 6464 | Py_DECREF(substring); |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 6465 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6466 | return result; |
| 6467 | } |
| 6468 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6469 | PyDoc_STRVAR(encode__doc__, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6470 | "S.encode([encoding[,errors]]) -> string or unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6471 | \n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6472 | Encodes S using the codec registered for encoding. encoding defaults\n\ |
| 6473 | 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] | 6474 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6475 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 6476 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 6477 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6478 | |
| 6479 | static PyObject * |
| 6480 | unicode_encode(PyUnicodeObject *self, PyObject *args) |
| 6481 | { |
| 6482 | char *encoding = NULL; |
| 6483 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6484 | PyObject *v; |
| 6485 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6486 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 6487 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6488 | v = PyUnicode_AsEncodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6489 | if (v == NULL) |
| 6490 | goto onError; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6491 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
| 6492 | PyErr_Format(PyExc_TypeError, |
| 6493 | "encoder did not return a string/unicode object " |
| 6494 | "(type=%.400s)", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 6495 | Py_TYPE(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6496 | Py_DECREF(v); |
| 6497 | return NULL; |
| 6498 | } |
| 6499 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6500 | |
| 6501 | onError: |
| 6502 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6503 | } |
| 6504 | |
| 6505 | PyDoc_STRVAR(decode__doc__, |
| 6506 | "S.decode([encoding[,errors]]) -> string or unicode\n\ |
| 6507 | \n\ |
| 6508 | Decodes S using the codec registered for encoding. encoding defaults\n\ |
| 6509 | to the default encoding. errors may be given to set a different error\n\ |
| 6510 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
| 6511 | a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ |
| 6512 | as well as any other name registerd with codecs.register_error that is\n\ |
| 6513 | able to handle UnicodeDecodeErrors."); |
| 6514 | |
| 6515 | static PyObject * |
Marc-André Lemburg | 126b44c | 2004-07-10 12:04:20 +0000 | [diff] [blame] | 6516 | unicode_decode(PyUnicodeObject *self, PyObject *args) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6517 | { |
| 6518 | char *encoding = NULL; |
| 6519 | char *errors = NULL; |
| 6520 | PyObject *v; |
| 6521 | |
| 6522 | if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors)) |
| 6523 | return NULL; |
| 6524 | v = PyUnicode_AsDecodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6525 | if (v == NULL) |
| 6526 | goto onError; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6527 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
| 6528 | PyErr_Format(PyExc_TypeError, |
| 6529 | "decoder did not return a string/unicode object " |
| 6530 | "(type=%.400s)", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 6531 | Py_TYPE(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6532 | Py_DECREF(v); |
| 6533 | return NULL; |
| 6534 | } |
| 6535 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6536 | |
| 6537 | onError: |
| 6538 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6539 | } |
| 6540 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6541 | PyDoc_STRVAR(expandtabs__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6542 | "S.expandtabs([tabsize]) -> unicode\n\ |
| 6543 | \n\ |
| 6544 | 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] | 6545 | 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] | 6546 | |
| 6547 | static PyObject* |
| 6548 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 6549 | { |
| 6550 | Py_UNICODE *e; |
| 6551 | Py_UNICODE *p; |
| 6552 | Py_UNICODE *q; |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6553 | Py_UNICODE *qe; |
| 6554 | Py_ssize_t i, j, incr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6555 | PyUnicodeObject *u; |
| 6556 | int tabsize = 8; |
| 6557 | |
| 6558 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
| 6559 | return NULL; |
| 6560 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6561 | /* First pass: determine size of output string */ |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6562 | i = 0; /* chars up to and including most recent \n or \r */ |
| 6563 | j = 0; /* chars since most recent \n or \r (use in tab calculations) */ |
| 6564 | e = self->str + self->length; /* end of input */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6565 | for (p = self->str; p < e; p++) |
| 6566 | if (*p == '\t') { |
Neal Norwitz | 7dbd2a3 | 2007-06-09 03:36:34 +0000 | [diff] [blame] | 6567 | if (tabsize > 0) { |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6568 | incr = tabsize - (j % tabsize); /* cannot overflow */ |
| 6569 | if (j > PY_SSIZE_T_MAX - incr) |
| 6570 | goto overflow1; |
| 6571 | j += incr; |
| 6572 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6573 | } |
| 6574 | else { |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6575 | if (j > PY_SSIZE_T_MAX - 1) |
| 6576 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6577 | j++; |
| 6578 | if (*p == '\n' || *p == '\r') { |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6579 | if (i > PY_SSIZE_T_MAX - j) |
| 6580 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6581 | i += j; |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6582 | j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6583 | } |
| 6584 | } |
| 6585 | |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6586 | if (i > PY_SSIZE_T_MAX - j) |
| 6587 | goto overflow1; |
Neal Norwitz | 7dbd2a3 | 2007-06-09 03:36:34 +0000 | [diff] [blame] | 6588 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6589 | /* Second pass: create output string and fill it */ |
| 6590 | u = _PyUnicode_New(i + j); |
| 6591 | if (!u) |
| 6592 | return NULL; |
| 6593 | |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6594 | j = 0; /* same as in first pass */ |
| 6595 | q = u->str; /* next output char */ |
| 6596 | qe = u->str + u->length; /* end of output */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6597 | |
| 6598 | for (p = self->str; p < e; p++) |
| 6599 | if (*p == '\t') { |
| 6600 | if (tabsize > 0) { |
| 6601 | i = tabsize - (j % tabsize); |
| 6602 | j += i; |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6603 | while (i--) { |
| 6604 | if (q >= qe) |
| 6605 | goto overflow2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6606 | *q++ = ' '; |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6607 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6608 | } |
| 6609 | } |
| 6610 | else { |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6611 | if (q >= qe) |
| 6612 | goto overflow2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6613 | *q++ = *p; |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6614 | j++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6615 | if (*p == '\n' || *p == '\r') |
| 6616 | j = 0; |
| 6617 | } |
| 6618 | |
| 6619 | return (PyObject*) u; |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6620 | |
| 6621 | overflow2: |
| 6622 | Py_DECREF(u); |
| 6623 | overflow1: |
| 6624 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 6625 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6626 | } |
| 6627 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6628 | PyDoc_STRVAR(find__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6629 | "S.find(sub [,start [,end]]) -> int\n\ |
| 6630 | \n\ |
| 6631 | Return the lowest index in S where substring sub is found,\n\ |
Georg Brandl | 9efd9b6 | 2007-07-29 17:38:35 +0000 | [diff] [blame] | 6632 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6633 | arguments start and end are interpreted as in slice notation.\n\ |
| 6634 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6635 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6636 | |
| 6637 | static PyObject * |
| 6638 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 6639 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6640 | PyObject *substring; |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 6641 | Py_ssize_t start; |
| 6642 | Py_ssize_t end; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6643 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6644 | |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 6645 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6646 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6647 | |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 6648 | result = stringlib_find_slice( |
| 6649 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6650 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6651 | start, end |
| 6652 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6653 | |
| 6654 | Py_DECREF(substring); |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6655 | |
| 6656 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6657 | } |
| 6658 | |
| 6659 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6660 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6661 | { |
| 6662 | if (index < 0 || index >= self->length) { |
| 6663 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 6664 | return NULL; |
| 6665 | } |
| 6666 | |
| 6667 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 6668 | } |
| 6669 | |
| 6670 | static long |
| 6671 | unicode_hash(PyUnicodeObject *self) |
| 6672 | { |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 6673 | /* Since Unicode objects compare equal to their ASCII string |
| 6674 | counterparts, they should use the individual character values |
| 6675 | as basis for their hash value. This is needed to assure that |
| 6676 | strings and Unicode objects behave in the same way as |
| 6677 | dictionary keys. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6678 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6679 | register Py_ssize_t len; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 6680 | register Py_UNICODE *p; |
| 6681 | register long x; |
| 6682 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6683 | if (self->hash != -1) |
| 6684 | return self->hash; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 6685 | len = PyUnicode_GET_SIZE(self); |
| 6686 | p = PyUnicode_AS_UNICODE(self); |
| 6687 | x = *p << 7; |
| 6688 | while (--len >= 0) |
| 6689 | x = (1000003*x) ^ *p++; |
| 6690 | x ^= PyUnicode_GET_SIZE(self); |
| 6691 | if (x == -1) |
| 6692 | x = -2; |
| 6693 | self->hash = x; |
| 6694 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6695 | } |
| 6696 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6697 | PyDoc_STRVAR(index__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6698 | "S.index(sub [,start [,end]]) -> int\n\ |
| 6699 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6700 | 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] | 6701 | |
| 6702 | static PyObject * |
| 6703 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 6704 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6705 | Py_ssize_t result; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6706 | PyObject *substring; |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 6707 | Py_ssize_t start; |
| 6708 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6709 | |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 6710 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6711 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6712 | |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 6713 | result = stringlib_find_slice( |
| 6714 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6715 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6716 | start, end |
| 6717 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6718 | |
| 6719 | Py_DECREF(substring); |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6720 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6721 | if (result < 0) { |
| 6722 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 6723 | return NULL; |
| 6724 | } |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6725 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6726 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6727 | } |
| 6728 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6729 | PyDoc_STRVAR(islower__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6730 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6731 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6732 | 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] | 6733 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6734 | |
| 6735 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6736 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6737 | { |
| 6738 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6739 | register const Py_UNICODE *e; |
| 6740 | int cased; |
| 6741 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6742 | /* Shortcut for single character strings */ |
| 6743 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6744 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6745 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6746 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6747 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6748 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6749 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6750 | e = p + PyUnicode_GET_SIZE(self); |
| 6751 | cased = 0; |
| 6752 | for (; p < e; p++) { |
| 6753 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6754 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6755 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6756 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6757 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 6758 | cased = 1; |
| 6759 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6760 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6761 | } |
| 6762 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6763 | PyDoc_STRVAR(isupper__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6764 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6765 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6766 | 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] | 6767 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6768 | |
| 6769 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6770 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6771 | { |
| 6772 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6773 | register const Py_UNICODE *e; |
| 6774 | int cased; |
| 6775 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6776 | /* Shortcut for single character strings */ |
| 6777 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6778 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6779 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6780 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6781 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6782 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6783 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6784 | e = p + PyUnicode_GET_SIZE(self); |
| 6785 | cased = 0; |
| 6786 | for (; p < e; p++) { |
| 6787 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6788 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6789 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6790 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6791 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 6792 | cased = 1; |
| 6793 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6794 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6795 | } |
| 6796 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6797 | PyDoc_STRVAR(istitle__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6798 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6799 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6800 | Return True if S is a titlecased string and there is at least one\n\ |
| 6801 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 6802 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 6803 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6804 | |
| 6805 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6806 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6807 | { |
| 6808 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6809 | register const Py_UNICODE *e; |
| 6810 | int cased, previous_is_cased; |
| 6811 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6812 | /* Shortcut for single character strings */ |
| 6813 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6814 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 6815 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6816 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6817 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6818 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6819 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6820 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6821 | e = p + PyUnicode_GET_SIZE(self); |
| 6822 | cased = 0; |
| 6823 | previous_is_cased = 0; |
| 6824 | for (; p < e; p++) { |
| 6825 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6826 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6827 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 6828 | if (previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6829 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6830 | previous_is_cased = 1; |
| 6831 | cased = 1; |
| 6832 | } |
| 6833 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 6834 | if (!previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6835 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6836 | previous_is_cased = 1; |
| 6837 | cased = 1; |
| 6838 | } |
| 6839 | else |
| 6840 | previous_is_cased = 0; |
| 6841 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6842 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6843 | } |
| 6844 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6845 | PyDoc_STRVAR(isspace__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6846 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6847 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6848 | Return True if all characters in S are whitespace\n\ |
| 6849 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6850 | |
| 6851 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6852 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6853 | { |
| 6854 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6855 | register const Py_UNICODE *e; |
| 6856 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6857 | /* Shortcut for single character strings */ |
| 6858 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6859 | Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6860 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6861 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6862 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6863 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6864 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6865 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6866 | e = p + PyUnicode_GET_SIZE(self); |
| 6867 | for (; p < e; p++) { |
| 6868 | if (!Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6869 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6870 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6871 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6872 | } |
| 6873 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6874 | PyDoc_STRVAR(isalpha__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6875 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6876 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6877 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6878 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6879 | |
| 6880 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6881 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6882 | { |
| 6883 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6884 | register const Py_UNICODE *e; |
| 6885 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6886 | /* Shortcut for single character strings */ |
| 6887 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6888 | Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6889 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6890 | |
| 6891 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6892 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6893 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6894 | |
| 6895 | e = p + PyUnicode_GET_SIZE(self); |
| 6896 | for (; p < e; p++) { |
| 6897 | if (!Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6898 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6899 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6900 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6901 | } |
| 6902 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6903 | PyDoc_STRVAR(isalnum__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6904 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6905 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6906 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6907 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6908 | |
| 6909 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6910 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6911 | { |
| 6912 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6913 | register const Py_UNICODE *e; |
| 6914 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6915 | /* Shortcut for single character strings */ |
| 6916 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6917 | Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6918 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6919 | |
| 6920 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6921 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6922 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6923 | |
| 6924 | e = p + PyUnicode_GET_SIZE(self); |
| 6925 | for (; p < e; p++) { |
| 6926 | if (!Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6927 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6928 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6929 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6930 | } |
| 6931 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6932 | PyDoc_STRVAR(isdecimal__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6933 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6934 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6935 | 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] | 6936 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6937 | |
| 6938 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6939 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6940 | { |
| 6941 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6942 | register const Py_UNICODE *e; |
| 6943 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6944 | /* Shortcut for single character strings */ |
| 6945 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6946 | Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6947 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6948 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6949 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6950 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6951 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6952 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6953 | e = p + PyUnicode_GET_SIZE(self); |
| 6954 | for (; p < e; p++) { |
| 6955 | if (!Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6956 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6957 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6958 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6959 | } |
| 6960 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6961 | PyDoc_STRVAR(isdigit__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6962 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6963 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6964 | Return True if all characters in S are digits\n\ |
| 6965 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6966 | |
| 6967 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6968 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6969 | { |
| 6970 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6971 | register const Py_UNICODE *e; |
| 6972 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6973 | /* Shortcut for single character strings */ |
| 6974 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6975 | Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6976 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6977 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6978 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6979 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6980 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6981 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6982 | e = p + PyUnicode_GET_SIZE(self); |
| 6983 | for (; p < e; p++) { |
| 6984 | if (!Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6985 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6986 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6987 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6988 | } |
| 6989 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6990 | PyDoc_STRVAR(isnumeric__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6991 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6992 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6993 | 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] | 6994 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6995 | |
| 6996 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6997 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6998 | { |
| 6999 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7000 | register const Py_UNICODE *e; |
| 7001 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7002 | /* Shortcut for single character strings */ |
| 7003 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 7004 | Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7005 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7006 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7007 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7008 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7009 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7010 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7011 | e = p + PyUnicode_GET_SIZE(self); |
| 7012 | for (; p < e; p++) { |
| 7013 | if (!Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7014 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7015 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7016 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7017 | } |
| 7018 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7019 | PyDoc_STRVAR(join__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7020 | "S.join(sequence) -> unicode\n\ |
| 7021 | \n\ |
| 7022 | 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] | 7023 | sequence. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7024 | |
| 7025 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7026 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7027 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7028 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7029 | } |
| 7030 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7031 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7032 | unicode_length(PyUnicodeObject *self) |
| 7033 | { |
| 7034 | return self->length; |
| 7035 | } |
| 7036 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7037 | PyDoc_STRVAR(ljust__doc__, |
Hye-Shik Chang | 974ed7c | 2004-06-02 16:49:17 +0000 | [diff] [blame] | 7038 | "S.ljust(width[, fillchar]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7039 | \n\ |
| 7040 | 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] | 7041 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7042 | |
| 7043 | static PyObject * |
| 7044 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 7045 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7046 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7047 | Py_UNICODE fillchar = ' '; |
| 7048 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7049 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7050 | return NULL; |
| 7051 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7052 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7053 | Py_INCREF(self); |
| 7054 | return (PyObject*) self; |
| 7055 | } |
| 7056 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7057 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7058 | } |
| 7059 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7060 | PyDoc_STRVAR(lower__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7061 | "S.lower() -> unicode\n\ |
| 7062 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7063 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7064 | |
| 7065 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7066 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7067 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7068 | return fixup(self, fixlower); |
| 7069 | } |
| 7070 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7071 | #define LEFTSTRIP 0 |
| 7072 | #define RIGHTSTRIP 1 |
| 7073 | #define BOTHSTRIP 2 |
| 7074 | |
| 7075 | /* Arrays indexed by above */ |
| 7076 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 7077 | |
| 7078 | #define STRIPNAME(i) (stripformat[i]+3) |
| 7079 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7080 | /* externally visible for str.strip(unicode) */ |
| 7081 | PyObject * |
| 7082 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 7083 | { |
| 7084 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7085 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7086 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7087 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 7088 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7089 | |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 7090 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
| 7091 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7092 | i = 0; |
| 7093 | if (striptype != RIGHTSTRIP) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 7094 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 7095 | i++; |
| 7096 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7097 | } |
| 7098 | |
| 7099 | j = len; |
| 7100 | if (striptype != LEFTSTRIP) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 7101 | do { |
| 7102 | j--; |
| 7103 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 7104 | j++; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7105 | } |
| 7106 | |
| 7107 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 7108 | Py_INCREF(self); |
| 7109 | return (PyObject*)self; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7110 | } |
| 7111 | else |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 7112 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7113 | } |
| 7114 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7115 | |
| 7116 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7117 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7118 | { |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7119 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7120 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7121 | |
| 7122 | i = 0; |
| 7123 | if (striptype != RIGHTSTRIP) { |
| 7124 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 7125 | i++; |
| 7126 | } |
| 7127 | } |
| 7128 | |
| 7129 | j = len; |
| 7130 | if (striptype != LEFTSTRIP) { |
| 7131 | do { |
| 7132 | j--; |
| 7133 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 7134 | j++; |
| 7135 | } |
| 7136 | |
| 7137 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 7138 | Py_INCREF(self); |
| 7139 | return (PyObject*)self; |
| 7140 | } |
| 7141 | else |
| 7142 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7143 | } |
| 7144 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7145 | |
| 7146 | static PyObject * |
| 7147 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 7148 | { |
| 7149 | PyObject *sep = NULL; |
| 7150 | |
| 7151 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 7152 | return NULL; |
| 7153 | |
| 7154 | if (sep != NULL && sep != Py_None) { |
| 7155 | if (PyUnicode_Check(sep)) |
| 7156 | return _PyUnicode_XStrip(self, striptype, sep); |
| 7157 | else if (PyString_Check(sep)) { |
| 7158 | PyObject *res; |
| 7159 | sep = PyUnicode_FromObject(sep); |
| 7160 | if (sep==NULL) |
| 7161 | return NULL; |
| 7162 | res = _PyUnicode_XStrip(self, striptype, sep); |
| 7163 | Py_DECREF(sep); |
| 7164 | return res; |
| 7165 | } |
| 7166 | else { |
| 7167 | PyErr_Format(PyExc_TypeError, |
| 7168 | "%s arg must be None, unicode or str", |
| 7169 | STRIPNAME(striptype)); |
| 7170 | return NULL; |
| 7171 | } |
| 7172 | } |
| 7173 | |
| 7174 | return do_strip(self, striptype); |
| 7175 | } |
| 7176 | |
| 7177 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7178 | PyDoc_STRVAR(strip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7179 | "S.strip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7180 | \n\ |
| 7181 | Return a copy of the string S with leading and trailing\n\ |
| 7182 | whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7183 | If chars is given and not None, remove characters in chars instead.\n\ |
| 7184 | 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] | 7185 | |
| 7186 | static PyObject * |
| 7187 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 7188 | { |
| 7189 | if (PyTuple_GET_SIZE(args) == 0) |
| 7190 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 7191 | else |
| 7192 | return do_argstrip(self, BOTHSTRIP, args); |
| 7193 | } |
| 7194 | |
| 7195 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7196 | PyDoc_STRVAR(lstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7197 | "S.lstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7198 | \n\ |
| 7199 | Return a copy of the string S with leading whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7200 | If chars is given and not None, remove characters in chars instead.\n\ |
| 7201 | 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] | 7202 | |
| 7203 | static PyObject * |
| 7204 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 7205 | { |
| 7206 | if (PyTuple_GET_SIZE(args) == 0) |
| 7207 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 7208 | else |
| 7209 | return do_argstrip(self, LEFTSTRIP, args); |
| 7210 | } |
| 7211 | |
| 7212 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7213 | PyDoc_STRVAR(rstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7214 | "S.rstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7215 | \n\ |
| 7216 | Return a copy of the string S with trailing whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7217 | If chars is given and not None, remove characters in chars instead.\n\ |
| 7218 | 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] | 7219 | |
| 7220 | static PyObject * |
| 7221 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 7222 | { |
| 7223 | if (PyTuple_GET_SIZE(args) == 0) |
| 7224 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 7225 | else |
| 7226 | return do_argstrip(self, RIGHTSTRIP, args); |
| 7227 | } |
| 7228 | |
| 7229 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7230 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7231 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7232 | { |
| 7233 | PyUnicodeObject *u; |
| 7234 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7235 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7236 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7237 | |
| 7238 | if (len < 0) |
| 7239 | len = 0; |
| 7240 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7241 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7242 | /* no repeat, return original string */ |
| 7243 | Py_INCREF(str); |
| 7244 | return (PyObject*) str; |
| 7245 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7246 | |
| 7247 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 7248 | * needed doesn't overflow size_t |
| 7249 | */ |
| 7250 | nchars = len * str->length; |
| 7251 | if (len && nchars / len != str->length) { |
| 7252 | PyErr_SetString(PyExc_OverflowError, |
| 7253 | "repeated string is too long"); |
| 7254 | return NULL; |
| 7255 | } |
| 7256 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 7257 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 7258 | PyErr_SetString(PyExc_OverflowError, |
| 7259 | "repeated string is too long"); |
| 7260 | return NULL; |
| 7261 | } |
| 7262 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7263 | if (!u) |
| 7264 | return NULL; |
| 7265 | |
| 7266 | p = u->str; |
| 7267 | |
Fredrik Lundh | f1d60a5 | 2006-05-22 16:29:30 +0000 | [diff] [blame] | 7268 | if (str->length == 1 && len > 0) { |
| 7269 | Py_UNICODE_FILL(p, str->str[0], len); |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 7270 | } else { |
Tim Peters | 1bacc64 | 2006-05-23 05:47:16 +0000 | [diff] [blame] | 7271 | Py_ssize_t done = 0; /* number of characters copied this far */ |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 7272 | if (done < nchars) { |
Fredrik Lundh | f1d60a5 | 2006-05-22 16:29:30 +0000 | [diff] [blame] | 7273 | Py_UNICODE_COPY(p, str->str, str->length); |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 7274 | done = str->length; |
| 7275 | } |
| 7276 | while (done < nchars) { |
Neal Norwitz | 4677fbf7 | 2008-03-25 04:18:18 +0000 | [diff] [blame] | 7277 | Py_ssize_t n = (done <= nchars-done) ? done : nchars-done; |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 7278 | Py_UNICODE_COPY(p+done, p, n); |
| 7279 | done += n; |
| 7280 | } |
| 7281 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7282 | |
| 7283 | return (PyObject*) u; |
| 7284 | } |
| 7285 | |
| 7286 | PyObject *PyUnicode_Replace(PyObject *obj, |
| 7287 | PyObject *subobj, |
| 7288 | PyObject *replobj, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7289 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7290 | { |
| 7291 | PyObject *self; |
| 7292 | PyObject *str1; |
| 7293 | PyObject *str2; |
| 7294 | PyObject *result; |
| 7295 | |
| 7296 | self = PyUnicode_FromObject(obj); |
| 7297 | if (self == NULL) |
| 7298 | return NULL; |
| 7299 | str1 = PyUnicode_FromObject(subobj); |
| 7300 | if (str1 == NULL) { |
| 7301 | Py_DECREF(self); |
| 7302 | return NULL; |
| 7303 | } |
| 7304 | str2 = PyUnicode_FromObject(replobj); |
| 7305 | if (str2 == NULL) { |
| 7306 | Py_DECREF(self); |
| 7307 | Py_DECREF(str1); |
| 7308 | return NULL; |
| 7309 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7310 | result = replace((PyUnicodeObject *)self, |
| 7311 | (PyUnicodeObject *)str1, |
| 7312 | (PyUnicodeObject *)str2, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7313 | maxcount); |
| 7314 | Py_DECREF(self); |
| 7315 | Py_DECREF(str1); |
| 7316 | Py_DECREF(str2); |
| 7317 | return result; |
| 7318 | } |
| 7319 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7320 | PyDoc_STRVAR(replace__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7321 | "S.replace (old, new[, maxsplit]) -> unicode\n\ |
| 7322 | \n\ |
| 7323 | Return a copy of S with all occurrences of substring\n\ |
| 7324 | old replaced by new. If the optional argument maxsplit is\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7325 | given, only the first maxsplit occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7326 | |
| 7327 | static PyObject* |
| 7328 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 7329 | { |
| 7330 | PyUnicodeObject *str1; |
| 7331 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7332 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7333 | PyObject *result; |
| 7334 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7335 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7336 | return NULL; |
| 7337 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 7338 | if (str1 == NULL) |
| 7339 | return NULL; |
| 7340 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7341 | if (str2 == NULL) { |
| 7342 | Py_DECREF(str1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7343 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7344 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7345 | |
| 7346 | result = replace(self, str1, str2, maxcount); |
| 7347 | |
| 7348 | Py_DECREF(str1); |
| 7349 | Py_DECREF(str2); |
| 7350 | return result; |
| 7351 | } |
| 7352 | |
| 7353 | static |
| 7354 | PyObject *unicode_repr(PyObject *unicode) |
| 7355 | { |
| 7356 | return unicodeescape_string(PyUnicode_AS_UNICODE(unicode), |
| 7357 | PyUnicode_GET_SIZE(unicode), |
| 7358 | 1); |
| 7359 | } |
| 7360 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7361 | PyDoc_STRVAR(rfind__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7362 | "S.rfind(sub [,start [,end]]) -> int\n\ |
| 7363 | \n\ |
| 7364 | Return the highest index in S where substring sub is found,\n\ |
Georg Brandl | 9efd9b6 | 2007-07-29 17:38:35 +0000 | [diff] [blame] | 7365 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7366 | arguments start and end are interpreted as in slice notation.\n\ |
| 7367 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7368 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7369 | |
| 7370 | static PyObject * |
| 7371 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 7372 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7373 | PyObject *substring; |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 7374 | Py_ssize_t start; |
| 7375 | Py_ssize_t end; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7376 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7377 | |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 7378 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
| 7379 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7380 | |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 7381 | result = stringlib_rfind_slice( |
| 7382 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7383 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7384 | start, end |
| 7385 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7386 | |
| 7387 | Py_DECREF(substring); |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7388 | |
| 7389 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7390 | } |
| 7391 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7392 | PyDoc_STRVAR(rindex__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7393 | "S.rindex(sub [,start [,end]]) -> int\n\ |
| 7394 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7395 | 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] | 7396 | |
| 7397 | static PyObject * |
| 7398 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 7399 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7400 | PyObject *substring; |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 7401 | Py_ssize_t start; |
| 7402 | Py_ssize_t end; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7403 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7404 | |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 7405 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
| 7406 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7407 | |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 7408 | result = stringlib_rfind_slice( |
| 7409 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7410 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7411 | start, end |
| 7412 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7413 | |
| 7414 | Py_DECREF(substring); |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7415 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7416 | if (result < 0) { |
| 7417 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 7418 | return NULL; |
| 7419 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7420 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7421 | } |
| 7422 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7423 | PyDoc_STRVAR(rjust__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7424 | "S.rjust(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7425 | \n\ |
| 7426 | 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] | 7427 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7428 | |
| 7429 | static PyObject * |
| 7430 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 7431 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7432 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7433 | Py_UNICODE fillchar = ' '; |
| 7434 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7435 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7436 | return NULL; |
| 7437 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7438 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7439 | Py_INCREF(self); |
| 7440 | return (PyObject*) self; |
| 7441 | } |
| 7442 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7443 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7444 | } |
| 7445 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7446 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7447 | unicode_slice(PyUnicodeObject *self, Py_ssize_t start, Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7448 | { |
| 7449 | /* standard clamping */ |
| 7450 | if (start < 0) |
| 7451 | start = 0; |
| 7452 | if (end < 0) |
| 7453 | end = 0; |
| 7454 | if (end > self->length) |
| 7455 | end = self->length; |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7456 | if (start == 0 && end == self->length && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7457 | /* full slice, return original string */ |
| 7458 | Py_INCREF(self); |
| 7459 | return (PyObject*) self; |
| 7460 | } |
| 7461 | if (start > end) |
| 7462 | start = end; |
| 7463 | /* copy slice */ |
| 7464 | return (PyObject*) PyUnicode_FromUnicode(self->str + start, |
| 7465 | end - start); |
| 7466 | } |
| 7467 | |
| 7468 | PyObject *PyUnicode_Split(PyObject *s, |
| 7469 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7470 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7471 | { |
| 7472 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7473 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7474 | s = PyUnicode_FromObject(s); |
| 7475 | if (s == NULL) |
| 7476 | return NULL; |
| 7477 | if (sep != NULL) { |
| 7478 | sep = PyUnicode_FromObject(sep); |
| 7479 | if (sep == NULL) { |
| 7480 | Py_DECREF(s); |
| 7481 | return NULL; |
| 7482 | } |
| 7483 | } |
| 7484 | |
| 7485 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 7486 | |
| 7487 | Py_DECREF(s); |
| 7488 | Py_XDECREF(sep); |
| 7489 | return result; |
| 7490 | } |
| 7491 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7492 | PyDoc_STRVAR(split__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7493 | "S.split([sep [,maxsplit]]) -> list of strings\n\ |
| 7494 | \n\ |
| 7495 | Return a list of the words in S, using sep as the\n\ |
| 7496 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Georg Brandl | dfb77db | 2008-05-11 09:11:40 +0000 | [diff] [blame] | 7497 | splits are done. If sep is not specified or is None, any\n\ |
Georg Brandl | ecbbd94 | 2008-05-11 20:53:55 +0000 | [diff] [blame] | 7498 | whitespace string is a separator and empty strings are\n\ |
| 7499 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7500 | |
| 7501 | static PyObject* |
| 7502 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 7503 | { |
| 7504 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7505 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7506 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7507 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7508 | return NULL; |
| 7509 | |
| 7510 | if (substring == Py_None) |
| 7511 | return split(self, NULL, maxcount); |
| 7512 | else if (PyUnicode_Check(substring)) |
| 7513 | return split(self, (PyUnicodeObject *)substring, maxcount); |
| 7514 | else |
| 7515 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
| 7516 | } |
| 7517 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7518 | PyObject * |
| 7519 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 7520 | { |
| 7521 | PyObject* str_obj; |
| 7522 | PyObject* sep_obj; |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7523 | PyObject* out; |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 7524 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7525 | str_obj = PyUnicode_FromObject(str_in); |
| 7526 | if (!str_obj) |
| 7527 | return NULL; |
| 7528 | sep_obj = PyUnicode_FromObject(sep_in); |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 7529 | if (!sep_obj) { |
| 7530 | Py_DECREF(str_obj); |
| 7531 | return NULL; |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7532 | } |
| 7533 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 7534 | out = stringlib_partition( |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 7535 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 7536 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 7537 | ); |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7538 | |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 7539 | Py_DECREF(sep_obj); |
| 7540 | Py_DECREF(str_obj); |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7541 | |
| 7542 | return out; |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7543 | } |
| 7544 | |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7545 | |
| 7546 | PyObject * |
| 7547 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 7548 | { |
| 7549 | PyObject* str_obj; |
| 7550 | PyObject* sep_obj; |
| 7551 | PyObject* out; |
| 7552 | |
| 7553 | str_obj = PyUnicode_FromObject(str_in); |
| 7554 | if (!str_obj) |
| 7555 | return NULL; |
| 7556 | sep_obj = PyUnicode_FromObject(sep_in); |
| 7557 | if (!sep_obj) { |
| 7558 | Py_DECREF(str_obj); |
| 7559 | return NULL; |
| 7560 | } |
| 7561 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 7562 | out = stringlib_rpartition( |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7563 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 7564 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 7565 | ); |
| 7566 | |
| 7567 | Py_DECREF(sep_obj); |
| 7568 | Py_DECREF(str_obj); |
| 7569 | |
| 7570 | return out; |
| 7571 | } |
| 7572 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7573 | PyDoc_STRVAR(partition__doc__, |
| 7574 | "S.partition(sep) -> (head, sep, tail)\n\ |
| 7575 | \n\ |
| 7576 | Searches for the separator sep in S, and returns the part before it,\n\ |
| 7577 | the separator itself, and the part after it. If the separator is not\n\ |
| 7578 | found, returns S and two empty strings."); |
| 7579 | |
| 7580 | static PyObject* |
Fredrik Lundh | 450277f | 2006-05-26 09:46:59 +0000 | [diff] [blame] | 7581 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7582 | { |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7583 | return PyUnicode_Partition((PyObject *)self, separator); |
| 7584 | } |
| 7585 | |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7586 | PyDoc_STRVAR(rpartition__doc__, |
Raymond Hettinger | a0c95fa | 2006-09-04 15:32:48 +0000 | [diff] [blame] | 7587 | "S.rpartition(sep) -> (tail, sep, head)\n\ |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7588 | \n\ |
| 7589 | Searches for the separator sep in S, starting at the end of S, and returns\n\ |
| 7590 | the part before it, the separator itself, and the part after it. If the\n\ |
Raymond Hettinger | a0c95fa | 2006-09-04 15:32:48 +0000 | [diff] [blame] | 7591 | separator is not found, returns two empty strings and S."); |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7592 | |
| 7593 | static PyObject* |
| 7594 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 7595 | { |
| 7596 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 7597 | } |
| 7598 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7599 | PyObject *PyUnicode_RSplit(PyObject *s, |
| 7600 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7601 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7602 | { |
| 7603 | PyObject *result; |
| 7604 | |
| 7605 | s = PyUnicode_FromObject(s); |
| 7606 | if (s == NULL) |
| 7607 | return NULL; |
| 7608 | if (sep != NULL) { |
| 7609 | sep = PyUnicode_FromObject(sep); |
| 7610 | if (sep == NULL) { |
| 7611 | Py_DECREF(s); |
| 7612 | return NULL; |
| 7613 | } |
| 7614 | } |
| 7615 | |
| 7616 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 7617 | |
| 7618 | Py_DECREF(s); |
| 7619 | Py_XDECREF(sep); |
| 7620 | return result; |
| 7621 | } |
| 7622 | |
| 7623 | PyDoc_STRVAR(rsplit__doc__, |
| 7624 | "S.rsplit([sep [,maxsplit]]) -> list of strings\n\ |
| 7625 | \n\ |
| 7626 | Return a list of the words in S, using sep as the\n\ |
| 7627 | delimiter string, starting at the end of the string and\n\ |
| 7628 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 7629 | splits are done. If sep is not specified, any whitespace string\n\ |
| 7630 | is a separator."); |
| 7631 | |
| 7632 | static PyObject* |
| 7633 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 7634 | { |
| 7635 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7636 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7637 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7638 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7639 | return NULL; |
| 7640 | |
| 7641 | if (substring == Py_None) |
| 7642 | return rsplit(self, NULL, maxcount); |
| 7643 | else if (PyUnicode_Check(substring)) |
| 7644 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
| 7645 | else |
| 7646 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
| 7647 | } |
| 7648 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7649 | PyDoc_STRVAR(splitlines__doc__, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7650 | "S.splitlines([keepends]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7651 | \n\ |
| 7652 | 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] | 7653 | 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] | 7654 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7655 | |
| 7656 | static PyObject* |
| 7657 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 7658 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7659 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7660 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7661 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7662 | return NULL; |
| 7663 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7664 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7665 | } |
| 7666 | |
| 7667 | static |
| 7668 | PyObject *unicode_str(PyUnicodeObject *self) |
| 7669 | { |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7670 | return PyUnicode_AsEncodedString((PyObject *)self, NULL, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7671 | } |
| 7672 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7673 | PyDoc_STRVAR(swapcase__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7674 | "S.swapcase() -> unicode\n\ |
| 7675 | \n\ |
| 7676 | 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] | 7677 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7678 | |
| 7679 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7680 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7681 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7682 | return fixup(self, fixswapcase); |
| 7683 | } |
| 7684 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7685 | PyDoc_STRVAR(translate__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7686 | "S.translate(table) -> unicode\n\ |
| 7687 | \n\ |
| 7688 | Return a copy of the string S, where all characters have been mapped\n\ |
| 7689 | 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] | 7690 | Unicode ordinals to Unicode ordinals, Unicode strings or None.\n\ |
| 7691 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 7692 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7693 | |
| 7694 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7695 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7696 | { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7697 | return PyUnicode_TranslateCharmap(self->str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7698 | self->length, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7699 | table, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7700 | "ignore"); |
| 7701 | } |
| 7702 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7703 | PyDoc_STRVAR(upper__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7704 | "S.upper() -> unicode\n\ |
| 7705 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7706 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7707 | |
| 7708 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7709 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7710 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7711 | return fixup(self, fixupper); |
| 7712 | } |
| 7713 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7714 | PyDoc_STRVAR(zfill__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7715 | "S.zfill(width) -> unicode\n\ |
| 7716 | \n\ |
| 7717 | 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] | 7718 | of the specified width. The string x is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7719 | |
| 7720 | static PyObject * |
| 7721 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 7722 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7723 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7724 | PyUnicodeObject *u; |
| 7725 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7726 | Py_ssize_t width; |
| 7727 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7728 | return NULL; |
| 7729 | |
| 7730 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 7731 | if (PyUnicode_CheckExact(self)) { |
| 7732 | Py_INCREF(self); |
| 7733 | return (PyObject*) self; |
| 7734 | } |
| 7735 | else |
| 7736 | return PyUnicode_FromUnicode( |
| 7737 | PyUnicode_AS_UNICODE(self), |
| 7738 | PyUnicode_GET_SIZE(self) |
| 7739 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7740 | } |
| 7741 | |
| 7742 | fill = width - self->length; |
| 7743 | |
| 7744 | u = pad(self, fill, 0, '0'); |
| 7745 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 7746 | if (u == NULL) |
| 7747 | return NULL; |
| 7748 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7749 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 7750 | /* move sign to beginning of string */ |
| 7751 | u->str[0] = u->str[fill]; |
| 7752 | u->str[fill] = '0'; |
| 7753 | } |
| 7754 | |
| 7755 | return (PyObject*) u; |
| 7756 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7757 | |
| 7758 | #if 0 |
| 7759 | static PyObject* |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 7760 | free_listsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7761 | { |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 7762 | return PyInt_FromLong(numfree); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7763 | } |
| 7764 | #endif |
| 7765 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7766 | PyDoc_STRVAR(startswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7767 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7768 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 7769 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 7770 | With optional start, test S beginning at that position.\n\ |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7771 | With optional end, stop comparing S at that position.\n\ |
| 7772 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7773 | |
| 7774 | static PyObject * |
| 7775 | unicode_startswith(PyUnicodeObject *self, |
| 7776 | PyObject *args) |
| 7777 | { |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7778 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7779 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7780 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7781 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7782 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7783 | |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7784 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 7785 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7786 | return NULL; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7787 | if (PyTuple_Check(subobj)) { |
| 7788 | Py_ssize_t i; |
| 7789 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 7790 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 7791 | PyTuple_GET_ITEM(subobj, i)); |
| 7792 | if (substring == NULL) |
| 7793 | return NULL; |
| 7794 | result = tailmatch(self, substring, start, end, -1); |
| 7795 | Py_DECREF(substring); |
| 7796 | if (result) { |
| 7797 | Py_RETURN_TRUE; |
| 7798 | } |
| 7799 | } |
| 7800 | /* nothing matched */ |
| 7801 | Py_RETURN_FALSE; |
| 7802 | } |
| 7803 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7804 | if (substring == NULL) |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7805 | return NULL; |
| 7806 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7807 | Py_DECREF(substring); |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7808 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7809 | } |
| 7810 | |
| 7811 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7812 | PyDoc_STRVAR(endswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7813 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7814 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 7815 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 7816 | With optional start, test S beginning at that position.\n\ |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7817 | With optional end, stop comparing S at that position.\n\ |
| 7818 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7819 | |
| 7820 | static PyObject * |
| 7821 | unicode_endswith(PyUnicodeObject *self, |
| 7822 | PyObject *args) |
| 7823 | { |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7824 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7825 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7826 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7827 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7828 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7829 | |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7830 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
| 7831 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7832 | return NULL; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7833 | if (PyTuple_Check(subobj)) { |
| 7834 | Py_ssize_t i; |
| 7835 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 7836 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 7837 | PyTuple_GET_ITEM(subobj, i)); |
| 7838 | if (substring == NULL) |
| 7839 | return NULL; |
| 7840 | result = tailmatch(self, substring, start, end, +1); |
| 7841 | Py_DECREF(substring); |
| 7842 | if (result) { |
| 7843 | Py_RETURN_TRUE; |
| 7844 | } |
| 7845 | } |
| 7846 | Py_RETURN_FALSE; |
| 7847 | } |
| 7848 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7849 | if (substring == NULL) |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7850 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7851 | |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7852 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7853 | Py_DECREF(substring); |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7854 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7855 | } |
| 7856 | |
| 7857 | |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 7858 | /* Implements do_string_format, which is unicode because of stringlib */ |
| 7859 | #include "stringlib/string_format.h" |
| 7860 | |
| 7861 | PyDoc_STRVAR(format__doc__, |
| 7862 | "S.format(*args, **kwargs) -> unicode\n\ |
| 7863 | \n\ |
| 7864 | "); |
| 7865 | |
| 7866 | PyDoc_STRVAR(p_format__doc__, |
| 7867 | "S.__format__(format_spec) -> unicode\n\ |
| 7868 | \n\ |
| 7869 | "); |
| 7870 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 7871 | |
| 7872 | static PyObject * |
| 7873 | unicode_getnewargs(PyUnicodeObject *v) |
| 7874 | { |
| 7875 | return Py_BuildValue("(u#)", v->str, v->length); |
| 7876 | } |
| 7877 | |
| 7878 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7879 | static PyMethodDef unicode_methods[] = { |
| 7880 | |
| 7881 | /* Order is according to common usage: often used methods should |
| 7882 | appear first, since lookup is done sequentially. */ |
| 7883 | |
Georg Brandl | ecdc0a9 | 2006-03-30 12:19:07 +0000 | [diff] [blame] | 7884 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7885 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 7886 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7887 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7888 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 7889 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 7890 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 7891 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 7892 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 7893 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 7894 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Fredrik Lundh | 450277f | 2006-05-26 09:46:59 +0000 | [diff] [blame] | 7895 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7896 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 7897 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 7898 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7899 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7900 | {"decode", (PyCFunction) unicode_decode, METH_VARARGS, decode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7901 | /* {"maketrans", (PyCFunction) unicode_maketrans, METH_VARARGS, maketrans__doc__}, */ |
| 7902 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 7903 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 7904 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7905 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7906 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7907 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7908 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7909 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 7910 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 7911 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 7912 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 7913 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 7914 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 7915 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 7916 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 7917 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 7918 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 7919 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 7920 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 7921 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 7922 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7923 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 7924 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
| 7925 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
| 7926 | {"_formatter_field_name_split", (PyCFunction) formatter_field_name_split, METH_NOARGS}, |
| 7927 | {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 7928 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7929 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7930 | #endif |
| 7931 | |
| 7932 | #if 0 |
| 7933 | /* This one is just used for debugging the implementation. */ |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 7934 | {"freelistsize", (PyCFunction) free_listsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7935 | #endif |
| 7936 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 7937 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7938 | {NULL, NULL} |
| 7939 | }; |
| 7940 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 7941 | static PyObject * |
| 7942 | unicode_mod(PyObject *v, PyObject *w) |
| 7943 | { |
| 7944 | if (!PyUnicode_Check(v)) { |
| 7945 | Py_INCREF(Py_NotImplemented); |
| 7946 | return Py_NotImplemented; |
| 7947 | } |
| 7948 | return PyUnicode_Format(v, w); |
| 7949 | } |
| 7950 | |
| 7951 | static PyNumberMethods unicode_as_number = { |
| 7952 | 0, /*nb_add*/ |
| 7953 | 0, /*nb_subtract*/ |
| 7954 | 0, /*nb_multiply*/ |
| 7955 | 0, /*nb_divide*/ |
| 7956 | unicode_mod, /*nb_remainder*/ |
| 7957 | }; |
| 7958 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7959 | static PySequenceMethods unicode_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7960 | (lenfunc) unicode_length, /* sq_length */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 7961 | PyUnicode_Concat, /* sq_concat */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7962 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 7963 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 7964 | (ssizessizeargfunc) unicode_slice, /* sq_slice */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7965 | 0, /* sq_ass_item */ |
| 7966 | 0, /* sq_ass_slice */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 7967 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7968 | }; |
| 7969 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7970 | static PyObject* |
| 7971 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 7972 | { |
Marc-André Lemburg | 3a45779 | 2006-08-14 12:57:27 +0000 | [diff] [blame] | 7973 | if (PyIndex_Check(item)) { |
| 7974 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7975 | if (i == -1 && PyErr_Occurred()) |
| 7976 | return NULL; |
| 7977 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7978 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7979 | return unicode_getitem(self, i); |
| 7980 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7981 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7982 | Py_UNICODE* source_buf; |
| 7983 | Py_UNICODE* result_buf; |
| 7984 | PyObject* result; |
| 7985 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7986 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7987 | &start, &stop, &step, &slicelength) < 0) { |
| 7988 | return NULL; |
| 7989 | } |
| 7990 | |
| 7991 | if (slicelength <= 0) { |
| 7992 | return PyUnicode_FromUnicode(NULL, 0); |
Thomas Wouters | 3ccec68 | 2007-08-28 15:28:19 +0000 | [diff] [blame] | 7993 | } else if (start == 0 && step == 1 && slicelength == self->length && |
| 7994 | PyUnicode_CheckExact(self)) { |
| 7995 | Py_INCREF(self); |
| 7996 | return (PyObject *)self; |
| 7997 | } else if (step == 1) { |
| 7998 | return PyUnicode_FromUnicode(self->str + start, slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 7999 | } else { |
| 8000 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 8001 | result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* |
| 8002 | sizeof(Py_UNICODE)); |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8003 | |
| 8004 | if (result_buf == NULL) |
| 8005 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8006 | |
| 8007 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 8008 | result_buf[i] = source_buf[cur]; |
| 8009 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8010 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8011 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 8012 | PyObject_FREE(result_buf); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8013 | return result; |
| 8014 | } |
| 8015 | } else { |
| 8016 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 8017 | return NULL; |
| 8018 | } |
| 8019 | } |
| 8020 | |
| 8021 | static PyMappingMethods unicode_as_mapping = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8022 | (lenfunc)unicode_length, /* mp_length */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8023 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 8024 | (objobjargproc)0, /* mp_ass_subscript */ |
| 8025 | }; |
| 8026 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8027 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8028 | unicode_buffer_getreadbuf(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8029 | Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8030 | const void **ptr) |
| 8031 | { |
| 8032 | if (index != 0) { |
| 8033 | PyErr_SetString(PyExc_SystemError, |
| 8034 | "accessing non-existent unicode segment"); |
| 8035 | return -1; |
| 8036 | } |
| 8037 | *ptr = (void *) self->str; |
| 8038 | return PyUnicode_GET_DATA_SIZE(self); |
| 8039 | } |
| 8040 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8041 | static Py_ssize_t |
| 8042 | unicode_buffer_getwritebuf(PyUnicodeObject *self, Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8043 | const void **ptr) |
| 8044 | { |
| 8045 | PyErr_SetString(PyExc_TypeError, |
Neal Norwitz | 20e7213 | 2002-06-13 21:25:17 +0000 | [diff] [blame] | 8046 | "cannot use unicode as modifiable buffer"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8047 | return -1; |
| 8048 | } |
| 8049 | |
| 8050 | static int |
| 8051 | unicode_buffer_getsegcount(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8052 | Py_ssize_t *lenp) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8053 | { |
| 8054 | if (lenp) |
| 8055 | *lenp = PyUnicode_GET_DATA_SIZE(self); |
| 8056 | return 1; |
| 8057 | } |
| 8058 | |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 8059 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8060 | unicode_buffer_getcharbuf(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8061 | Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8062 | const void **ptr) |
| 8063 | { |
| 8064 | PyObject *str; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8065 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8066 | if (index != 0) { |
| 8067 | PyErr_SetString(PyExc_SystemError, |
| 8068 | "accessing non-existent unicode segment"); |
| 8069 | return -1; |
| 8070 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 8071 | str = _PyUnicode_AsDefaultEncodedString((PyObject *)self, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8072 | if (str == NULL) |
| 8073 | return -1; |
| 8074 | *ptr = (void *) PyString_AS_STRING(str); |
| 8075 | return PyString_GET_SIZE(str); |
| 8076 | } |
| 8077 | |
| 8078 | /* Helpers for PyUnicode_Format() */ |
| 8079 | |
| 8080 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8081 | 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] | 8082 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8083 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8084 | if (argidx < arglen) { |
| 8085 | (*p_argidx)++; |
| 8086 | if (arglen < 0) |
| 8087 | return args; |
| 8088 | else |
| 8089 | return PyTuple_GetItem(args, argidx); |
| 8090 | } |
| 8091 | PyErr_SetString(PyExc_TypeError, |
| 8092 | "not enough arguments for format string"); |
| 8093 | return NULL; |
| 8094 | } |
| 8095 | |
| 8096 | #define F_LJUST (1<<0) |
| 8097 | #define F_SIGN (1<<1) |
| 8098 | #define F_BLANK (1<<2) |
| 8099 | #define F_ALT (1<<3) |
| 8100 | #define F_ZERO (1<<4) |
| 8101 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8102 | static Py_ssize_t |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8103 | strtounicode(Py_UNICODE *buffer, const char *charbuffer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8104 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8105 | register Py_ssize_t i; |
| 8106 | Py_ssize_t len = strlen(charbuffer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8107 | for (i = len - 1; i >= 0; i--) |
| 8108 | buffer[i] = (Py_UNICODE) charbuffer[i]; |
| 8109 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8110 | return len; |
| 8111 | } |
| 8112 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8113 | static int |
| 8114 | doubletounicode(Py_UNICODE *buffer, size_t len, const char *format, double x) |
| 8115 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8116 | Py_ssize_t result; |
| 8117 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8118 | PyOS_ascii_formatd((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8119 | result = strtounicode(buffer, (char *)buffer); |
| 8120 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8121 | } |
| 8122 | |
| 8123 | static int |
| 8124 | longtounicode(Py_UNICODE *buffer, size_t len, const char *format, long x) |
| 8125 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8126 | Py_ssize_t result; |
| 8127 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8128 | PyOS_snprintf((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8129 | result = strtounicode(buffer, (char *)buffer); |
| 8130 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8131 | } |
| 8132 | |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 8133 | /* XXX To save some code duplication, formatfloat/long/int could have been |
| 8134 | shared with stringobject.c, converting from 8-bit to Unicode after the |
| 8135 | formatting is done. */ |
| 8136 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8137 | static int |
| 8138 | formatfloat(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8139 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8140 | int flags, |
| 8141 | int prec, |
| 8142 | int type, |
| 8143 | PyObject *v) |
| 8144 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8145 | /* fmt = '%#.' + `prec` + `type` |
| 8146 | 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] | 8147 | char fmt[20]; |
| 8148 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8149 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8150 | x = PyFloat_AsDouble(v); |
| 8151 | if (x == -1.0 && PyErr_Occurred()) |
| 8152 | return -1; |
| 8153 | if (prec < 0) |
| 8154 | prec = 6; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8155 | if (type == 'f' && (fabs(x) / 1e25) >= 1e25) |
| 8156 | type = 'g'; |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8157 | /* Worst case length calc to ensure no buffer overrun: |
| 8158 | |
| 8159 | 'g' formats: |
| 8160 | fmt = %#.<prec>g |
| 8161 | buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
| 8162 | for any double rep.) |
| 8163 | len = 1 + prec + 1 + 2 + 5 = 9 + prec |
| 8164 | |
| 8165 | 'f' formats: |
| 8166 | buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
| 8167 | len = 1 + 50 + 1 + prec = 52 + prec |
| 8168 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8169 | If prec=0 the effective precision is 1 (the leading digit is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8170 | always given), therefore increase the length by one. |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8171 | |
| 8172 | */ |
Georg Brandl | 7c3b50d | 2007-07-12 08:38:00 +0000 | [diff] [blame] | 8173 | if (((type == 'g' || type == 'G') && |
| 8174 | buflen <= (size_t)10 + (size_t)prec) || |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8175 | (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8176 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8177 | "formatted float is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8178 | return -1; |
| 8179 | } |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8180 | PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
| 8181 | (flags&F_ALT) ? "#" : "", |
| 8182 | prec, type); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8183 | return doubletounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8184 | } |
| 8185 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8186 | static PyObject* |
| 8187 | formatlong(PyObject *val, int flags, int prec, int type) |
| 8188 | { |
| 8189 | char *buf; |
| 8190 | int i, len; |
| 8191 | PyObject *str; /* temporary string object. */ |
| 8192 | PyUnicodeObject *result; |
| 8193 | |
| 8194 | str = _PyString_FormatLong(val, flags, prec, type, &buf, &len); |
| 8195 | if (!str) |
| 8196 | return NULL; |
| 8197 | result = _PyUnicode_New(len); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 8198 | if (!result) { |
| 8199 | Py_DECREF(str); |
| 8200 | return NULL; |
| 8201 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8202 | for (i = 0; i < len; i++) |
| 8203 | result->str[i] = buf[i]; |
| 8204 | result->str[len] = 0; |
| 8205 | Py_DECREF(str); |
| 8206 | return (PyObject*)result; |
| 8207 | } |
| 8208 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8209 | static int |
| 8210 | formatint(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8211 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8212 | int flags, |
| 8213 | int prec, |
| 8214 | int type, |
| 8215 | PyObject *v) |
| 8216 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8217 | /* fmt = '%#.' + `prec` + 'l' + `type` |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8218 | * worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
| 8219 | * + 1 + 1 |
| 8220 | * = 24 |
| 8221 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8222 | char fmt[64]; /* plenty big enough! */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8223 | char *sign; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8224 | long x; |
| 8225 | |
| 8226 | x = PyInt_AsLong(v); |
| 8227 | if (x == -1 && PyErr_Occurred()) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8228 | return -1; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8229 | if (x < 0 && type == 'u') { |
| 8230 | type = 'd'; |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 8231 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8232 | if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) |
| 8233 | sign = "-"; |
| 8234 | else |
| 8235 | sign = ""; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8236 | if (prec < 0) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8237 | prec = 1; |
| 8238 | |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8239 | /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) |
| 8240 | * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8241 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8242 | if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8243 | PyErr_SetString(PyExc_OverflowError, |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8244 | "formatted integer is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8245 | return -1; |
| 8246 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8247 | |
| 8248 | if ((flags & F_ALT) && |
| 8249 | (type == 'x' || type == 'X')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8250 | /* When converting under %#x or %#X, there are a number |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8251 | * of issues that cause pain: |
| 8252 | * - when 0 is being converted, the C standard leaves off |
| 8253 | * the '0x' or '0X', which is inconsistent with other |
| 8254 | * %#x/%#X conversions and inconsistent with Python's |
| 8255 | * hex() function |
| 8256 | * - there are platforms that violate the standard and |
| 8257 | * convert 0 with the '0x' or '0X' |
| 8258 | * (Metrowerks, Compaq Tru64) |
| 8259 | * - there are platforms that give '0x' when converting |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8260 | * under %#X, but convert 0 in accordance with the |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8261 | * standard (OS/2 EMX) |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8262 | * |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8263 | * We can achieve the desired consistency by inserting our |
| 8264 | * own '0x' or '0X' prefix, and substituting %x/%X in place |
| 8265 | * of %#x/%#X. |
| 8266 | * |
| 8267 | * Note that this is the same approach as used in |
| 8268 | * formatint() in stringobject.c |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 8269 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8270 | PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", |
| 8271 | sign, type, prec, type); |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 8272 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8273 | else { |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8274 | PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", |
| 8275 | sign, (flags&F_ALT) ? "#" : "", |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8276 | prec, type); |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 8277 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8278 | if (sign[0]) |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8279 | return longtounicode(buf, buflen, fmt, -x); |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8280 | else |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8281 | return longtounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8282 | } |
| 8283 | |
| 8284 | static int |
| 8285 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8286 | size_t buflen, |
| 8287 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8288 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8289 | /* presume that the buffer is at least 2 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8290 | if (PyUnicode_Check(v)) { |
| 8291 | if (PyUnicode_GET_SIZE(v) != 1) |
| 8292 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8293 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8294 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8295 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8296 | else if (PyString_Check(v)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8297 | if (PyString_GET_SIZE(v) != 1) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8298 | goto onError; |
| 8299 | buf[0] = (Py_UNICODE)PyString_AS_STRING(v)[0]; |
| 8300 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8301 | |
| 8302 | else { |
| 8303 | /* Integer input truncated to a character */ |
| 8304 | long x; |
| 8305 | x = PyInt_AsLong(v); |
| 8306 | if (x == -1 && PyErr_Occurred()) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8307 | goto onError; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8308 | #ifdef Py_UNICODE_WIDE |
| 8309 | if (x < 0 || x > 0x10ffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 8310 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8311 | "%c arg not in range(0x110000) " |
| 8312 | "(wide Python build)"); |
| 8313 | return -1; |
| 8314 | } |
| 8315 | #else |
| 8316 | if (x < 0 || x > 0xffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 8317 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8318 | "%c arg not in range(0x10000) " |
| 8319 | "(narrow Python build)"); |
| 8320 | return -1; |
| 8321 | } |
| 8322 | #endif |
| 8323 | buf[0] = (Py_UNICODE) x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8324 | } |
| 8325 | buf[1] = '\0'; |
| 8326 | return 1; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8327 | |
| 8328 | onError: |
| 8329 | PyErr_SetString(PyExc_TypeError, |
| 8330 | "%c requires int or char"); |
| 8331 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8332 | } |
| 8333 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8334 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 8335 | |
| 8336 | FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
| 8337 | chars are formatted. XXX This is a magic number. Each formatting |
| 8338 | routine does bounds checking to ensure no overflow, but a better |
| 8339 | solution may be to malloc a buffer of appropriate size for each |
| 8340 | format. For now, the current solution is sufficient. |
| 8341 | */ |
| 8342 | #define FORMATBUFLEN (size_t)120 |
| 8343 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8344 | PyObject *PyUnicode_Format(PyObject *format, |
| 8345 | PyObject *args) |
| 8346 | { |
| 8347 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8348 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8349 | int args_owned = 0; |
| 8350 | PyUnicodeObject *result = NULL; |
| 8351 | PyObject *dict = NULL; |
| 8352 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8353 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8354 | if (format == NULL || args == NULL) { |
| 8355 | PyErr_BadInternalCall(); |
| 8356 | return NULL; |
| 8357 | } |
| 8358 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8359 | if (uformat == NULL) |
| 8360 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8361 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 8362 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 8363 | |
| 8364 | reslen = rescnt = fmtcnt + 100; |
| 8365 | result = _PyUnicode_New(reslen); |
| 8366 | if (result == NULL) |
| 8367 | goto onError; |
| 8368 | res = PyUnicode_AS_UNICODE(result); |
| 8369 | |
| 8370 | if (PyTuple_Check(args)) { |
| 8371 | arglen = PyTuple_Size(args); |
| 8372 | argidx = 0; |
| 8373 | } |
| 8374 | else { |
| 8375 | arglen = -1; |
| 8376 | argidx = -2; |
| 8377 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 8378 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Neal Norwitz | 80a1bf4 | 2002-11-12 23:01:12 +0000 | [diff] [blame] | 8379 | !PyObject_TypeCheck(args, &PyBaseString_Type)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8380 | dict = args; |
| 8381 | |
| 8382 | while (--fmtcnt >= 0) { |
| 8383 | if (*fmt != '%') { |
| 8384 | if (--rescnt < 0) { |
| 8385 | rescnt = fmtcnt + 100; |
| 8386 | reslen += rescnt; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8387 | if (_PyUnicode_Resize(&result, reslen) < 0) |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 8388 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8389 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 8390 | --rescnt; |
| 8391 | } |
| 8392 | *res++ = *fmt++; |
| 8393 | } |
| 8394 | else { |
| 8395 | /* Got a format specifier */ |
| 8396 | int flags = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8397 | Py_ssize_t width = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8398 | int prec = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8399 | Py_UNICODE c = '\0'; |
| 8400 | Py_UNICODE fill; |
Facundo Batista | c11cecf | 2008-02-24 03:17:21 +0000 | [diff] [blame] | 8401 | int isnumok; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8402 | PyObject *v = NULL; |
| 8403 | PyObject *temp = NULL; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8404 | Py_UNICODE *pbuf; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8405 | Py_UNICODE sign; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8406 | Py_ssize_t len; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8407 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8408 | |
| 8409 | fmt++; |
| 8410 | if (*fmt == '(') { |
| 8411 | Py_UNICODE *keystart; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8412 | Py_ssize_t keylen; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8413 | PyObject *key; |
| 8414 | int pcount = 1; |
| 8415 | |
| 8416 | if (dict == NULL) { |
| 8417 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8418 | "format requires a mapping"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8419 | goto onError; |
| 8420 | } |
| 8421 | ++fmt; |
| 8422 | --fmtcnt; |
| 8423 | keystart = fmt; |
| 8424 | /* Skip over balanced parentheses */ |
| 8425 | while (pcount > 0 && --fmtcnt >= 0) { |
| 8426 | if (*fmt == ')') |
| 8427 | --pcount; |
| 8428 | else if (*fmt == '(') |
| 8429 | ++pcount; |
| 8430 | fmt++; |
| 8431 | } |
| 8432 | keylen = fmt - keystart - 1; |
| 8433 | if (fmtcnt < 0 || pcount > 0) { |
| 8434 | PyErr_SetString(PyExc_ValueError, |
| 8435 | "incomplete format key"); |
| 8436 | goto onError; |
| 8437 | } |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 8438 | #if 0 |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8439 | /* keys are converted to strings using UTF-8 and |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8440 | then looked up since Python uses strings to hold |
| 8441 | variables names etc. in its namespaces and we |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8442 | wouldn't want to break common idioms. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8443 | key = PyUnicode_EncodeUTF8(keystart, |
| 8444 | keylen, |
| 8445 | NULL); |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 8446 | #else |
| 8447 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 8448 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8449 | if (key == NULL) |
| 8450 | goto onError; |
| 8451 | if (args_owned) { |
| 8452 | Py_DECREF(args); |
| 8453 | args_owned = 0; |
| 8454 | } |
| 8455 | args = PyObject_GetItem(dict, key); |
| 8456 | Py_DECREF(key); |
| 8457 | if (args == NULL) { |
| 8458 | goto onError; |
| 8459 | } |
| 8460 | args_owned = 1; |
| 8461 | arglen = -1; |
| 8462 | argidx = -2; |
| 8463 | } |
| 8464 | while (--fmtcnt >= 0) { |
| 8465 | switch (c = *fmt++) { |
| 8466 | case '-': flags |= F_LJUST; continue; |
| 8467 | case '+': flags |= F_SIGN; continue; |
| 8468 | case ' ': flags |= F_BLANK; continue; |
| 8469 | case '#': flags |= F_ALT; continue; |
| 8470 | case '0': flags |= F_ZERO; continue; |
| 8471 | } |
| 8472 | break; |
| 8473 | } |
| 8474 | if (c == '*') { |
| 8475 | v = getnextarg(args, arglen, &argidx); |
| 8476 | if (v == NULL) |
| 8477 | goto onError; |
| 8478 | if (!PyInt_Check(v)) { |
| 8479 | PyErr_SetString(PyExc_TypeError, |
| 8480 | "* wants int"); |
| 8481 | goto onError; |
| 8482 | } |
| 8483 | width = PyInt_AsLong(v); |
| 8484 | if (width < 0) { |
| 8485 | flags |= F_LJUST; |
| 8486 | width = -width; |
| 8487 | } |
| 8488 | if (--fmtcnt >= 0) |
| 8489 | c = *fmt++; |
| 8490 | } |
| 8491 | else if (c >= '0' && c <= '9') { |
| 8492 | width = c - '0'; |
| 8493 | while (--fmtcnt >= 0) { |
| 8494 | c = *fmt++; |
| 8495 | if (c < '0' || c > '9') |
| 8496 | break; |
| 8497 | if ((width*10) / 10 != width) { |
| 8498 | PyErr_SetString(PyExc_ValueError, |
| 8499 | "width too big"); |
| 8500 | goto onError; |
| 8501 | } |
| 8502 | width = width*10 + (c - '0'); |
| 8503 | } |
| 8504 | } |
| 8505 | if (c == '.') { |
| 8506 | prec = 0; |
| 8507 | if (--fmtcnt >= 0) |
| 8508 | c = *fmt++; |
| 8509 | if (c == '*') { |
| 8510 | v = getnextarg(args, arglen, &argidx); |
| 8511 | if (v == NULL) |
| 8512 | goto onError; |
| 8513 | if (!PyInt_Check(v)) { |
| 8514 | PyErr_SetString(PyExc_TypeError, |
| 8515 | "* wants int"); |
| 8516 | goto onError; |
| 8517 | } |
| 8518 | prec = PyInt_AsLong(v); |
| 8519 | if (prec < 0) |
| 8520 | prec = 0; |
| 8521 | if (--fmtcnt >= 0) |
| 8522 | c = *fmt++; |
| 8523 | } |
| 8524 | else if (c >= '0' && c <= '9') { |
| 8525 | prec = c - '0'; |
| 8526 | while (--fmtcnt >= 0) { |
| 8527 | c = Py_CHARMASK(*fmt++); |
| 8528 | if (c < '0' || c > '9') |
| 8529 | break; |
| 8530 | if ((prec*10) / 10 != prec) { |
| 8531 | PyErr_SetString(PyExc_ValueError, |
| 8532 | "prec too big"); |
| 8533 | goto onError; |
| 8534 | } |
| 8535 | prec = prec*10 + (c - '0'); |
| 8536 | } |
| 8537 | } |
| 8538 | } /* prec */ |
| 8539 | if (fmtcnt >= 0) { |
| 8540 | if (c == 'h' || c == 'l' || c == 'L') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8541 | if (--fmtcnt >= 0) |
| 8542 | c = *fmt++; |
| 8543 | } |
| 8544 | } |
| 8545 | if (fmtcnt < 0) { |
| 8546 | PyErr_SetString(PyExc_ValueError, |
| 8547 | "incomplete format"); |
| 8548 | goto onError; |
| 8549 | } |
| 8550 | if (c != '%') { |
| 8551 | v = getnextarg(args, arglen, &argidx); |
| 8552 | if (v == NULL) |
| 8553 | goto onError; |
| 8554 | } |
| 8555 | sign = 0; |
| 8556 | fill = ' '; |
| 8557 | switch (c) { |
| 8558 | |
| 8559 | case '%': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8560 | pbuf = formatbuf; |
| 8561 | /* presume that buffer length is at least 1 */ |
| 8562 | pbuf[0] = '%'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8563 | len = 1; |
| 8564 | break; |
| 8565 | |
| 8566 | case 's': |
| 8567 | case 'r': |
| 8568 | if (PyUnicode_Check(v) && c == 's') { |
| 8569 | temp = v; |
| 8570 | Py_INCREF(temp); |
| 8571 | } |
| 8572 | else { |
| 8573 | PyObject *unicode; |
| 8574 | if (c == 's') |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8575 | temp = PyObject_Unicode(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8576 | else |
| 8577 | temp = PyObject_Repr(v); |
| 8578 | if (temp == NULL) |
| 8579 | goto onError; |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8580 | if (PyUnicode_Check(temp)) |
| 8581 | /* nothing to do */; |
| 8582 | else if (PyString_Check(temp)) { |
| 8583 | /* convert to string to Unicode */ |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8584 | unicode = PyUnicode_Decode(PyString_AS_STRING(temp), |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8585 | PyString_GET_SIZE(temp), |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8586 | NULL, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8587 | "strict"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8588 | Py_DECREF(temp); |
| 8589 | temp = unicode; |
| 8590 | if (temp == NULL) |
| 8591 | goto onError; |
| 8592 | } |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8593 | else { |
| 8594 | Py_DECREF(temp); |
| 8595 | PyErr_SetString(PyExc_TypeError, |
| 8596 | "%s argument has non-string str()"); |
| 8597 | goto onError; |
| 8598 | } |
| 8599 | } |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8600 | pbuf = PyUnicode_AS_UNICODE(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8601 | len = PyUnicode_GET_SIZE(temp); |
| 8602 | if (prec >= 0 && len > prec) |
| 8603 | len = prec; |
| 8604 | break; |
| 8605 | |
| 8606 | case 'i': |
| 8607 | case 'd': |
| 8608 | case 'u': |
| 8609 | case 'o': |
| 8610 | case 'x': |
| 8611 | case 'X': |
| 8612 | if (c == 'i') |
| 8613 | c = 'd'; |
Facundo Batista | c11cecf | 2008-02-24 03:17:21 +0000 | [diff] [blame] | 8614 | isnumok = 0; |
| 8615 | if (PyNumber_Check(v)) { |
| 8616 | PyObject *iobj=NULL; |
| 8617 | |
| 8618 | if (PyInt_Check(v) || (PyLong_Check(v))) { |
| 8619 | iobj = v; |
| 8620 | Py_INCREF(iobj); |
| 8621 | } |
| 8622 | else { |
| 8623 | iobj = PyNumber_Int(v); |
| 8624 | if (iobj==NULL) iobj = PyNumber_Long(v); |
| 8625 | } |
| 8626 | if (iobj!=NULL) { |
| 8627 | if (PyInt_Check(iobj)) { |
| 8628 | isnumok = 1; |
| 8629 | pbuf = formatbuf; |
| 8630 | len = formatint(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 8631 | flags, prec, c, iobj); |
| 8632 | Py_DECREF(iobj); |
| 8633 | if (len < 0) |
| 8634 | goto onError; |
| 8635 | sign = 1; |
| 8636 | } |
| 8637 | else if (PyLong_Check(iobj)) { |
| 8638 | isnumok = 1; |
| 8639 | temp = formatlong(iobj, flags, prec, c); |
| 8640 | Py_DECREF(iobj); |
| 8641 | if (!temp) |
| 8642 | goto onError; |
| 8643 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 8644 | len = PyUnicode_GET_SIZE(temp); |
| 8645 | sign = 1; |
| 8646 | } |
| 8647 | else { |
| 8648 | Py_DECREF(iobj); |
| 8649 | } |
| 8650 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8651 | } |
Facundo Batista | c11cecf | 2008-02-24 03:17:21 +0000 | [diff] [blame] | 8652 | if (!isnumok) { |
| 8653 | PyErr_Format(PyExc_TypeError, |
| 8654 | "%%%c format: a number is required, " |
Martin v. Löwis | d918e4e | 2008-04-07 03:08:28 +0000 | [diff] [blame] | 8655 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8656 | goto onError; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8657 | } |
| 8658 | if (flags & F_ZERO) |
| 8659 | fill = '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8660 | break; |
| 8661 | |
| 8662 | case 'e': |
| 8663 | case 'E': |
| 8664 | case 'f': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 8665 | case 'F': |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8666 | case 'g': |
| 8667 | case 'G': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 8668 | if (c == 'F') |
| 8669 | c = 'f'; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8670 | pbuf = formatbuf; |
| 8671 | len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 8672 | flags, prec, c, v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8673 | if (len < 0) |
| 8674 | goto onError; |
| 8675 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8676 | if (flags & F_ZERO) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8677 | fill = '0'; |
| 8678 | break; |
| 8679 | |
| 8680 | case 'c': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8681 | pbuf = formatbuf; |
| 8682 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8683 | if (len < 0) |
| 8684 | goto onError; |
| 8685 | break; |
| 8686 | |
| 8687 | default: |
| 8688 | PyErr_Format(PyExc_ValueError, |
Andrew M. Kuchling | 6ca8917 | 2000-12-15 13:07:46 +0000 | [diff] [blame] | 8689 | "unsupported format character '%c' (0x%x) " |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 8690 | "at index %zd", |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8691 | (31<=c && c<=126) ? (char)c : '?', |
Marc-André Lemburg | 24e53b6 | 2002-09-24 09:32:14 +0000 | [diff] [blame] | 8692 | (int)c, |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 8693 | (Py_ssize_t)(fmt - 1 - |
| 8694 | PyUnicode_AS_UNICODE(uformat))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8695 | goto onError; |
| 8696 | } |
| 8697 | if (sign) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8698 | if (*pbuf == '-' || *pbuf == '+') { |
| 8699 | sign = *pbuf++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8700 | len--; |
| 8701 | } |
| 8702 | else if (flags & F_SIGN) |
| 8703 | sign = '+'; |
| 8704 | else if (flags & F_BLANK) |
| 8705 | sign = ' '; |
| 8706 | else |
| 8707 | sign = 0; |
| 8708 | } |
| 8709 | if (width < len) |
| 8710 | width = len; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8711 | if (rescnt - (sign != 0) < width) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8712 | reslen -= rescnt; |
| 8713 | rescnt = width + fmtcnt + 100; |
| 8714 | reslen += rescnt; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8715 | if (reslen < 0) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 8716 | Py_XDECREF(temp); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8717 | PyErr_NoMemory(); |
| 8718 | goto onError; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8719 | } |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8720 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 8721 | Py_XDECREF(temp); |
| 8722 | goto onError; |
| 8723 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8724 | res = PyUnicode_AS_UNICODE(result) |
| 8725 | + reslen - rescnt; |
| 8726 | } |
| 8727 | if (sign) { |
| 8728 | if (fill != ' ') |
| 8729 | *res++ = sign; |
| 8730 | rescnt--; |
| 8731 | if (width > len) |
| 8732 | width--; |
| 8733 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8734 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
| 8735 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8736 | assert(pbuf[1] == c); |
| 8737 | if (fill != ' ') { |
| 8738 | *res++ = *pbuf++; |
| 8739 | *res++ = *pbuf++; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8740 | } |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8741 | rescnt -= 2; |
| 8742 | width -= 2; |
| 8743 | if (width < 0) |
| 8744 | width = 0; |
| 8745 | len -= 2; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8746 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8747 | if (width > len && !(flags & F_LJUST)) { |
| 8748 | do { |
| 8749 | --rescnt; |
| 8750 | *res++ = fill; |
| 8751 | } while (--width > len); |
| 8752 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8753 | if (fill == ' ') { |
| 8754 | if (sign) |
| 8755 | *res++ = sign; |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8756 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8757 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8758 | assert(pbuf[1] == c); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8759 | *res++ = *pbuf++; |
| 8760 | *res++ = *pbuf++; |
| 8761 | } |
| 8762 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8763 | Py_UNICODE_COPY(res, pbuf, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8764 | res += len; |
| 8765 | rescnt -= len; |
| 8766 | while (--width >= len) { |
| 8767 | --rescnt; |
| 8768 | *res++ = ' '; |
| 8769 | } |
| 8770 | if (dict && (argidx < arglen) && c != '%') { |
| 8771 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 8772 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8773 | Py_XDECREF(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8774 | goto onError; |
| 8775 | } |
| 8776 | Py_XDECREF(temp); |
| 8777 | } /* '%' */ |
| 8778 | } /* until end */ |
| 8779 | if (argidx < arglen && !dict) { |
| 8780 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 8781 | "not all arguments converted during string formatting"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8782 | goto onError; |
| 8783 | } |
| 8784 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8785 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
| 8786 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8787 | if (args_owned) { |
| 8788 | Py_DECREF(args); |
| 8789 | } |
| 8790 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8791 | return (PyObject *)result; |
| 8792 | |
| 8793 | onError: |
| 8794 | Py_XDECREF(result); |
| 8795 | Py_DECREF(uformat); |
| 8796 | if (args_owned) { |
| 8797 | Py_DECREF(args); |
| 8798 | } |
| 8799 | return NULL; |
| 8800 | } |
| 8801 | |
| 8802 | static PyBufferProcs unicode_as_buffer = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8803 | (readbufferproc) unicode_buffer_getreadbuf, |
| 8804 | (writebufferproc) unicode_buffer_getwritebuf, |
| 8805 | (segcountproc) unicode_buffer_getsegcount, |
| 8806 | (charbufferproc) unicode_buffer_getcharbuf, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8807 | }; |
| 8808 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 8809 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8810 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 8811 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8812 | static PyObject * |
| 8813 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 8814 | { |
| 8815 | PyObject *x = NULL; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 8816 | static char *kwlist[] = {"string", "encoding", "errors", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8817 | char *encoding = NULL; |
| 8818 | char *errors = NULL; |
| 8819 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8820 | if (type != &PyUnicode_Type) |
| 8821 | return unicode_subtype_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8822 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:unicode", |
| 8823 | kwlist, &x, &encoding, &errors)) |
| 8824 | return NULL; |
| 8825 | if (x == NULL) |
| 8826 | return (PyObject *)_PyUnicode_New(0); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 8827 | if (encoding == NULL && errors == NULL) |
| 8828 | return PyObject_Unicode(x); |
| 8829 | else |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8830 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
| 8831 | } |
| 8832 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8833 | static PyObject * |
| 8834 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 8835 | { |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8836 | PyUnicodeObject *tmp, *pnew; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8837 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8838 | |
| 8839 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 8840 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 8841 | if (tmp == NULL) |
| 8842 | return NULL; |
| 8843 | assert(PyUnicode_Check(tmp)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8844 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 8845 | if (pnew == NULL) { |
| 8846 | Py_DECREF(tmp); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8847 | return NULL; |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 8848 | } |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 8849 | pnew->str = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8850 | if (pnew->str == NULL) { |
| 8851 | _Py_ForgetReference((PyObject *)pnew); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 8852 | PyObject_Del(pnew); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 8853 | Py_DECREF(tmp); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 8854 | return PyErr_NoMemory(); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8855 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8856 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 8857 | pnew->length = n; |
| 8858 | pnew->hash = tmp->hash; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8859 | Py_DECREF(tmp); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8860 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8861 | } |
| 8862 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8863 | PyDoc_STRVAR(unicode_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8864 | "unicode(string [, encoding[, errors]]) -> object\n\ |
| 8865 | \n\ |
| 8866 | Create a new Unicode object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 8867 | encoding defaults to the current default string encoding.\n\ |
| 8868 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8869 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8870 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 8871 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8872 | "unicode", /* tp_name */ |
| 8873 | sizeof(PyUnicodeObject), /* tp_size */ |
| 8874 | 0, /* tp_itemsize */ |
| 8875 | /* Slots */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 8876 | (destructor)unicode_dealloc, /* tp_dealloc */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8877 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8878 | 0, /* tp_getattr */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8879 | 0, /* tp_setattr */ |
Marc-André Lemburg | 040f76b | 2006-08-14 10:55:19 +0000 | [diff] [blame] | 8880 | 0, /* tp_compare */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 8881 | unicode_repr, /* tp_repr */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8882 | &unicode_as_number, /* tp_as_number */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8883 | &unicode_as_sequence, /* tp_as_sequence */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8884 | &unicode_as_mapping, /* tp_as_mapping */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8885 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 8886 | 0, /* tp_call*/ |
| 8887 | (reprfunc) unicode_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8888 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 8889 | 0, /* tp_setattro */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8890 | &unicode_as_buffer, /* tp_as_buffer */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8891 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
Neal Norwitz | ee3a1b5 | 2007-02-25 19:44:48 +0000 | [diff] [blame] | 8892 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8893 | unicode_doc, /* tp_doc */ |
| 8894 | 0, /* tp_traverse */ |
| 8895 | 0, /* tp_clear */ |
Marc-André Lemburg | 040f76b | 2006-08-14 10:55:19 +0000 | [diff] [blame] | 8896 | PyUnicode_RichCompare, /* tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8897 | 0, /* tp_weaklistoffset */ |
| 8898 | 0, /* tp_iter */ |
| 8899 | 0, /* tp_iternext */ |
| 8900 | unicode_methods, /* tp_methods */ |
| 8901 | 0, /* tp_members */ |
| 8902 | 0, /* tp_getset */ |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 8903 | &PyBaseString_Type, /* tp_base */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8904 | 0, /* tp_dict */ |
| 8905 | 0, /* tp_descr_get */ |
| 8906 | 0, /* tp_descr_set */ |
| 8907 | 0, /* tp_dictoffset */ |
| 8908 | 0, /* tp_init */ |
| 8909 | 0, /* tp_alloc */ |
| 8910 | unicode_new, /* tp_new */ |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 8911 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8912 | }; |
| 8913 | |
| 8914 | /* Initialize the Unicode implementation */ |
| 8915 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 8916 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8917 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8918 | int i; |
| 8919 | |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 8920 | /* XXX - move this array to unicodectype.c ? */ |
| 8921 | Py_UNICODE linebreak[] = { |
| 8922 | 0x000A, /* LINE FEED */ |
| 8923 | 0x000D, /* CARRIAGE RETURN */ |
| 8924 | 0x001C, /* FILE SEPARATOR */ |
| 8925 | 0x001D, /* GROUP SEPARATOR */ |
| 8926 | 0x001E, /* RECORD SEPARATOR */ |
| 8927 | 0x0085, /* NEXT LINE */ |
| 8928 | 0x2028, /* LINE SEPARATOR */ |
| 8929 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 8930 | }; |
| 8931 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8932 | /* Init the implementation */ |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 8933 | free_list = NULL; |
| 8934 | numfree = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8935 | unicode_empty = _PyUnicode_New(0); |
Neal Norwitz | e1fdb32 | 2006-07-21 05:32:28 +0000 | [diff] [blame] | 8936 | if (!unicode_empty) |
| 8937 | return; |
| 8938 | |
Marc-André Lemburg | 90e8147 | 2000-06-07 09:13:21 +0000 | [diff] [blame] | 8939 | strcpy(unicode_default_encoding, "ascii"); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8940 | for (i = 0; i < 256; i++) |
| 8941 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 8942 | if (PyType_Ready(&PyUnicode_Type) < 0) |
| 8943 | Py_FatalError("Can't initialize 'unicode'"); |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 8944 | |
| 8945 | /* initialize the linebreak bloom filter */ |
| 8946 | bloom_linebreak = make_bloom_mask( |
| 8947 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 8948 | ); |
Neal Norwitz | de4c78a | 2006-06-13 08:28:19 +0000 | [diff] [blame] | 8949 | |
| 8950 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8951 | } |
| 8952 | |
| 8953 | /* Finalize the Unicode implementation */ |
| 8954 | |
Christian Heimes | 3b718a7 | 2008-02-14 12:47:33 +0000 | [diff] [blame] | 8955 | int |
| 8956 | PyUnicode_ClearFreeList(void) |
| 8957 | { |
| 8958 | int freelist_size = numfree; |
| 8959 | PyUnicodeObject *u; |
| 8960 | |
| 8961 | for (u = free_list; u != NULL;) { |
| 8962 | PyUnicodeObject *v = u; |
| 8963 | u = *(PyUnicodeObject **)u; |
| 8964 | if (v->str) |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 8965 | PyObject_DEL(v->str); |
Christian Heimes | 3b718a7 | 2008-02-14 12:47:33 +0000 | [diff] [blame] | 8966 | Py_XDECREF(v->defenc); |
| 8967 | PyObject_Del(v); |
| 8968 | numfree--; |
| 8969 | } |
| 8970 | free_list = NULL; |
| 8971 | assert(numfree == 0); |
| 8972 | return freelist_size; |
| 8973 | } |
| 8974 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8975 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 8976 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8977 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8978 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8979 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 8980 | Py_XDECREF(unicode_empty); |
| 8981 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 8982 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8983 | for (i = 0; i < 256; i++) { |
| 8984 | if (unicode_latin1[i]) { |
| 8985 | Py_DECREF(unicode_latin1[i]); |
| 8986 | unicode_latin1[i] = NULL; |
| 8987 | } |
| 8988 | } |
Christian Heimes | 3b718a7 | 2008-02-14 12:47:33 +0000 | [diff] [blame] | 8989 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8990 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 8991 | |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 8992 | #ifdef __cplusplus |
| 8993 | } |
| 8994 | #endif |
| 8995 | |
| 8996 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 8997 | /* |
| 8998 | Local variables: |
| 8999 | c-basic-offset: 4 |
| 9000 | indent-tabs-mode: nil |
| 9001 | End: |
| 9002 | */ |