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 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 45 | #include "unicodeobject.h" |
Marc-André Lemburg | d49e5b4 | 2000-06-30 14:58:20 +0000 | [diff] [blame] | 46 | #include "ucnhash.h" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 47 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 48 | #ifdef MS_WINDOWS |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 49 | #include <windows.h> |
| 50 | #endif |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 51 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 52 | /* Limit for the Unicode object free list */ |
| 53 | |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 54 | #define PyUnicode_MAXFREELIST 1024 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 55 | |
| 56 | /* Limit for the Unicode object free list stay alive optimization. |
| 57 | |
| 58 | The implementation will keep allocated Unicode memory intact for |
| 59 | all objects on the free list having a size less than this |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 60 | limit. This reduces malloc() overhead for small Unicode objects. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 61 | |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 62 | At worst this will result in PyUnicode_MAXFREELIST * |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 63 | (sizeof(PyUnicodeObject) + KEEPALIVE_SIZE_LIMIT + |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 64 | malloc()-overhead) bytes of unused garbage. |
| 65 | |
| 66 | Setting the limit to 0 effectively turns the feature off. |
| 67 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 68 | Note: This is an experimental feature ! If you get core dumps when |
| 69 | using Unicode objects, turn this feature off. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 70 | |
| 71 | */ |
| 72 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 73 | #define KEEPALIVE_SIZE_LIMIT 9 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 74 | |
| 75 | /* Endianness switches; defaults to little endian */ |
| 76 | |
| 77 | #ifdef WORDS_BIGENDIAN |
| 78 | # define BYTEORDER_IS_BIG_ENDIAN |
| 79 | #else |
| 80 | # define BYTEORDER_IS_LITTLE_ENDIAN |
| 81 | #endif |
| 82 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 83 | /* --- Globals ------------------------------------------------------------ |
| 84 | |
| 85 | The globals are initialized by the _PyUnicode_Init() API and should |
| 86 | not be used before calling that API. |
| 87 | |
| 88 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 89 | |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 90 | |
| 91 | #ifdef __cplusplus |
| 92 | extern "C" { |
| 93 | #endif |
| 94 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 95 | /* Free list for Unicode objects */ |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 96 | static PyUnicodeObject *free_list; |
| 97 | static int numfree; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 98 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 99 | /* The empty Unicode object is shared to improve performance. */ |
| 100 | static PyUnicodeObject *unicode_empty; |
| 101 | |
| 102 | /* Single character Unicode strings in the Latin-1 range are being |
| 103 | shared as well. */ |
| 104 | static PyUnicodeObject *unicode_latin1[256]; |
| 105 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 106 | /* Default encoding to use and assume when NULL is passed as encoding |
| 107 | parameter; it is initialized by _PyUnicode_Init(). |
| 108 | |
| 109 | Always use the PyUnicode_SetDefaultEncoding() and |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 110 | PyUnicode_GetDefaultEncoding() APIs to access this global. |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 111 | |
| 112 | */ |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 113 | static char unicode_default_encoding[100]; |
| 114 | |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 115 | /* Fast detection of the most frequent whitespace characters */ |
| 116 | const unsigned char _Py_ascii_whitespace[] = { |
| 117 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 32a66a0 | 2008-10-02 19:47:50 +0000 | [diff] [blame] | 118 | /* case 0x0009: * HORIZONTAL TABULATION */ |
| 119 | /* case 0x000A: * LINE FEED */ |
| 120 | /* case 0x000B: * VERTICAL TABULATION */ |
| 121 | /* case 0x000C: * FORM FEED */ |
| 122 | /* case 0x000D: * CARRIAGE RETURN */ |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 123 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 124 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 32a66a0 | 2008-10-02 19:47:50 +0000 | [diff] [blame] | 125 | /* case 0x001C: * FILE SEPARATOR */ |
| 126 | /* case 0x001D: * GROUP SEPARATOR */ |
| 127 | /* case 0x001E: * RECORD SEPARATOR */ |
| 128 | /* case 0x001F: * UNIT SEPARATOR */ |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 129 | 0, 0, 0, 0, 1, 1, 1, 1, |
Christian Heimes | 32a66a0 | 2008-10-02 19:47:50 +0000 | [diff] [blame] | 130 | /* case 0x0020: * SPACE */ |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 131 | 1, 0, 0, 0, 0, 0, 0, 0, |
| 132 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 133 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 134 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 135 | |
| 136 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 137 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 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 | }; |
| 145 | |
| 146 | /* Same for linebreaks */ |
| 147 | static unsigned char ascii_linebreak[] = { |
| 148 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 32a66a0 | 2008-10-02 19:47:50 +0000 | [diff] [blame] | 149 | /* 0x000A, * LINE FEED */ |
| 150 | /* 0x000D, * CARRIAGE RETURN */ |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 151 | 0, 0, 1, 0, 0, 1, 0, 0, |
| 152 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 32a66a0 | 2008-10-02 19:47:50 +0000 | [diff] [blame] | 153 | /* 0x001C, * FILE SEPARATOR */ |
| 154 | /* 0x001D, * GROUP SEPARATOR */ |
| 155 | /* 0x001E, * RECORD SEPARATOR */ |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 156 | 0, 0, 0, 0, 1, 1, 1, 0, |
| 157 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 158 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 159 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 160 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 161 | |
| 162 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 163 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 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 | }; |
| 171 | |
| 172 | |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 173 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 174 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 175 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 176 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 177 | return 0x10FFFF; |
| 178 | #else |
| 179 | /* This is actually an illegal character, so it should |
| 180 | not be passed to unichr. */ |
| 181 | return 0xFFFF; |
| 182 | #endif |
| 183 | } |
| 184 | |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 185 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 186 | |
| 187 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 188 | to keep things simple, we use a single bitmask, using the least 5 |
| 189 | bits from each unicode characters as the bit index. */ |
| 190 | |
| 191 | /* the linebreak mask is set up by Unicode_Init below */ |
| 192 | |
| 193 | #define BLOOM_MASK unsigned long |
| 194 | |
| 195 | static BLOOM_MASK bloom_linebreak; |
| 196 | |
| 197 | #define BLOOM(mask, ch) ((mask & (1 << ((ch) & 0x1F)))) |
| 198 | |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 199 | #define BLOOM_LINEBREAK(ch) \ |
| 200 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 201 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 202 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 203 | 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] | 204 | { |
| 205 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 206 | |
| 207 | long mask; |
| 208 | Py_ssize_t i; |
| 209 | |
| 210 | mask = 0; |
| 211 | for (i = 0; i < len; i++) |
| 212 | mask |= (1 << (ptr[i] & 0x1F)); |
| 213 | |
| 214 | return mask; |
| 215 | } |
| 216 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 217 | 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] | 218 | { |
| 219 | Py_ssize_t i; |
| 220 | |
| 221 | for (i = 0; i < setlen; i++) |
| 222 | if (set[i] == chr) |
| 223 | return 1; |
| 224 | |
Fredrik Lundh | 7763351 | 2006-05-23 19:47:35 +0000 | [diff] [blame] | 225 | return 0; |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | #define BLOOM_MEMBER(mask, chr, set, setlen)\ |
| 229 | BLOOM(mask, chr) && unicode_member(chr, set, setlen) |
| 230 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 231 | /* --- Unicode Object ----------------------------------------------------- */ |
| 232 | |
| 233 | static |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 234 | int unicode_resize(register PyUnicodeObject *unicode, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 235 | Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 236 | { |
| 237 | void *oldstr; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 238 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 239 | /* Shortcut if there's nothing much to do. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 240 | if (unicode->length == length) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 241 | goto reset; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 242 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 243 | /* Resizing shared object (unicode_empty or single character |
| 244 | objects) in-place is not allowed. Use PyUnicode_Resize() |
| 245 | instead ! */ |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 246 | |
Martin v. Löwis | 80d2e59 | 2006-04-13 06:06:08 +0000 | [diff] [blame] | 247 | if (unicode == unicode_empty || |
| 248 | (unicode->length == 1 && |
| 249 | unicode->str[0] < 256U && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 250 | unicode_latin1[unicode->str[0]] == unicode)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 251 | PyErr_SetString(PyExc_SystemError, |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 252 | "can't resize shared unicode objects"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 253 | return -1; |
| 254 | } |
| 255 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 256 | /* We allocate one more byte to make sure the string is Ux0000 terminated. |
| 257 | 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] | 258 | safe to look at str[length] (without making any assumptions about what |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 259 | it contains). */ |
| 260 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 261 | oldstr = unicode->str; |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 262 | unicode->str = PyObject_REALLOC(unicode->str, |
| 263 | sizeof(Py_UNICODE) * (length + 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 264 | if (!unicode->str) { |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 265 | unicode->str = (Py_UNICODE *)oldstr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 266 | PyErr_NoMemory(); |
| 267 | return -1; |
| 268 | } |
| 269 | unicode->str[length] = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 270 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 271 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 272 | reset: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 273 | /* Reset the object caches */ |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 274 | if (unicode->defenc) { |
| 275 | Py_DECREF(unicode->defenc); |
| 276 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 277 | } |
| 278 | unicode->hash = -1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 279 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | /* We allocate one more byte to make sure the string is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 284 | Ux0000 terminated -- XXX is this needed ? |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 285 | |
| 286 | XXX This allocator could further be enhanced by assuring that the |
| 287 | free list never reduces its size below 1. |
| 288 | |
| 289 | */ |
| 290 | |
| 291 | static |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 292 | PyUnicodeObject *_PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 293 | { |
| 294 | register PyUnicodeObject *unicode; |
| 295 | |
Andrew Dalke | e0df762 | 2006-05-27 11:04:36 +0000 | [diff] [blame] | 296 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 297 | if (length == 0 && unicode_empty != NULL) { |
| 298 | Py_INCREF(unicode_empty); |
| 299 | return unicode_empty; |
| 300 | } |
| 301 | |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 302 | /* Ensure we won't overflow the size. */ |
| 303 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 304 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 305 | } |
| 306 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 307 | /* Unicode freelist & memory allocation */ |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 308 | if (free_list) { |
| 309 | unicode = free_list; |
| 310 | free_list = *(PyUnicodeObject **)unicode; |
| 311 | numfree--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 312 | if (unicode->str) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 313 | /* Keep-Alive optimization: we only upsize the buffer, |
| 314 | never downsize it. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 315 | if ((unicode->length < length) && |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 316 | unicode_resize(unicode, length) < 0) { |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 317 | PyObject_DEL(unicode->str); |
Amaury Forgeot d'Arc | 06847b1 | 2008-07-31 23:39:05 +0000 | [diff] [blame] | 318 | unicode->str = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 319 | } |
| 320 | } |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 321 | else { |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 322 | size_t new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 323 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 324 | } |
| 325 | PyObject_INIT(unicode, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 326 | } |
| 327 | else { |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 328 | size_t new_size; |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 329 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 330 | if (unicode == NULL) |
| 331 | return NULL; |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 332 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 333 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 336 | if (!unicode->str) { |
| 337 | PyErr_NoMemory(); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 338 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 339 | } |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 340 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 341 | * the caller fails before initializing str -- unicode_resize() |
| 342 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 343 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 344 | * We don't want unicode_resize to read uninitialized memory in |
| 345 | * that case. |
| 346 | */ |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 347 | unicode->str[0] = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 348 | unicode->str[length] = 0; |
Martin v. Löwis | f15da69 | 2006-04-13 07:24:50 +0000 | [diff] [blame] | 349 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 350 | unicode->hash = -1; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 351 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 352 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 353 | |
| 354 | onError: |
Amaury Forgeot d'Arc | 06847b1 | 2008-07-31 23:39:05 +0000 | [diff] [blame] | 355 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 356 | _Py_DEC_REFTOTAL; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 357 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 358 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 359 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | static |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 363 | void unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 364 | { |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 365 | if (PyUnicode_CheckExact(unicode) && |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 366 | numfree < PyUnicode_MAXFREELIST) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 367 | /* Keep-Alive optimization */ |
| 368 | if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 369 | PyObject_DEL(unicode->str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 370 | unicode->str = NULL; |
| 371 | unicode->length = 0; |
| 372 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 373 | if (unicode->defenc) { |
| 374 | Py_DECREF(unicode->defenc); |
| 375 | unicode->defenc = NULL; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 376 | } |
| 377 | /* Add to free list */ |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 378 | *(PyUnicodeObject **)unicode = free_list; |
| 379 | free_list = unicode; |
| 380 | numfree++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 381 | } |
| 382 | else { |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 383 | PyObject_DEL(unicode->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 384 | Py_XDECREF(unicode->defenc); |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 385 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | |
Alexandre Vassalotti | 034e08c | 2008-12-27 06:36:10 +0000 | [diff] [blame] | 389 | static |
| 390 | int _PyUnicode_Resize(PyUnicodeObject **unicode, Py_ssize_t length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 391 | { |
| 392 | register PyUnicodeObject *v; |
| 393 | |
| 394 | /* Argument checks */ |
| 395 | if (unicode == NULL) { |
| 396 | PyErr_BadInternalCall(); |
| 397 | return -1; |
| 398 | } |
Alexandre Vassalotti | 034e08c | 2008-12-27 06:36:10 +0000 | [diff] [blame] | 399 | v = *unicode; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 400 | 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] | 401 | PyErr_BadInternalCall(); |
| 402 | return -1; |
| 403 | } |
| 404 | |
| 405 | /* Resizing unicode_empty and single character objects is not |
| 406 | possible since these are being shared. We simply return a fresh |
| 407 | copy with the same Unicode content. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 408 | if (v->length != length && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 409 | (v == unicode_empty || v->length == 1)) { |
| 410 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 411 | if (w == NULL) |
| 412 | return -1; |
| 413 | Py_UNICODE_COPY(w->str, v->str, |
| 414 | length < v->length ? length : v->length); |
Raymond Hettinger | c8df578 | 2003-03-09 07:30:43 +0000 | [diff] [blame] | 415 | Py_DECREF(*unicode); |
Alexandre Vassalotti | 034e08c | 2008-12-27 06:36:10 +0000 | [diff] [blame] | 416 | *unicode = w; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 421 | objects, since we can modify them in-place. */ |
| 422 | return unicode_resize(v, length); |
| 423 | } |
| 424 | |
Alexandre Vassalotti | 034e08c | 2008-12-27 06:36:10 +0000 | [diff] [blame] | 425 | int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) |
| 426 | { |
| 427 | return _PyUnicode_Resize((PyUnicodeObject **)unicode, length); |
| 428 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 429 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 430 | PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 431 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 432 | { |
| 433 | PyUnicodeObject *unicode; |
| 434 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 435 | /* If the Unicode data is known at construction time, we can apply |
| 436 | some optimizations which share commonly used objects. */ |
| 437 | if (u != NULL) { |
| 438 | |
| 439 | /* Optimization for empty strings */ |
| 440 | if (size == 0 && unicode_empty != NULL) { |
| 441 | Py_INCREF(unicode_empty); |
| 442 | return (PyObject *)unicode_empty; |
| 443 | } |
| 444 | |
| 445 | /* Single character Unicode objects in the Latin-1 range are |
| 446 | shared when using this constructor */ |
| 447 | if (size == 1 && *u < 256) { |
| 448 | unicode = unicode_latin1[*u]; |
| 449 | if (!unicode) { |
| 450 | unicode = _PyUnicode_New(1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 451 | if (!unicode) |
| 452 | return NULL; |
Marc-André Lemburg | 8879a33 | 2001-06-07 12:26:56 +0000 | [diff] [blame] | 453 | unicode->str[0] = *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 454 | unicode_latin1[*u] = unicode; |
| 455 | } |
| 456 | Py_INCREF(unicode); |
| 457 | return (PyObject *)unicode; |
| 458 | } |
| 459 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 460 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 461 | unicode = _PyUnicode_New(size); |
| 462 | if (!unicode) |
| 463 | return NULL; |
| 464 | |
| 465 | /* Copy the Unicode data into the new object */ |
| 466 | if (u != NULL) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 467 | Py_UNICODE_COPY(unicode->str, u, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 468 | |
| 469 | return (PyObject *)unicode; |
| 470 | } |
| 471 | |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 472 | PyObject *PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
| 473 | { |
| 474 | PyUnicodeObject *unicode; |
Gregory P. Smith | c00eb73 | 2008-04-09 23:16:37 +0000 | [diff] [blame] | 475 | |
Gregory P. Smith | c00eb73 | 2008-04-09 23:16:37 +0000 | [diff] [blame] | 476 | if (size < 0) { |
| 477 | PyErr_SetString(PyExc_SystemError, |
| 478 | "Negative size passed to PyUnicode_FromStringAndSize"); |
| 479 | return NULL; |
| 480 | } |
| 481 | |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 482 | /* If the Unicode data is known at construction time, we can apply |
| 483 | some optimizations which share commonly used objects. |
| 484 | Also, this means the input must be UTF-8, so fall back to the |
| 485 | UTF-8 decoder at the end. */ |
| 486 | if (u != NULL) { |
| 487 | |
| 488 | /* Optimization for empty strings */ |
| 489 | if (size == 0 && unicode_empty != NULL) { |
| 490 | Py_INCREF(unicode_empty); |
| 491 | return (PyObject *)unicode_empty; |
| 492 | } |
| 493 | |
| 494 | /* Single characters are shared when using this constructor. |
| 495 | Restrict to ASCII, since the input must be UTF-8. */ |
| 496 | if (size == 1 && Py_CHARMASK(*u) < 128) { |
Neal Norwitz | d183bdd | 2008-03-28 04:58:51 +0000 | [diff] [blame] | 497 | unicode = unicode_latin1[Py_CHARMASK(*u)]; |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 498 | if (!unicode) { |
| 499 | unicode = _PyUnicode_New(1); |
| 500 | if (!unicode) |
| 501 | return NULL; |
| 502 | unicode->str[0] = Py_CHARMASK(*u); |
Neal Norwitz | d183bdd | 2008-03-28 04:58:51 +0000 | [diff] [blame] | 503 | unicode_latin1[Py_CHARMASK(*u)] = unicode; |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 504 | } |
| 505 | Py_INCREF(unicode); |
| 506 | return (PyObject *)unicode; |
| 507 | } |
| 508 | |
| 509 | return PyUnicode_DecodeUTF8(u, size, NULL); |
| 510 | } |
| 511 | |
| 512 | unicode = _PyUnicode_New(size); |
| 513 | if (!unicode) |
| 514 | return NULL; |
| 515 | |
| 516 | return (PyObject *)unicode; |
| 517 | } |
| 518 | |
| 519 | PyObject *PyUnicode_FromString(const char *u) |
| 520 | { |
| 521 | size_t size = strlen(u); |
| 522 | if (size > PY_SSIZE_T_MAX) { |
| 523 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 524 | return NULL; |
| 525 | } |
| 526 | |
| 527 | return PyUnicode_FromStringAndSize(u, size); |
| 528 | } |
| 529 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 530 | #ifdef HAVE_WCHAR_H |
| 531 | |
| 532 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 533 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 534 | { |
| 535 | PyUnicodeObject *unicode; |
| 536 | |
| 537 | if (w == NULL) { |
| 538 | PyErr_BadInternalCall(); |
| 539 | return NULL; |
| 540 | } |
| 541 | |
| 542 | unicode = _PyUnicode_New(size); |
| 543 | if (!unicode) |
| 544 | return NULL; |
| 545 | |
| 546 | /* Copy the wchar_t data into the new object */ |
| 547 | #ifdef HAVE_USABLE_WCHAR_T |
| 548 | memcpy(unicode->str, w, size * sizeof(wchar_t)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 549 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 550 | { |
| 551 | register Py_UNICODE *u; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 552 | register Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 553 | u = PyUnicode_AS_UNICODE(unicode); |
Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 554 | for (i = size; i > 0; i--) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 555 | *u++ = *w++; |
| 556 | } |
| 557 | #endif |
| 558 | |
| 559 | return (PyObject *)unicode; |
| 560 | } |
| 561 | |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 562 | static void |
| 563 | makefmt(char *fmt, int longflag, int size_tflag, int zeropad, int width, int precision, char c) |
| 564 | { |
| 565 | *fmt++ = '%'; |
| 566 | if (width) { |
| 567 | if (zeropad) |
| 568 | *fmt++ = '0'; |
| 569 | fmt += sprintf(fmt, "%d", width); |
| 570 | } |
| 571 | if (precision) |
| 572 | fmt += sprintf(fmt, ".%d", precision); |
| 573 | if (longflag) |
| 574 | *fmt++ = 'l'; |
| 575 | else if (size_tflag) { |
| 576 | char *f = PY_FORMAT_SIZE_T; |
| 577 | while (*f) |
| 578 | *fmt++ = *f++; |
| 579 | } |
| 580 | *fmt++ = c; |
| 581 | *fmt = '\0'; |
| 582 | } |
| 583 | |
| 584 | #define appendstring(string) {for (copy = string;*copy;) *s++ = *copy++;} |
| 585 | |
| 586 | PyObject * |
| 587 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 588 | { |
| 589 | va_list count; |
| 590 | Py_ssize_t callcount = 0; |
| 591 | PyObject **callresults = NULL; |
| 592 | PyObject **callresult = NULL; |
| 593 | Py_ssize_t n = 0; |
| 594 | int width = 0; |
| 595 | int precision = 0; |
| 596 | int zeropad; |
| 597 | const char* f; |
| 598 | Py_UNICODE *s; |
| 599 | PyObject *string; |
| 600 | /* used by sprintf */ |
| 601 | char buffer[21]; |
| 602 | /* use abuffer instead of buffer, if we need more space |
| 603 | * (which can happen if there's a format specifier with width). */ |
| 604 | char *abuffer = NULL; |
| 605 | char *realbuffer; |
| 606 | Py_ssize_t abuffersize = 0; |
| 607 | char fmt[60]; /* should be enough for %0width.precisionld */ |
| 608 | const char *copy; |
| 609 | |
| 610 | #ifdef VA_LIST_IS_ARRAY |
| 611 | Py_MEMCPY(count, vargs, sizeof(va_list)); |
| 612 | #else |
| 613 | #ifdef __va_copy |
| 614 | __va_copy(count, vargs); |
| 615 | #else |
| 616 | count = vargs; |
| 617 | #endif |
| 618 | #endif |
| 619 | /* step 1: count the number of %S/%R format specifications |
| 620 | * (we call PyObject_Str()/PyObject_Repr() for these objects |
| 621 | * once during step 3 and put the result in an array) */ |
| 622 | for (f = format; *f; f++) { |
| 623 | if (*f == '%' && (*(f+1)=='S' || *(f+1)=='R')) |
| 624 | ++callcount; |
| 625 | } |
| 626 | /* step 2: allocate memory for the results of |
| 627 | * PyObject_Str()/PyObject_Repr() calls */ |
| 628 | if (callcount) { |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 629 | callresults = PyObject_Malloc(sizeof(PyObject *)*callcount); |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 630 | if (!callresults) { |
| 631 | PyErr_NoMemory(); |
| 632 | return NULL; |
| 633 | } |
| 634 | callresult = callresults; |
| 635 | } |
| 636 | /* step 3: figure out how large a buffer we need */ |
| 637 | for (f = format; *f; f++) { |
| 638 | if (*f == '%') { |
| 639 | const char* p = f; |
| 640 | width = 0; |
Neal Norwitz | ade57d0 | 2008-03-23 06:19:57 +0000 | [diff] [blame] | 641 | while (isdigit((unsigned)*f)) |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 642 | width = (width*10) + *f++ - '0'; |
Neal Norwitz | ade57d0 | 2008-03-23 06:19:57 +0000 | [diff] [blame] | 643 | while (*++f && *f != '%' && !isalpha((unsigned)*f)) |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 644 | ; |
| 645 | |
| 646 | /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
| 647 | * they don't affect the amount of space we reserve. |
| 648 | */ |
| 649 | if ((*f == 'l' || *f == 'z') && |
| 650 | (f[1] == 'd' || f[1] == 'u')) |
| 651 | ++f; |
| 652 | |
| 653 | switch (*f) { |
| 654 | case 'c': |
| 655 | (void)va_arg(count, int); |
| 656 | /* fall through... */ |
| 657 | case '%': |
| 658 | n++; |
| 659 | break; |
| 660 | case 'd': case 'u': case 'i': case 'x': |
| 661 | (void) va_arg(count, int); |
| 662 | /* 20 bytes is enough to hold a 64-bit |
| 663 | integer. Decimal takes the most space. |
| 664 | This isn't enough for octal. |
| 665 | If a width is specified we need more |
| 666 | (which we allocate later). */ |
| 667 | if (width < 20) |
| 668 | width = 20; |
| 669 | n += width; |
| 670 | if (abuffersize < width) |
| 671 | abuffersize = width; |
| 672 | break; |
| 673 | case 's': |
| 674 | { |
| 675 | /* UTF-8 */ |
| 676 | unsigned char*s; |
| 677 | s = va_arg(count, unsigned char*); |
| 678 | while (*s) { |
| 679 | if (*s < 128) { |
| 680 | n++; s++; |
| 681 | } else if (*s < 0xc0) { |
| 682 | /* invalid UTF-8 */ |
| 683 | n++; s++; |
| 684 | } else if (*s < 0xc0) { |
| 685 | n++; |
| 686 | s++; if(!*s)break; |
| 687 | s++; |
| 688 | } else if (*s < 0xe0) { |
| 689 | n++; |
| 690 | s++; if(!*s)break; |
| 691 | s++; if(!*s)break; |
| 692 | s++; |
| 693 | } else { |
| 694 | #ifdef Py_UNICODE_WIDE |
| 695 | n++; |
| 696 | #else |
| 697 | n+=2; |
| 698 | #endif |
| 699 | s++; if(!*s)break; |
| 700 | s++; if(!*s)break; |
| 701 | s++; if(!*s)break; |
| 702 | s++; |
| 703 | } |
| 704 | } |
| 705 | break; |
| 706 | } |
| 707 | case 'U': |
| 708 | { |
| 709 | PyObject *obj = va_arg(count, PyObject *); |
| 710 | assert(obj && PyUnicode_Check(obj)); |
| 711 | n += PyUnicode_GET_SIZE(obj); |
| 712 | break; |
| 713 | } |
| 714 | case 'V': |
| 715 | { |
| 716 | PyObject *obj = va_arg(count, PyObject *); |
| 717 | const char *str = va_arg(count, const char *); |
| 718 | assert(obj || str); |
| 719 | assert(!obj || PyUnicode_Check(obj)); |
| 720 | if (obj) |
| 721 | n += PyUnicode_GET_SIZE(obj); |
| 722 | else |
| 723 | n += strlen(str); |
| 724 | break; |
| 725 | } |
| 726 | case 'S': |
| 727 | { |
| 728 | PyObject *obj = va_arg(count, PyObject *); |
| 729 | PyObject *str; |
| 730 | assert(obj); |
| 731 | str = PyObject_Str(obj); |
| 732 | if (!str) |
| 733 | goto fail; |
| 734 | n += PyUnicode_GET_SIZE(str); |
| 735 | /* Remember the str and switch to the next slot */ |
| 736 | *callresult++ = str; |
| 737 | break; |
| 738 | } |
| 739 | case 'R': |
| 740 | { |
| 741 | PyObject *obj = va_arg(count, PyObject *); |
| 742 | PyObject *repr; |
| 743 | assert(obj); |
| 744 | repr = PyObject_Repr(obj); |
| 745 | if (!repr) |
| 746 | goto fail; |
| 747 | n += PyUnicode_GET_SIZE(repr); |
| 748 | /* Remember the repr and switch to the next slot */ |
| 749 | *callresult++ = repr; |
| 750 | break; |
| 751 | } |
| 752 | case 'p': |
| 753 | (void) va_arg(count, int); |
| 754 | /* maximum 64-bit pointer representation: |
| 755 | * 0xffffffffffffffff |
| 756 | * so 19 characters is enough. |
| 757 | * XXX I count 18 -- what's the extra for? |
| 758 | */ |
| 759 | n += 19; |
| 760 | break; |
| 761 | default: |
| 762 | /* if we stumble upon an unknown |
| 763 | formatting code, copy the rest of |
| 764 | the format string to the output |
| 765 | string. (we cannot just skip the |
| 766 | code, since there's no way to know |
| 767 | what's in the argument list) */ |
| 768 | n += strlen(p); |
| 769 | goto expand; |
| 770 | } |
| 771 | } else |
| 772 | n++; |
| 773 | } |
| 774 | expand: |
| 775 | if (abuffersize > 20) { |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 776 | abuffer = PyObject_Malloc(abuffersize); |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 777 | if (!abuffer) { |
| 778 | PyErr_NoMemory(); |
| 779 | goto fail; |
| 780 | } |
| 781 | realbuffer = abuffer; |
| 782 | } |
| 783 | else |
| 784 | realbuffer = buffer; |
| 785 | /* step 4: fill the buffer */ |
| 786 | /* Since we've analyzed how much space we need for the worst case, |
| 787 | we don't have to resize the string. |
| 788 | There can be no errors beyond this point. */ |
| 789 | string = PyUnicode_FromUnicode(NULL, n); |
| 790 | if (!string) |
| 791 | goto fail; |
| 792 | |
| 793 | s = PyUnicode_AS_UNICODE(string); |
| 794 | callresult = callresults; |
| 795 | |
| 796 | for (f = format; *f; f++) { |
| 797 | if (*f == '%') { |
| 798 | const char* p = f++; |
| 799 | int longflag = 0; |
| 800 | int size_tflag = 0; |
| 801 | zeropad = (*f == '0'); |
| 802 | /* parse the width.precision part */ |
| 803 | width = 0; |
Neal Norwitz | ade57d0 | 2008-03-23 06:19:57 +0000 | [diff] [blame] | 804 | while (isdigit((unsigned)*f)) |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 805 | width = (width*10) + *f++ - '0'; |
| 806 | precision = 0; |
| 807 | if (*f == '.') { |
| 808 | f++; |
Neal Norwitz | ade57d0 | 2008-03-23 06:19:57 +0000 | [diff] [blame] | 809 | while (isdigit((unsigned)*f)) |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 810 | precision = (precision*10) + *f++ - '0'; |
| 811 | } |
| 812 | /* handle the long flag, but only for %ld and %lu. |
| 813 | others can be added when necessary. */ |
| 814 | if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) { |
| 815 | longflag = 1; |
| 816 | ++f; |
| 817 | } |
| 818 | /* handle the size_t flag. */ |
| 819 | if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
| 820 | size_tflag = 1; |
| 821 | ++f; |
| 822 | } |
| 823 | |
| 824 | switch (*f) { |
| 825 | case 'c': |
| 826 | *s++ = va_arg(vargs, int); |
| 827 | break; |
| 828 | case 'd': |
| 829 | makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'd'); |
| 830 | if (longflag) |
| 831 | sprintf(realbuffer, fmt, va_arg(vargs, long)); |
| 832 | else if (size_tflag) |
| 833 | sprintf(realbuffer, fmt, va_arg(vargs, Py_ssize_t)); |
| 834 | else |
| 835 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 836 | appendstring(realbuffer); |
| 837 | break; |
| 838 | case 'u': |
| 839 | makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'u'); |
| 840 | if (longflag) |
| 841 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned long)); |
| 842 | else if (size_tflag) |
| 843 | sprintf(realbuffer, fmt, va_arg(vargs, size_t)); |
| 844 | else |
| 845 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned int)); |
| 846 | appendstring(realbuffer); |
| 847 | break; |
| 848 | case 'i': |
| 849 | makefmt(fmt, 0, 0, zeropad, width, precision, 'i'); |
| 850 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 851 | appendstring(realbuffer); |
| 852 | break; |
| 853 | case 'x': |
| 854 | makefmt(fmt, 0, 0, zeropad, width, precision, 'x'); |
| 855 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 856 | appendstring(realbuffer); |
| 857 | break; |
| 858 | case 's': |
| 859 | { |
| 860 | /* Parameter must be UTF-8 encoded. |
| 861 | In case of encoding errors, use |
| 862 | the replacement character. */ |
| 863 | PyObject *u; |
| 864 | p = va_arg(vargs, char*); |
| 865 | u = PyUnicode_DecodeUTF8(p, strlen(p), |
| 866 | "replace"); |
| 867 | if (!u) |
| 868 | goto fail; |
| 869 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(u), |
| 870 | PyUnicode_GET_SIZE(u)); |
| 871 | s += PyUnicode_GET_SIZE(u); |
| 872 | Py_DECREF(u); |
| 873 | break; |
| 874 | } |
| 875 | case 'U': |
| 876 | { |
| 877 | PyObject *obj = va_arg(vargs, PyObject *); |
| 878 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 879 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 880 | s += size; |
| 881 | break; |
| 882 | } |
| 883 | case 'V': |
| 884 | { |
| 885 | PyObject *obj = va_arg(vargs, PyObject *); |
| 886 | const char *str = va_arg(vargs, const char *); |
| 887 | if (obj) { |
| 888 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 889 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 890 | s += size; |
| 891 | } else { |
| 892 | appendstring(str); |
| 893 | } |
| 894 | break; |
| 895 | } |
| 896 | case 'S': |
| 897 | case 'R': |
| 898 | { |
| 899 | Py_UNICODE *ucopy; |
| 900 | Py_ssize_t usize; |
| 901 | Py_ssize_t upos; |
| 902 | /* unused, since we already have the result */ |
| 903 | (void) va_arg(vargs, PyObject *); |
| 904 | ucopy = PyUnicode_AS_UNICODE(*callresult); |
| 905 | usize = PyUnicode_GET_SIZE(*callresult); |
| 906 | for (upos = 0; upos<usize;) |
| 907 | *s++ = ucopy[upos++]; |
| 908 | /* We're done with the unicode()/repr() => forget it */ |
| 909 | Py_DECREF(*callresult); |
| 910 | /* switch to next unicode()/repr() result */ |
| 911 | ++callresult; |
| 912 | break; |
| 913 | } |
| 914 | case 'p': |
| 915 | sprintf(buffer, "%p", va_arg(vargs, void*)); |
| 916 | /* %p is ill-defined: ensure leading 0x. */ |
| 917 | if (buffer[1] == 'X') |
| 918 | buffer[1] = 'x'; |
| 919 | else if (buffer[1] != 'x') { |
| 920 | memmove(buffer+2, buffer, strlen(buffer)+1); |
| 921 | buffer[0] = '0'; |
| 922 | buffer[1] = 'x'; |
| 923 | } |
| 924 | appendstring(buffer); |
| 925 | break; |
| 926 | case '%': |
| 927 | *s++ = '%'; |
| 928 | break; |
| 929 | default: |
| 930 | appendstring(p); |
| 931 | goto end; |
| 932 | } |
| 933 | } else |
| 934 | *s++ = *f; |
| 935 | } |
| 936 | |
| 937 | end: |
| 938 | if (callresults) |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 939 | PyObject_Free(callresults); |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 940 | if (abuffer) |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 941 | PyObject_Free(abuffer); |
Alexandre Vassalotti | 034e08c | 2008-12-27 06:36:10 +0000 | [diff] [blame] | 942 | PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string)); |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 943 | return string; |
| 944 | fail: |
| 945 | if (callresults) { |
| 946 | PyObject **callresult2 = callresults; |
| 947 | while (callresult2 < callresult) { |
| 948 | Py_DECREF(*callresult2); |
| 949 | ++callresult2; |
| 950 | } |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 951 | PyObject_Free(callresults); |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 952 | } |
| 953 | if (abuffer) |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 954 | PyObject_Free(abuffer); |
Christian Heimes | 7f39c9f | 2008-01-25 12:18:43 +0000 | [diff] [blame] | 955 | return NULL; |
| 956 | } |
| 957 | |
| 958 | #undef appendstring |
| 959 | |
| 960 | PyObject * |
| 961 | PyUnicode_FromFormat(const char *format, ...) |
| 962 | { |
| 963 | PyObject* ret; |
| 964 | va_list vargs; |
| 965 | |
| 966 | #ifdef HAVE_STDARG_PROTOTYPES |
| 967 | va_start(vargs, format); |
| 968 | #else |
| 969 | va_start(vargs); |
| 970 | #endif |
| 971 | ret = PyUnicode_FromFormatV(format, vargs); |
| 972 | va_end(vargs); |
| 973 | return ret; |
| 974 | } |
| 975 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 976 | Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
| 977 | wchar_t *w, |
| 978 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 979 | { |
| 980 | if (unicode == NULL) { |
| 981 | PyErr_BadInternalCall(); |
| 982 | return -1; |
| 983 | } |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 984 | |
| 985 | /* If possible, try to copy the 0-termination as well */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 986 | if (size > PyUnicode_GET_SIZE(unicode)) |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 987 | size = PyUnicode_GET_SIZE(unicode) + 1; |
| 988 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 989 | #ifdef HAVE_USABLE_WCHAR_T |
| 990 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 991 | #else |
| 992 | { |
| 993 | register Py_UNICODE *u; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 994 | register Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 995 | u = PyUnicode_AS_UNICODE(unicode); |
Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 996 | for (i = size; i > 0; i--) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 997 | *w++ = *u++; |
| 998 | } |
| 999 | #endif |
| 1000 | |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1001 | if (size > PyUnicode_GET_SIZE(unicode)) |
| 1002 | return PyUnicode_GET_SIZE(unicode); |
| 1003 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1004 | return size; |
| 1005 | } |
| 1006 | |
| 1007 | #endif |
| 1008 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1009 | PyObject *PyUnicode_FromOrdinal(int ordinal) |
| 1010 | { |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 1011 | Py_UNICODE s[1]; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1012 | |
| 1013 | #ifdef Py_UNICODE_WIDE |
| 1014 | if (ordinal < 0 || ordinal > 0x10ffff) { |
| 1015 | PyErr_SetString(PyExc_ValueError, |
| 1016 | "unichr() arg not in range(0x110000) " |
| 1017 | "(wide Python build)"); |
| 1018 | return NULL; |
| 1019 | } |
| 1020 | #else |
| 1021 | if (ordinal < 0 || ordinal > 0xffff) { |
| 1022 | PyErr_SetString(PyExc_ValueError, |
| 1023 | "unichr() arg not in range(0x10000) " |
| 1024 | "(narrow Python build)"); |
| 1025 | return NULL; |
| 1026 | } |
| 1027 | #endif |
| 1028 | |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 1029 | s[0] = (Py_UNICODE)ordinal; |
| 1030 | return PyUnicode_FromUnicode(s, 1); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1033 | PyObject *PyUnicode_FromObject(register PyObject *obj) |
| 1034 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1035 | /* XXX Perhaps we should make this API an alias of |
| 1036 | PyObject_Unicode() instead ?! */ |
| 1037 | if (PyUnicode_CheckExact(obj)) { |
| 1038 | Py_INCREF(obj); |
| 1039 | return obj; |
| 1040 | } |
| 1041 | if (PyUnicode_Check(obj)) { |
| 1042 | /* For a Unicode subtype that's not a Unicode object, |
| 1043 | return a true Unicode object with the same data. */ |
| 1044 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 1045 | PyUnicode_GET_SIZE(obj)); |
| 1046 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1047 | return PyUnicode_FromEncodedObject(obj, NULL, "strict"); |
| 1048 | } |
| 1049 | |
| 1050 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
| 1051 | const char *encoding, |
| 1052 | const char *errors) |
| 1053 | { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1054 | const char *s = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1055 | Py_ssize_t len; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1056 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1057 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1058 | if (obj == NULL) { |
| 1059 | PyErr_BadInternalCall(); |
| 1060 | return NULL; |
| 1061 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1062 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1063 | #if 0 |
| 1064 | /* For b/w compatibility we also accept Unicode objects provided |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 1065 | that no encodings is given and then redirect to |
| 1066 | PyObject_Unicode() which then applies the additional logic for |
| 1067 | Unicode subclasses. |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1068 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1069 | NOTE: This API should really only be used for object which |
| 1070 | represent *encoded* Unicode ! |
| 1071 | |
| 1072 | */ |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1073 | if (PyUnicode_Check(obj)) { |
| 1074 | if (encoding) { |
| 1075 | PyErr_SetString(PyExc_TypeError, |
| 1076 | "decoding Unicode is not supported"); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1077 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1078 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1079 | return PyObject_Unicode(obj); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1080 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1081 | #else |
| 1082 | if (PyUnicode_Check(obj)) { |
| 1083 | PyErr_SetString(PyExc_TypeError, |
| 1084 | "decoding Unicode is not supported"); |
| 1085 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1086 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1087 | #endif |
| 1088 | |
| 1089 | /* Coerce object */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1090 | if (PyString_Check(obj)) { |
| 1091 | s = PyString_AS_STRING(obj); |
| 1092 | len = PyString_GET_SIZE(obj); |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 1093 | } |
Christian Heimes | 3497f94 | 2008-05-26 12:29:14 +0000 | [diff] [blame] | 1094 | else if (PyByteArray_Check(obj)) { |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 1095 | /* Python 2.x specific */ |
| 1096 | PyErr_Format(PyExc_TypeError, |
| 1097 | "decoding bytearray is not supported"); |
| 1098 | return NULL; |
| 1099 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1100 | else if (PyObject_AsCharBuffer(obj, &s, &len)) { |
| 1101 | /* Overwrite the error message with something more useful in |
| 1102 | case of a TypeError. */ |
| 1103 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1104 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1105 | "coercing to Unicode: need string or buffer, " |
| 1106 | "%.80s found", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1107 | Py_TYPE(obj)->tp_name); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1108 | goto onError; |
| 1109 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1110 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1111 | /* Convert to Unicode */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1112 | if (len == 0) { |
| 1113 | Py_INCREF(unicode_empty); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1114 | v = (PyObject *)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1115 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1116 | else |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1117 | v = PyUnicode_Decode(s, len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 1118 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1119 | return v; |
| 1120 | |
| 1121 | onError: |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1122 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | PyObject *PyUnicode_Decode(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1126 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1127 | const char *encoding, |
| 1128 | const char *errors) |
| 1129 | { |
| 1130 | PyObject *buffer = NULL, *unicode; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1131 | |
| 1132 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1133 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1134 | |
| 1135 | /* Shortcuts for common default encodings */ |
| 1136 | if (strcmp(encoding, "utf-8") == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1137 | return PyUnicode_DecodeUTF8(s, size, errors); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1138 | else if (strcmp(encoding, "latin-1") == 0) |
| 1139 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1140 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 1141 | else if (strcmp(encoding, "mbcs") == 0) |
| 1142 | return PyUnicode_DecodeMBCS(s, size, errors); |
| 1143 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1144 | else if (strcmp(encoding, "ascii") == 0) |
| 1145 | return PyUnicode_DecodeASCII(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1146 | |
| 1147 | /* Decode via the codec registry */ |
| 1148 | buffer = PyBuffer_FromMemory((void *)s, size); |
| 1149 | if (buffer == NULL) |
| 1150 | goto onError; |
| 1151 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 1152 | if (unicode == NULL) |
| 1153 | goto onError; |
| 1154 | if (!PyUnicode_Check(unicode)) { |
| 1155 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 1156 | "decoder did not return an unicode object (type=%.400s)", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1157 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1158 | Py_DECREF(unicode); |
| 1159 | goto onError; |
| 1160 | } |
| 1161 | Py_DECREF(buffer); |
| 1162 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1163 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1164 | onError: |
| 1165 | Py_XDECREF(buffer); |
| 1166 | return NULL; |
| 1167 | } |
| 1168 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1169 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 1170 | const char *encoding, |
| 1171 | const char *errors) |
| 1172 | { |
| 1173 | PyObject *v; |
| 1174 | |
| 1175 | if (!PyUnicode_Check(unicode)) { |
| 1176 | PyErr_BadArgument(); |
| 1177 | goto onError; |
| 1178 | } |
| 1179 | |
| 1180 | if (encoding == NULL) |
| 1181 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1182 | |
| 1183 | /* Decode via the codec registry */ |
| 1184 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1185 | if (v == NULL) |
| 1186 | goto onError; |
| 1187 | return v; |
| 1188 | |
| 1189 | onError: |
| 1190 | return NULL; |
| 1191 | } |
| 1192 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1193 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1194 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1195 | const char *encoding, |
| 1196 | const char *errors) |
| 1197 | { |
| 1198 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1199 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1200 | unicode = PyUnicode_FromUnicode(s, size); |
| 1201 | if (unicode == NULL) |
| 1202 | return NULL; |
| 1203 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 1204 | Py_DECREF(unicode); |
| 1205 | return v; |
| 1206 | } |
| 1207 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1208 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 1209 | const char *encoding, |
| 1210 | const char *errors) |
| 1211 | { |
| 1212 | PyObject *v; |
| 1213 | |
| 1214 | if (!PyUnicode_Check(unicode)) { |
| 1215 | PyErr_BadArgument(); |
| 1216 | goto onError; |
| 1217 | } |
| 1218 | |
| 1219 | if (encoding == NULL) |
| 1220 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1221 | |
| 1222 | /* Encode via the codec registry */ |
| 1223 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1224 | if (v == NULL) |
| 1225 | goto onError; |
| 1226 | return v; |
| 1227 | |
| 1228 | onError: |
| 1229 | return NULL; |
| 1230 | } |
| 1231 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1232 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 1233 | const char *encoding, |
| 1234 | const char *errors) |
| 1235 | { |
| 1236 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1237 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1238 | if (!PyUnicode_Check(unicode)) { |
| 1239 | PyErr_BadArgument(); |
| 1240 | goto onError; |
| 1241 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1242 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1243 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1244 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1245 | |
| 1246 | /* Shortcuts for common default encodings */ |
| 1247 | if (errors == NULL) { |
| 1248 | if (strcmp(encoding, "utf-8") == 0) |
Jeremy Hylton | 9cea41c | 2001-05-29 17:13:15 +0000 | [diff] [blame] | 1249 | return PyUnicode_AsUTF8String(unicode); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1250 | else if (strcmp(encoding, "latin-1") == 0) |
| 1251 | return PyUnicode_AsLatin1String(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1252 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 1253 | else if (strcmp(encoding, "mbcs") == 0) |
| 1254 | return PyUnicode_AsMBCSString(unicode); |
| 1255 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1256 | else if (strcmp(encoding, "ascii") == 0) |
| 1257 | return PyUnicode_AsASCIIString(unicode); |
| 1258 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1259 | |
| 1260 | /* Encode via the codec registry */ |
| 1261 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1262 | if (v == NULL) |
| 1263 | goto onError; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1264 | if (!PyString_Check(v)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1265 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 1266 | "encoder did not return a string object (type=%.400s)", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1267 | Py_TYPE(v)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1268 | Py_DECREF(v); |
| 1269 | goto onError; |
| 1270 | } |
| 1271 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1272 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1273 | onError: |
| 1274 | return NULL; |
| 1275 | } |
| 1276 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1277 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
| 1278 | const char *errors) |
| 1279 | { |
| 1280 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
| 1281 | |
| 1282 | if (v) |
| 1283 | return v; |
| 1284 | v = PyUnicode_AsEncodedString(unicode, NULL, errors); |
| 1285 | if (v && errors == NULL) |
| 1286 | ((PyUnicodeObject *)unicode)->defenc = v; |
| 1287 | return v; |
| 1288 | } |
| 1289 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1290 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 1291 | { |
| 1292 | if (!PyUnicode_Check(unicode)) { |
| 1293 | PyErr_BadArgument(); |
| 1294 | goto onError; |
| 1295 | } |
| 1296 | return PyUnicode_AS_UNICODE(unicode); |
| 1297 | |
| 1298 | onError: |
| 1299 | return NULL; |
| 1300 | } |
| 1301 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1302 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1303 | { |
| 1304 | if (!PyUnicode_Check(unicode)) { |
| 1305 | PyErr_BadArgument(); |
| 1306 | goto onError; |
| 1307 | } |
| 1308 | return PyUnicode_GET_SIZE(unicode); |
| 1309 | |
| 1310 | onError: |
| 1311 | return -1; |
| 1312 | } |
| 1313 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 1314 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1315 | { |
| 1316 | return unicode_default_encoding; |
| 1317 | } |
| 1318 | |
| 1319 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 1320 | { |
| 1321 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1322 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1323 | /* Make sure the encoding is valid. As side effect, this also |
| 1324 | loads the encoding into the codec registry cache. */ |
| 1325 | v = _PyCodec_Lookup(encoding); |
| 1326 | if (v == NULL) |
| 1327 | goto onError; |
| 1328 | Py_DECREF(v); |
| 1329 | strncpy(unicode_default_encoding, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1330 | encoding, |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1331 | sizeof(unicode_default_encoding)); |
| 1332 | return 0; |
| 1333 | |
| 1334 | onError: |
| 1335 | return -1; |
| 1336 | } |
| 1337 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1338 | /* error handling callback helper: |
| 1339 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 1340 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1341 | and adjust various state variables. |
| 1342 | return 0 on success, -1 on error |
| 1343 | */ |
| 1344 | |
| 1345 | static |
| 1346 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
| 1347 | const char *encoding, const char *reason, |
Walter Dörwald | 8757878 | 2007-08-30 15:30:09 +0000 | [diff] [blame] | 1348 | const char *input, Py_ssize_t insize, Py_ssize_t *startinpos, |
| 1349 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
Alexandre Vassalotti | 034e08c | 2008-12-27 06:36:10 +0000 | [diff] [blame] | 1350 | PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1351 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1352 | 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] | 1353 | |
| 1354 | PyObject *restuple = NULL; |
| 1355 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1356 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
| 1357 | Py_ssize_t requiredsize; |
| 1358 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1359 | Py_UNICODE *repptr; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1360 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1361 | int res = -1; |
| 1362 | |
| 1363 | if (*errorHandler == NULL) { |
| 1364 | *errorHandler = PyCodec_LookupError(errors); |
| 1365 | if (*errorHandler == NULL) |
| 1366 | goto onError; |
| 1367 | } |
| 1368 | |
| 1369 | if (*exceptionObject == NULL) { |
| 1370 | *exceptionObject = PyUnicodeDecodeError_Create( |
| 1371 | encoding, input, insize, *startinpos, *endinpos, reason); |
| 1372 | if (*exceptionObject == NULL) |
| 1373 | goto onError; |
| 1374 | } |
| 1375 | else { |
| 1376 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 1377 | goto onError; |
| 1378 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
| 1379 | goto onError; |
| 1380 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 1381 | goto onError; |
| 1382 | } |
| 1383 | |
| 1384 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 1385 | if (restuple == NULL) |
| 1386 | goto onError; |
| 1387 | if (!PyTuple_Check(restuple)) { |
| 1388 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 1389 | goto onError; |
| 1390 | } |
| 1391 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
| 1392 | goto onError; |
| 1393 | if (newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1394 | newpos = insize+newpos; |
| 1395 | if (newpos<0 || newpos>insize) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 1396 | 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] | 1397 | goto onError; |
| 1398 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1399 | |
| 1400 | /* need more space? (at least enough for what we |
| 1401 | have+the replacement+the rest of the string (starting |
| 1402 | at the new input position), so we won't have to check space |
| 1403 | when there are no errors in the rest of the string) */ |
| 1404 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 1405 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 1406 | requiredsize = *outpos + repsize + insize-newpos; |
| 1407 | if (requiredsize > outsize) { |
| 1408 | if (requiredsize<2*outsize) |
| 1409 | requiredsize = 2*outsize; |
Alexandre Vassalotti | 034e08c | 2008-12-27 06:36:10 +0000 | [diff] [blame] | 1410 | if (_PyUnicode_Resize(output, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1411 | goto onError; |
| 1412 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
| 1413 | } |
| 1414 | *endinpos = newpos; |
| 1415 | *inptr = input + newpos; |
| 1416 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 1417 | *outptr += repsize; |
| 1418 | *outpos += repsize; |
| 1419 | /* we made it! */ |
| 1420 | res = 0; |
| 1421 | |
| 1422 | onError: |
| 1423 | Py_XDECREF(restuple); |
| 1424 | return res; |
| 1425 | } |
| 1426 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1427 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 1428 | |
| 1429 | /* see RFC2152 for details */ |
| 1430 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1431 | static |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1432 | char utf7_special[128] = { |
| 1433 | /* indicate whether a UTF-7 character is special i.e. cannot be directly |
| 1434 | encoded: |
| 1435 | 0 - not special |
| 1436 | 1 - special |
| 1437 | 2 - whitespace (optional) |
| 1438 | 3 - RFC2152 Set O (optional) */ |
| 1439 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, |
| 1440 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1441 | 2, 3, 3, 3, 3, 3, 3, 0, 0, 0, 3, 1, 0, 0, 0, 1, |
| 1442 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, |
| 1443 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1444 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, |
| 1445 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1446 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 1, 1, |
| 1447 | |
| 1448 | }; |
| 1449 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1450 | /* Note: The comparison (c) <= 0 is a trick to work-around gcc |
| 1451 | warnings about the comparison always being false; since |
| 1452 | utf7_special[0] is 1, we can safely make that one comparison |
| 1453 | true */ |
| 1454 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1455 | #define SPECIAL(c, encodeO, encodeWS) \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1456 | ((c) > 127 || (c) <= 0 || utf7_special[(c)] == 1 || \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1457 | (encodeWS && (utf7_special[(c)] == 2)) || \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1458 | (encodeO && (utf7_special[(c)] == 3))) |
| 1459 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1460 | #define B64(n) \ |
| 1461 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 1462 | #define B64CHAR(c) \ |
| 1463 | (isalnum(c) || (c) == '+' || (c) == '/') |
| 1464 | #define UB64(c) \ |
| 1465 | ((c) == '+' ? 62 : (c) == '/' ? 63 : (c) >= 'a' ? \ |
| 1466 | (c) - 71 : (c) >= 'A' ? (c) - 65 : (c) + 4 ) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1467 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1468 | #define ENCODE(out, ch, bits) \ |
| 1469 | while (bits >= 6) { \ |
| 1470 | *out++ = B64(ch >> (bits-6)); \ |
| 1471 | bits -= 6; \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1474 | #define DECODE(out, ch, bits, surrogate) \ |
| 1475 | while (bits >= 16) { \ |
| 1476 | Py_UNICODE outCh = (Py_UNICODE) ((ch >> (bits-16)) & 0xffff); \ |
| 1477 | bits -= 16; \ |
| 1478 | if (surrogate) { \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1479 | /* We have already generated an error for the high surrogate \ |
| 1480 | 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] | 1481 | surrogate = 0; \ |
| 1482 | } else if (0xDC00 <= outCh && outCh <= 0xDFFF) { \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1483 | /* This is a surrogate pair. Unfortunately we can't represent \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1484 | it in a 16-bit character */ \ |
| 1485 | surrogate = 1; \ |
| 1486 | errmsg = "code pairs are not supported"; \ |
| 1487 | goto utf7Error; \ |
| 1488 | } else { \ |
| 1489 | *out++ = outCh; \ |
| 1490 | } \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1491 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1492 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1493 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1494 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1495 | const char *errors) |
| 1496 | { |
Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1497 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 1498 | } |
| 1499 | |
| 1500 | PyObject *PyUnicode_DecodeUTF7Stateful(const char *s, |
| 1501 | Py_ssize_t size, |
| 1502 | const char *errors, |
| 1503 | Py_ssize_t *consumed) |
| 1504 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1505 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1506 | Py_ssize_t startinpos; |
| 1507 | Py_ssize_t endinpos; |
| 1508 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1509 | const char *e; |
| 1510 | PyUnicodeObject *unicode; |
| 1511 | Py_UNICODE *p; |
| 1512 | const char *errmsg = ""; |
| 1513 | int inShift = 0; |
| 1514 | unsigned int bitsleft = 0; |
| 1515 | unsigned long charsleft = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1516 | int surrogate = 0; |
| 1517 | PyObject *errorHandler = NULL; |
| 1518 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1519 | |
| 1520 | unicode = _PyUnicode_New(size); |
| 1521 | if (!unicode) |
| 1522 | return NULL; |
Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1523 | if (size == 0) { |
| 1524 | if (consumed) |
| 1525 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1526 | return (PyObject *)unicode; |
Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1527 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1528 | |
| 1529 | p = unicode->str; |
| 1530 | e = s + size; |
| 1531 | |
| 1532 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1533 | Py_UNICODE ch; |
| 1534 | restart: |
Antoine Pitrou | 4982d5d | 2008-07-25 17:45:59 +0000 | [diff] [blame] | 1535 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1536 | |
| 1537 | if (inShift) { |
| 1538 | if ((ch == '-') || !B64CHAR(ch)) { |
| 1539 | inShift = 0; |
| 1540 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1541 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1542 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1543 | if (bitsleft >= 6) { |
| 1544 | /* The shift sequence has a partial character in it. If |
| 1545 | bitsleft < 6 then we could just classify it as padding |
| 1546 | but that is not the case here */ |
| 1547 | |
| 1548 | errmsg = "partial character in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1549 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1550 | } |
| 1551 | /* According to RFC2152 the remaining bits should be zero. We |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1552 | choose to signal an error/insert a replacement character |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1553 | here so indicate the potential of a misencoded character. */ |
| 1554 | |
| 1555 | /* On x86, a << b == a << (b%32) so make sure that bitsleft != 0 */ |
| 1556 | if (bitsleft && charsleft << (sizeof(charsleft) * 8 - bitsleft)) { |
| 1557 | errmsg = "non-zero padding bits in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1558 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1559 | } |
| 1560 | |
| 1561 | if (ch == '-') { |
| 1562 | if ((s < e) && (*(s) == '-')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1563 | *p++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1564 | inShift = 1; |
| 1565 | } |
| 1566 | } else if (SPECIAL(ch,0,0)) { |
| 1567 | errmsg = "unexpected special character"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1568 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1569 | } else { |
| 1570 | *p++ = ch; |
| 1571 | } |
| 1572 | } else { |
| 1573 | charsleft = (charsleft << 6) | UB64(ch); |
| 1574 | bitsleft += 6; |
| 1575 | s++; |
| 1576 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1577 | } |
| 1578 | } |
| 1579 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1580 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1581 | s++; |
| 1582 | if (s < e && *s == '-') { |
| 1583 | s++; |
| 1584 | *p++ = '+'; |
| 1585 | } else |
| 1586 | { |
| 1587 | inShift = 1; |
| 1588 | bitsleft = 0; |
| 1589 | } |
| 1590 | } |
| 1591 | else if (SPECIAL(ch,0,0)) { |
Walter Dörwald | 9d04542 | 2007-08-30 15:34:55 +0000 | [diff] [blame] | 1592 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1593 | errmsg = "unexpected special character"; |
| 1594 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1595 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1596 | } |
| 1597 | else { |
| 1598 | *p++ = ch; |
| 1599 | s++; |
| 1600 | } |
| 1601 | continue; |
| 1602 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1603 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1604 | endinpos = s-starts; |
| 1605 | if (unicode_decode_call_errorhandler( |
| 1606 | errors, &errorHandler, |
| 1607 | "utf7", errmsg, |
| 1608 | starts, size, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | 034e08c | 2008-12-27 06:36:10 +0000 | [diff] [blame] | 1609 | &unicode, &outpos, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1610 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1613 | if (inShift && !consumed) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1614 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1615 | endinpos = size; |
| 1616 | if (unicode_decode_call_errorhandler( |
| 1617 | errors, &errorHandler, |
| 1618 | "utf7", "unterminated shift sequence", |
| 1619 | starts, size, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | 034e08c | 2008-12-27 06:36:10 +0000 | [diff] [blame] | 1620 | &unicode, &outpos, &p)) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1621 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1622 | if (s < e) |
| 1623 | goto restart; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1624 | } |
Amaury Forgeot d'Arc | 5087980 | 2007-11-20 23:31:27 +0000 | [diff] [blame] | 1625 | if (consumed) { |
| 1626 | if(inShift) |
| 1627 | *consumed = startinpos; |
| 1628 | else |
| 1629 | *consumed = s-starts; |
| 1630 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1631 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1632 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1633 | goto onError; |
| 1634 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1635 | Py_XDECREF(errorHandler); |
| 1636 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1637 | return (PyObject *)unicode; |
| 1638 | |
| 1639 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1640 | Py_XDECREF(errorHandler); |
| 1641 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1642 | Py_DECREF(unicode); |
| 1643 | return NULL; |
| 1644 | } |
| 1645 | |
| 1646 | |
| 1647 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1648 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1649 | int encodeSetO, |
| 1650 | int encodeWhiteSpace, |
| 1651 | const char *errors) |
| 1652 | { |
| 1653 | PyObject *v; |
| 1654 | /* It might be possible to tighten this worst case */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1655 | Py_ssize_t cbAllocated = 5 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1656 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1657 | Py_ssize_t i = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1658 | unsigned int bitsleft = 0; |
| 1659 | unsigned long charsleft = 0; |
| 1660 | char * out; |
| 1661 | char * start; |
| 1662 | |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 1663 | if (cbAllocated / 5 != size) |
| 1664 | return PyErr_NoMemory(); |
| 1665 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1666 | if (size == 0) |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1667 | return PyString_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1668 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1669 | v = PyString_FromStringAndSize(NULL, cbAllocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1670 | if (v == NULL) |
| 1671 | return NULL; |
| 1672 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1673 | start = out = PyString_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1674 | for (;i < size; ++i) { |
| 1675 | Py_UNICODE ch = s[i]; |
| 1676 | |
| 1677 | if (!inShift) { |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1678 | if (ch == '+') { |
| 1679 | *out++ = '+'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1680 | *out++ = '-'; |
| 1681 | } else if (SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1682 | charsleft = ch; |
| 1683 | bitsleft = 16; |
| 1684 | *out++ = '+'; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1685 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1686 | inShift = bitsleft > 0; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1687 | } else { |
| 1688 | *out++ = (char) ch; |
| 1689 | } |
| 1690 | } else { |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1691 | if (!SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1692 | *out++ = B64(charsleft << (6-bitsleft)); |
| 1693 | charsleft = 0; |
| 1694 | bitsleft = 0; |
| 1695 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 1696 | so no '-' is required, except if the character is itself a '-' */ |
| 1697 | if (B64CHAR(ch) || ch == '-') { |
| 1698 | *out++ = '-'; |
| 1699 | } |
| 1700 | inShift = 0; |
| 1701 | *out++ = (char) ch; |
| 1702 | } else { |
| 1703 | bitsleft += 16; |
| 1704 | charsleft = (charsleft << 16) | ch; |
| 1705 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
| 1706 | |
| 1707 | /* If the next character is special then we dont' need to terminate |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1708 | 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] | 1709 | or '-' then the shift sequence will be terminated implicitly and we |
| 1710 | don't have to insert a '-'. */ |
| 1711 | |
| 1712 | if (bitsleft == 0) { |
| 1713 | if (i + 1 < size) { |
| 1714 | Py_UNICODE ch2 = s[i+1]; |
| 1715 | |
| 1716 | if (SPECIAL(ch2, encodeSetO, encodeWhiteSpace)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1717 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1718 | } else if (B64CHAR(ch2) || ch2 == '-') { |
| 1719 | *out++ = '-'; |
| 1720 | inShift = 0; |
| 1721 | } else { |
| 1722 | inShift = 0; |
| 1723 | } |
| 1724 | |
| 1725 | } |
| 1726 | else { |
| 1727 | *out++ = '-'; |
| 1728 | inShift = 0; |
| 1729 | } |
| 1730 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1731 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1732 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1733 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1734 | if (bitsleft) { |
| 1735 | *out++= B64(charsleft << (6-bitsleft) ); |
| 1736 | *out++ = '-'; |
| 1737 | } |
| 1738 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1739 | _PyString_Resize(&v, out - start); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1740 | return v; |
| 1741 | } |
| 1742 | |
| 1743 | #undef SPECIAL |
| 1744 | #undef B64 |
| 1745 | #undef B64CHAR |
| 1746 | #undef UB64 |
| 1747 | #undef ENCODE |
| 1748 | #undef DECODE |
| 1749 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1750 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 1751 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1752 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1753 | char utf8_code_length[256] = { |
| 1754 | /* Map UTF-8 encoded prefix byte to sequence length. zero means |
| 1755 | illegal prefix. see RFC 2279 for details */ |
| 1756 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1757 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1758 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1759 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1760 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1761 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1762 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1763 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1764 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1765 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1766 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1767 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1768 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1769 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1770 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 1771 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 |
| 1772 | }; |
| 1773 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1774 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1775 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1776 | const char *errors) |
| 1777 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1778 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 1779 | } |
| 1780 | |
| 1781 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1782 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1783 | const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1784 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1785 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1786 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1787 | int n; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1788 | Py_ssize_t startinpos; |
| 1789 | Py_ssize_t endinpos; |
| 1790 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1791 | const char *e; |
| 1792 | PyUnicodeObject *unicode; |
| 1793 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1794 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1795 | PyObject *errorHandler = NULL; |
| 1796 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1797 | |
| 1798 | /* Note: size will always be longer than the resulting Unicode |
| 1799 | character count */ |
| 1800 | unicode = _PyUnicode_New(size); |
| 1801 | if (!unicode) |
| 1802 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1803 | if (size == 0) { |
| 1804 | if (consumed) |
| 1805 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1806 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1807 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1808 | |
| 1809 | /* Unpack UTF-8 encoded data */ |
| 1810 | p = unicode->str; |
| 1811 | e = s + size; |
| 1812 | |
| 1813 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1814 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1815 | |
| 1816 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1817 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1818 | s++; |
| 1819 | continue; |
| 1820 | } |
| 1821 | |
| 1822 | n = utf8_code_length[ch]; |
| 1823 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1824 | if (s + n > e) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1825 | if (consumed) |
| 1826 | break; |
| 1827 | else { |
| 1828 | errmsg = "unexpected end of data"; |
| 1829 | startinpos = s-starts; |
| 1830 | endinpos = size; |
| 1831 | goto utf8Error; |
| 1832 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1833 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1834 | |
| 1835 | switch (n) { |
| 1836 | |
| 1837 | case 0: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1838 | errmsg = "unexpected code byte"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1839 | startinpos = s-starts; |
| 1840 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1841 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1842 | |
| 1843 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1844 | errmsg = "internal error"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1845 | startinpos = s-starts; |
| 1846 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1847 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1848 | |
| 1849 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1850 | if ((s[1] & 0xc0) != 0x80) { |
| 1851 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1852 | startinpos = s-starts; |
| 1853 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1854 | goto utf8Error; |
| 1855 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1856 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1857 | if (ch < 0x80) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1858 | startinpos = s-starts; |
| 1859 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1860 | errmsg = "illegal encoding"; |
| 1861 | goto utf8Error; |
| 1862 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1863 | else |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1864 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1865 | break; |
| 1866 | |
| 1867 | case 3: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1868 | if ((s[1] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1869 | (s[2] & 0xc0) != 0x80) { |
| 1870 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1871 | startinpos = s-starts; |
| 1872 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1873 | goto utf8Error; |
| 1874 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1875 | 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] | 1876 | if (ch < 0x0800) { |
| 1877 | /* Note: UTF-8 encodings of surrogates are considered |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1878 | legal UTF-8 sequences; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1879 | |
| 1880 | XXX For wide builds (UCS-4) we should probably try |
| 1881 | to recombine the surrogates into a single code |
| 1882 | unit. |
| 1883 | */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1884 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1885 | startinpos = s-starts; |
| 1886 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1887 | goto utf8Error; |
| 1888 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1889 | else |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1890 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1891 | break; |
| 1892 | |
| 1893 | case 4: |
| 1894 | if ((s[1] & 0xc0) != 0x80 || |
| 1895 | (s[2] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1896 | (s[3] & 0xc0) != 0x80) { |
| 1897 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1898 | startinpos = s-starts; |
| 1899 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1900 | goto utf8Error; |
| 1901 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1902 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 1903 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 1904 | /* validate and convert to UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1905 | if ((ch < 0x10000) /* minimum value allowed for 4 |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1906 | byte encoding */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1907 | || (ch > 0x10ffff)) /* maximum value allowed for |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1908 | UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1909 | { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1910 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1911 | startinpos = s-starts; |
| 1912 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1913 | goto utf8Error; |
| 1914 | } |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 1915 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1916 | *p++ = (Py_UNICODE)ch; |
| 1917 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1918 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1919 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1920 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 1921 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1922 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1923 | /* high surrogate = top 10 bits added to D800 */ |
| 1924 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1925 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 1926 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 1927 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 1928 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1929 | break; |
| 1930 | |
| 1931 | default: |
| 1932 | /* Other sizes are only needed for UCS-4 */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1933 | errmsg = "unsupported Unicode code range"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1934 | startinpos = s-starts; |
| 1935 | endinpos = startinpos+n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1936 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1937 | } |
| 1938 | s += n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1939 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1940 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 1941 | utf8Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1942 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1943 | if (unicode_decode_call_errorhandler( |
| 1944 | errors, &errorHandler, |
| 1945 | "utf8", errmsg, |
| 1946 | starts, size, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | 034e08c | 2008-12-27 06:36:10 +0000 | [diff] [blame] | 1947 | &unicode, &outpos, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1948 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1949 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1950 | if (consumed) |
| 1951 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1952 | |
| 1953 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1954 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1955 | goto onError; |
| 1956 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1957 | Py_XDECREF(errorHandler); |
| 1958 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1959 | return (PyObject *)unicode; |
| 1960 | |
| 1961 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1962 | Py_XDECREF(errorHandler); |
| 1963 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1964 | Py_DECREF(unicode); |
| 1965 | return NULL; |
| 1966 | } |
| 1967 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1968 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 1969 | and allocate exactly as much space needed at the end. Else allocate the |
| 1970 | maximum possible needed (4 result bytes per Unicode character), and return |
| 1971 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 1972 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1973 | PyObject * |
| 1974 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1975 | Py_ssize_t size, |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 1976 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1977 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1978 | #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] | 1979 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1980 | Py_ssize_t i; /* index into s of next input byte */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1981 | PyObject *v; /* result string object */ |
| 1982 | char *p; /* next free byte in output buffer */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1983 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 1984 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1985 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 1986 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1987 | assert(s != NULL); |
| 1988 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1989 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 1990 | if (size <= MAX_SHORT_UNICHARS) { |
| 1991 | /* Write into the stack buffer; nallocated can't overflow. |
| 1992 | * At the end, we'll allocate exactly as much heap space as it |
| 1993 | * turns out we need. |
| 1994 | */ |
| 1995 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
| 1996 | v = NULL; /* will allocate after we're done */ |
| 1997 | p = stackbuf; |
| 1998 | } |
| 1999 | else { |
| 2000 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 2001 | nallocated = size * 4; |
| 2002 | if (nallocated / 4 != size) /* overflow! */ |
| 2003 | return PyErr_NoMemory(); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2004 | v = PyString_FromStringAndSize(NULL, nallocated); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2005 | if (v == NULL) |
| 2006 | return NULL; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2007 | p = PyString_AS_STRING(v); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2008 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2009 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2010 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2011 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2012 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2013 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2014 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2015 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2016 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2017 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2018 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 2019 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 2020 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2021 | } |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2022 | else { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2023 | /* Encode UCS2 Unicode ordinals */ |
| 2024 | if (ch < 0x10000) { |
| 2025 | /* Special case: check for high surrogate */ |
| 2026 | if (0xD800 <= ch && ch <= 0xDBFF && i != size) { |
| 2027 | Py_UCS4 ch2 = s[i]; |
| 2028 | /* Check for low surrogate and combine the two to |
| 2029 | form a UCS4 value */ |
| 2030 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2031 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2032 | i++; |
| 2033 | goto encodeUCS4; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2034 | } |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2035 | /* Fall through: handles isolated high surrogates */ |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2036 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2037 | *p++ = (char)(0xe0 | (ch >> 12)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2038 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2039 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2040 | continue; |
| 2041 | } |
| 2042 | encodeUCS4: |
| 2043 | /* Encode UCS4 Unicode ordinals */ |
| 2044 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2045 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 2046 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2047 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2048 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2049 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 2050 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2051 | if (v == NULL) { |
| 2052 | /* This was stack allocated. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2053 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2054 | assert(nneeded <= nallocated); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2055 | v = PyString_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2056 | } |
| 2057 | else { |
| 2058 | /* Cut back to size actually needed. */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2059 | nneeded = p - PyString_AS_STRING(v); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2060 | assert(nneeded <= nallocated); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2061 | _PyString_Resize(&v, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2062 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2063 | return v; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2064 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2065 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2066 | } |
| 2067 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2068 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 2069 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2070 | if (!PyUnicode_Check(unicode)) { |
| 2071 | PyErr_BadArgument(); |
| 2072 | return NULL; |
| 2073 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 2074 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 2075 | PyUnicode_GET_SIZE(unicode), |
| 2076 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2077 | } |
| 2078 | |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2079 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 2080 | |
| 2081 | PyObject * |
| 2082 | PyUnicode_DecodeUTF32(const char *s, |
| 2083 | Py_ssize_t size, |
| 2084 | const char *errors, |
| 2085 | int *byteorder) |
| 2086 | { |
| 2087 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 2088 | } |
| 2089 | |
| 2090 | PyObject * |
| 2091 | PyUnicode_DecodeUTF32Stateful(const char *s, |
| 2092 | Py_ssize_t size, |
| 2093 | const char *errors, |
| 2094 | int *byteorder, |
| 2095 | Py_ssize_t *consumed) |
| 2096 | { |
| 2097 | const char *starts = s; |
| 2098 | Py_ssize_t startinpos; |
| 2099 | Py_ssize_t endinpos; |
| 2100 | Py_ssize_t outpos; |
| 2101 | PyUnicodeObject *unicode; |
| 2102 | Py_UNICODE *p; |
| 2103 | #ifndef Py_UNICODE_WIDE |
| 2104 | int i, pairs; |
| 2105 | #else |
| 2106 | const int pairs = 0; |
| 2107 | #endif |
| 2108 | const unsigned char *q, *e; |
| 2109 | int bo = 0; /* assume native ordering by default */ |
| 2110 | const char *errmsg = ""; |
Walter Dörwald | 20b40d3 | 2007-08-17 16:52:50 +0000 | [diff] [blame] | 2111 | /* Offsets from q for retrieving bytes in the right order. */ |
| 2112 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2113 | int iorder[] = {0, 1, 2, 3}; |
| 2114 | #else |
| 2115 | int iorder[] = {3, 2, 1, 0}; |
| 2116 | #endif |
Walter Dörwald | 9ab80a9 | 2007-08-17 16:58:43 +0000 | [diff] [blame] | 2117 | PyObject *errorHandler = NULL; |
| 2118 | PyObject *exc = NULL; |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2119 | /* On narrow builds we split characters outside the BMP into two |
| 2120 | codepoints => count how much extra space we need. */ |
| 2121 | #ifndef Py_UNICODE_WIDE |
| 2122 | for (i = pairs = 0; i < size/4; i++) |
| 2123 | if (((Py_UCS4 *)s)[i] >= 0x10000) |
| 2124 | pairs++; |
| 2125 | #endif |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2126 | |
| 2127 | /* This might be one to much, because of a BOM */ |
| 2128 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 2129 | if (!unicode) |
| 2130 | return NULL; |
| 2131 | if (size == 0) |
| 2132 | return (PyObject *)unicode; |
| 2133 | |
| 2134 | /* Unpack UTF-32 encoded data */ |
| 2135 | p = unicode->str; |
| 2136 | q = (unsigned char *)s; |
| 2137 | e = q + size; |
| 2138 | |
| 2139 | if (byteorder) |
| 2140 | bo = *byteorder; |
| 2141 | |
| 2142 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2143 | byte order setting accordingly. In native mode, the leading BOM |
| 2144 | mark is skipped, in all other modes, it is copied to the output |
| 2145 | stream as-is (giving a ZWNBSP character). */ |
| 2146 | if (bo == 0) { |
| 2147 | if (size >= 4) { |
| 2148 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2149 | (q[iorder[1]] << 8) | q[iorder[0]]; |
| 2150 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2151 | if (bom == 0x0000FEFF) { |
| 2152 | q += 4; |
| 2153 | bo = -1; |
| 2154 | } |
| 2155 | else if (bom == 0xFFFE0000) { |
| 2156 | q += 4; |
| 2157 | bo = 1; |
| 2158 | } |
| 2159 | #else |
| 2160 | if (bom == 0x0000FEFF) { |
| 2161 | q += 4; |
| 2162 | bo = 1; |
| 2163 | } |
| 2164 | else if (bom == 0xFFFE0000) { |
| 2165 | q += 4; |
| 2166 | bo = -1; |
| 2167 | } |
| 2168 | #endif |
| 2169 | } |
| 2170 | } |
| 2171 | |
| 2172 | if (bo == -1) { |
| 2173 | /* force LE */ |
| 2174 | iorder[0] = 0; |
| 2175 | iorder[1] = 1; |
| 2176 | iorder[2] = 2; |
| 2177 | iorder[3] = 3; |
| 2178 | } |
| 2179 | else if (bo == 1) { |
| 2180 | /* force BE */ |
| 2181 | iorder[0] = 3; |
| 2182 | iorder[1] = 2; |
| 2183 | iorder[2] = 1; |
| 2184 | iorder[3] = 0; |
| 2185 | } |
| 2186 | |
| 2187 | while (q < e) { |
| 2188 | Py_UCS4 ch; |
| 2189 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 2190 | if (e-q<4) { |
| 2191 | if (consumed) |
| 2192 | break; |
| 2193 | errmsg = "truncated data"; |
| 2194 | startinpos = ((const char *)q)-starts; |
| 2195 | endinpos = ((const char *)e)-starts; |
| 2196 | goto utf32Error; |
| 2197 | /* The remaining input chars are ignored if the callback |
| 2198 | chooses to skip the input */ |
| 2199 | } |
| 2200 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2201 | (q[iorder[1]] << 8) | q[iorder[0]]; |
| 2202 | |
| 2203 | if (ch >= 0x110000) |
| 2204 | { |
| 2205 | errmsg = "codepoint not in range(0x110000)"; |
| 2206 | startinpos = ((const char *)q)-starts; |
| 2207 | endinpos = startinpos+4; |
| 2208 | goto utf32Error; |
| 2209 | } |
| 2210 | #ifndef Py_UNICODE_WIDE |
| 2211 | if (ch >= 0x10000) |
| 2212 | { |
| 2213 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 2214 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2215 | } |
| 2216 | else |
| 2217 | #endif |
| 2218 | *p++ = ch; |
| 2219 | q += 4; |
| 2220 | continue; |
| 2221 | utf32Error: |
| 2222 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2223 | if (unicode_decode_call_errorhandler( |
| 2224 | errors, &errorHandler, |
| 2225 | "utf32", errmsg, |
| 2226 | starts, size, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | 034e08c | 2008-12-27 06:36:10 +0000 | [diff] [blame] | 2227 | &unicode, &outpos, &p)) |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2228 | goto onError; |
| 2229 | } |
| 2230 | |
| 2231 | if (byteorder) |
| 2232 | *byteorder = bo; |
| 2233 | |
| 2234 | if (consumed) |
| 2235 | *consumed = (const char *)q-starts; |
| 2236 | |
| 2237 | /* Adjust length */ |
| 2238 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
| 2239 | goto onError; |
| 2240 | |
| 2241 | Py_XDECREF(errorHandler); |
| 2242 | Py_XDECREF(exc); |
| 2243 | return (PyObject *)unicode; |
| 2244 | |
| 2245 | onError: |
| 2246 | Py_DECREF(unicode); |
| 2247 | Py_XDECREF(errorHandler); |
| 2248 | Py_XDECREF(exc); |
| 2249 | return NULL; |
| 2250 | } |
| 2251 | |
| 2252 | PyObject * |
| 2253 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 2254 | Py_ssize_t size, |
| 2255 | const char *errors, |
| 2256 | int byteorder) |
| 2257 | { |
| 2258 | PyObject *v; |
| 2259 | unsigned char *p; |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2260 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2261 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2262 | Py_ssize_t i, pairs; |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2263 | #else |
| 2264 | const int pairs = 0; |
| 2265 | #endif |
| 2266 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2267 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2268 | int iorder[] = {0, 1, 2, 3}; |
| 2269 | #else |
| 2270 | int iorder[] = {3, 2, 1, 0}; |
| 2271 | #endif |
| 2272 | |
| 2273 | #define STORECHAR(CH) \ |
| 2274 | do { \ |
| 2275 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 2276 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 2277 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 2278 | p[iorder[0]] = (CH) & 0xff; \ |
| 2279 | p += 4; \ |
| 2280 | } while(0) |
| 2281 | |
| 2282 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 2283 | so we need less space. */ |
| 2284 | #ifndef Py_UNICODE_WIDE |
| 2285 | for (i = pairs = 0; i < size-1; i++) |
| 2286 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 2287 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 2288 | pairs++; |
| 2289 | #endif |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2290 | nsize = (size - pairs + (byteorder == 0)); |
| 2291 | bytesize = nsize * 4; |
| 2292 | if (bytesize / 4 != nsize) |
| 2293 | return PyErr_NoMemory(); |
| 2294 | v = PyString_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2295 | if (v == NULL) |
| 2296 | return NULL; |
| 2297 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2298 | p = (unsigned char *)PyString_AS_STRING(v); |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 2299 | if (byteorder == 0) |
| 2300 | STORECHAR(0xFEFF); |
| 2301 | if (size == 0) |
| 2302 | return v; |
| 2303 | |
| 2304 | if (byteorder == -1) { |
| 2305 | /* force LE */ |
| 2306 | iorder[0] = 0; |
| 2307 | iorder[1] = 1; |
| 2308 | iorder[2] = 2; |
| 2309 | iorder[3] = 3; |
| 2310 | } |
| 2311 | else if (byteorder == 1) { |
| 2312 | /* force BE */ |
| 2313 | iorder[0] = 3; |
| 2314 | iorder[1] = 2; |
| 2315 | iorder[2] = 1; |
| 2316 | iorder[3] = 0; |
| 2317 | } |
| 2318 | |
| 2319 | while (size-- > 0) { |
| 2320 | Py_UCS4 ch = *s++; |
| 2321 | #ifndef Py_UNICODE_WIDE |
| 2322 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 2323 | Py_UCS4 ch2 = *s; |
| 2324 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 2325 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 2326 | s++; |
| 2327 | size--; |
| 2328 | } |
| 2329 | } |
| 2330 | #endif |
| 2331 | STORECHAR(ch); |
| 2332 | } |
| 2333 | return v; |
| 2334 | #undef STORECHAR |
| 2335 | } |
| 2336 | |
| 2337 | PyObject *PyUnicode_AsUTF32String(PyObject *unicode) |
| 2338 | { |
| 2339 | if (!PyUnicode_Check(unicode)) { |
| 2340 | PyErr_BadArgument(); |
| 2341 | return NULL; |
| 2342 | } |
| 2343 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
| 2344 | PyUnicode_GET_SIZE(unicode), |
| 2345 | NULL, |
| 2346 | 0); |
| 2347 | } |
| 2348 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2349 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 2350 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2351 | PyObject * |
| 2352 | PyUnicode_DecodeUTF16(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2353 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2354 | const char *errors, |
| 2355 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2356 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2357 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 2358 | } |
| 2359 | |
| 2360 | PyObject * |
| 2361 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2362 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2363 | const char *errors, |
| 2364 | int *byteorder, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2365 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2366 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2367 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2368 | Py_ssize_t startinpos; |
| 2369 | Py_ssize_t endinpos; |
| 2370 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2371 | PyUnicodeObject *unicode; |
| 2372 | Py_UNICODE *p; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2373 | const unsigned char *q, *e; |
| 2374 | int bo = 0; /* assume native ordering by default */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2375 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2376 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 2377 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2378 | int ihi = 1, ilo = 0; |
| 2379 | #else |
| 2380 | int ihi = 0, ilo = 1; |
| 2381 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2382 | PyObject *errorHandler = NULL; |
| 2383 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2384 | |
| 2385 | /* Note: size will always be longer than the resulting Unicode |
| 2386 | character count */ |
| 2387 | unicode = _PyUnicode_New(size); |
| 2388 | if (!unicode) |
| 2389 | return NULL; |
| 2390 | if (size == 0) |
| 2391 | return (PyObject *)unicode; |
| 2392 | |
| 2393 | /* Unpack UTF-16 encoded data */ |
| 2394 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2395 | q = (unsigned char *)s; |
| 2396 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2397 | |
| 2398 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2399 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2400 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2401 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2402 | byte order setting accordingly. In native mode, the leading BOM |
| 2403 | mark is skipped, in all other modes, it is copied to the output |
| 2404 | stream as-is (giving a ZWNBSP character). */ |
| 2405 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2406 | if (size >= 2) { |
| 2407 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2408 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2409 | if (bom == 0xFEFF) { |
| 2410 | q += 2; |
| 2411 | bo = -1; |
| 2412 | } |
| 2413 | else if (bom == 0xFFFE) { |
| 2414 | q += 2; |
| 2415 | bo = 1; |
| 2416 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2417 | #else |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2418 | if (bom == 0xFEFF) { |
| 2419 | q += 2; |
| 2420 | bo = 1; |
| 2421 | } |
| 2422 | else if (bom == 0xFFFE) { |
| 2423 | q += 2; |
| 2424 | bo = -1; |
| 2425 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2426 | #endif |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2427 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2428 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2429 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2430 | if (bo == -1) { |
| 2431 | /* force LE */ |
| 2432 | ihi = 1; |
| 2433 | ilo = 0; |
| 2434 | } |
| 2435 | else if (bo == 1) { |
| 2436 | /* force BE */ |
| 2437 | ihi = 0; |
| 2438 | ilo = 1; |
| 2439 | } |
| 2440 | |
| 2441 | while (q < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2442 | Py_UNICODE ch; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2443 | /* remaining bytes at the end? (size should be even) */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2444 | if (e-q<2) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2445 | if (consumed) |
| 2446 | break; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2447 | errmsg = "truncated data"; |
| 2448 | startinpos = ((const char *)q)-starts; |
| 2449 | endinpos = ((const char *)e)-starts; |
| 2450 | goto utf16Error; |
| 2451 | /* The remaining input chars are ignored if the callback |
| 2452 | chooses to skip the input */ |
| 2453 | } |
| 2454 | ch = (q[ihi] << 8) | q[ilo]; |
| 2455 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2456 | q += 2; |
| 2457 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2458 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 2459 | *p++ = ch; |
| 2460 | continue; |
| 2461 | } |
| 2462 | |
| 2463 | /* UTF-16 code pair: */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2464 | if (q >= e) { |
| 2465 | errmsg = "unexpected end of data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2466 | startinpos = (((const char *)q)-2)-starts; |
| 2467 | endinpos = ((const char *)e)-starts; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2468 | goto utf16Error; |
| 2469 | } |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 2470 | if (0xD800 <= ch && ch <= 0xDBFF) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2471 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 2472 | q += 2; |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 2473 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2474 | #ifndef Py_UNICODE_WIDE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2475 | *p++ = ch; |
| 2476 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2477 | #else |
| 2478 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2479 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2480 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2481 | } |
| 2482 | else { |
| 2483 | errmsg = "illegal UTF-16 surrogate"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2484 | startinpos = (((const char *)q)-4)-starts; |
| 2485 | endinpos = startinpos+2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2486 | goto utf16Error; |
| 2487 | } |
| 2488 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2489 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2490 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2491 | startinpos = (((const char *)q)-2)-starts; |
| 2492 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2493 | /* Fall through to report the error */ |
| 2494 | |
| 2495 | utf16Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2496 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2497 | if (unicode_decode_call_errorhandler( |
| 2498 | errors, &errorHandler, |
| 2499 | "utf16", errmsg, |
| 2500 | starts, size, &startinpos, &endinpos, &exc, (const char **)&q, |
Alexandre Vassalotti | 034e08c | 2008-12-27 06:36:10 +0000 | [diff] [blame] | 2501 | &unicode, &outpos, &p)) |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2502 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2503 | } |
| 2504 | |
| 2505 | if (byteorder) |
| 2506 | *byteorder = bo; |
| 2507 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2508 | if (consumed) |
| 2509 | *consumed = (const char *)q-starts; |
| 2510 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2511 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2512 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2513 | goto onError; |
| 2514 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2515 | Py_XDECREF(errorHandler); |
| 2516 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2517 | return (PyObject *)unicode; |
| 2518 | |
| 2519 | onError: |
| 2520 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2521 | Py_XDECREF(errorHandler); |
| 2522 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2523 | return NULL; |
| 2524 | } |
| 2525 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2526 | PyObject * |
| 2527 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2528 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2529 | const char *errors, |
| 2530 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2531 | { |
| 2532 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2533 | unsigned char *p; |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2534 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2535 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2536 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2537 | #else |
| 2538 | const int pairs = 0; |
| 2539 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2540 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2541 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2542 | int ihi = 1, ilo = 0; |
| 2543 | #else |
| 2544 | int ihi = 0, ilo = 1; |
| 2545 | #endif |
| 2546 | |
| 2547 | #define STORECHAR(CH) \ |
| 2548 | do { \ |
| 2549 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 2550 | p[ilo] = (CH) & 0xff; \ |
| 2551 | p += 2; \ |
| 2552 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2553 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2554 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2555 | for (i = pairs = 0; i < size; i++) |
| 2556 | if (s[i] >= 0x10000) |
| 2557 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2558 | #endif |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2559 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 2560 | if (size > PY_SSIZE_T_MAX || |
| 2561 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
| 2562 | return PyErr_NoMemory(); |
| 2563 | nsize = size + pairs + (byteorder == 0); |
| 2564 | bytesize = nsize * 2; |
| 2565 | if (bytesize / 2 != nsize) |
| 2566 | return PyErr_NoMemory(); |
| 2567 | v = PyString_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2568 | if (v == NULL) |
| 2569 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2570 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2571 | p = (unsigned char *)PyString_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2572 | if (byteorder == 0) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2573 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2574 | if (size == 0) |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 2575 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2576 | |
| 2577 | if (byteorder == -1) { |
| 2578 | /* force LE */ |
| 2579 | ihi = 1; |
| 2580 | ilo = 0; |
| 2581 | } |
| 2582 | else if (byteorder == 1) { |
| 2583 | /* force BE */ |
| 2584 | ihi = 0; |
| 2585 | ilo = 1; |
| 2586 | } |
| 2587 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2588 | while (size-- > 0) { |
| 2589 | Py_UNICODE ch = *s++; |
| 2590 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2591 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2592 | if (ch >= 0x10000) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2593 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2594 | ch = 0xD800 | ((ch-0x10000) >> 10); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2595 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2596 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2597 | STORECHAR(ch); |
| 2598 | if (ch2) |
| 2599 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2600 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2601 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2602 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2603 | } |
| 2604 | |
| 2605 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 2606 | { |
| 2607 | if (!PyUnicode_Check(unicode)) { |
| 2608 | PyErr_BadArgument(); |
| 2609 | return NULL; |
| 2610 | } |
| 2611 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
| 2612 | PyUnicode_GET_SIZE(unicode), |
| 2613 | NULL, |
| 2614 | 0); |
| 2615 | } |
| 2616 | |
| 2617 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 2618 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2619 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 2620 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2621 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2622 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2623 | const char *errors) |
| 2624 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2625 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2626 | Py_ssize_t startinpos; |
| 2627 | Py_ssize_t endinpos; |
| 2628 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2629 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2630 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2631 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2632 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2633 | char* message; |
| 2634 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2635 | PyObject *errorHandler = NULL; |
| 2636 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2637 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2638 | /* Escaped strings will always be longer than the resulting |
| 2639 | 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] | 2640 | length after conversion to the true value. |
| 2641 | (but if the error callback returns a long replacement string |
| 2642 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2643 | v = _PyUnicode_New(size); |
| 2644 | if (v == NULL) |
| 2645 | goto onError; |
| 2646 | if (size == 0) |
| 2647 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2648 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2649 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2650 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2651 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2652 | while (s < end) { |
| 2653 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2654 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2655 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2656 | |
| 2657 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 2658 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2659 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2660 | continue; |
| 2661 | } |
| 2662 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2663 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2664 | /* \ - Escapes */ |
| 2665 | s++; |
Guido van Rossum | 1c1ac38 | 2007-10-29 22:15:05 +0000 | [diff] [blame] | 2666 | c = *s++; |
| 2667 | if (s > end) |
| 2668 | c = '\0'; /* Invalid after \ */ |
| 2669 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2670 | |
| 2671 | /* \x escapes */ |
| 2672 | case '\n': break; |
| 2673 | case '\\': *p++ = '\\'; break; |
| 2674 | case '\'': *p++ = '\''; break; |
| 2675 | case '\"': *p++ = '\"'; break; |
| 2676 | case 'b': *p++ = '\b'; break; |
| 2677 | case 'f': *p++ = '\014'; break; /* FF */ |
| 2678 | case 't': *p++ = '\t'; break; |
| 2679 | case 'n': *p++ = '\n'; break; |
| 2680 | case 'r': *p++ = '\r'; break; |
| 2681 | case 'v': *p++ = '\013'; break; /* VT */ |
| 2682 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 2683 | |
| 2684 | /* \OOO (octal) escapes */ |
| 2685 | case '0': case '1': case '2': case '3': |
| 2686 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2687 | x = s[-1] - '0'; |
Guido van Rossum | 1c1ac38 | 2007-10-29 22:15:05 +0000 | [diff] [blame] | 2688 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2689 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 1c1ac38 | 2007-10-29 22:15:05 +0000 | [diff] [blame] | 2690 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2691 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2692 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2693 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2694 | break; |
| 2695 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2696 | /* hex escapes */ |
| 2697 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2698 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2699 | digits = 2; |
| 2700 | message = "truncated \\xXX escape"; |
| 2701 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2702 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2703 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2704 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2705 | digits = 4; |
| 2706 | message = "truncated \\uXXXX escape"; |
| 2707 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2708 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2709 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2710 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2711 | digits = 8; |
| 2712 | message = "truncated \\UXXXXXXXX escape"; |
| 2713 | hexescape: |
| 2714 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2715 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2716 | if (s+digits>end) { |
| 2717 | endinpos = size; |
| 2718 | if (unicode_decode_call_errorhandler( |
| 2719 | errors, &errorHandler, |
| 2720 | "unicodeescape", "end of string in escape sequence", |
| 2721 | starts, size, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | 034e08c | 2008-12-27 06:36:10 +0000 | [diff] [blame] | 2722 | &v, &outpos, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2723 | goto onError; |
| 2724 | goto nextByte; |
| 2725 | } |
| 2726 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2727 | c = (unsigned char) s[i]; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2728 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2729 | endinpos = (s+i+1)-starts; |
| 2730 | if (unicode_decode_call_errorhandler( |
| 2731 | errors, &errorHandler, |
| 2732 | "unicodeescape", message, |
| 2733 | starts, size, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | 034e08c | 2008-12-27 06:36:10 +0000 | [diff] [blame] | 2734 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2735 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2736 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2737 | } |
| 2738 | chr = (chr<<4) & ~0xF; |
| 2739 | if (c >= '0' && c <= '9') |
| 2740 | chr += c - '0'; |
| 2741 | else if (c >= 'a' && c <= 'f') |
| 2742 | chr += 10 + c - 'a'; |
| 2743 | else |
| 2744 | chr += 10 + c - 'A'; |
| 2745 | } |
| 2746 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 2747 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2748 | /* _decoding_error will have already written into the |
| 2749 | target buffer. */ |
| 2750 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2751 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2752 | /* when we get here, chr is a 32-bit unicode character */ |
| 2753 | if (chr <= 0xffff) |
| 2754 | /* UCS-2 character */ |
| 2755 | *p++ = (Py_UNICODE) chr; |
| 2756 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2757 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2758 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2759 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2760 | *p++ = chr; |
| 2761 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2762 | chr -= 0x10000L; |
| 2763 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2764 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2765 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2766 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2767 | endinpos = s-starts; |
| 2768 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2769 | if (unicode_decode_call_errorhandler( |
| 2770 | errors, &errorHandler, |
| 2771 | "unicodeescape", "illegal Unicode character", |
| 2772 | starts, size, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | 034e08c | 2008-12-27 06:36:10 +0000 | [diff] [blame] | 2773 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2774 | goto onError; |
| 2775 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2776 | break; |
| 2777 | |
| 2778 | /* \N{name} */ |
| 2779 | case 'N': |
| 2780 | message = "malformed \\N character escape"; |
| 2781 | if (ucnhash_CAPI == NULL) { |
| 2782 | /* load the unicode data module */ |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2783 | PyObject *m, *api; |
Christian Heimes | 000a074 | 2008-01-03 22:16:32 +0000 | [diff] [blame] | 2784 | m = PyImport_ImportModuleNoBlock("unicodedata"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2785 | if (m == NULL) |
| 2786 | goto ucnhashError; |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2787 | api = PyObject_GetAttrString(m, "ucnhash_CAPI"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2788 | Py_DECREF(m); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2789 | if (api == NULL) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2790 | goto ucnhashError; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2791 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCObject_AsVoidPtr(api); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2792 | Py_DECREF(api); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2793 | if (ucnhash_CAPI == NULL) |
| 2794 | goto ucnhashError; |
| 2795 | } |
| 2796 | if (*s == '{') { |
| 2797 | const char *start = s+1; |
| 2798 | /* look for the closing brace */ |
| 2799 | while (*s != '}' && s < end) |
| 2800 | s++; |
| 2801 | if (s > start && s < end && *s == '}') { |
| 2802 | /* found a name. look it up in the unicode database */ |
| 2803 | message = "unknown Unicode character name"; |
| 2804 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 2805 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2806 | goto store; |
| 2807 | } |
| 2808 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2809 | endinpos = s-starts; |
| 2810 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2811 | if (unicode_decode_call_errorhandler( |
| 2812 | errors, &errorHandler, |
| 2813 | "unicodeescape", message, |
| 2814 | starts, size, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | 034e08c | 2008-12-27 06:36:10 +0000 | [diff] [blame] | 2815 | &v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2816 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2817 | break; |
| 2818 | |
| 2819 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2820 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2821 | message = "\\ at end of string"; |
| 2822 | s--; |
| 2823 | endinpos = s-starts; |
| 2824 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2825 | if (unicode_decode_call_errorhandler( |
| 2826 | errors, &errorHandler, |
| 2827 | "unicodeescape", message, |
| 2828 | starts, size, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | 034e08c | 2008-12-27 06:36:10 +0000 | [diff] [blame] | 2829 | &v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2830 | goto onError; |
| 2831 | } |
| 2832 | else { |
| 2833 | *p++ = '\\'; |
| 2834 | *p++ = (unsigned char)s[-1]; |
| 2835 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2836 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2837 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2838 | nextByte: |
| 2839 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2840 | } |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 2841 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2842 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 2843 | Py_XDECREF(errorHandler); |
| 2844 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2845 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2846 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2847 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2848 | PyErr_SetString( |
| 2849 | PyExc_UnicodeError, |
| 2850 | "\\N escapes not supported (can't load unicodedata module)" |
| 2851 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 2852 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2853 | Py_XDECREF(errorHandler); |
| 2854 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 2855 | return NULL; |
| 2856 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2857 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2858 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2859 | Py_XDECREF(errorHandler); |
| 2860 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2861 | return NULL; |
| 2862 | } |
| 2863 | |
| 2864 | /* Return a Unicode-Escape string version of the Unicode object. |
| 2865 | |
| 2866 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 2867 | appropriate. |
| 2868 | |
| 2869 | */ |
| 2870 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 2871 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
Fredrik Lundh | 95e2a91 | 2006-05-26 11:38:15 +0000 | [diff] [blame] | 2872 | Py_ssize_t size, |
| 2873 | Py_UNICODE ch) |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 2874 | { |
| 2875 | /* like wcschr, but doesn't stop at NULL characters */ |
| 2876 | |
| 2877 | while (size-- > 0) { |
| 2878 | if (*s == ch) |
| 2879 | return s; |
| 2880 | s++; |
| 2881 | } |
| 2882 | |
| 2883 | return NULL; |
| 2884 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 2885 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2886 | static |
| 2887 | PyObject *unicodeescape_string(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2888 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2889 | int quotes) |
| 2890 | { |
| 2891 | PyObject *repr; |
| 2892 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2893 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 2894 | static const char *hexdigit = "0123456789abcdef"; |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2895 | #ifdef Py_UNICODE_WIDE |
| 2896 | const Py_ssize_t expandsize = 10; |
| 2897 | #else |
| 2898 | const Py_ssize_t expandsize = 6; |
| 2899 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2900 | |
Neal Norwitz | 17753ec | 2006-08-21 22:21:19 +0000 | [diff] [blame] | 2901 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 2902 | better to choose a different scheme. Perhaps scan the |
| 2903 | first N-chars of the string and allocate based on that size. |
| 2904 | */ |
| 2905 | /* Initial allocation is based on the longest-possible unichr |
| 2906 | escape. |
| 2907 | |
| 2908 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 2909 | unichr, so in this case it's the longest unichr escape. In |
| 2910 | narrow (UTF-16) builds this is five chars per source unichr |
| 2911 | since there are two unichrs in the surrogate pair, so in narrow |
| 2912 | (UTF-16) builds it's not the longest unichr escape. |
| 2913 | |
| 2914 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 2915 | so in the narrow (UTF-16) build case it's the longest unichr |
| 2916 | escape. |
| 2917 | */ |
| 2918 | |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2919 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
| 2920 | return PyErr_NoMemory(); |
| 2921 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2922 | repr = PyString_FromStringAndSize(NULL, |
Neal Norwitz | 17753ec | 2006-08-21 22:21:19 +0000 | [diff] [blame] | 2923 | 2 |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 2924 | + expandsize*size |
Neal Norwitz | 17753ec | 2006-08-21 22:21:19 +0000 | [diff] [blame] | 2925 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2926 | if (repr == NULL) |
| 2927 | return NULL; |
| 2928 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2929 | p = PyString_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2930 | |
| 2931 | if (quotes) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2932 | *p++ = 'u'; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2933 | *p++ = (findchar(s, size, '\'') && |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2934 | !findchar(s, size, '"')) ? '"' : '\''; |
| 2935 | } |
| 2936 | while (size-- > 0) { |
| 2937 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2938 | |
Hye-Shik Chang | 835b243 | 2005-12-17 04:38:31 +0000 | [diff] [blame] | 2939 | /* Escape quotes and backslashes */ |
| 2940 | if ((quotes && |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2941 | ch == (Py_UNICODE) PyString_AS_STRING(repr)[1]) || ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2942 | *p++ = '\\'; |
| 2943 | *p++ = (char) ch; |
Guido van Rossum | ad9744a | 2001-09-21 15:38:17 +0000 | [diff] [blame] | 2944 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2945 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2946 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 2947 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2948 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 2949 | else if (ch >= 0x10000) { |
| 2950 | *p++ = '\\'; |
| 2951 | *p++ = 'U'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2952 | *p++ = hexdigit[(ch >> 28) & 0x0000000F]; |
| 2953 | *p++ = hexdigit[(ch >> 24) & 0x0000000F]; |
| 2954 | *p++ = hexdigit[(ch >> 20) & 0x0000000F]; |
| 2955 | *p++ = hexdigit[(ch >> 16) & 0x0000000F]; |
| 2956 | *p++ = hexdigit[(ch >> 12) & 0x0000000F]; |
| 2957 | *p++ = hexdigit[(ch >> 8) & 0x0000000F]; |
| 2958 | *p++ = hexdigit[(ch >> 4) & 0x0000000F]; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2959 | *p++ = hexdigit[ch & 0x0000000F]; |
| 2960 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2961 | } |
Neal Norwitz | 17753ec | 2006-08-21 22:21:19 +0000 | [diff] [blame] | 2962 | #else |
| 2963 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2964 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 2965 | Py_UNICODE ch2; |
| 2966 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2967 | |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2968 | ch2 = *s++; |
| 2969 | size--; |
| 2970 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 2971 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 2972 | *p++ = '\\'; |
| 2973 | *p++ = 'U'; |
| 2974 | *p++ = hexdigit[(ucs >> 28) & 0x0000000F]; |
| 2975 | *p++ = hexdigit[(ucs >> 24) & 0x0000000F]; |
| 2976 | *p++ = hexdigit[(ucs >> 20) & 0x0000000F]; |
| 2977 | *p++ = hexdigit[(ucs >> 16) & 0x0000000F]; |
| 2978 | *p++ = hexdigit[(ucs >> 12) & 0x0000000F]; |
| 2979 | *p++ = hexdigit[(ucs >> 8) & 0x0000000F]; |
| 2980 | *p++ = hexdigit[(ucs >> 4) & 0x0000000F]; |
| 2981 | *p++ = hexdigit[ucs & 0x0000000F]; |
| 2982 | continue; |
| 2983 | } |
| 2984 | /* Fall through: isolated surrogates are copied as-is */ |
| 2985 | s--; |
| 2986 | size++; |
| 2987 | } |
Neal Norwitz | 17753ec | 2006-08-21 22:21:19 +0000 | [diff] [blame] | 2988 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2989 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2990 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2991 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2992 | *p++ = '\\'; |
| 2993 | *p++ = 'u'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2994 | *p++ = hexdigit[(ch >> 12) & 0x000F]; |
| 2995 | *p++ = hexdigit[(ch >> 8) & 0x000F]; |
| 2996 | *p++ = hexdigit[(ch >> 4) & 0x000F]; |
| 2997 | *p++ = hexdigit[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2998 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 2999 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3000 | /* Map special whitespace to '\t', \n', '\r' */ |
| 3001 | else if (ch == '\t') { |
| 3002 | *p++ = '\\'; |
| 3003 | *p++ = 't'; |
| 3004 | } |
| 3005 | else if (ch == '\n') { |
| 3006 | *p++ = '\\'; |
| 3007 | *p++ = 'n'; |
| 3008 | } |
| 3009 | else if (ch == '\r') { |
| 3010 | *p++ = '\\'; |
| 3011 | *p++ = 'r'; |
| 3012 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3013 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3014 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 3015 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3016 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3017 | *p++ = 'x'; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3018 | *p++ = hexdigit[(ch >> 4) & 0x000F]; |
| 3019 | *p++ = hexdigit[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3020 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3021 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3022 | /* Copy everything else as-is */ |
| 3023 | else |
| 3024 | *p++ = (char) ch; |
| 3025 | } |
| 3026 | if (quotes) |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3027 | *p++ = PyString_AS_STRING(repr)[1]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3028 | |
| 3029 | *p = '\0'; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3030 | _PyString_Resize(&repr, p - PyString_AS_STRING(repr)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3031 | return repr; |
| 3032 | } |
| 3033 | |
| 3034 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3035 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3036 | { |
| 3037 | return unicodeescape_string(s, size, 0); |
| 3038 | } |
| 3039 | |
| 3040 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
| 3041 | { |
| 3042 | if (!PyUnicode_Check(unicode)) { |
| 3043 | PyErr_BadArgument(); |
| 3044 | return NULL; |
| 3045 | } |
| 3046 | return PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3047 | PyUnicode_GET_SIZE(unicode)); |
| 3048 | } |
| 3049 | |
| 3050 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 3051 | |
| 3052 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3053 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3054 | const char *errors) |
| 3055 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3056 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3057 | Py_ssize_t startinpos; |
| 3058 | Py_ssize_t endinpos; |
| 3059 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3060 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3061 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3062 | const char *end; |
| 3063 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3064 | PyObject *errorHandler = NULL; |
| 3065 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3066 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3067 | /* Escaped strings will always be longer than the resulting |
| 3068 | 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] | 3069 | length after conversion to the true value. (But decoding error |
| 3070 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3071 | v = _PyUnicode_New(size); |
| 3072 | if (v == NULL) |
| 3073 | goto onError; |
| 3074 | if (size == 0) |
| 3075 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3076 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3077 | end = s + size; |
| 3078 | while (s < end) { |
| 3079 | unsigned char c; |
Martin v. Löwis | 047c05e | 2002-03-21 08:55:28 +0000 | [diff] [blame] | 3080 | Py_UCS4 x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3081 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3082 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3083 | |
| 3084 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3085 | if (*s != '\\') { |
| 3086 | *p++ = (unsigned char)*s++; |
| 3087 | continue; |
| 3088 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3089 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3090 | |
| 3091 | /* \u-escapes are only interpreted iff the number of leading |
| 3092 | backslashes if odd */ |
| 3093 | bs = s; |
| 3094 | for (;s < end;) { |
| 3095 | if (*s != '\\') |
| 3096 | break; |
| 3097 | *p++ = (unsigned char)*s++; |
| 3098 | } |
| 3099 | if (((s - bs) & 1) == 0 || |
| 3100 | s >= end || |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3101 | (*s != 'u' && *s != 'U')) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3102 | continue; |
| 3103 | } |
| 3104 | p--; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3105 | count = *s=='u' ? 4 : 8; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3106 | s++; |
| 3107 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3108 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3109 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3110 | for (x = 0, i = 0; i < count; ++i, ++s) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3111 | c = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3112 | if (!isxdigit(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3113 | endinpos = s-starts; |
| 3114 | if (unicode_decode_call_errorhandler( |
| 3115 | errors, &errorHandler, |
| 3116 | "rawunicodeescape", "truncated \\uXXXX", |
| 3117 | starts, size, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | 034e08c | 2008-12-27 06:36:10 +0000 | [diff] [blame] | 3118 | &v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3119 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3120 | goto nextByte; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3121 | } |
| 3122 | x = (x<<4) & ~0xF; |
| 3123 | if (c >= '0' && c <= '9') |
| 3124 | x += c - '0'; |
| 3125 | else if (c >= 'a' && c <= 'f') |
| 3126 | x += 10 + c - 'a'; |
| 3127 | else |
| 3128 | x += 10 + c - 'A'; |
| 3129 | } |
Amaury Forgeot d'Arc | 9a0d346 | 2008-03-23 09:55:29 +0000 | [diff] [blame] | 3130 | if (x <= 0xffff) |
| 3131 | /* UCS-2 character */ |
| 3132 | *p++ = (Py_UNICODE) x; |
| 3133 | else if (x <= 0x10ffff) { |
| 3134 | /* UCS-4 character. Either store directly, or as |
| 3135 | surrogate pair. */ |
| 3136 | #ifdef Py_UNICODE_WIDE |
Amaury Forgeot d'Arc | fac02fa | 2008-03-24 21:04:10 +0000 | [diff] [blame] | 3137 | *p++ = (Py_UNICODE) x; |
Amaury Forgeot d'Arc | 9a0d346 | 2008-03-23 09:55:29 +0000 | [diff] [blame] | 3138 | #else |
| 3139 | x -= 0x10000L; |
| 3140 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 3141 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
| 3142 | #endif |
| 3143 | } else { |
| 3144 | endinpos = s-starts; |
| 3145 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3146 | if (unicode_decode_call_errorhandler( |
| 3147 | errors, &errorHandler, |
| 3148 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
| 3149 | starts, size, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | 034e08c | 2008-12-27 06:36:10 +0000 | [diff] [blame] | 3150 | &v, &outpos, &p)) |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3151 | goto onError; |
| 3152 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3153 | nextByte: |
| 3154 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3155 | } |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 3156 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3157 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3158 | Py_XDECREF(errorHandler); |
| 3159 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3160 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3161 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3162 | onError: |
| 3163 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3164 | Py_XDECREF(errorHandler); |
| 3165 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3166 | return NULL; |
| 3167 | } |
| 3168 | |
| 3169 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3170 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3171 | { |
| 3172 | PyObject *repr; |
| 3173 | char *p; |
| 3174 | char *q; |
| 3175 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3176 | static const char *hexdigit = "0123456789abcdef"; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3177 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 3178 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3179 | #else |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 3180 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3181 | #endif |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 3182 | |
| 3183 | if (size > PY_SSIZE_T_MAX / expandsize) |
| 3184 | return PyErr_NoMemory(); |
| 3185 | |
| 3186 | repr = PyString_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3187 | if (repr == NULL) |
| 3188 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3189 | if (size == 0) |
| 3190 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3191 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3192 | p = q = PyString_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3193 | while (size-- > 0) { |
| 3194 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3195 | #ifdef Py_UNICODE_WIDE |
| 3196 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 3197 | if (ch >= 0x10000) { |
| 3198 | *p++ = '\\'; |
| 3199 | *p++ = 'U'; |
| 3200 | *p++ = hexdigit[(ch >> 28) & 0xf]; |
| 3201 | *p++ = hexdigit[(ch >> 24) & 0xf]; |
| 3202 | *p++ = hexdigit[(ch >> 20) & 0xf]; |
| 3203 | *p++ = hexdigit[(ch >> 16) & 0xf]; |
| 3204 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 3205 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 3206 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 3207 | *p++ = hexdigit[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3208 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3209 | else |
Amaury Forgeot d'Arc | 9a0d346 | 2008-03-23 09:55:29 +0000 | [diff] [blame] | 3210 | #else |
| 3211 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 3212 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 3213 | Py_UNICODE ch2; |
| 3214 | Py_UCS4 ucs; |
| 3215 | |
| 3216 | ch2 = *s++; |
| 3217 | size--; |
| 3218 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 3219 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 3220 | *p++ = '\\'; |
| 3221 | *p++ = 'U'; |
| 3222 | *p++ = hexdigit[(ucs >> 28) & 0xf]; |
| 3223 | *p++ = hexdigit[(ucs >> 24) & 0xf]; |
| 3224 | *p++ = hexdigit[(ucs >> 20) & 0xf]; |
| 3225 | *p++ = hexdigit[(ucs >> 16) & 0xf]; |
| 3226 | *p++ = hexdigit[(ucs >> 12) & 0xf]; |
| 3227 | *p++ = hexdigit[(ucs >> 8) & 0xf]; |
| 3228 | *p++ = hexdigit[(ucs >> 4) & 0xf]; |
| 3229 | *p++ = hexdigit[ucs & 0xf]; |
| 3230 | continue; |
| 3231 | } |
| 3232 | /* Fall through: isolated surrogates are copied as-is */ |
| 3233 | s--; |
| 3234 | size++; |
| 3235 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3236 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3237 | /* Map 16-bit characters to '\uxxxx' */ |
| 3238 | if (ch >= 256) { |
| 3239 | *p++ = '\\'; |
| 3240 | *p++ = 'u'; |
| 3241 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 3242 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 3243 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 3244 | *p++ = hexdigit[ch & 15]; |
| 3245 | } |
| 3246 | /* Copy everything else as-is */ |
| 3247 | else |
| 3248 | *p++ = (char) ch; |
| 3249 | } |
| 3250 | *p = '\0'; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3251 | _PyString_Resize(&repr, p - q); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3252 | return repr; |
| 3253 | } |
| 3254 | |
| 3255 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 3256 | { |
| 3257 | if (!PyUnicode_Check(unicode)) { |
| 3258 | PyErr_BadArgument(); |
| 3259 | return NULL; |
| 3260 | } |
| 3261 | return PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3262 | PyUnicode_GET_SIZE(unicode)); |
| 3263 | } |
| 3264 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3265 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 3266 | |
| 3267 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3268 | Py_ssize_t size, |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3269 | const char *errors) |
| 3270 | { |
| 3271 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3272 | Py_ssize_t startinpos; |
| 3273 | Py_ssize_t endinpos; |
| 3274 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3275 | PyUnicodeObject *v; |
| 3276 | Py_UNICODE *p; |
| 3277 | const char *end; |
| 3278 | const char *reason; |
| 3279 | PyObject *errorHandler = NULL; |
| 3280 | PyObject *exc = NULL; |
| 3281 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 3282 | #ifdef Py_UNICODE_WIDE |
| 3283 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 3284 | #endif |
| 3285 | |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 3286 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3287 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 3288 | if (v == NULL) |
| 3289 | goto onError; |
| 3290 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
| 3291 | return (PyObject *)v; |
| 3292 | p = PyUnicode_AS_UNICODE(v); |
| 3293 | end = s + size; |
| 3294 | |
| 3295 | while (s < end) { |
Neal Norwitz | 1004a53 | 2006-05-15 07:17:23 +0000 | [diff] [blame] | 3296 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3297 | /* We have to sanity check the raw data, otherwise doom looms for |
| 3298 | some malformed UCS-4 data. */ |
| 3299 | if ( |
| 3300 | #ifdef Py_UNICODE_WIDE |
| 3301 | *p > unimax || *p < 0 || |
| 3302 | #endif |
| 3303 | end-s < Py_UNICODE_SIZE |
| 3304 | ) |
| 3305 | { |
| 3306 | startinpos = s - starts; |
| 3307 | if (end-s < Py_UNICODE_SIZE) { |
| 3308 | endinpos = end-starts; |
| 3309 | reason = "truncated input"; |
| 3310 | } |
| 3311 | else { |
| 3312 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 3313 | reason = "illegal code point (> 0x10FFFF)"; |
| 3314 | } |
| 3315 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 3316 | if (unicode_decode_call_errorhandler( |
| 3317 | errors, &errorHandler, |
| 3318 | "unicode_internal", reason, |
| 3319 | starts, size, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | 034e08c | 2008-12-27 06:36:10 +0000 | [diff] [blame] | 3320 | &v, &outpos, &p)) { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3321 | goto onError; |
| 3322 | } |
| 3323 | } |
| 3324 | else { |
| 3325 | p++; |
| 3326 | s += Py_UNICODE_SIZE; |
| 3327 | } |
| 3328 | } |
| 3329 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 3330 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3331 | goto onError; |
| 3332 | Py_XDECREF(errorHandler); |
| 3333 | Py_XDECREF(exc); |
| 3334 | return (PyObject *)v; |
| 3335 | |
| 3336 | onError: |
| 3337 | Py_XDECREF(v); |
| 3338 | Py_XDECREF(errorHandler); |
| 3339 | Py_XDECREF(exc); |
| 3340 | return NULL; |
| 3341 | } |
| 3342 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3343 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 3344 | |
| 3345 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3346 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3347 | const char *errors) |
| 3348 | { |
| 3349 | PyUnicodeObject *v; |
| 3350 | Py_UNICODE *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3351 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3352 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3353 | if (size == 1) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3354 | Py_UNICODE r = *(unsigned char*)s; |
| 3355 | return PyUnicode_FromUnicode(&r, 1); |
| 3356 | } |
| 3357 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3358 | v = _PyUnicode_New(size); |
| 3359 | if (v == NULL) |
| 3360 | goto onError; |
| 3361 | if (size == 0) |
| 3362 | return (PyObject *)v; |
| 3363 | p = PyUnicode_AS_UNICODE(v); |
| 3364 | while (size-- > 0) |
| 3365 | *p++ = (unsigned char)*s++; |
| 3366 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3367 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3368 | onError: |
| 3369 | Py_XDECREF(v); |
| 3370 | return NULL; |
| 3371 | } |
| 3372 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3373 | /* create or adjust a UnicodeEncodeError */ |
| 3374 | static void make_encode_exception(PyObject **exceptionObject, |
| 3375 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3376 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3377 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3378 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3379 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3380 | if (*exceptionObject == NULL) { |
| 3381 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 3382 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3383 | } |
| 3384 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3385 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 3386 | goto onError; |
| 3387 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 3388 | goto onError; |
| 3389 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 3390 | goto onError; |
| 3391 | return; |
| 3392 | onError: |
| 3393 | Py_DECREF(*exceptionObject); |
| 3394 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3395 | } |
| 3396 | } |
| 3397 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3398 | /* raises a UnicodeEncodeError */ |
| 3399 | static void raise_encode_exception(PyObject **exceptionObject, |
| 3400 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3401 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3402 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3403 | const char *reason) |
| 3404 | { |
| 3405 | make_encode_exception(exceptionObject, |
| 3406 | encoding, unicode, size, startpos, endpos, reason); |
| 3407 | if (*exceptionObject != NULL) |
| 3408 | PyCodec_StrictErrors(*exceptionObject); |
| 3409 | } |
| 3410 | |
| 3411 | /* error handling callback helper: |
| 3412 | build arguments, call the callback and check the arguments, |
| 3413 | put the result into newpos and return the replacement string, which |
| 3414 | has to be freed by the caller */ |
| 3415 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
| 3416 | PyObject **errorHandler, |
| 3417 | const char *encoding, const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3418 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 3419 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3420 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3421 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3422 | 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] | 3423 | |
| 3424 | PyObject *restuple; |
| 3425 | PyObject *resunicode; |
| 3426 | |
| 3427 | if (*errorHandler == NULL) { |
| 3428 | *errorHandler = PyCodec_LookupError(errors); |
| 3429 | if (*errorHandler == NULL) |
| 3430 | return NULL; |
| 3431 | } |
| 3432 | |
| 3433 | make_encode_exception(exceptionObject, |
| 3434 | encoding, unicode, size, startpos, endpos, reason); |
| 3435 | if (*exceptionObject == NULL) |
| 3436 | return NULL; |
| 3437 | |
| 3438 | restuple = PyObject_CallFunctionObjArgs( |
| 3439 | *errorHandler, *exceptionObject, NULL); |
| 3440 | if (restuple == NULL) |
| 3441 | return NULL; |
| 3442 | if (!PyTuple_Check(restuple)) { |
| 3443 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 3444 | Py_DECREF(restuple); |
| 3445 | return NULL; |
| 3446 | } |
| 3447 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
| 3448 | &resunicode, newpos)) { |
| 3449 | Py_DECREF(restuple); |
| 3450 | return NULL; |
| 3451 | } |
| 3452 | if (*newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3453 | *newpos = size+*newpos; |
| 3454 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 3455 | 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] | 3456 | Py_DECREF(restuple); |
| 3457 | return NULL; |
| 3458 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3459 | Py_INCREF(resunicode); |
| 3460 | Py_DECREF(restuple); |
| 3461 | return resunicode; |
| 3462 | } |
| 3463 | |
| 3464 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3465 | Py_ssize_t size, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3466 | const char *errors, |
| 3467 | int limit) |
| 3468 | { |
| 3469 | /* output object */ |
| 3470 | PyObject *res; |
| 3471 | /* pointers to the beginning and end+1 of input */ |
| 3472 | const Py_UNICODE *startp = p; |
| 3473 | const Py_UNICODE *endp = p + size; |
| 3474 | /* pointer to the beginning of the unencodable characters */ |
| 3475 | /* const Py_UNICODE *badp = NULL; */ |
| 3476 | /* pointer into the output */ |
| 3477 | char *str; |
| 3478 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3479 | Py_ssize_t respos = 0; |
| 3480 | Py_ssize_t ressize; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 3481 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 3482 | 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] | 3483 | PyObject *errorHandler = NULL; |
| 3484 | PyObject *exc = NULL; |
| 3485 | /* the following variable is used for caching string comparisons |
| 3486 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 3487 | int known_errorHandler = -1; |
| 3488 | |
| 3489 | /* allocate enough for a simple encoding without |
| 3490 | replacements, if we need more, we'll resize */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3491 | res = PyString_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3492 | if (res == NULL) |
| 3493 | goto onError; |
| 3494 | if (size == 0) |
| 3495 | return res; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3496 | str = PyString_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3497 | ressize = size; |
| 3498 | |
| 3499 | while (p<endp) { |
| 3500 | Py_UNICODE c = *p; |
| 3501 | |
| 3502 | /* can we encode this? */ |
| 3503 | if (c<limit) { |
| 3504 | /* no overflow check, because we know that the space is enough */ |
| 3505 | *str++ = (char)c; |
| 3506 | ++p; |
| 3507 | } |
| 3508 | else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3509 | Py_ssize_t unicodepos = p-startp; |
| 3510 | Py_ssize_t requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3511 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3512 | Py_ssize_t repsize; |
| 3513 | Py_ssize_t newpos; |
| 3514 | Py_ssize_t respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3515 | Py_UNICODE *uni2; |
| 3516 | /* startpos for collecting unencodable chars */ |
| 3517 | const Py_UNICODE *collstart = p; |
| 3518 | const Py_UNICODE *collend = p; |
| 3519 | /* find all unecodable characters */ |
| 3520 | while ((collend < endp) && ((*collend)>=limit)) |
| 3521 | ++collend; |
| 3522 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 3523 | if (known_errorHandler==-1) { |
| 3524 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3525 | known_errorHandler = 1; |
| 3526 | else if (!strcmp(errors, "replace")) |
| 3527 | known_errorHandler = 2; |
| 3528 | else if (!strcmp(errors, "ignore")) |
| 3529 | known_errorHandler = 3; |
| 3530 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3531 | known_errorHandler = 4; |
| 3532 | else |
| 3533 | known_errorHandler = 0; |
| 3534 | } |
| 3535 | switch (known_errorHandler) { |
| 3536 | case 1: /* strict */ |
| 3537 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 3538 | goto onError; |
| 3539 | case 2: /* replace */ |
| 3540 | while (collstart++<collend) |
| 3541 | *str++ = '?'; /* fall through */ |
| 3542 | case 3: /* ignore */ |
| 3543 | p = collend; |
| 3544 | break; |
| 3545 | case 4: /* xmlcharrefreplace */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3546 | respos = str-PyString_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3547 | /* determine replacement size (temporarily (mis)uses p) */ |
| 3548 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 3549 | if (*p<10) |
| 3550 | repsize += 2+1+1; |
| 3551 | else if (*p<100) |
| 3552 | repsize += 2+2+1; |
| 3553 | else if (*p<1000) |
| 3554 | repsize += 2+3+1; |
| 3555 | else if (*p<10000) |
| 3556 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 3557 | #ifndef Py_UNICODE_WIDE |
| 3558 | else |
| 3559 | repsize += 2+5+1; |
| 3560 | #else |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3561 | else if (*p<100000) |
| 3562 | repsize += 2+5+1; |
| 3563 | else if (*p<1000000) |
| 3564 | repsize += 2+6+1; |
| 3565 | else |
| 3566 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3567 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3568 | } |
| 3569 | requiredsize = respos+repsize+(endp-collend); |
| 3570 | if (requiredsize > ressize) { |
| 3571 | if (requiredsize<2*ressize) |
| 3572 | requiredsize = 2*ressize; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3573 | if (_PyString_Resize(&res, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3574 | goto onError; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3575 | str = PyString_AS_STRING(res) + respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3576 | ressize = requiredsize; |
| 3577 | } |
| 3578 | /* generate replacement (temporarily (mis)uses p) */ |
| 3579 | for (p = collstart; p < collend; ++p) { |
| 3580 | str += sprintf(str, "&#%d;", (int)*p); |
| 3581 | } |
| 3582 | p = collend; |
| 3583 | break; |
| 3584 | default: |
| 3585 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 3586 | encoding, reason, startp, size, &exc, |
| 3587 | collstart-startp, collend-startp, &newpos); |
| 3588 | if (repunicode == NULL) |
| 3589 | goto onError; |
| 3590 | /* need more space? (at least enough for what we |
| 3591 | have+the replacement+the rest of the string, so |
| 3592 | we won't have to check space for encodable characters) */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3593 | respos = str-PyString_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3594 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3595 | requiredsize = respos+repsize+(endp-collend); |
| 3596 | if (requiredsize > ressize) { |
| 3597 | if (requiredsize<2*ressize) |
| 3598 | requiredsize = 2*ressize; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3599 | if (_PyString_Resize(&res, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3600 | Py_DECREF(repunicode); |
| 3601 | goto onError; |
| 3602 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3603 | str = PyString_AS_STRING(res) + respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3604 | ressize = requiredsize; |
| 3605 | } |
| 3606 | /* check if there is anything unencodable in the replacement |
| 3607 | and copy it to the output */ |
| 3608 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 3609 | c = *uni2; |
| 3610 | if (c >= limit) { |
| 3611 | raise_encode_exception(&exc, encoding, startp, size, |
| 3612 | unicodepos, unicodepos+1, reason); |
| 3613 | Py_DECREF(repunicode); |
| 3614 | goto onError; |
| 3615 | } |
| 3616 | *str = (char)c; |
| 3617 | } |
| 3618 | p = startp + newpos; |
| 3619 | Py_DECREF(repunicode); |
| 3620 | } |
| 3621 | } |
| 3622 | } |
| 3623 | /* Resize if we allocated to much */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3624 | respos = str-PyString_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3625 | if (respos<ressize) |
| 3626 | /* If this falls res will be NULL */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3627 | _PyString_Resize(&res, respos); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3628 | Py_XDECREF(errorHandler); |
| 3629 | Py_XDECREF(exc); |
| 3630 | return res; |
| 3631 | |
| 3632 | onError: |
| 3633 | Py_XDECREF(res); |
| 3634 | Py_XDECREF(errorHandler); |
| 3635 | Py_XDECREF(exc); |
| 3636 | return NULL; |
| 3637 | } |
| 3638 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3639 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3640 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3641 | const char *errors) |
| 3642 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3643 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3644 | } |
| 3645 | |
| 3646 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 3647 | { |
| 3648 | if (!PyUnicode_Check(unicode)) { |
| 3649 | PyErr_BadArgument(); |
| 3650 | return NULL; |
| 3651 | } |
| 3652 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 3653 | PyUnicode_GET_SIZE(unicode), |
| 3654 | NULL); |
| 3655 | } |
| 3656 | |
| 3657 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 3658 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3659 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3660 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3661 | const char *errors) |
| 3662 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3663 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3664 | PyUnicodeObject *v; |
| 3665 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3666 | Py_ssize_t startinpos; |
| 3667 | Py_ssize_t endinpos; |
| 3668 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3669 | const char *e; |
| 3670 | PyObject *errorHandler = NULL; |
| 3671 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3672 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3673 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3674 | if (size == 1 && *(unsigned char*)s < 128) { |
| 3675 | Py_UNICODE r = *(unsigned char*)s; |
| 3676 | return PyUnicode_FromUnicode(&r, 1); |
| 3677 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3678 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3679 | v = _PyUnicode_New(size); |
| 3680 | if (v == NULL) |
| 3681 | goto onError; |
| 3682 | if (size == 0) |
| 3683 | return (PyObject *)v; |
| 3684 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3685 | e = s + size; |
| 3686 | while (s < e) { |
| 3687 | register unsigned char c = (unsigned char)*s; |
| 3688 | if (c < 128) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3689 | *p++ = c; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3690 | ++s; |
| 3691 | } |
| 3692 | else { |
| 3693 | startinpos = s-starts; |
| 3694 | endinpos = startinpos + 1; |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 3695 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3696 | if (unicode_decode_call_errorhandler( |
| 3697 | errors, &errorHandler, |
| 3698 | "ascii", "ordinal not in range(128)", |
| 3699 | starts, size, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | 034e08c | 2008-12-27 06:36:10 +0000 | [diff] [blame] | 3700 | &v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3701 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3702 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3703 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3704 | if (p - PyUnicode_AS_UNICODE(v) < PyString_GET_SIZE(v)) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 3705 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3706 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3707 | Py_XDECREF(errorHandler); |
| 3708 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3709 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3710 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3711 | onError: |
| 3712 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3713 | Py_XDECREF(errorHandler); |
| 3714 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3715 | return NULL; |
| 3716 | } |
| 3717 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3718 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3719 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3720 | const char *errors) |
| 3721 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3722 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3723 | } |
| 3724 | |
| 3725 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 3726 | { |
| 3727 | if (!PyUnicode_Check(unicode)) { |
| 3728 | PyErr_BadArgument(); |
| 3729 | return NULL; |
| 3730 | } |
| 3731 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 3732 | PyUnicode_GET_SIZE(unicode), |
| 3733 | NULL); |
| 3734 | } |
| 3735 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3736 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3737 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3738 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3739 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3740 | #if SIZEOF_INT < SIZEOF_SSIZE_T |
| 3741 | #define NEED_RETRY |
| 3742 | #endif |
| 3743 | |
| 3744 | /* XXX This code is limited to "true" double-byte encodings, as |
| 3745 | a) it assumes an incomplete character consists of a single byte, and |
| 3746 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
| 3747 | encodings, see IsDBCSLeadByteEx documentation. */ |
| 3748 | |
| 3749 | static int is_dbcs_lead_byte(const char *s, int offset) |
| 3750 | { |
| 3751 | const char *curr = s + offset; |
| 3752 | |
| 3753 | if (IsDBCSLeadByte(*curr)) { |
| 3754 | const char *prev = CharPrev(s, curr); |
| 3755 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
| 3756 | } |
| 3757 | return 0; |
| 3758 | } |
| 3759 | |
| 3760 | /* |
| 3761 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 3762 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 3763 | */ |
| 3764 | static int decode_mbcs(PyUnicodeObject **v, |
| 3765 | const char *s, /* MBCS string */ |
| 3766 | int size, /* sizeof MBCS string */ |
| 3767 | int final) |
| 3768 | { |
| 3769 | Py_UNICODE *p; |
| 3770 | Py_ssize_t n = 0; |
| 3771 | int usize = 0; |
| 3772 | |
| 3773 | assert(size >= 0); |
| 3774 | |
| 3775 | /* Skip trailing lead-byte unless 'final' is set */ |
| 3776 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
| 3777 | --size; |
| 3778 | |
| 3779 | /* First get the size of the result */ |
| 3780 | if (size > 0) { |
| 3781 | usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); |
| 3782 | if (usize == 0) { |
| 3783 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3784 | return -1; |
| 3785 | } |
| 3786 | } |
| 3787 | |
| 3788 | if (*v == NULL) { |
| 3789 | /* Create unicode object */ |
| 3790 | *v = _PyUnicode_New(usize); |
| 3791 | if (*v == NULL) |
| 3792 | return -1; |
| 3793 | } |
| 3794 | else { |
| 3795 | /* Extend unicode object */ |
| 3796 | n = PyUnicode_GET_SIZE(*v); |
| 3797 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 3798 | return -1; |
| 3799 | } |
| 3800 | |
| 3801 | /* Do the conversion */ |
| 3802 | if (size > 0) { |
| 3803 | p = PyUnicode_AS_UNICODE(*v) + n; |
| 3804 | if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { |
| 3805 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3806 | return -1; |
| 3807 | } |
| 3808 | } |
| 3809 | |
| 3810 | return size; |
| 3811 | } |
| 3812 | |
| 3813 | PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
| 3814 | Py_ssize_t size, |
| 3815 | const char *errors, |
| 3816 | Py_ssize_t *consumed) |
| 3817 | { |
| 3818 | PyUnicodeObject *v = NULL; |
| 3819 | int done; |
| 3820 | |
| 3821 | if (consumed) |
| 3822 | *consumed = 0; |
| 3823 | |
| 3824 | #ifdef NEED_RETRY |
| 3825 | retry: |
| 3826 | if (size > INT_MAX) |
| 3827 | done = decode_mbcs(&v, s, INT_MAX, 0); |
| 3828 | else |
| 3829 | #endif |
| 3830 | done = decode_mbcs(&v, s, (int)size, !consumed); |
| 3831 | |
| 3832 | if (done < 0) { |
| 3833 | Py_XDECREF(v); |
| 3834 | return NULL; |
| 3835 | } |
| 3836 | |
| 3837 | if (consumed) |
| 3838 | *consumed += done; |
| 3839 | |
| 3840 | #ifdef NEED_RETRY |
| 3841 | if (size > INT_MAX) { |
| 3842 | s += done; |
| 3843 | size -= done; |
| 3844 | goto retry; |
| 3845 | } |
| 3846 | #endif |
| 3847 | |
| 3848 | return (PyObject *)v; |
| 3849 | } |
| 3850 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3851 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3852 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3853 | const char *errors) |
| 3854 | { |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3855 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 3856 | } |
| 3857 | |
| 3858 | /* |
| 3859 | * Convert unicode into string object (MBCS). |
| 3860 | * Returns 0 if succeed, -1 otherwise. |
| 3861 | */ |
| 3862 | static int encode_mbcs(PyObject **repr, |
| 3863 | const Py_UNICODE *p, /* unicode */ |
| 3864 | int size) /* size of unicode */ |
| 3865 | { |
| 3866 | int mbcssize = 0; |
| 3867 | Py_ssize_t n = 0; |
| 3868 | |
| 3869 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3870 | |
| 3871 | /* First get the size of the result */ |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3872 | if (size > 0) { |
| 3873 | mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); |
| 3874 | if (mbcssize == 0) { |
| 3875 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3876 | return -1; |
| 3877 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3878 | } |
| 3879 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3880 | if (*repr == NULL) { |
| 3881 | /* Create string object */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3882 | *repr = PyString_FromStringAndSize(NULL, mbcssize); |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3883 | if (*repr == NULL) |
| 3884 | return -1; |
| 3885 | } |
| 3886 | else { |
| 3887 | /* Extend string object */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3888 | n = PyString_Size(*repr); |
| 3889 | if (_PyString_Resize(repr, n + mbcssize) < 0) |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3890 | return -1; |
| 3891 | } |
| 3892 | |
| 3893 | /* Do the conversion */ |
| 3894 | if (size > 0) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 3895 | char *s = PyString_AS_STRING(*repr) + n; |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3896 | if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { |
| 3897 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3898 | return -1; |
| 3899 | } |
| 3900 | } |
| 3901 | |
| 3902 | return 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3903 | } |
| 3904 | |
| 3905 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3906 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3907 | const char *errors) |
| 3908 | { |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3909 | PyObject *repr = NULL; |
| 3910 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 3911 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3912 | #ifdef NEED_RETRY |
| 3913 | retry: |
| 3914 | if (size > INT_MAX) |
| 3915 | ret = encode_mbcs(&repr, p, INT_MAX); |
| 3916 | else |
| 3917 | #endif |
| 3918 | ret = encode_mbcs(&repr, p, (int)size); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3919 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3920 | if (ret < 0) { |
| 3921 | Py_XDECREF(repr); |
| 3922 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3923 | } |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3924 | |
| 3925 | #ifdef NEED_RETRY |
| 3926 | if (size > INT_MAX) { |
| 3927 | p += INT_MAX; |
| 3928 | size -= INT_MAX; |
| 3929 | goto retry; |
| 3930 | } |
| 3931 | #endif |
| 3932 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3933 | return repr; |
| 3934 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3935 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 3936 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 3937 | { |
| 3938 | if (!PyUnicode_Check(unicode)) { |
| 3939 | PyErr_BadArgument(); |
| 3940 | return NULL; |
| 3941 | } |
| 3942 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 3943 | PyUnicode_GET_SIZE(unicode), |
| 3944 | NULL); |
| 3945 | } |
| 3946 | |
Martin v. Löwis | d825143 | 2006-06-14 05:21:04 +0000 | [diff] [blame] | 3947 | #undef NEED_RETRY |
| 3948 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3949 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3950 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3951 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 3952 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3953 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3954 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3955 | PyObject *mapping, |
| 3956 | const char *errors) |
| 3957 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3958 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3959 | Py_ssize_t startinpos; |
| 3960 | Py_ssize_t endinpos; |
| 3961 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3962 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3963 | PyUnicodeObject *v; |
| 3964 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3965 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3966 | PyObject *errorHandler = NULL; |
| 3967 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3968 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3969 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3970 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3971 | /* Default to Latin-1 */ |
| 3972 | if (mapping == NULL) |
| 3973 | return PyUnicode_DecodeLatin1(s, size, errors); |
| 3974 | |
| 3975 | v = _PyUnicode_New(size); |
| 3976 | if (v == NULL) |
| 3977 | goto onError; |
| 3978 | if (size == 0) |
| 3979 | return (PyObject *)v; |
| 3980 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3981 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3982 | if (PyUnicode_CheckExact(mapping)) { |
| 3983 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 3984 | maplen = PyUnicode_GET_SIZE(mapping); |
| 3985 | while (s < e) { |
| 3986 | unsigned char ch = *s; |
| 3987 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3988 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3989 | if (ch < maplen) |
| 3990 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3991 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 3992 | if (x == 0xfffe) { |
| 3993 | /* undefined mapping */ |
| 3994 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3995 | startinpos = s-starts; |
| 3996 | endinpos = startinpos+1; |
| 3997 | if (unicode_decode_call_errorhandler( |
| 3998 | errors, &errorHandler, |
| 3999 | "charmap", "character maps to <undefined>", |
| 4000 | starts, size, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | 034e08c | 2008-12-27 06:36:10 +0000 | [diff] [blame] | 4001 | &v, &outpos, &p)) { |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4002 | goto onError; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 4003 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4004 | continue; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 4005 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4006 | *p++ = x; |
| 4007 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4008 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4009 | } |
| 4010 | else { |
| 4011 | while (s < e) { |
| 4012 | unsigned char ch = *s; |
| 4013 | PyObject *w, *x; |
| 4014 | |
| 4015 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 4016 | w = PyInt_FromLong((long)ch); |
| 4017 | if (w == NULL) |
| 4018 | goto onError; |
| 4019 | x = PyObject_GetItem(mapping, w); |
| 4020 | Py_DECREF(w); |
| 4021 | if (x == NULL) { |
| 4022 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4023 | /* No mapping found means: mapping is undefined. */ |
| 4024 | PyErr_Clear(); |
| 4025 | x = Py_None; |
| 4026 | Py_INCREF(x); |
| 4027 | } else |
| 4028 | goto onError; |
| 4029 | } |
| 4030 | |
| 4031 | /* Apply mapping */ |
| 4032 | if (PyInt_Check(x)) { |
| 4033 | long value = PyInt_AS_LONG(x); |
| 4034 | if (value < 0 || value > 65535) { |
| 4035 | PyErr_SetString(PyExc_TypeError, |
| 4036 | "character mapping must be in range(65536)"); |
| 4037 | Py_DECREF(x); |
| 4038 | goto onError; |
| 4039 | } |
| 4040 | *p++ = (Py_UNICODE)value; |
| 4041 | } |
| 4042 | else if (x == Py_None) { |
| 4043 | /* undefined mapping */ |
| 4044 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4045 | startinpos = s-starts; |
| 4046 | endinpos = startinpos+1; |
| 4047 | if (unicode_decode_call_errorhandler( |
| 4048 | errors, &errorHandler, |
| 4049 | "charmap", "character maps to <undefined>", |
| 4050 | starts, size, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | 034e08c | 2008-12-27 06:36:10 +0000 | [diff] [blame] | 4051 | &v, &outpos, &p)) { |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4052 | Py_DECREF(x); |
| 4053 | goto onError; |
| 4054 | } |
Walter Dörwald | d4fff17 | 2005-11-28 22:15:56 +0000 | [diff] [blame] | 4055 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4056 | continue; |
| 4057 | } |
| 4058 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4059 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4060 | |
| 4061 | if (targetsize == 1) |
| 4062 | /* 1-1 mapping */ |
| 4063 | *p++ = *PyUnicode_AS_UNICODE(x); |
| 4064 | |
| 4065 | else if (targetsize > 1) { |
| 4066 | /* 1-n mapping */ |
| 4067 | if (targetsize > extrachars) { |
| 4068 | /* resize first */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4069 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 4070 | Py_ssize_t needed = (targetsize - extrachars) + \ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4071 | (targetsize << 2); |
| 4072 | extrachars += needed; |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 4073 | /* XXX overflow detection missing */ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4074 | if (_PyUnicode_Resize(&v, |
| 4075 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 4076 | Py_DECREF(x); |
| 4077 | goto onError; |
| 4078 | } |
| 4079 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 4080 | } |
| 4081 | Py_UNICODE_COPY(p, |
| 4082 | PyUnicode_AS_UNICODE(x), |
| 4083 | targetsize); |
| 4084 | p += targetsize; |
| 4085 | extrachars -= targetsize; |
| 4086 | } |
| 4087 | /* 1-0 mapping: skip the character */ |
| 4088 | } |
| 4089 | else { |
| 4090 | /* wrong return value */ |
| 4091 | PyErr_SetString(PyExc_TypeError, |
| 4092 | "character mapping must return integer, None or unicode"); |
| 4093 | Py_DECREF(x); |
| 4094 | goto onError; |
| 4095 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4096 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4097 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4098 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4099 | } |
| 4100 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4101 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4102 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4103 | Py_XDECREF(errorHandler); |
| 4104 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4105 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4106 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4107 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4108 | Py_XDECREF(errorHandler); |
| 4109 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4110 | Py_XDECREF(v); |
| 4111 | return NULL; |
| 4112 | } |
| 4113 | |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4114 | /* Charmap encoding: the lookup table */ |
| 4115 | |
| 4116 | struct encoding_map{ |
| 4117 | PyObject_HEAD |
| 4118 | unsigned char level1[32]; |
| 4119 | int count2, count3; |
| 4120 | unsigned char level23[1]; |
| 4121 | }; |
| 4122 | |
| 4123 | static PyObject* |
| 4124 | encoding_map_size(PyObject *obj, PyObject* args) |
| 4125 | { |
| 4126 | struct encoding_map *map = (struct encoding_map*)obj; |
| 4127 | return PyInt_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
| 4128 | 128*map->count3); |
| 4129 | } |
| 4130 | |
| 4131 | static PyMethodDef encoding_map_methods[] = { |
| 4132 | {"size", encoding_map_size, METH_NOARGS, |
| 4133 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 4134 | { 0 } |
| 4135 | }; |
| 4136 | |
| 4137 | static void |
| 4138 | encoding_map_dealloc(PyObject* o) |
| 4139 | { |
| 4140 | PyObject_FREE(o); |
| 4141 | } |
| 4142 | |
| 4143 | static PyTypeObject EncodingMapType = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 4144 | PyVarObject_HEAD_INIT(NULL, 0) |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4145 | "EncodingMap", /*tp_name*/ |
| 4146 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 4147 | 0, /*tp_itemsize*/ |
| 4148 | /* methods */ |
| 4149 | encoding_map_dealloc, /*tp_dealloc*/ |
| 4150 | 0, /*tp_print*/ |
| 4151 | 0, /*tp_getattr*/ |
| 4152 | 0, /*tp_setattr*/ |
| 4153 | 0, /*tp_compare*/ |
| 4154 | 0, /*tp_repr*/ |
| 4155 | 0, /*tp_as_number*/ |
| 4156 | 0, /*tp_as_sequence*/ |
| 4157 | 0, /*tp_as_mapping*/ |
| 4158 | 0, /*tp_hash*/ |
| 4159 | 0, /*tp_call*/ |
| 4160 | 0, /*tp_str*/ |
| 4161 | 0, /*tp_getattro*/ |
| 4162 | 0, /*tp_setattro*/ |
| 4163 | 0, /*tp_as_buffer*/ |
| 4164 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 4165 | 0, /*tp_doc*/ |
| 4166 | 0, /*tp_traverse*/ |
| 4167 | 0, /*tp_clear*/ |
| 4168 | 0, /*tp_richcompare*/ |
| 4169 | 0, /*tp_weaklistoffset*/ |
| 4170 | 0, /*tp_iter*/ |
| 4171 | 0, /*tp_iternext*/ |
| 4172 | encoding_map_methods, /*tp_methods*/ |
| 4173 | 0, /*tp_members*/ |
| 4174 | 0, /*tp_getset*/ |
| 4175 | 0, /*tp_base*/ |
| 4176 | 0, /*tp_dict*/ |
| 4177 | 0, /*tp_descr_get*/ |
| 4178 | 0, /*tp_descr_set*/ |
| 4179 | 0, /*tp_dictoffset*/ |
| 4180 | 0, /*tp_init*/ |
| 4181 | 0, /*tp_alloc*/ |
| 4182 | 0, /*tp_new*/ |
| 4183 | 0, /*tp_free*/ |
| 4184 | 0, /*tp_is_gc*/ |
| 4185 | }; |
| 4186 | |
| 4187 | PyObject* |
| 4188 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 4189 | { |
| 4190 | Py_UNICODE *decode; |
| 4191 | PyObject *result; |
| 4192 | struct encoding_map *mresult; |
| 4193 | int i; |
| 4194 | int need_dict = 0; |
| 4195 | unsigned char level1[32]; |
| 4196 | unsigned char level2[512]; |
| 4197 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 4198 | int count2 = 0, count3 = 0; |
| 4199 | |
| 4200 | if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { |
| 4201 | PyErr_BadArgument(); |
| 4202 | return NULL; |
| 4203 | } |
| 4204 | decode = PyUnicode_AS_UNICODE(string); |
| 4205 | memset(level1, 0xFF, sizeof level1); |
| 4206 | memset(level2, 0xFF, sizeof level2); |
| 4207 | |
| 4208 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 4209 | or if there are non-BMP characters, we need to use |
| 4210 | a mapping dictionary. */ |
| 4211 | if (decode[0] != 0) |
| 4212 | need_dict = 1; |
| 4213 | for (i = 1; i < 256; i++) { |
| 4214 | int l1, l2; |
| 4215 | if (decode[i] == 0 |
| 4216 | #ifdef Py_UNICODE_WIDE |
| 4217 | || decode[i] > 0xFFFF |
| 4218 | #endif |
| 4219 | ) { |
| 4220 | need_dict = 1; |
| 4221 | break; |
| 4222 | } |
| 4223 | if (decode[i] == 0xFFFE) |
| 4224 | /* unmapped character */ |
| 4225 | continue; |
| 4226 | l1 = decode[i] >> 11; |
| 4227 | l2 = decode[i] >> 7; |
| 4228 | if (level1[l1] == 0xFF) |
| 4229 | level1[l1] = count2++; |
| 4230 | if (level2[l2] == 0xFF) |
| 4231 | level2[l2] = count3++; |
| 4232 | } |
| 4233 | |
| 4234 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 4235 | need_dict = 1; |
| 4236 | |
| 4237 | if (need_dict) { |
| 4238 | PyObject *result = PyDict_New(); |
| 4239 | PyObject *key, *value; |
| 4240 | if (!result) |
| 4241 | return NULL; |
| 4242 | for (i = 0; i < 256; i++) { |
| 4243 | key = value = NULL; |
| 4244 | key = PyInt_FromLong(decode[i]); |
| 4245 | value = PyInt_FromLong(i); |
| 4246 | if (!key || !value) |
| 4247 | goto failed1; |
| 4248 | if (PyDict_SetItem(result, key, value) == -1) |
| 4249 | goto failed1; |
Georg Brandl | 9f16760 | 2006-06-04 21:46:16 +0000 | [diff] [blame] | 4250 | Py_DECREF(key); |
| 4251 | Py_DECREF(value); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4252 | } |
| 4253 | return result; |
| 4254 | failed1: |
| 4255 | Py_XDECREF(key); |
| 4256 | Py_XDECREF(value); |
| 4257 | Py_DECREF(result); |
| 4258 | return NULL; |
| 4259 | } |
| 4260 | |
| 4261 | /* Create a three-level trie */ |
| 4262 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 4263 | 16*count2 + 128*count3 - 1); |
| 4264 | if (!result) |
| 4265 | return PyErr_NoMemory(); |
| 4266 | PyObject_Init(result, &EncodingMapType); |
| 4267 | mresult = (struct encoding_map*)result; |
| 4268 | mresult->count2 = count2; |
| 4269 | mresult->count3 = count3; |
| 4270 | mlevel1 = mresult->level1; |
| 4271 | mlevel2 = mresult->level23; |
| 4272 | mlevel3 = mresult->level23 + 16*count2; |
| 4273 | memcpy(mlevel1, level1, 32); |
| 4274 | memset(mlevel2, 0xFF, 16*count2); |
| 4275 | memset(mlevel3, 0, 128*count3); |
| 4276 | count3 = 0; |
| 4277 | for (i = 1; i < 256; i++) { |
| 4278 | int o1, o2, o3, i2, i3; |
| 4279 | if (decode[i] == 0xFFFE) |
| 4280 | /* unmapped character */ |
| 4281 | continue; |
| 4282 | o1 = decode[i]>>11; |
| 4283 | o2 = (decode[i]>>7) & 0xF; |
| 4284 | i2 = 16*mlevel1[o1] + o2; |
| 4285 | if (mlevel2[i2] == 0xFF) |
| 4286 | mlevel2[i2] = count3++; |
| 4287 | o3 = decode[i] & 0x7F; |
| 4288 | i3 = 128*mlevel2[i2] + o3; |
| 4289 | mlevel3[i3] = i; |
| 4290 | } |
| 4291 | return result; |
| 4292 | } |
| 4293 | |
| 4294 | static int |
| 4295 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 4296 | { |
| 4297 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 4298 | int l1 = c>>11; |
| 4299 | int l2 = (c>>7) & 0xF; |
| 4300 | int l3 = c & 0x7F; |
| 4301 | int i; |
| 4302 | |
| 4303 | #ifdef Py_UNICODE_WIDE |
| 4304 | if (c > 0xFFFF) { |
| 4305 | return -1; |
| 4306 | } |
| 4307 | #endif |
| 4308 | if (c == 0) |
| 4309 | return 0; |
| 4310 | /* level 1*/ |
| 4311 | i = map->level1[l1]; |
| 4312 | if (i == 0xFF) { |
| 4313 | return -1; |
| 4314 | } |
| 4315 | /* level 2*/ |
| 4316 | i = map->level23[16*i+l2]; |
| 4317 | if (i == 0xFF) { |
| 4318 | return -1; |
| 4319 | } |
| 4320 | /* level 3 */ |
| 4321 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 4322 | if (i == 0) { |
| 4323 | return -1; |
| 4324 | } |
| 4325 | return i; |
| 4326 | } |
| 4327 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4328 | /* Lookup the character ch in the mapping. If the character |
| 4329 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 4330 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4331 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4332 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4333 | PyObject *w = PyInt_FromLong((long)c); |
| 4334 | PyObject *x; |
| 4335 | |
| 4336 | if (w == NULL) |
| 4337 | return NULL; |
| 4338 | x = PyObject_GetItem(mapping, w); |
| 4339 | Py_DECREF(w); |
| 4340 | if (x == NULL) { |
| 4341 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4342 | /* No mapping found means: mapping is undefined. */ |
| 4343 | PyErr_Clear(); |
| 4344 | x = Py_None; |
| 4345 | Py_INCREF(x); |
| 4346 | return x; |
| 4347 | } else |
| 4348 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4349 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 4350 | else if (x == Py_None) |
| 4351 | return x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4352 | else if (PyInt_Check(x)) { |
| 4353 | long value = PyInt_AS_LONG(x); |
| 4354 | if (value < 0 || value > 255) { |
| 4355 | PyErr_SetString(PyExc_TypeError, |
| 4356 | "character mapping must be in range(256)"); |
| 4357 | Py_DECREF(x); |
| 4358 | return NULL; |
| 4359 | } |
| 4360 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4361 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4362 | else if (PyString_Check(x)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4363 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4364 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4365 | /* wrong return value */ |
| 4366 | PyErr_SetString(PyExc_TypeError, |
| 4367 | "character mapping must return integer, None or str"); |
| 4368 | Py_DECREF(x); |
| 4369 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4370 | } |
| 4371 | } |
| 4372 | |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4373 | static int |
| 4374 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
| 4375 | { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4376 | Py_ssize_t outsize = PyString_GET_SIZE(*outobj); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4377 | /* exponentially overallocate to minimize reallocations */ |
| 4378 | if (requiredsize < 2*outsize) |
| 4379 | requiredsize = 2*outsize; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4380 | if (_PyString_Resize(outobj, requiredsize)) { |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4381 | return 0; |
| 4382 | } |
| 4383 | return 1; |
| 4384 | } |
| 4385 | |
| 4386 | typedef enum charmapencode_result { |
| 4387 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
| 4388 | }charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4389 | /* lookup the character, put the result in the output string and adjust |
| 4390 | various state variables. Reallocate the output string if not enough |
| 4391 | space is available. Return a new reference to the object that |
| 4392 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 4393 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 4394 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4395 | static |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4396 | charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4397 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4398 | { |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4399 | PyObject *rep; |
| 4400 | char *outstart; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4401 | Py_ssize_t outsize = PyString_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4402 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4403 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4404 | int res = encoding_map_lookup(c, mapping); |
| 4405 | Py_ssize_t requiredsize = *outpos+1; |
| 4406 | if (res == -1) |
| 4407 | return enc_FAILED; |
| 4408 | if (outsize<requiredsize) |
| 4409 | if (!charmapencode_resize(outobj, outpos, requiredsize)) |
| 4410 | return enc_EXCEPTION; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4411 | outstart = PyString_AS_STRING(*outobj); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4412 | outstart[(*outpos)++] = (char)res; |
| 4413 | return enc_SUCCESS; |
| 4414 | } |
| 4415 | |
| 4416 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4417 | if (rep==NULL) |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4418 | return enc_EXCEPTION; |
| 4419 | else if (rep==Py_None) { |
| 4420 | Py_DECREF(rep); |
| 4421 | return enc_FAILED; |
| 4422 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4423 | if (PyInt_Check(rep)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4424 | Py_ssize_t requiredsize = *outpos+1; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4425 | if (outsize<requiredsize) |
| 4426 | if (!charmapencode_resize(outobj, outpos, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4427 | Py_DECREF(rep); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4428 | return enc_EXCEPTION; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4429 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4430 | outstart = PyString_AS_STRING(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4431 | outstart[(*outpos)++] = (char)PyInt_AS_LONG(rep); |
| 4432 | } |
| 4433 | else { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4434 | const char *repchars = PyString_AS_STRING(rep); |
| 4435 | Py_ssize_t repsize = PyString_GET_SIZE(rep); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4436 | Py_ssize_t requiredsize = *outpos+repsize; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4437 | if (outsize<requiredsize) |
| 4438 | if (!charmapencode_resize(outobj, outpos, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4439 | Py_DECREF(rep); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4440 | return enc_EXCEPTION; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4441 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4442 | outstart = PyString_AS_STRING(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4443 | memcpy(outstart + *outpos, repchars, repsize); |
| 4444 | *outpos += repsize; |
| 4445 | } |
| 4446 | } |
Georg Brandl | 9f16760 | 2006-06-04 21:46:16 +0000 | [diff] [blame] | 4447 | Py_DECREF(rep); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4448 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4449 | } |
| 4450 | |
| 4451 | /* handle an error in PyUnicode_EncodeCharmap |
| 4452 | Return 0 on success, -1 on error */ |
| 4453 | static |
| 4454 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4455 | 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] | 4456 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4457 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4458 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4459 | { |
| 4460 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4461 | Py_ssize_t repsize; |
| 4462 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4463 | Py_UNICODE *uni2; |
| 4464 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4465 | Py_ssize_t collstartpos = *inpos; |
| 4466 | Py_ssize_t collendpos = *inpos+1; |
| 4467 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4468 | char *encoding = "charmap"; |
| 4469 | char *reason = "character maps to <undefined>"; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4470 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4471 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4472 | /* find all unencodable characters */ |
| 4473 | while (collendpos < size) { |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4474 | PyObject *rep; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4475 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4476 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 4477 | if (res != -1) |
| 4478 | break; |
| 4479 | ++collendpos; |
| 4480 | continue; |
| 4481 | } |
| 4482 | |
| 4483 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 4484 | if (rep==NULL) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4485 | return -1; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4486 | else if (rep!=Py_None) { |
| 4487 | Py_DECREF(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4488 | break; |
| 4489 | } |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4490 | Py_DECREF(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4491 | ++collendpos; |
| 4492 | } |
| 4493 | /* cache callback name lookup |
| 4494 | * (if not done yet, i.e. it's the first error) */ |
| 4495 | if (*known_errorHandler==-1) { |
| 4496 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4497 | *known_errorHandler = 1; |
| 4498 | else if (!strcmp(errors, "replace")) |
| 4499 | *known_errorHandler = 2; |
| 4500 | else if (!strcmp(errors, "ignore")) |
| 4501 | *known_errorHandler = 3; |
| 4502 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4503 | *known_errorHandler = 4; |
| 4504 | else |
| 4505 | *known_errorHandler = 0; |
| 4506 | } |
| 4507 | switch (*known_errorHandler) { |
| 4508 | case 1: /* strict */ |
| 4509 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4510 | return -1; |
| 4511 | case 2: /* replace */ |
| 4512 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
| 4513 | x = charmapencode_output('?', mapping, res, respos); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4514 | if (x==enc_EXCEPTION) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4515 | return -1; |
| 4516 | } |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4517 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4518 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4519 | return -1; |
| 4520 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4521 | } |
| 4522 | /* fall through */ |
| 4523 | case 3: /* ignore */ |
| 4524 | *inpos = collendpos; |
| 4525 | break; |
| 4526 | case 4: /* xmlcharrefreplace */ |
| 4527 | /* generate replacement (temporarily (mis)uses p) */ |
| 4528 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
| 4529 | char buffer[2+29+1+1]; |
| 4530 | char *cp; |
| 4531 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 4532 | for (cp = buffer; *cp; ++cp) { |
| 4533 | x = charmapencode_output(*cp, mapping, res, respos); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4534 | if (x==enc_EXCEPTION) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4535 | return -1; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4536 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4537 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4538 | return -1; |
| 4539 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4540 | } |
| 4541 | } |
| 4542 | *inpos = collendpos; |
| 4543 | break; |
| 4544 | default: |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4545 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4546 | encoding, reason, p, size, exceptionObject, |
| 4547 | collstartpos, collendpos, &newpos); |
| 4548 | if (repunicode == NULL) |
| 4549 | return -1; |
| 4550 | /* generate replacement */ |
| 4551 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4552 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 4553 | x = charmapencode_output(*uni2, mapping, res, respos); |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4554 | if (x==enc_EXCEPTION) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4555 | return -1; |
| 4556 | } |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4557 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4558 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4559 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4560 | return -1; |
| 4561 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4562 | } |
| 4563 | *inpos = newpos; |
| 4564 | Py_DECREF(repunicode); |
| 4565 | } |
| 4566 | return 0; |
| 4567 | } |
| 4568 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4569 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4570 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4571 | PyObject *mapping, |
| 4572 | const char *errors) |
| 4573 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4574 | /* output object */ |
| 4575 | PyObject *res = NULL; |
| 4576 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4577 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4578 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4579 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4580 | PyObject *errorHandler = NULL; |
| 4581 | PyObject *exc = NULL; |
| 4582 | /* the following variable is used for caching string comparisons |
| 4583 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 4584 | * 3=ignore, 4=xmlcharrefreplace */ |
| 4585 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4586 | |
| 4587 | /* Default to Latin-1 */ |
| 4588 | if (mapping == NULL) |
| 4589 | return PyUnicode_EncodeLatin1(p, size, errors); |
| 4590 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4591 | /* allocate enough for a simple encoding without |
| 4592 | replacements, if we need more, we'll resize */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4593 | res = PyString_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4594 | if (res == NULL) |
| 4595 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 4596 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4597 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4598 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4599 | while (inpos<size) { |
| 4600 | /* try to encode it */ |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4601 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 4602 | if (x==enc_EXCEPTION) /* error */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4603 | goto onError; |
Martin v. Löwis | 3f76779 | 2006-06-04 19:36:28 +0000 | [diff] [blame] | 4604 | if (x==enc_FAILED) { /* unencodable character */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4605 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 4606 | &exc, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4607 | &known_errorHandler, &errorHandler, errors, |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 4608 | &res, &respos)) { |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 4609 | goto onError; |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 4610 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4611 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4612 | else |
| 4613 | /* done with this character => adjust input position */ |
| 4614 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4615 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4616 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4617 | /* Resize if we allocated to much */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 4618 | if (respos<PyString_GET_SIZE(res)) { |
| 4619 | if (_PyString_Resize(&res, respos)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4620 | goto onError; |
| 4621 | } |
| 4622 | Py_XDECREF(exc); |
| 4623 | Py_XDECREF(errorHandler); |
| 4624 | return res; |
| 4625 | |
| 4626 | onError: |
| 4627 | Py_XDECREF(res); |
| 4628 | Py_XDECREF(exc); |
| 4629 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4630 | return NULL; |
| 4631 | } |
| 4632 | |
| 4633 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
| 4634 | PyObject *mapping) |
| 4635 | { |
| 4636 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
| 4637 | PyErr_BadArgument(); |
| 4638 | return NULL; |
| 4639 | } |
| 4640 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
| 4641 | PyUnicode_GET_SIZE(unicode), |
| 4642 | mapping, |
| 4643 | NULL); |
| 4644 | } |
| 4645 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4646 | /* create or adjust a UnicodeTranslateError */ |
| 4647 | static void make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4648 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4649 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4650 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4651 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4652 | if (*exceptionObject == NULL) { |
| 4653 | *exceptionObject = PyUnicodeTranslateError_Create( |
| 4654 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4655 | } |
| 4656 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4657 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 4658 | goto onError; |
| 4659 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 4660 | goto onError; |
| 4661 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 4662 | goto onError; |
| 4663 | return; |
| 4664 | onError: |
| 4665 | Py_DECREF(*exceptionObject); |
| 4666 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4667 | } |
| 4668 | } |
| 4669 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4670 | /* raises a UnicodeTranslateError */ |
| 4671 | static void raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4672 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4673 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4674 | const char *reason) |
| 4675 | { |
| 4676 | make_translate_exception(exceptionObject, |
| 4677 | unicode, size, startpos, endpos, reason); |
| 4678 | if (*exceptionObject != NULL) |
| 4679 | PyCodec_StrictErrors(*exceptionObject); |
| 4680 | } |
| 4681 | |
| 4682 | /* error handling callback helper: |
| 4683 | build arguments, call the callback and check the arguments, |
| 4684 | put the result into newpos and return the replacement string, which |
| 4685 | has to be freed by the caller */ |
| 4686 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
| 4687 | PyObject **errorHandler, |
| 4688 | const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4689 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 4690 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4691 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4692 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4693 | 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] | 4694 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 4695 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4696 | PyObject *restuple; |
| 4697 | PyObject *resunicode; |
| 4698 | |
| 4699 | if (*errorHandler == NULL) { |
| 4700 | *errorHandler = PyCodec_LookupError(errors); |
| 4701 | if (*errorHandler == NULL) |
| 4702 | return NULL; |
| 4703 | } |
| 4704 | |
| 4705 | make_translate_exception(exceptionObject, |
| 4706 | unicode, size, startpos, endpos, reason); |
| 4707 | if (*exceptionObject == NULL) |
| 4708 | return NULL; |
| 4709 | |
| 4710 | restuple = PyObject_CallFunctionObjArgs( |
| 4711 | *errorHandler, *exceptionObject, NULL); |
| 4712 | if (restuple == NULL) |
| 4713 | return NULL; |
| 4714 | if (!PyTuple_Check(restuple)) { |
| 4715 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 4716 | Py_DECREF(restuple); |
| 4717 | return NULL; |
| 4718 | } |
| 4719 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4720 | &resunicode, &i_newpos)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4721 | Py_DECREF(restuple); |
| 4722 | return NULL; |
| 4723 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4724 | if (i_newpos<0) |
| 4725 | *newpos = size+i_newpos; |
| 4726 | else |
| 4727 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4728 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 4729 | 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] | 4730 | Py_DECREF(restuple); |
| 4731 | return NULL; |
| 4732 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4733 | Py_INCREF(resunicode); |
| 4734 | Py_DECREF(restuple); |
| 4735 | return resunicode; |
| 4736 | } |
| 4737 | |
| 4738 | /* Lookup the character ch in the mapping and put the result in result, |
| 4739 | which must be decrefed by the caller. |
| 4740 | Return 0 on success, -1 on error */ |
| 4741 | static |
| 4742 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 4743 | { |
| 4744 | PyObject *w = PyInt_FromLong((long)c); |
| 4745 | PyObject *x; |
| 4746 | |
| 4747 | if (w == NULL) |
| 4748 | return -1; |
| 4749 | x = PyObject_GetItem(mapping, w); |
| 4750 | Py_DECREF(w); |
| 4751 | if (x == NULL) { |
| 4752 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4753 | /* No mapping found means: use 1:1 mapping. */ |
| 4754 | PyErr_Clear(); |
| 4755 | *result = NULL; |
| 4756 | return 0; |
| 4757 | } else |
| 4758 | return -1; |
| 4759 | } |
| 4760 | else if (x == Py_None) { |
| 4761 | *result = x; |
| 4762 | return 0; |
| 4763 | } |
| 4764 | else if (PyInt_Check(x)) { |
| 4765 | long value = PyInt_AS_LONG(x); |
| 4766 | long max = PyUnicode_GetMax(); |
| 4767 | if (value < 0 || value > max) { |
| 4768 | PyErr_Format(PyExc_TypeError, |
| 4769 | "character mapping must be in range(0x%lx)", max+1); |
| 4770 | Py_DECREF(x); |
| 4771 | return -1; |
| 4772 | } |
| 4773 | *result = x; |
| 4774 | return 0; |
| 4775 | } |
| 4776 | else if (PyUnicode_Check(x)) { |
| 4777 | *result = x; |
| 4778 | return 0; |
| 4779 | } |
| 4780 | else { |
| 4781 | /* wrong return value */ |
| 4782 | PyErr_SetString(PyExc_TypeError, |
| 4783 | "character mapping must return integer, None or unicode"); |
Walter Dörwald | 150523e | 2003-08-15 16:52:19 +0000 | [diff] [blame] | 4784 | Py_DECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4785 | return -1; |
| 4786 | } |
| 4787 | } |
| 4788 | /* ensure that *outobj is at least requiredsize characters long, |
| 4789 | if not reallocate and adjust various state variables. |
| 4790 | Return 0 on success, -1 on error */ |
| 4791 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4792 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4793 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4794 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4795 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4796 | if (requiredsize > oldsize) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4797 | /* remember old output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4798 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4799 | /* exponentially overallocate to minimize reallocations */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4800 | if (requiredsize < 2 * oldsize) |
| 4801 | requiredsize = 2 * oldsize; |
Alexandre Vassalotti | 034e08c | 2008-12-27 06:36:10 +0000 | [diff] [blame] | 4802 | if (PyUnicode_Resize(outobj, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4803 | return -1; |
| 4804 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4805 | } |
| 4806 | return 0; |
| 4807 | } |
| 4808 | /* lookup the character, put the result in the output string and adjust |
| 4809 | various state variables. Return a new reference to the object that |
| 4810 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 4811 | undefined (in which case no character was written). |
| 4812 | The called must decref result. |
| 4813 | Return 0 on success, -1 on error. */ |
| 4814 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4815 | 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] | 4816 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4817 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4818 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4819 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4820 | return -1; |
| 4821 | if (*res==NULL) { |
| 4822 | /* not found => default to 1:1 mapping */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4823 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4824 | } |
| 4825 | else if (*res==Py_None) |
| 4826 | ; |
| 4827 | else if (PyInt_Check(*res)) { |
| 4828 | /* no overflow check, because we know that the space is enough */ |
| 4829 | *(*outp)++ = (Py_UNICODE)PyInt_AS_LONG(*res); |
| 4830 | } |
| 4831 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4832 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4833 | if (repsize==1) { |
| 4834 | /* no overflow check, because we know that the space is enough */ |
| 4835 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 4836 | } |
| 4837 | else if (repsize!=0) { |
| 4838 | /* more than one character */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4839 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
Walter Dörwald | cd736e7 | 2004-02-05 17:36:00 +0000 | [diff] [blame] | 4840 | (insize - (curinp-startinp)) + |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4841 | repsize - 1; |
| 4842 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4843 | return -1; |
| 4844 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 4845 | *outp += repsize; |
| 4846 | } |
| 4847 | } |
| 4848 | else |
| 4849 | return -1; |
| 4850 | return 0; |
| 4851 | } |
| 4852 | |
| 4853 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4854 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4855 | PyObject *mapping, |
| 4856 | const char *errors) |
| 4857 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4858 | /* output object */ |
| 4859 | PyObject *res = NULL; |
| 4860 | /* pointers to the beginning and end+1 of input */ |
| 4861 | const Py_UNICODE *startp = p; |
| 4862 | const Py_UNICODE *endp = p + size; |
| 4863 | /* pointer into the output */ |
| 4864 | Py_UNICODE *str; |
| 4865 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4866 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4867 | char *reason = "character maps to <undefined>"; |
| 4868 | PyObject *errorHandler = NULL; |
| 4869 | PyObject *exc = NULL; |
| 4870 | /* the following variable is used for caching string comparisons |
| 4871 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 4872 | * 3=ignore, 4=xmlcharrefreplace */ |
| 4873 | int known_errorHandler = -1; |
| 4874 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4875 | if (mapping == NULL) { |
| 4876 | PyErr_BadArgument(); |
| 4877 | return NULL; |
| 4878 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4879 | |
| 4880 | /* allocate enough for a simple 1:1 translation without |
| 4881 | replacements, if we need more, we'll resize */ |
| 4882 | res = PyUnicode_FromUnicode(NULL, size); |
| 4883 | if (res == NULL) |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4884 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4885 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4886 | return res; |
| 4887 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4888 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4889 | while (p<endp) { |
| 4890 | /* try to encode it */ |
| 4891 | PyObject *x = NULL; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4892 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4893 | Py_XDECREF(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4894 | goto onError; |
| 4895 | } |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 4896 | Py_XDECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4897 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 4898 | ++p; |
| 4899 | else { /* untranslatable character */ |
| 4900 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4901 | Py_ssize_t repsize; |
| 4902 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4903 | Py_UNICODE *uni2; |
| 4904 | /* startpos for collecting untranslatable chars */ |
| 4905 | const Py_UNICODE *collstart = p; |
| 4906 | const Py_UNICODE *collend = p+1; |
| 4907 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4908 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4909 | /* find all untranslatable characters */ |
| 4910 | while (collend < endp) { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4911 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4912 | goto onError; |
| 4913 | Py_XDECREF(x); |
| 4914 | if (x!=Py_None) |
| 4915 | break; |
| 4916 | ++collend; |
| 4917 | } |
| 4918 | /* cache callback name lookup |
| 4919 | * (if not done yet, i.e. it's the first error) */ |
| 4920 | if (known_errorHandler==-1) { |
| 4921 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4922 | known_errorHandler = 1; |
| 4923 | else if (!strcmp(errors, "replace")) |
| 4924 | known_errorHandler = 2; |
| 4925 | else if (!strcmp(errors, "ignore")) |
| 4926 | known_errorHandler = 3; |
| 4927 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4928 | known_errorHandler = 4; |
| 4929 | else |
| 4930 | known_errorHandler = 0; |
| 4931 | } |
| 4932 | switch (known_errorHandler) { |
| 4933 | case 1: /* strict */ |
| 4934 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
| 4935 | goto onError; |
| 4936 | case 2: /* replace */ |
| 4937 | /* No need to check for space, this is a 1:1 replacement */ |
| 4938 | for (coll = collstart; coll<collend; ++coll) |
| 4939 | *str++ = '?'; |
| 4940 | /* fall through */ |
| 4941 | case 3: /* ignore */ |
| 4942 | p = collend; |
| 4943 | break; |
| 4944 | case 4: /* xmlcharrefreplace */ |
| 4945 | /* generate replacement (temporarily (mis)uses p) */ |
| 4946 | for (p = collstart; p < collend; ++p) { |
| 4947 | char buffer[2+29+1+1]; |
| 4948 | char *cp; |
| 4949 | sprintf(buffer, "&#%d;", (int)*p); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4950 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4951 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 4952 | goto onError; |
| 4953 | for (cp = buffer; *cp; ++cp) |
| 4954 | *str++ = *cp; |
| 4955 | } |
| 4956 | p = collend; |
| 4957 | break; |
| 4958 | default: |
| 4959 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 4960 | reason, startp, size, &exc, |
| 4961 | collstart-startp, collend-startp, &newpos); |
| 4962 | if (repunicode == NULL) |
| 4963 | goto onError; |
| 4964 | /* generate replacement */ |
| 4965 | repsize = PyUnicode_GET_SIZE(repunicode); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4966 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4967 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 4968 | Py_DECREF(repunicode); |
| 4969 | goto onError; |
| 4970 | } |
| 4971 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 4972 | *str++ = *uni2; |
| 4973 | p = startp + newpos; |
| 4974 | Py_DECREF(repunicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4975 | } |
| 4976 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4977 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4978 | /* Resize if we allocated to much */ |
| 4979 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 4980 | if (respos<PyUnicode_GET_SIZE(res)) { |
Alexandre Vassalotti | 034e08c | 2008-12-27 06:36:10 +0000 | [diff] [blame] | 4981 | if (PyUnicode_Resize(&res, respos) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 4982 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4983 | } |
| 4984 | Py_XDECREF(exc); |
| 4985 | Py_XDECREF(errorHandler); |
| 4986 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4987 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4988 | onError: |
| 4989 | Py_XDECREF(res); |
| 4990 | Py_XDECREF(exc); |
| 4991 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4992 | return NULL; |
| 4993 | } |
| 4994 | |
| 4995 | PyObject *PyUnicode_Translate(PyObject *str, |
| 4996 | PyObject *mapping, |
| 4997 | const char *errors) |
| 4998 | { |
| 4999 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5000 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5001 | str = PyUnicode_FromObject(str); |
| 5002 | if (str == NULL) |
| 5003 | goto onError; |
| 5004 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
| 5005 | PyUnicode_GET_SIZE(str), |
| 5006 | mapping, |
| 5007 | errors); |
| 5008 | Py_DECREF(str); |
| 5009 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5010 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5011 | onError: |
| 5012 | Py_XDECREF(str); |
| 5013 | return NULL; |
| 5014 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5015 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5016 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 5017 | |
| 5018 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5019 | Py_ssize_t length, |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5020 | char *output, |
| 5021 | const char *errors) |
| 5022 | { |
| 5023 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5024 | PyObject *errorHandler = NULL; |
| 5025 | PyObject *exc = NULL; |
| 5026 | const char *encoding = "decimal"; |
| 5027 | const char *reason = "invalid decimal Unicode string"; |
| 5028 | /* the following variable is used for caching string comparisons |
| 5029 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 5030 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5031 | |
| 5032 | if (output == NULL) { |
| 5033 | PyErr_BadArgument(); |
| 5034 | return -1; |
| 5035 | } |
| 5036 | |
| 5037 | p = s; |
| 5038 | end = s + length; |
| 5039 | while (p < end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5040 | register Py_UNICODE ch = *p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5041 | int decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5042 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5043 | Py_ssize_t repsize; |
| 5044 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5045 | Py_UNICODE *uni2; |
| 5046 | Py_UNICODE *collstart; |
| 5047 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5048 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5049 | if (Py_UNICODE_ISSPACE(ch)) { |
| 5050 | *output++ = ' '; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5051 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5052 | continue; |
| 5053 | } |
| 5054 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5055 | if (decimal >= 0) { |
| 5056 | *output++ = '0' + decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5057 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5058 | continue; |
| 5059 | } |
Guido van Rossum | ba47704 | 2000-04-06 18:18:10 +0000 | [diff] [blame] | 5060 | if (0 < ch && ch < 256) { |
Guido van Rossum | 42c29aa | 2000-05-03 23:58:29 +0000 | [diff] [blame] | 5061 | *output++ = (char)ch; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5062 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5063 | continue; |
| 5064 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5065 | /* All other characters are considered unencodable */ |
| 5066 | collstart = p; |
| 5067 | collend = p+1; |
| 5068 | while (collend < end) { |
| 5069 | if ((0 < *collend && *collend < 256) || |
| 5070 | !Py_UNICODE_ISSPACE(*collend) || |
| 5071 | Py_UNICODE_TODECIMAL(*collend)) |
| 5072 | break; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5073 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5074 | /* cache callback name lookup |
| 5075 | * (if not done yet, i.e. it's the first error) */ |
| 5076 | if (known_errorHandler==-1) { |
| 5077 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5078 | known_errorHandler = 1; |
| 5079 | else if (!strcmp(errors, "replace")) |
| 5080 | known_errorHandler = 2; |
| 5081 | else if (!strcmp(errors, "ignore")) |
| 5082 | known_errorHandler = 3; |
| 5083 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5084 | known_errorHandler = 4; |
| 5085 | else |
| 5086 | known_errorHandler = 0; |
| 5087 | } |
| 5088 | switch (known_errorHandler) { |
| 5089 | case 1: /* strict */ |
| 5090 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 5091 | goto onError; |
| 5092 | case 2: /* replace */ |
| 5093 | for (p = collstart; p < collend; ++p) |
| 5094 | *output++ = '?'; |
| 5095 | /* fall through */ |
| 5096 | case 3: /* ignore */ |
| 5097 | p = collend; |
| 5098 | break; |
| 5099 | case 4: /* xmlcharrefreplace */ |
| 5100 | /* generate replacement (temporarily (mis)uses p) */ |
| 5101 | for (p = collstart; p < collend; ++p) |
| 5102 | output += sprintf(output, "&#%d;", (int)*p); |
| 5103 | p = collend; |
| 5104 | break; |
| 5105 | default: |
| 5106 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 5107 | encoding, reason, s, length, &exc, |
| 5108 | collstart-s, collend-s, &newpos); |
| 5109 | if (repunicode == NULL) |
| 5110 | goto onError; |
| 5111 | /* generate replacement */ |
| 5112 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5113 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 5114 | Py_UNICODE ch = *uni2; |
| 5115 | if (Py_UNICODE_ISSPACE(ch)) |
| 5116 | *output++ = ' '; |
| 5117 | else { |
| 5118 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5119 | if (decimal >= 0) |
| 5120 | *output++ = '0' + decimal; |
| 5121 | else if (0 < ch && ch < 256) |
| 5122 | *output++ = (char)ch; |
| 5123 | else { |
| 5124 | Py_DECREF(repunicode); |
| 5125 | raise_encode_exception(&exc, encoding, |
| 5126 | s, length, collstart-s, collend-s, reason); |
| 5127 | goto onError; |
| 5128 | } |
| 5129 | } |
| 5130 | } |
| 5131 | p = s + newpos; |
| 5132 | Py_DECREF(repunicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5133 | } |
| 5134 | } |
| 5135 | /* 0-terminate the output string */ |
| 5136 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5137 | Py_XDECREF(exc); |
| 5138 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5139 | return 0; |
| 5140 | |
| 5141 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5142 | Py_XDECREF(exc); |
| 5143 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5144 | return -1; |
| 5145 | } |
| 5146 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5147 | /* --- Helpers ------------------------------------------------------------ */ |
| 5148 | |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 5149 | #include "stringlib/unicodedefs.h" |
Fredrik Lundh | 6471ee4 | 2006-05-24 14:28:11 +0000 | [diff] [blame] | 5150 | |
Facundo Batista | 6f7e6fb | 2007-11-16 19:16:15 +0000 | [diff] [blame] | 5151 | #define FROM_UNICODE |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 5152 | |
Fredrik Lundh | a50d201 | 2006-05-26 17:04:58 +0000 | [diff] [blame] | 5153 | #include "stringlib/fastsearch.h" |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5154 | |
| 5155 | #include "stringlib/count.h" |
| 5156 | #include "stringlib/find.h" |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 5157 | #include "stringlib/partition.h" |
| 5158 | |
Fredrik Lundh | c816281 | 2006-05-26 19:33:03 +0000 | [diff] [blame] | 5159 | /* helper macro to fixup start/end slice values */ |
| 5160 | #define FIX_START_END(obj) \ |
| 5161 | if (start < 0) \ |
| 5162 | start += (obj)->length; \ |
| 5163 | if (start < 0) \ |
| 5164 | start = 0; \ |
| 5165 | if (end > (obj)->length) \ |
| 5166 | end = (obj)->length; \ |
| 5167 | if (end < 0) \ |
| 5168 | end += (obj)->length; \ |
| 5169 | if (end < 0) \ |
| 5170 | end = 0; |
| 5171 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5172 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5173 | PyObject *substr, |
| 5174 | Py_ssize_t start, |
| 5175 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5176 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5177 | Py_ssize_t result; |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5178 | PyUnicodeObject* str_obj; |
| 5179 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5180 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5181 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 5182 | if (!str_obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5183 | return -1; |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5184 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 5185 | if (!sub_obj) { |
| 5186 | Py_DECREF(str_obj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5187 | return -1; |
| 5188 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5189 | |
Fredrik Lundh | c816281 | 2006-05-26 19:33:03 +0000 | [diff] [blame] | 5190 | FIX_START_END(str_obj); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5191 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 5192 | result = stringlib_count( |
| 5193 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length |
| 5194 | ); |
| 5195 | |
| 5196 | Py_DECREF(sub_obj); |
| 5197 | Py_DECREF(str_obj); |
| 5198 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5199 | return result; |
| 5200 | } |
| 5201 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5202 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5203 | PyObject *sub, |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 5204 | Py_ssize_t start, |
| 5205 | Py_ssize_t end, |
| 5206 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5207 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5208 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5209 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5210 | str = PyUnicode_FromObject(str); |
| 5211 | if (!str) |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 5212 | return -2; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5213 | sub = PyUnicode_FromObject(sub); |
| 5214 | if (!sub) { |
| 5215 | Py_DECREF(str); |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 5216 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5217 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5218 | |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 5219 | if (direction > 0) |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 5220 | result = stringlib_find_slice( |
| 5221 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5222 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5223 | start, end |
| 5224 | ); |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 5225 | else |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 5226 | result = stringlib_rfind_slice( |
| 5227 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5228 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5229 | start, end |
| 5230 | ); |
Fredrik Lundh | ce4eccb | 2006-05-26 19:29:05 +0000 | [diff] [blame] | 5231 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 5232 | Py_DECREF(str); |
| 5233 | Py_DECREF(sub); |
| 5234 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5235 | return result; |
| 5236 | } |
| 5237 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5238 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5239 | int tailmatch(PyUnicodeObject *self, |
| 5240 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5241 | Py_ssize_t start, |
| 5242 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5243 | int direction) |
| 5244 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5245 | if (substring->length == 0) |
| 5246 | return 1; |
| 5247 | |
Fredrik Lundh | c816281 | 2006-05-26 19:33:03 +0000 | [diff] [blame] | 5248 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5249 | |
| 5250 | end -= substring->length; |
| 5251 | if (end < start) |
| 5252 | return 0; |
| 5253 | |
| 5254 | if (direction > 0) { |
| 5255 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 5256 | return 1; |
| 5257 | } else { |
| 5258 | if (Py_UNICODE_MATCH(self, start, substring)) |
| 5259 | return 1; |
| 5260 | } |
| 5261 | |
| 5262 | return 0; |
| 5263 | } |
| 5264 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5265 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5266 | PyObject *substr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5267 | Py_ssize_t start, |
| 5268 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5269 | int direction) |
| 5270 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5271 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5272 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5273 | str = PyUnicode_FromObject(str); |
| 5274 | if (str == NULL) |
| 5275 | return -1; |
| 5276 | substr = PyUnicode_FromObject(substr); |
| 5277 | if (substr == NULL) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5278 | Py_DECREF(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5279 | return -1; |
| 5280 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5281 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5282 | result = tailmatch((PyUnicodeObject *)str, |
| 5283 | (PyUnicodeObject *)substr, |
| 5284 | start, end, direction); |
| 5285 | Py_DECREF(str); |
| 5286 | Py_DECREF(substr); |
| 5287 | return result; |
| 5288 | } |
| 5289 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5290 | /* Apply fixfct filter to the Unicode object self and return a |
| 5291 | reference to the modified object */ |
| 5292 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5293 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5294 | PyObject *fixup(PyUnicodeObject *self, |
| 5295 | int (*fixfct)(PyUnicodeObject *s)) |
| 5296 | { |
| 5297 | |
| 5298 | PyUnicodeObject *u; |
| 5299 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5300 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5301 | if (u == NULL) |
| 5302 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5303 | |
| 5304 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5305 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5306 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5307 | /* fixfct should return TRUE if it modified the buffer. If |
| 5308 | FALSE, return a reference to the original buffer instead |
| 5309 | (to save space, not time) */ |
| 5310 | Py_INCREF(self); |
| 5311 | Py_DECREF(u); |
| 5312 | return (PyObject*) self; |
| 5313 | } |
| 5314 | return (PyObject*) u; |
| 5315 | } |
| 5316 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5317 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5318 | int fixupper(PyUnicodeObject *self) |
| 5319 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5320 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5321 | Py_UNICODE *s = self->str; |
| 5322 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5323 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5324 | while (len-- > 0) { |
| 5325 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5326 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5327 | ch = Py_UNICODE_TOUPPER(*s); |
| 5328 | if (ch != *s) { |
| 5329 | status = 1; |
| 5330 | *s = ch; |
| 5331 | } |
| 5332 | s++; |
| 5333 | } |
| 5334 | |
| 5335 | return status; |
| 5336 | } |
| 5337 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5338 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5339 | int fixlower(PyUnicodeObject *self) |
| 5340 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5341 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5342 | Py_UNICODE *s = self->str; |
| 5343 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5344 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5345 | while (len-- > 0) { |
| 5346 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5347 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5348 | ch = Py_UNICODE_TOLOWER(*s); |
| 5349 | if (ch != *s) { |
| 5350 | status = 1; |
| 5351 | *s = ch; |
| 5352 | } |
| 5353 | s++; |
| 5354 | } |
| 5355 | |
| 5356 | return status; |
| 5357 | } |
| 5358 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5359 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5360 | int fixswapcase(PyUnicodeObject *self) |
| 5361 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5362 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5363 | Py_UNICODE *s = self->str; |
| 5364 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5365 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5366 | while (len-- > 0) { |
| 5367 | if (Py_UNICODE_ISUPPER(*s)) { |
| 5368 | *s = Py_UNICODE_TOLOWER(*s); |
| 5369 | status = 1; |
| 5370 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 5371 | *s = Py_UNICODE_TOUPPER(*s); |
| 5372 | status = 1; |
| 5373 | } |
| 5374 | s++; |
| 5375 | } |
| 5376 | |
| 5377 | return status; |
| 5378 | } |
| 5379 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5380 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5381 | int fixcapitalize(PyUnicodeObject *self) |
| 5382 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5383 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5384 | Py_UNICODE *s = self->str; |
| 5385 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5386 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5387 | if (len == 0) |
| 5388 | return 0; |
| 5389 | if (Py_UNICODE_ISLOWER(*s)) { |
| 5390 | *s = Py_UNICODE_TOUPPER(*s); |
| 5391 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5392 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5393 | s++; |
| 5394 | while (--len > 0) { |
| 5395 | if (Py_UNICODE_ISUPPER(*s)) { |
| 5396 | *s = Py_UNICODE_TOLOWER(*s); |
| 5397 | status = 1; |
| 5398 | } |
| 5399 | s++; |
| 5400 | } |
| 5401 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5402 | } |
| 5403 | |
| 5404 | static |
| 5405 | int fixtitle(PyUnicodeObject *self) |
| 5406 | { |
| 5407 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5408 | register Py_UNICODE *e; |
| 5409 | int previous_is_cased; |
| 5410 | |
| 5411 | /* Shortcut for single character strings */ |
| 5412 | if (PyUnicode_GET_SIZE(self) == 1) { |
| 5413 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 5414 | if (*p != ch) { |
| 5415 | *p = ch; |
| 5416 | return 1; |
| 5417 | } |
| 5418 | else |
| 5419 | return 0; |
| 5420 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5421 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5422 | e = p + PyUnicode_GET_SIZE(self); |
| 5423 | previous_is_cased = 0; |
| 5424 | for (; p < e; p++) { |
| 5425 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5426 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5427 | if (previous_is_cased) |
| 5428 | *p = Py_UNICODE_TOLOWER(ch); |
| 5429 | else |
| 5430 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5431 | |
| 5432 | if (Py_UNICODE_ISLOWER(ch) || |
| 5433 | Py_UNICODE_ISUPPER(ch) || |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5434 | Py_UNICODE_ISTITLE(ch)) |
| 5435 | previous_is_cased = 1; |
| 5436 | else |
| 5437 | previous_is_cased = 0; |
| 5438 | } |
| 5439 | return 1; |
| 5440 | } |
| 5441 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5442 | PyObject * |
| 5443 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5444 | { |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5445 | PyObject *internal_separator = NULL; |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 5446 | const Py_UNICODE blank = ' '; |
| 5447 | const Py_UNICODE *sep = ␣ |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 5448 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5449 | PyUnicodeObject *res = NULL; /* the result */ |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 5450 | Py_ssize_t res_alloc = 100; /* # allocated bytes for string in res */ |
| 5451 | Py_ssize_t res_used; /* # used bytes */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5452 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 5453 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5454 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5455 | PyObject *item; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5456 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5457 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5458 | fseq = PySequence_Fast(seq, ""); |
| 5459 | if (fseq == NULL) { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5460 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5461 | } |
| 5462 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5463 | /* Grrrr. A codec may be invoked to convert str objects to |
| 5464 | * Unicode, and so it's possible to call back into Python code |
| 5465 | * during PyUnicode_FromObject(), and so it's possible for a sick |
| 5466 | * codec to change the size of fseq (if seq is a list). Therefore |
| 5467 | * we have to keep refetching the size -- can't assume seqlen |
| 5468 | * is invariant. |
| 5469 | */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5470 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 5471 | /* If empty sequence, return u"". */ |
| 5472 | if (seqlen == 0) { |
| 5473 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 5474 | goto Done; |
| 5475 | } |
| 5476 | /* If singleton sequence with an exact Unicode, return that. */ |
| 5477 | if (seqlen == 1) { |
| 5478 | item = PySequence_Fast_GET_ITEM(fseq, 0); |
| 5479 | if (PyUnicode_CheckExact(item)) { |
| 5480 | Py_INCREF(item); |
| 5481 | res = (PyUnicodeObject *)item; |
| 5482 | goto Done; |
| 5483 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5484 | } |
| 5485 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5486 | /* At least two items to join, or one that isn't exact Unicode. */ |
| 5487 | if (seqlen > 1) { |
| 5488 | /* Set up sep and seplen -- they're needed. */ |
| 5489 | if (separator == NULL) { |
| 5490 | sep = ␣ |
| 5491 | seplen = 1; |
| 5492 | } |
| 5493 | else { |
| 5494 | internal_separator = PyUnicode_FromObject(separator); |
| 5495 | if (internal_separator == NULL) |
| 5496 | goto onError; |
| 5497 | sep = PyUnicode_AS_UNICODE(internal_separator); |
| 5498 | seplen = PyUnicode_GET_SIZE(internal_separator); |
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); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5501 | } |
| 5502 | } |
| 5503 | |
| 5504 | /* Get space. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5505 | res = _PyUnicode_New(res_alloc); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5506 | if (res == NULL) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5507 | goto onError; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5508 | res_p = PyUnicode_AS_UNICODE(res); |
| 5509 | res_used = 0; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5510 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5511 | for (i = 0; i < seqlen; ++i) { |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 5512 | Py_ssize_t itemlen; |
| 5513 | Py_ssize_t new_res_used; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5514 | |
| 5515 | item = PySequence_Fast_GET_ITEM(fseq, i); |
| 5516 | /* Convert item to Unicode. */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 5517 | if (! PyUnicode_Check(item) && ! PyString_Check(item)) { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5518 | PyErr_Format(PyExc_TypeError, |
Thomas Wouters | 715a4cd | 2006-04-16 22:04:49 +0000 | [diff] [blame] | 5519 | "sequence item %zd: expected string or Unicode," |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5520 | " %.80s found", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5521 | i, Py_TYPE(item)->tp_name); |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 5522 | goto onError; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5523 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5524 | item = PyUnicode_FromObject(item); |
| 5525 | if (item == NULL) |
| 5526 | goto onError; |
| 5527 | /* We own a reference to item from here on. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5528 | |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5529 | /* In case PyUnicode_FromObject() mutated seq. */ |
| 5530 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 5531 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5532 | /* 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] | 5533 | itemlen = PyUnicode_GET_SIZE(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5534 | new_res_used = res_used + itemlen; |
Georg Brandl | 90e27d3 | 2006-06-10 06:40:50 +0000 | [diff] [blame] | 5535 | if (new_res_used < 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5536 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5537 | if (i < seqlen - 1) { |
| 5538 | new_res_used += seplen; |
Georg Brandl | 90e27d3 | 2006-06-10 06:40:50 +0000 | [diff] [blame] | 5539 | if (new_res_used < 0) |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5540 | goto Overflow; |
| 5541 | } |
| 5542 | if (new_res_used > res_alloc) { |
| 5543 | /* double allocated size until it's big enough */ |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5544 | do { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5545 | res_alloc += res_alloc; |
Tim Peters | 286085c | 2006-05-22 19:17:04 +0000 | [diff] [blame] | 5546 | if (res_alloc <= 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5547 | goto Overflow; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5548 | } while (new_res_used > res_alloc); |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5549 | if (_PyUnicode_Resize(&res, res_alloc) < 0) { |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 5550 | Py_DECREF(item); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5551 | goto onError; |
Marc-André Lemburg | 3508e30 | 2001-09-20 17:22:58 +0000 | [diff] [blame] | 5552 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5553 | res_p = PyUnicode_AS_UNICODE(res) + res_used; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5554 | } |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5555 | |
| 5556 | /* Copy item, and maybe the separator. */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5557 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5558 | res_p += itemlen; |
| 5559 | if (i < seqlen - 1) { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5560 | Py_UNICODE_COPY(res_p, sep, seplen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5561 | res_p += seplen; |
| 5562 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5563 | Py_DECREF(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5564 | res_used = new_res_used; |
| 5565 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5566 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5567 | /* Shrink res to match the used area; this probably can't fail, |
| 5568 | * but it's cheap to check. |
| 5569 | */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5570 | if (_PyUnicode_Resize(&res, res_used) < 0) |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5571 | goto onError; |
| 5572 | |
| 5573 | Done: |
| 5574 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5575 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5576 | return (PyObject *)res; |
| 5577 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5578 | Overflow: |
| 5579 | PyErr_SetString(PyExc_OverflowError, |
Georg Brandl | 90e27d3 | 2006-06-10 06:40:50 +0000 | [diff] [blame] | 5580 | "join() result is too long for a Python string"); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5581 | Py_DECREF(item); |
| 5582 | /* fall through */ |
| 5583 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5584 | onError: |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5585 | Py_XDECREF(internal_separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5586 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5587 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5588 | return NULL; |
| 5589 | } |
| 5590 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5591 | static |
| 5592 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5593 | Py_ssize_t left, |
| 5594 | Py_ssize_t right, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5595 | Py_UNICODE fill) |
| 5596 | { |
| 5597 | PyUnicodeObject *u; |
| 5598 | |
| 5599 | if (left < 0) |
| 5600 | left = 0; |
| 5601 | if (right < 0) |
| 5602 | right = 0; |
| 5603 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5604 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5605 | Py_INCREF(self); |
| 5606 | return self; |
| 5607 | } |
| 5608 | |
Neal Norwitz | e7d8be8 | 2008-07-31 17:17:14 +0000 | [diff] [blame] | 5609 | if (left > PY_SSIZE_T_MAX - self->length || |
| 5610 | right > PY_SSIZE_T_MAX - (left + self->length)) { |
| 5611 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 5612 | return NULL; |
| 5613 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5614 | u = _PyUnicode_New(left + self->length + right); |
| 5615 | if (u) { |
| 5616 | if (left) |
| 5617 | Py_UNICODE_FILL(u->str, fill, left); |
| 5618 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 5619 | if (right) |
| 5620 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 5621 | } |
| 5622 | |
| 5623 | return u; |
| 5624 | } |
| 5625 | |
| 5626 | #define SPLIT_APPEND(data, left, right) \ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5627 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5628 | if (!str) \ |
| 5629 | goto onError; \ |
| 5630 | if (PyList_Append(list, str)) { \ |
| 5631 | Py_DECREF(str); \ |
| 5632 | goto onError; \ |
| 5633 | } \ |
| 5634 | else \ |
| 5635 | Py_DECREF(str); |
| 5636 | |
| 5637 | static |
| 5638 | PyObject *split_whitespace(PyUnicodeObject *self, |
| 5639 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5640 | Py_ssize_t maxcount) |
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 = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5645 | PyObject *str; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5646 | register const Py_UNICODE *buf = self->str; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5647 | |
| 5648 | for (i = j = 0; i < len; ) { |
| 5649 | /* find a token */ |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5650 | while (i < len && Py_UNICODE_ISSPACE(buf[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5651 | i++; |
| 5652 | j = i; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5653 | while (i < len && !Py_UNICODE_ISSPACE(buf[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5654 | i++; |
| 5655 | if (j < i) { |
| 5656 | if (maxcount-- <= 0) |
| 5657 | break; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5658 | SPLIT_APPEND(buf, j, i); |
| 5659 | while (i < len && Py_UNICODE_ISSPACE(buf[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5660 | i++; |
| 5661 | j = i; |
| 5662 | } |
| 5663 | } |
| 5664 | if (j < len) { |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5665 | SPLIT_APPEND(buf, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5666 | } |
| 5667 | return list; |
| 5668 | |
| 5669 | onError: |
| 5670 | Py_DECREF(list); |
| 5671 | return NULL; |
| 5672 | } |
| 5673 | |
| 5674 | PyObject *PyUnicode_Splitlines(PyObject *string, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5675 | int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5676 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5677 | register Py_ssize_t i; |
| 5678 | register Py_ssize_t j; |
| 5679 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5680 | PyObject *list; |
| 5681 | PyObject *str; |
| 5682 | Py_UNICODE *data; |
| 5683 | |
| 5684 | string = PyUnicode_FromObject(string); |
| 5685 | if (string == NULL) |
| 5686 | return NULL; |
| 5687 | data = PyUnicode_AS_UNICODE(string); |
| 5688 | len = PyUnicode_GET_SIZE(string); |
| 5689 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5690 | list = PyList_New(0); |
| 5691 | if (!list) |
| 5692 | goto onError; |
| 5693 | |
| 5694 | for (i = j = 0; i < len; ) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5695 | Py_ssize_t eol; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5696 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5697 | /* Find a line and append it */ |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5698 | while (i < len && !BLOOM_LINEBREAK(data[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5699 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5700 | |
| 5701 | /* Skip the line break reading CRLF as one line break */ |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5702 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5703 | if (i < len) { |
| 5704 | if (data[i] == '\r' && i + 1 < len && |
| 5705 | data[i+1] == '\n') |
| 5706 | i += 2; |
| 5707 | else |
| 5708 | i++; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5709 | if (keepends) |
| 5710 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5711 | } |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5712 | SPLIT_APPEND(data, j, eol); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5713 | j = i; |
| 5714 | } |
| 5715 | if (j < len) { |
| 5716 | SPLIT_APPEND(data, j, len); |
| 5717 | } |
| 5718 | |
| 5719 | Py_DECREF(string); |
| 5720 | return list; |
| 5721 | |
| 5722 | onError: |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5723 | Py_XDECREF(list); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5724 | Py_DECREF(string); |
| 5725 | return NULL; |
| 5726 | } |
| 5727 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5728 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5729 | PyObject *split_char(PyUnicodeObject *self, |
| 5730 | PyObject *list, |
| 5731 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5732 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5733 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5734 | register Py_ssize_t i; |
| 5735 | register Py_ssize_t j; |
| 5736 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5737 | PyObject *str; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5738 | register const Py_UNICODE *buf = self->str; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5739 | |
| 5740 | for (i = j = 0; i < len; ) { |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5741 | if (buf[i] == ch) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5742 | if (maxcount-- <= 0) |
| 5743 | break; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5744 | SPLIT_APPEND(buf, j, i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5745 | i = j = i + 1; |
| 5746 | } else |
| 5747 | i++; |
| 5748 | } |
| 5749 | if (j <= len) { |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5750 | SPLIT_APPEND(buf, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5751 | } |
| 5752 | return list; |
| 5753 | |
| 5754 | onError: |
| 5755 | Py_DECREF(list); |
| 5756 | return NULL; |
| 5757 | } |
| 5758 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5759 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5760 | PyObject *split_substring(PyUnicodeObject *self, |
| 5761 | PyObject *list, |
| 5762 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5763 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5764 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5765 | register Py_ssize_t i; |
| 5766 | register Py_ssize_t j; |
| 5767 | Py_ssize_t len = self->length; |
| 5768 | Py_ssize_t sublen = substring->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5769 | PyObject *str; |
| 5770 | |
Guido van Rossum | cda4f9a | 2000-12-19 02:23:19 +0000 | [diff] [blame] | 5771 | for (i = j = 0; i <= len - sublen; ) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5772 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 5773 | if (maxcount-- <= 0) |
| 5774 | break; |
| 5775 | SPLIT_APPEND(self->str, j, i); |
| 5776 | i = j = i + sublen; |
| 5777 | } else |
| 5778 | i++; |
| 5779 | } |
| 5780 | if (j <= len) { |
| 5781 | SPLIT_APPEND(self->str, j, len); |
| 5782 | } |
| 5783 | return list; |
| 5784 | |
| 5785 | onError: |
| 5786 | Py_DECREF(list); |
| 5787 | return NULL; |
| 5788 | } |
| 5789 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5790 | static |
| 5791 | PyObject *rsplit_whitespace(PyUnicodeObject *self, |
| 5792 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5793 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5794 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5795 | register Py_ssize_t i; |
| 5796 | register Py_ssize_t j; |
| 5797 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5798 | PyObject *str; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5799 | register const Py_UNICODE *buf = self->str; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5800 | |
| 5801 | for (i = j = len - 1; i >= 0; ) { |
| 5802 | /* find a token */ |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5803 | while (i >= 0 && Py_UNICODE_ISSPACE(buf[i])) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5804 | i--; |
| 5805 | j = i; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5806 | while (i >= 0 && !Py_UNICODE_ISSPACE(buf[i])) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5807 | i--; |
| 5808 | if (j > i) { |
| 5809 | if (maxcount-- <= 0) |
| 5810 | break; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5811 | SPLIT_APPEND(buf, i + 1, j + 1); |
| 5812 | while (i >= 0 && Py_UNICODE_ISSPACE(buf[i])) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5813 | i--; |
| 5814 | j = i; |
| 5815 | } |
| 5816 | } |
| 5817 | if (j >= 0) { |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5818 | SPLIT_APPEND(buf, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5819 | } |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5820 | if (PyList_Reverse(list) < 0) |
| 5821 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5822 | return list; |
| 5823 | |
| 5824 | onError: |
| 5825 | Py_DECREF(list); |
| 5826 | return NULL; |
| 5827 | } |
| 5828 | |
| 5829 | static |
| 5830 | PyObject *rsplit_char(PyUnicodeObject *self, |
| 5831 | PyObject *list, |
| 5832 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5833 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5834 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5835 | register Py_ssize_t i; |
| 5836 | register Py_ssize_t j; |
| 5837 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5838 | PyObject *str; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5839 | register const Py_UNICODE *buf = self->str; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5840 | |
| 5841 | for (i = j = len - 1; i >= 0; ) { |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5842 | if (buf[i] == ch) { |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5843 | if (maxcount-- <= 0) |
| 5844 | break; |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5845 | SPLIT_APPEND(buf, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5846 | j = i = i - 1; |
| 5847 | } else |
| 5848 | i--; |
| 5849 | } |
Hye-Shik Chang | 7fc4cf5 | 2003-12-23 09:10:16 +0000 | [diff] [blame] | 5850 | if (j >= -1) { |
Christian Heimes | 4d4f270 | 2008-01-30 11:32:37 +0000 | [diff] [blame] | 5851 | SPLIT_APPEND(buf, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5852 | } |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5853 | if (PyList_Reverse(list) < 0) |
| 5854 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5855 | return list; |
| 5856 | |
| 5857 | onError: |
| 5858 | Py_DECREF(list); |
| 5859 | return NULL; |
| 5860 | } |
| 5861 | |
| 5862 | static |
| 5863 | PyObject *rsplit_substring(PyUnicodeObject *self, |
| 5864 | PyObject *list, |
| 5865 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5866 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5867 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5868 | register Py_ssize_t i; |
| 5869 | register Py_ssize_t j; |
| 5870 | Py_ssize_t len = self->length; |
| 5871 | Py_ssize_t sublen = substring->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5872 | PyObject *str; |
| 5873 | |
| 5874 | for (i = len - sublen, j = len; i >= 0; ) { |
| 5875 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 5876 | if (maxcount-- <= 0) |
| 5877 | break; |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5878 | SPLIT_APPEND(self->str, i + sublen, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5879 | j = i; |
| 5880 | i -= sublen; |
| 5881 | } else |
| 5882 | i--; |
| 5883 | } |
| 5884 | if (j >= 0) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5885 | SPLIT_APPEND(self->str, 0, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5886 | } |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 5887 | if (PyList_Reverse(list) < 0) |
| 5888 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5889 | return list; |
| 5890 | |
| 5891 | onError: |
| 5892 | Py_DECREF(list); |
| 5893 | return NULL; |
| 5894 | } |
| 5895 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5896 | #undef SPLIT_APPEND |
| 5897 | |
| 5898 | static |
| 5899 | PyObject *split(PyUnicodeObject *self, |
| 5900 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5901 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5902 | { |
| 5903 | PyObject *list; |
| 5904 | |
| 5905 | if (maxcount < 0) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5906 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5907 | |
| 5908 | list = PyList_New(0); |
| 5909 | if (!list) |
| 5910 | return NULL; |
| 5911 | |
| 5912 | if (substring == NULL) |
| 5913 | return split_whitespace(self,list,maxcount); |
| 5914 | |
| 5915 | else if (substring->length == 1) |
| 5916 | return split_char(self,list,substring->str[0],maxcount); |
| 5917 | |
| 5918 | else if (substring->length == 0) { |
| 5919 | Py_DECREF(list); |
| 5920 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 5921 | return NULL; |
| 5922 | } |
| 5923 | else |
| 5924 | return split_substring(self,list,substring,maxcount); |
| 5925 | } |
| 5926 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5927 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5928 | PyObject *rsplit(PyUnicodeObject *self, |
| 5929 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5930 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5931 | { |
| 5932 | PyObject *list; |
| 5933 | |
| 5934 | if (maxcount < 0) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5935 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5936 | |
| 5937 | list = PyList_New(0); |
| 5938 | if (!list) |
| 5939 | return NULL; |
| 5940 | |
| 5941 | if (substring == NULL) |
| 5942 | return rsplit_whitespace(self,list,maxcount); |
| 5943 | |
| 5944 | else if (substring->length == 1) |
| 5945 | return rsplit_char(self,list,substring->str[0],maxcount); |
| 5946 | |
| 5947 | else if (substring->length == 0) { |
| 5948 | Py_DECREF(list); |
| 5949 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 5950 | return NULL; |
| 5951 | } |
| 5952 | else |
| 5953 | return rsplit_substring(self,list,substring,maxcount); |
| 5954 | } |
| 5955 | |
| 5956 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5957 | PyObject *replace(PyUnicodeObject *self, |
| 5958 | PyUnicodeObject *str1, |
| 5959 | PyUnicodeObject *str2, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5960 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5961 | { |
| 5962 | PyUnicodeObject *u; |
| 5963 | |
| 5964 | if (maxcount < 0) |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5965 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5966 | |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5967 | if (str1->length == str2->length) { |
| 5968 | /* same length */ |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 5969 | Py_ssize_t i; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5970 | if (str1->length == 1) { |
| 5971 | /* replace characters */ |
| 5972 | Py_UNICODE u1, u2; |
| 5973 | if (!findchar(self->str, self->length, str1->str[0])) |
| 5974 | goto nothing; |
| 5975 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 5976 | if (!u) |
| 5977 | return NULL; |
| 5978 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5979 | u1 = str1->str[0]; |
| 5980 | u2 = str2->str[0]; |
| 5981 | for (i = 0; i < u->length; i++) |
| 5982 | if (u->str[i] == u1) { |
| 5983 | if (--maxcount < 0) |
| 5984 | break; |
| 5985 | u->str[i] = u2; |
| 5986 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5987 | } else { |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5988 | i = fastsearch( |
| 5989 | self->str, self->length, str1->str, str1->length, FAST_SEARCH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5990 | ); |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 5991 | if (i < 0) |
| 5992 | goto nothing; |
| 5993 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 5994 | if (!u) |
| 5995 | return NULL; |
| 5996 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5997 | while (i <= self->length - str1->length) |
| 5998 | if (Py_UNICODE_MATCH(self, i, str1)) { |
| 5999 | if (--maxcount < 0) |
| 6000 | break; |
| 6001 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 6002 | i += str1->length; |
| 6003 | } else |
| 6004 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6005 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6006 | } else { |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6007 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 6008 | Py_ssize_t n, i, j, e; |
Fredrik Lundh | 0c71f88 | 2006-05-25 16:46:54 +0000 | [diff] [blame] | 6009 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6010 | Py_UNICODE *p; |
| 6011 | |
| 6012 | /* replace strings */ |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 6013 | n = stringlib_count(self->str, self->length, str1->str, str1->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6014 | if (n > maxcount) |
| 6015 | n = maxcount; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6016 | if (n == 0) |
| 6017 | goto nothing; |
Fredrik Lundh | 0c71f88 | 2006-05-25 16:46:54 +0000 | [diff] [blame] | 6018 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 6019 | delta = (str2->length - str1->length); |
| 6020 | if (delta == 0) { |
| 6021 | new_size = self->length; |
| 6022 | } else { |
| 6023 | product = n * (str2->length - str1->length); |
| 6024 | if ((product / (str2->length - str1->length)) != n) { |
| 6025 | PyErr_SetString(PyExc_OverflowError, |
| 6026 | "replace string is too long"); |
| 6027 | return NULL; |
| 6028 | } |
| 6029 | new_size = self->length + product; |
| 6030 | if (new_size < 0) { |
| 6031 | PyErr_SetString(PyExc_OverflowError, |
| 6032 | "replace string is too long"); |
| 6033 | return NULL; |
| 6034 | } |
| 6035 | } |
| 6036 | u = _PyUnicode_New(new_size); |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6037 | if (!u) |
| 6038 | return NULL; |
| 6039 | i = 0; |
| 6040 | p = u->str; |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 6041 | e = self->length - str1->length; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6042 | if (str1->length > 0) { |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 6043 | while (n-- > 0) { |
| 6044 | /* look for next match */ |
| 6045 | j = i; |
| 6046 | while (j <= e) { |
| 6047 | if (Py_UNICODE_MATCH(self, j, str1)) |
| 6048 | break; |
| 6049 | j++; |
| 6050 | } |
| 6051 | if (j > i) { |
| 6052 | if (j > e) |
| 6053 | break; |
| 6054 | /* copy unchanged part [i:j] */ |
| 6055 | Py_UNICODE_COPY(p, self->str+i, j-i); |
| 6056 | p += j - i; |
| 6057 | } |
| 6058 | /* copy substitution string */ |
| 6059 | if (str2->length > 0) { |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6060 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6061 | p += str2->length; |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 6062 | } |
| 6063 | i = j + str1->length; |
| 6064 | } |
| 6065 | if (i < self->length) |
| 6066 | /* copy tail [i:] */ |
| 6067 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6068 | } else { |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 6069 | /* interleave */ |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6070 | while (n > 0) { |
| 6071 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6072 | p += str2->length; |
| 6073 | if (--n <= 0) |
| 6074 | break; |
| 6075 | *p++ = self->str[i++]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6076 | } |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6077 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6078 | } |
| 6079 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6080 | return (PyObject *) u; |
Fredrik Lundh | 347ee27 | 2006-05-24 16:35:18 +0000 | [diff] [blame] | 6081 | |
| 6082 | nothing: |
| 6083 | /* nothing to replace; return original string (when possible) */ |
| 6084 | if (PyUnicode_CheckExact(self)) { |
| 6085 | Py_INCREF(self); |
| 6086 | return (PyObject *) self; |
| 6087 | } |
| 6088 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6089 | } |
| 6090 | |
| 6091 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 6092 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6093 | PyDoc_STRVAR(title__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6094 | "S.title() -> unicode\n\ |
| 6095 | \n\ |
| 6096 | 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] | 6097 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6098 | |
| 6099 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6100 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6101 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6102 | return fixup(self, fixtitle); |
| 6103 | } |
| 6104 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6105 | PyDoc_STRVAR(capitalize__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6106 | "S.capitalize() -> unicode\n\ |
| 6107 | \n\ |
| 6108 | 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] | 6109 | have upper case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6110 | |
| 6111 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6112 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6113 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6114 | return fixup(self, fixcapitalize); |
| 6115 | } |
| 6116 | |
| 6117 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6118 | PyDoc_STRVAR(capwords__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6119 | "S.capwords() -> unicode\n\ |
| 6120 | \n\ |
| 6121 | 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] | 6122 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6123 | |
| 6124 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6125 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6126 | { |
| 6127 | PyObject *list; |
| 6128 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6129 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6130 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6131 | /* Split into words */ |
| 6132 | list = split(self, NULL, -1); |
| 6133 | if (!list) |
| 6134 | return NULL; |
| 6135 | |
| 6136 | /* Capitalize each word */ |
| 6137 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 6138 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
| 6139 | fixcapitalize); |
| 6140 | if (item == NULL) |
| 6141 | goto onError; |
| 6142 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 6143 | PyList_SET_ITEM(list, i, item); |
| 6144 | } |
| 6145 | |
| 6146 | /* Join the words to form a new string */ |
| 6147 | item = PyUnicode_Join(NULL, list); |
| 6148 | |
| 6149 | onError: |
| 6150 | Py_DECREF(list); |
| 6151 | return (PyObject *)item; |
| 6152 | } |
| 6153 | #endif |
| 6154 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6155 | /* Argument converter. Coerces to a single unicode character */ |
| 6156 | |
| 6157 | static int |
| 6158 | convert_uc(PyObject *obj, void *addr) |
| 6159 | { |
| 6160 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 6161 | PyObject *uniobj; |
| 6162 | Py_UNICODE *unistr; |
| 6163 | |
| 6164 | uniobj = PyUnicode_FromObject(obj); |
| 6165 | if (uniobj == NULL) { |
| 6166 | PyErr_SetString(PyExc_TypeError, |
| 6167 | "The fill character cannot be converted to Unicode"); |
| 6168 | return 0; |
| 6169 | } |
| 6170 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 6171 | PyErr_SetString(PyExc_TypeError, |
| 6172 | "The fill character must be exactly one character long"); |
| 6173 | Py_DECREF(uniobj); |
| 6174 | return 0; |
| 6175 | } |
| 6176 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 6177 | *fillcharloc = unistr[0]; |
| 6178 | Py_DECREF(uniobj); |
| 6179 | return 1; |
| 6180 | } |
| 6181 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6182 | PyDoc_STRVAR(center__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6183 | "S.center(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6184 | \n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6185 | Return S centered in a Unicode string of length width. Padding is\n\ |
| 6186 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6187 | |
| 6188 | static PyObject * |
| 6189 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 6190 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6191 | Py_ssize_t marg, left; |
| 6192 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6193 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6194 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 6195 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6196 | return NULL; |
| 6197 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6198 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6199 | Py_INCREF(self); |
| 6200 | return (PyObject*) self; |
| 6201 | } |
| 6202 | |
| 6203 | marg = width - self->length; |
| 6204 | left = marg / 2 + (marg & width & 1); |
| 6205 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6206 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6207 | } |
| 6208 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6209 | #if 0 |
| 6210 | |
| 6211 | /* This code should go into some future Unicode collation support |
| 6212 | module. The basic comparison should compare ordinals on a naive |
Trent Mick | 20abf57 | 2000-08-12 22:14:34 +0000 | [diff] [blame] | 6213 | basis (this is what Java does and thus JPython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6214 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6215 | /* speedy UTF-16 code point order comparison */ |
| 6216 | /* gleaned from: */ |
| 6217 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 6218 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6219 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6220 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6221 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6222 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 6223 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6224 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6225 | }; |
| 6226 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6227 | static int |
| 6228 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6229 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6230 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6231 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6232 | Py_UNICODE *s1 = str1->str; |
| 6233 | Py_UNICODE *s2 = str2->str; |
| 6234 | |
| 6235 | len1 = str1->length; |
| 6236 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6237 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6238 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6239 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6240 | |
| 6241 | c1 = *s1++; |
| 6242 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6243 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6244 | if (c1 > (1<<11) * 26) |
| 6245 | c1 += utf16Fixup[c1>>11]; |
| 6246 | if (c2 > (1<<11) * 26) |
| 6247 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6248 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6249 | |
| 6250 | if (c1 != c2) |
| 6251 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6252 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6253 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6254 | } |
| 6255 | |
| 6256 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6257 | } |
| 6258 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6259 | #else |
| 6260 | |
| 6261 | static int |
| 6262 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6263 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6264 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6265 | |
| 6266 | Py_UNICODE *s1 = str1->str; |
| 6267 | Py_UNICODE *s2 = str2->str; |
| 6268 | |
| 6269 | len1 = str1->length; |
| 6270 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6271 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6272 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6273 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6274 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6275 | c1 = *s1++; |
| 6276 | c2 = *s2++; |
| 6277 | |
| 6278 | if (c1 != c2) |
| 6279 | return (c1 < c2) ? -1 : 1; |
| 6280 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6281 | len1--; len2--; |
| 6282 | } |
| 6283 | |
| 6284 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6285 | } |
| 6286 | |
| 6287 | #endif |
| 6288 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6289 | int PyUnicode_Compare(PyObject *left, |
| 6290 | PyObject *right) |
| 6291 | { |
| 6292 | PyUnicodeObject *u = NULL, *v = NULL; |
| 6293 | int result; |
| 6294 | |
| 6295 | /* Coerce the two arguments */ |
| 6296 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 6297 | if (u == NULL) |
| 6298 | goto onError; |
| 6299 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 6300 | if (v == NULL) |
| 6301 | goto onError; |
| 6302 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6303 | /* Shortcut for empty or interned objects */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6304 | if (v == u) { |
| 6305 | Py_DECREF(u); |
| 6306 | Py_DECREF(v); |
| 6307 | return 0; |
| 6308 | } |
| 6309 | |
| 6310 | result = unicode_compare(u, v); |
| 6311 | |
| 6312 | Py_DECREF(u); |
| 6313 | Py_DECREF(v); |
| 6314 | return result; |
| 6315 | |
| 6316 | onError: |
| 6317 | Py_XDECREF(u); |
| 6318 | Py_XDECREF(v); |
| 6319 | return -1; |
| 6320 | } |
| 6321 | |
Marc-André Lemburg | 040f76b | 2006-08-14 10:55:19 +0000 | [diff] [blame] | 6322 | PyObject *PyUnicode_RichCompare(PyObject *left, |
| 6323 | PyObject *right, |
| 6324 | int op) |
| 6325 | { |
| 6326 | int result; |
| 6327 | |
| 6328 | result = PyUnicode_Compare(left, right); |
| 6329 | if (result == -1 && PyErr_Occurred()) |
| 6330 | goto onError; |
| 6331 | |
| 6332 | /* Convert the return value to a Boolean */ |
| 6333 | switch (op) { |
| 6334 | case Py_EQ: |
| 6335 | result = (result == 0); |
| 6336 | break; |
| 6337 | case Py_NE: |
| 6338 | result = (result != 0); |
| 6339 | break; |
| 6340 | case Py_LE: |
| 6341 | result = (result <= 0); |
| 6342 | break; |
| 6343 | case Py_GE: |
| 6344 | result = (result >= 0); |
| 6345 | break; |
| 6346 | case Py_LT: |
| 6347 | result = (result == -1); |
| 6348 | break; |
| 6349 | case Py_GT: |
| 6350 | result = (result == 1); |
| 6351 | break; |
| 6352 | } |
| 6353 | return PyBool_FromLong(result); |
| 6354 | |
| 6355 | onError: |
| 6356 | |
| 6357 | /* Standard case |
| 6358 | |
| 6359 | Type errors mean that PyUnicode_FromObject() could not convert |
| 6360 | one of the arguments (usually the right hand side) to Unicode, |
| 6361 | ie. we can't handle the comparison request. However, it is |
| 6362 | possible that the other object knows a comparison method, which |
| 6363 | is why we return Py_NotImplemented to give the other object a |
| 6364 | chance. |
| 6365 | |
| 6366 | */ |
| 6367 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 6368 | PyErr_Clear(); |
| 6369 | Py_INCREF(Py_NotImplemented); |
| 6370 | return Py_NotImplemented; |
| 6371 | } |
| 6372 | if (op != Py_EQ && op != Py_NE) |
| 6373 | return NULL; |
| 6374 | |
| 6375 | /* Equality comparison. |
| 6376 | |
| 6377 | This is a special case: we silence any PyExc_UnicodeDecodeError |
| 6378 | and instead turn it into a PyErr_UnicodeWarning. |
| 6379 | |
| 6380 | */ |
| 6381 | if (!PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) |
| 6382 | return NULL; |
| 6383 | PyErr_Clear(); |
| 6384 | if (PyErr_Warn(PyExc_UnicodeWarning, |
| 6385 | (op == Py_EQ) ? |
| 6386 | "Unicode equal comparison " |
| 6387 | "failed to convert both arguments to Unicode - " |
| 6388 | "interpreting them as being unequal" : |
| 6389 | "Unicode unequal comparison " |
| 6390 | "failed to convert both arguments to Unicode - " |
| 6391 | "interpreting them as being unequal" |
| 6392 | ) < 0) |
| 6393 | return NULL; |
| 6394 | result = (op == Py_NE); |
| 6395 | return PyBool_FromLong(result); |
| 6396 | } |
| 6397 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6398 | int PyUnicode_Contains(PyObject *container, |
| 6399 | PyObject *element) |
| 6400 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6401 | PyObject *str, *sub; |
| 6402 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6403 | |
| 6404 | /* Coerce the two arguments */ |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6405 | sub = PyUnicode_FromObject(element); |
| 6406 | if (!sub) { |
Marc-André Lemburg | 7c01468 | 2000-06-28 08:11:47 +0000 | [diff] [blame] | 6407 | PyErr_SetString(PyExc_TypeError, |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 6408 | "'in <string>' requires string as left operand"); |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 6409 | return -1; |
Marc-André Lemburg | 7c01468 | 2000-06-28 08:11:47 +0000 | [diff] [blame] | 6410 | } |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 6411 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6412 | str = PyUnicode_FromObject(container); |
| 6413 | if (!str) { |
| 6414 | Py_DECREF(sub); |
Fredrik Lundh | 833bf94 | 2006-05-23 10:12:21 +0000 | [diff] [blame] | 6415 | return -1; |
| 6416 | } |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6417 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6418 | result = stringlib_contains_obj(str, sub); |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 6419 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6420 | Py_DECREF(str); |
| 6421 | Py_DECREF(sub); |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6422 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6423 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6424 | } |
| 6425 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6426 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 6427 | |
| 6428 | PyObject *PyUnicode_Concat(PyObject *left, |
| 6429 | PyObject *right) |
| 6430 | { |
| 6431 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 6432 | |
| 6433 | /* Coerce the two arguments */ |
| 6434 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 6435 | if (u == NULL) |
| 6436 | goto onError; |
| 6437 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 6438 | if (v == NULL) |
| 6439 | goto onError; |
| 6440 | |
| 6441 | /* Shortcuts */ |
| 6442 | if (v == unicode_empty) { |
| 6443 | Py_DECREF(v); |
| 6444 | return (PyObject *)u; |
| 6445 | } |
| 6446 | if (u == unicode_empty) { |
| 6447 | Py_DECREF(u); |
| 6448 | return (PyObject *)v; |
| 6449 | } |
| 6450 | |
| 6451 | /* Concat the two Unicode strings */ |
| 6452 | w = _PyUnicode_New(u->length + v->length); |
| 6453 | if (w == NULL) |
| 6454 | goto onError; |
| 6455 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 6456 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 6457 | |
| 6458 | Py_DECREF(u); |
| 6459 | Py_DECREF(v); |
| 6460 | return (PyObject *)w; |
| 6461 | |
| 6462 | onError: |
| 6463 | Py_XDECREF(u); |
| 6464 | Py_XDECREF(v); |
| 6465 | return NULL; |
| 6466 | } |
| 6467 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6468 | PyDoc_STRVAR(count__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6469 | "S.count(sub[, start[, end]]) -> int\n\ |
| 6470 | \n\ |
Fredrik Lundh | 763b50f | 2006-05-22 15:35:12 +0000 | [diff] [blame] | 6471 | Return the number of non-overlapping occurrences of substring sub in\n\ |
| 6472 | 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] | 6473 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6474 | |
| 6475 | static PyObject * |
| 6476 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 6477 | { |
| 6478 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6479 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 6480 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6481 | PyObject *result; |
| 6482 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6483 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
| 6484 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6485 | return NULL; |
| 6486 | |
| 6487 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 6488 | (PyObject *)substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6489 | if (substring == NULL) |
| 6490 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6491 | |
Fredrik Lundh | c816281 | 2006-05-26 19:33:03 +0000 | [diff] [blame] | 6492 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6493 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 6494 | result = PyInt_FromSsize_t( |
| 6495 | stringlib_count(self->str + start, end - start, |
| 6496 | substring->str, substring->length) |
| 6497 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6498 | |
| 6499 | Py_DECREF(substring); |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 6500 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6501 | return result; |
| 6502 | } |
| 6503 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6504 | PyDoc_STRVAR(encode__doc__, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6505 | "S.encode([encoding[,errors]]) -> string or unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6506 | \n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6507 | Encodes S using the codec registered for encoding. encoding defaults\n\ |
| 6508 | 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] | 6509 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6510 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 6511 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 6512 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6513 | |
| 6514 | static PyObject * |
| 6515 | unicode_encode(PyUnicodeObject *self, PyObject *args) |
| 6516 | { |
| 6517 | char *encoding = NULL; |
| 6518 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6519 | PyObject *v; |
| 6520 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6521 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 6522 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6523 | v = PyUnicode_AsEncodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6524 | if (v == NULL) |
| 6525 | goto onError; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 6526 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6527 | PyErr_Format(PyExc_TypeError, |
| 6528 | "encoder did not return a string/unicode object " |
| 6529 | "(type=%.400s)", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 6530 | Py_TYPE(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6531 | Py_DECREF(v); |
| 6532 | return NULL; |
| 6533 | } |
| 6534 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6535 | |
| 6536 | onError: |
| 6537 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6538 | } |
| 6539 | |
| 6540 | PyDoc_STRVAR(decode__doc__, |
| 6541 | "S.decode([encoding[,errors]]) -> string or unicode\n\ |
| 6542 | \n\ |
| 6543 | Decodes S using the codec registered for encoding. encoding defaults\n\ |
| 6544 | to the default encoding. errors may be given to set a different error\n\ |
| 6545 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
| 6546 | a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ |
| 6547 | as well as any other name registerd with codecs.register_error that is\n\ |
| 6548 | able to handle UnicodeDecodeErrors."); |
| 6549 | |
| 6550 | static PyObject * |
Marc-André Lemburg | 126b44c | 2004-07-10 12:04:20 +0000 | [diff] [blame] | 6551 | unicode_decode(PyUnicodeObject *self, PyObject *args) |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6552 | { |
| 6553 | char *encoding = NULL; |
| 6554 | char *errors = NULL; |
| 6555 | PyObject *v; |
| 6556 | |
| 6557 | if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors)) |
| 6558 | return NULL; |
| 6559 | v = PyUnicode_AsDecodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6560 | if (v == NULL) |
| 6561 | goto onError; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 6562 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6563 | PyErr_Format(PyExc_TypeError, |
| 6564 | "decoder did not return a string/unicode object " |
| 6565 | "(type=%.400s)", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 6566 | Py_TYPE(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6567 | Py_DECREF(v); |
| 6568 | return NULL; |
| 6569 | } |
| 6570 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6571 | |
| 6572 | onError: |
| 6573 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6574 | } |
| 6575 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6576 | PyDoc_STRVAR(expandtabs__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6577 | "S.expandtabs([tabsize]) -> unicode\n\ |
| 6578 | \n\ |
| 6579 | 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] | 6580 | 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] | 6581 | |
| 6582 | static PyObject* |
| 6583 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 6584 | { |
| 6585 | Py_UNICODE *e; |
| 6586 | Py_UNICODE *p; |
| 6587 | Py_UNICODE *q; |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6588 | Py_UNICODE *qe; |
| 6589 | Py_ssize_t i, j, incr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6590 | PyUnicodeObject *u; |
| 6591 | int tabsize = 8; |
| 6592 | |
| 6593 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
| 6594 | return NULL; |
| 6595 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6596 | /* First pass: determine size of output string */ |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6597 | i = 0; /* chars up to and including most recent \n or \r */ |
| 6598 | j = 0; /* chars since most recent \n or \r (use in tab calculations) */ |
| 6599 | e = self->str + self->length; /* end of input */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6600 | for (p = self->str; p < e; p++) |
| 6601 | if (*p == '\t') { |
Neal Norwitz | 7dbd2a3 | 2007-06-09 03:36:34 +0000 | [diff] [blame] | 6602 | if (tabsize > 0) { |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6603 | incr = tabsize - (j % tabsize); /* cannot overflow */ |
| 6604 | if (j > PY_SSIZE_T_MAX - incr) |
| 6605 | goto overflow1; |
| 6606 | j += incr; |
| 6607 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6608 | } |
| 6609 | else { |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6610 | if (j > PY_SSIZE_T_MAX - 1) |
| 6611 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6612 | j++; |
| 6613 | if (*p == '\n' || *p == '\r') { |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6614 | if (i > PY_SSIZE_T_MAX - j) |
| 6615 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6616 | i += j; |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6617 | j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6618 | } |
| 6619 | } |
| 6620 | |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6621 | if (i > PY_SSIZE_T_MAX - j) |
| 6622 | goto overflow1; |
Neal Norwitz | 7dbd2a3 | 2007-06-09 03:36:34 +0000 | [diff] [blame] | 6623 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6624 | /* Second pass: create output string and fill it */ |
| 6625 | u = _PyUnicode_New(i + j); |
| 6626 | if (!u) |
| 6627 | return NULL; |
| 6628 | |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6629 | j = 0; /* same as in first pass */ |
| 6630 | q = u->str; /* next output char */ |
| 6631 | qe = u->str + u->length; /* end of output */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6632 | |
| 6633 | for (p = self->str; p < e; p++) |
| 6634 | if (*p == '\t') { |
| 6635 | if (tabsize > 0) { |
| 6636 | i = tabsize - (j % tabsize); |
| 6637 | j += i; |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6638 | while (i--) { |
| 6639 | if (q >= qe) |
| 6640 | goto overflow2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6641 | *q++ = ' '; |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6642 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6643 | } |
| 6644 | } |
| 6645 | else { |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6646 | if (q >= qe) |
| 6647 | goto overflow2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6648 | *q++ = *p; |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6649 | j++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6650 | if (*p == '\n' || *p == '\r') |
| 6651 | j = 0; |
| 6652 | } |
| 6653 | |
| 6654 | return (PyObject*) u; |
Guido van Rossum | 5bdff60 | 2008-03-11 21:18:06 +0000 | [diff] [blame] | 6655 | |
| 6656 | overflow2: |
| 6657 | Py_DECREF(u); |
| 6658 | overflow1: |
| 6659 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 6660 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6661 | } |
| 6662 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6663 | PyDoc_STRVAR(find__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6664 | "S.find(sub [,start [,end]]) -> int\n\ |
| 6665 | \n\ |
| 6666 | Return the lowest index in S where substring sub is found,\n\ |
Georg Brandl | 9efd9b6 | 2007-07-29 17:38:35 +0000 | [diff] [blame] | 6667 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6668 | arguments start and end are interpreted as in slice notation.\n\ |
| 6669 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6670 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6671 | |
| 6672 | static PyObject * |
| 6673 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 6674 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6675 | PyObject *substring; |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 6676 | Py_ssize_t start; |
| 6677 | Py_ssize_t end; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6678 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6679 | |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 6680 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6681 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6682 | |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 6683 | result = stringlib_find_slice( |
| 6684 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6685 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6686 | start, end |
| 6687 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6688 | |
| 6689 | Py_DECREF(substring); |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6690 | |
| 6691 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6692 | } |
| 6693 | |
| 6694 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6695 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6696 | { |
| 6697 | if (index < 0 || index >= self->length) { |
| 6698 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 6699 | return NULL; |
| 6700 | } |
| 6701 | |
| 6702 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 6703 | } |
| 6704 | |
| 6705 | static long |
| 6706 | unicode_hash(PyUnicodeObject *self) |
| 6707 | { |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 6708 | /* Since Unicode objects compare equal to their ASCII string |
| 6709 | counterparts, they should use the individual character values |
| 6710 | as basis for their hash value. This is needed to assure that |
| 6711 | strings and Unicode objects behave in the same way as |
| 6712 | dictionary keys. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6713 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6714 | register Py_ssize_t len; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 6715 | register Py_UNICODE *p; |
| 6716 | register long x; |
| 6717 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6718 | if (self->hash != -1) |
| 6719 | return self->hash; |
Fredrik Lundh | dde6164 | 2000-07-10 18:27:47 +0000 | [diff] [blame] | 6720 | len = PyUnicode_GET_SIZE(self); |
| 6721 | p = PyUnicode_AS_UNICODE(self); |
| 6722 | x = *p << 7; |
| 6723 | while (--len >= 0) |
| 6724 | x = (1000003*x) ^ *p++; |
| 6725 | x ^= PyUnicode_GET_SIZE(self); |
| 6726 | if (x == -1) |
| 6727 | x = -2; |
| 6728 | self->hash = x; |
| 6729 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6730 | } |
| 6731 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6732 | PyDoc_STRVAR(index__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6733 | "S.index(sub [,start [,end]]) -> int\n\ |
| 6734 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6735 | 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] | 6736 | |
| 6737 | static PyObject * |
| 6738 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 6739 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6740 | Py_ssize_t result; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6741 | PyObject *substring; |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 6742 | Py_ssize_t start; |
| 6743 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6744 | |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 6745 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6746 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6747 | |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 6748 | result = stringlib_find_slice( |
| 6749 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6750 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6751 | start, end |
| 6752 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6753 | |
| 6754 | Py_DECREF(substring); |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6755 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6756 | if (result < 0) { |
| 6757 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 6758 | return NULL; |
| 6759 | } |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 6760 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6761 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6762 | } |
| 6763 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6764 | PyDoc_STRVAR(islower__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6765 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6766 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6767 | 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] | 6768 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6769 | |
| 6770 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6771 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6772 | { |
| 6773 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6774 | register const Py_UNICODE *e; |
| 6775 | int cased; |
| 6776 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6777 | /* Shortcut for single character strings */ |
| 6778 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6779 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6780 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6781 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6782 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6783 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6784 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6785 | e = p + PyUnicode_GET_SIZE(self); |
| 6786 | cased = 0; |
| 6787 | for (; p < e; p++) { |
| 6788 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6789 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6790 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6791 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6792 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 6793 | cased = 1; |
| 6794 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6795 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6796 | } |
| 6797 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6798 | PyDoc_STRVAR(isupper__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6799 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6800 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6801 | 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] | 6802 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6803 | |
| 6804 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6805 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6806 | { |
| 6807 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6808 | register const Py_UNICODE *e; |
| 6809 | int cased; |
| 6810 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6811 | /* Shortcut for single character strings */ |
| 6812 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6813 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6814 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6815 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6816 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6817 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6818 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6819 | e = p + PyUnicode_GET_SIZE(self); |
| 6820 | cased = 0; |
| 6821 | for (; p < e; p++) { |
| 6822 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6823 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6824 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6825 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6826 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 6827 | cased = 1; |
| 6828 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6829 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6830 | } |
| 6831 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6832 | PyDoc_STRVAR(istitle__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6833 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6834 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6835 | Return True if S is a titlecased string and there is at least one\n\ |
| 6836 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 6837 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 6838 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6839 | |
| 6840 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6841 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6842 | { |
| 6843 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6844 | register const Py_UNICODE *e; |
| 6845 | int cased, previous_is_cased; |
| 6846 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6847 | /* Shortcut for single character strings */ |
| 6848 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6849 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 6850 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6851 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6852 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6853 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6854 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6855 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6856 | e = p + PyUnicode_GET_SIZE(self); |
| 6857 | cased = 0; |
| 6858 | previous_is_cased = 0; |
| 6859 | for (; p < e; p++) { |
| 6860 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6861 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6862 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 6863 | if (previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6864 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6865 | previous_is_cased = 1; |
| 6866 | cased = 1; |
| 6867 | } |
| 6868 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 6869 | if (!previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6870 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6871 | previous_is_cased = 1; |
| 6872 | cased = 1; |
| 6873 | } |
| 6874 | else |
| 6875 | previous_is_cased = 0; |
| 6876 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6877 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6878 | } |
| 6879 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6880 | PyDoc_STRVAR(isspace__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6881 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6882 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6883 | Return True if all characters in S are whitespace\n\ |
| 6884 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6885 | |
| 6886 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6887 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6888 | { |
| 6889 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6890 | register const Py_UNICODE *e; |
| 6891 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6892 | /* Shortcut for single character strings */ |
| 6893 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6894 | Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6895 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6896 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6897 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6898 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6899 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6900 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6901 | e = p + PyUnicode_GET_SIZE(self); |
| 6902 | for (; p < e; p++) { |
| 6903 | if (!Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6904 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6905 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6906 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6907 | } |
| 6908 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6909 | PyDoc_STRVAR(isalpha__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6910 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6911 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6912 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6913 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6914 | |
| 6915 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6916 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6917 | { |
| 6918 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6919 | register const Py_UNICODE *e; |
| 6920 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6921 | /* Shortcut for single character strings */ |
| 6922 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6923 | Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6924 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6925 | |
| 6926 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6927 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6928 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6929 | |
| 6930 | e = p + PyUnicode_GET_SIZE(self); |
| 6931 | for (; p < e; p++) { |
| 6932 | if (!Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6933 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6934 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6935 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6936 | } |
| 6937 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6938 | PyDoc_STRVAR(isalnum__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6939 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6940 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6941 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6942 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6943 | |
| 6944 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6945 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6946 | { |
| 6947 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6948 | register const Py_UNICODE *e; |
| 6949 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6950 | /* Shortcut for single character strings */ |
| 6951 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6952 | Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6953 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6954 | |
| 6955 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6956 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6957 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6958 | |
| 6959 | e = p + PyUnicode_GET_SIZE(self); |
| 6960 | for (; p < e; p++) { |
| 6961 | if (!Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6962 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6963 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6964 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 6965 | } |
| 6966 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6967 | PyDoc_STRVAR(isdecimal__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6968 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6969 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6970 | 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] | 6971 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6972 | |
| 6973 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6974 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6975 | { |
| 6976 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6977 | register const Py_UNICODE *e; |
| 6978 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6979 | /* Shortcut for single character strings */ |
| 6980 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 6981 | Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6982 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6983 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6984 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6985 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6986 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6987 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6988 | e = p + PyUnicode_GET_SIZE(self); |
| 6989 | for (; p < e; p++) { |
| 6990 | if (!Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6991 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6992 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6993 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6994 | } |
| 6995 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6996 | PyDoc_STRVAR(isdigit__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6997 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6998 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6999 | Return True if all characters in S are digits\n\ |
| 7000 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7001 | |
| 7002 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7003 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7004 | { |
| 7005 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7006 | register const Py_UNICODE *e; |
| 7007 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7008 | /* Shortcut for single character strings */ |
| 7009 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 7010 | Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7011 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7012 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7013 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7014 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7015 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7016 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7017 | e = p + PyUnicode_GET_SIZE(self); |
| 7018 | for (; p < e; p++) { |
| 7019 | if (!Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7020 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7021 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7022 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7023 | } |
| 7024 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7025 | PyDoc_STRVAR(isnumeric__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7026 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7027 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7028 | 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] | 7029 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7030 | |
| 7031 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7032 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7033 | { |
| 7034 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7035 | register const Py_UNICODE *e; |
| 7036 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7037 | /* Shortcut for single character strings */ |
| 7038 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 7039 | Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7040 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7041 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7042 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7043 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7044 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7045 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7046 | e = p + PyUnicode_GET_SIZE(self); |
| 7047 | for (; p < e; p++) { |
| 7048 | if (!Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7049 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7050 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7051 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7052 | } |
| 7053 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7054 | PyDoc_STRVAR(join__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7055 | "S.join(sequence) -> unicode\n\ |
| 7056 | \n\ |
| 7057 | 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] | 7058 | sequence. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7059 | |
| 7060 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7061 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7062 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7063 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7064 | } |
| 7065 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7066 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7067 | unicode_length(PyUnicodeObject *self) |
| 7068 | { |
| 7069 | return self->length; |
| 7070 | } |
| 7071 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7072 | PyDoc_STRVAR(ljust__doc__, |
Hye-Shik Chang | 974ed7c | 2004-06-02 16:49:17 +0000 | [diff] [blame] | 7073 | "S.ljust(width[, fillchar]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7074 | \n\ |
Andrew M. Kuchling | efeb43e | 2008-10-04 01:05:56 +0000 | [diff] [blame] | 7075 | 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] | 7076 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7077 | |
| 7078 | static PyObject * |
| 7079 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 7080 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7081 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7082 | Py_UNICODE fillchar = ' '; |
| 7083 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7084 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7085 | return NULL; |
| 7086 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7087 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7088 | Py_INCREF(self); |
| 7089 | return (PyObject*) self; |
| 7090 | } |
| 7091 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7092 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7093 | } |
| 7094 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7095 | PyDoc_STRVAR(lower__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7096 | "S.lower() -> unicode\n\ |
| 7097 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7098 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7099 | |
| 7100 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7101 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7102 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7103 | return fixup(self, fixlower); |
| 7104 | } |
| 7105 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7106 | #define LEFTSTRIP 0 |
| 7107 | #define RIGHTSTRIP 1 |
| 7108 | #define BOTHSTRIP 2 |
| 7109 | |
| 7110 | /* Arrays indexed by above */ |
| 7111 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 7112 | |
| 7113 | #define STRIPNAME(i) (stripformat[i]+3) |
| 7114 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7115 | /* externally visible for str.strip(unicode) */ |
| 7116 | PyObject * |
| 7117 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 7118 | { |
| 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); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7121 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7122 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 7123 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7124 | |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 7125 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
| 7126 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7127 | i = 0; |
| 7128 | if (striptype != RIGHTSTRIP) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 7129 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 7130 | i++; |
| 7131 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7132 | } |
| 7133 | |
| 7134 | j = len; |
| 7135 | if (striptype != LEFTSTRIP) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 7136 | do { |
| 7137 | j--; |
| 7138 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 7139 | j++; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7140 | } |
| 7141 | |
| 7142 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 7143 | Py_INCREF(self); |
| 7144 | return (PyObject*)self; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7145 | } |
| 7146 | else |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 7147 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7148 | } |
| 7149 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7150 | |
| 7151 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7152 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7153 | { |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7154 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7155 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7156 | |
| 7157 | i = 0; |
| 7158 | if (striptype != RIGHTSTRIP) { |
| 7159 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 7160 | i++; |
| 7161 | } |
| 7162 | } |
| 7163 | |
| 7164 | j = len; |
| 7165 | if (striptype != LEFTSTRIP) { |
| 7166 | do { |
| 7167 | j--; |
| 7168 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 7169 | j++; |
| 7170 | } |
| 7171 | |
| 7172 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 7173 | Py_INCREF(self); |
| 7174 | return (PyObject*)self; |
| 7175 | } |
| 7176 | else |
| 7177 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7178 | } |
| 7179 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7180 | |
| 7181 | static PyObject * |
| 7182 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 7183 | { |
| 7184 | PyObject *sep = NULL; |
| 7185 | |
| 7186 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 7187 | return NULL; |
| 7188 | |
| 7189 | if (sep != NULL && sep != Py_None) { |
| 7190 | if (PyUnicode_Check(sep)) |
| 7191 | return _PyUnicode_XStrip(self, striptype, sep); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 7192 | else if (PyString_Check(sep)) { |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7193 | PyObject *res; |
| 7194 | sep = PyUnicode_FromObject(sep); |
| 7195 | if (sep==NULL) |
| 7196 | return NULL; |
| 7197 | res = _PyUnicode_XStrip(self, striptype, sep); |
| 7198 | Py_DECREF(sep); |
| 7199 | return res; |
| 7200 | } |
| 7201 | else { |
| 7202 | PyErr_Format(PyExc_TypeError, |
| 7203 | "%s arg must be None, unicode or str", |
| 7204 | STRIPNAME(striptype)); |
| 7205 | return NULL; |
| 7206 | } |
| 7207 | } |
| 7208 | |
| 7209 | return do_strip(self, striptype); |
| 7210 | } |
| 7211 | |
| 7212 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7213 | PyDoc_STRVAR(strip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7214 | "S.strip([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 leading and trailing\n\ |
| 7217 | whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7218 | If chars is given and not None, remove characters in chars instead.\n\ |
| 7219 | 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] | 7220 | |
| 7221 | static PyObject * |
| 7222 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 7223 | { |
| 7224 | if (PyTuple_GET_SIZE(args) == 0) |
| 7225 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 7226 | else |
| 7227 | return do_argstrip(self, BOTHSTRIP, args); |
| 7228 | } |
| 7229 | |
| 7230 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7231 | PyDoc_STRVAR(lstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7232 | "S.lstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7233 | \n\ |
| 7234 | Return a copy of the string S with leading whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7235 | If chars is given and not None, remove characters in chars instead.\n\ |
| 7236 | 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] | 7237 | |
| 7238 | static PyObject * |
| 7239 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 7240 | { |
| 7241 | if (PyTuple_GET_SIZE(args) == 0) |
| 7242 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 7243 | else |
| 7244 | return do_argstrip(self, LEFTSTRIP, args); |
| 7245 | } |
| 7246 | |
| 7247 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7248 | PyDoc_STRVAR(rstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7249 | "S.rstrip([chars]) -> unicode\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7250 | \n\ |
| 7251 | Return a copy of the string S with trailing whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 7252 | If chars is given and not None, remove characters in chars instead.\n\ |
| 7253 | 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] | 7254 | |
| 7255 | static PyObject * |
| 7256 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 7257 | { |
| 7258 | if (PyTuple_GET_SIZE(args) == 0) |
| 7259 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 7260 | else |
| 7261 | return do_argstrip(self, RIGHTSTRIP, args); |
| 7262 | } |
| 7263 | |
| 7264 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7265 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7266 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7267 | { |
| 7268 | PyUnicodeObject *u; |
| 7269 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7270 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7271 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7272 | |
| 7273 | if (len < 0) |
| 7274 | len = 0; |
| 7275 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7276 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7277 | /* no repeat, return original string */ |
| 7278 | Py_INCREF(str); |
| 7279 | return (PyObject*) str; |
| 7280 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7281 | |
| 7282 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 7283 | * needed doesn't overflow size_t |
| 7284 | */ |
| 7285 | nchars = len * str->length; |
| 7286 | if (len && nchars / len != str->length) { |
| 7287 | PyErr_SetString(PyExc_OverflowError, |
| 7288 | "repeated string is too long"); |
| 7289 | return NULL; |
| 7290 | } |
| 7291 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 7292 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 7293 | PyErr_SetString(PyExc_OverflowError, |
| 7294 | "repeated string is too long"); |
| 7295 | return NULL; |
| 7296 | } |
| 7297 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7298 | if (!u) |
| 7299 | return NULL; |
| 7300 | |
| 7301 | p = u->str; |
| 7302 | |
Fredrik Lundh | f1d60a5 | 2006-05-22 16:29:30 +0000 | [diff] [blame] | 7303 | if (str->length == 1 && len > 0) { |
| 7304 | Py_UNICODE_FILL(p, str->str[0], len); |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 7305 | } else { |
Tim Peters | 1bacc64 | 2006-05-23 05:47:16 +0000 | [diff] [blame] | 7306 | Py_ssize_t done = 0; /* number of characters copied this far */ |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 7307 | if (done < nchars) { |
Fredrik Lundh | f1d60a5 | 2006-05-22 16:29:30 +0000 | [diff] [blame] | 7308 | Py_UNICODE_COPY(p, str->str, str->length); |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 7309 | done = str->length; |
| 7310 | } |
| 7311 | while (done < nchars) { |
Neal Norwitz | 4677fbf7 | 2008-03-25 04:18:18 +0000 | [diff] [blame] | 7312 | Py_ssize_t n = (done <= nchars-done) ? done : nchars-done; |
Fredrik Lundh | 8a8e05a | 2006-05-22 17:12:58 +0000 | [diff] [blame] | 7313 | Py_UNICODE_COPY(p+done, p, n); |
| 7314 | done += n; |
| 7315 | } |
| 7316 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7317 | |
| 7318 | return (PyObject*) u; |
| 7319 | } |
| 7320 | |
| 7321 | PyObject *PyUnicode_Replace(PyObject *obj, |
| 7322 | PyObject *subobj, |
| 7323 | PyObject *replobj, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7324 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7325 | { |
| 7326 | PyObject *self; |
| 7327 | PyObject *str1; |
| 7328 | PyObject *str2; |
| 7329 | PyObject *result; |
| 7330 | |
| 7331 | self = PyUnicode_FromObject(obj); |
| 7332 | if (self == NULL) |
| 7333 | return NULL; |
| 7334 | str1 = PyUnicode_FromObject(subobj); |
| 7335 | if (str1 == NULL) { |
| 7336 | Py_DECREF(self); |
| 7337 | return NULL; |
| 7338 | } |
| 7339 | str2 = PyUnicode_FromObject(replobj); |
| 7340 | if (str2 == NULL) { |
| 7341 | Py_DECREF(self); |
| 7342 | Py_DECREF(str1); |
| 7343 | return NULL; |
| 7344 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7345 | result = replace((PyUnicodeObject *)self, |
| 7346 | (PyUnicodeObject *)str1, |
| 7347 | (PyUnicodeObject *)str2, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7348 | maxcount); |
| 7349 | Py_DECREF(self); |
| 7350 | Py_DECREF(str1); |
| 7351 | Py_DECREF(str2); |
| 7352 | return result; |
| 7353 | } |
| 7354 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7355 | PyDoc_STRVAR(replace__doc__, |
Georg Brandl | 30fadc1 | 2008-05-30 07:54:16 +0000 | [diff] [blame] | 7356 | "S.replace (old, new[, count]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7357 | \n\ |
| 7358 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | 30fadc1 | 2008-05-30 07:54:16 +0000 | [diff] [blame] | 7359 | old replaced by new. If the optional argument count is\n\ |
| 7360 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7361 | |
| 7362 | static PyObject* |
| 7363 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 7364 | { |
| 7365 | PyUnicodeObject *str1; |
| 7366 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7367 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7368 | PyObject *result; |
| 7369 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7370 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7371 | return NULL; |
| 7372 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 7373 | if (str1 == NULL) |
| 7374 | return NULL; |
| 7375 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7376 | if (str2 == NULL) { |
| 7377 | Py_DECREF(str1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7378 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7379 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7380 | |
| 7381 | result = replace(self, str1, str2, maxcount); |
| 7382 | |
| 7383 | Py_DECREF(str1); |
| 7384 | Py_DECREF(str2); |
| 7385 | return result; |
| 7386 | } |
| 7387 | |
| 7388 | static |
| 7389 | PyObject *unicode_repr(PyObject *unicode) |
| 7390 | { |
| 7391 | return unicodeescape_string(PyUnicode_AS_UNICODE(unicode), |
| 7392 | PyUnicode_GET_SIZE(unicode), |
| 7393 | 1); |
| 7394 | } |
| 7395 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7396 | PyDoc_STRVAR(rfind__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7397 | "S.rfind(sub [,start [,end]]) -> int\n\ |
| 7398 | \n\ |
| 7399 | Return the highest index in S where substring sub is found,\n\ |
Georg Brandl | 9efd9b6 | 2007-07-29 17:38:35 +0000 | [diff] [blame] | 7400 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7401 | arguments start and end are interpreted as in slice notation.\n\ |
| 7402 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7403 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7404 | |
| 7405 | static PyObject * |
| 7406 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 7407 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7408 | PyObject *substring; |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 7409 | Py_ssize_t start; |
| 7410 | Py_ssize_t end; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7411 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7412 | |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 7413 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
| 7414 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7415 | |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 7416 | result = stringlib_rfind_slice( |
| 7417 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7418 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7419 | start, end |
| 7420 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7421 | |
| 7422 | Py_DECREF(substring); |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7423 | |
| 7424 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7425 | } |
| 7426 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7427 | PyDoc_STRVAR(rindex__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7428 | "S.rindex(sub [,start [,end]]) -> int\n\ |
| 7429 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7430 | 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] | 7431 | |
| 7432 | static PyObject * |
| 7433 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 7434 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7435 | PyObject *substring; |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 7436 | Py_ssize_t start; |
| 7437 | Py_ssize_t end; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7438 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7439 | |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 7440 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
| 7441 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7442 | |
Fredrik Lundh | 60d8b18 | 2006-05-27 15:20:22 +0000 | [diff] [blame] | 7443 | result = stringlib_rfind_slice( |
| 7444 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7445 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7446 | start, end |
| 7447 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7448 | |
| 7449 | Py_DECREF(substring); |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 7450 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7451 | if (result < 0) { |
| 7452 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 7453 | return NULL; |
| 7454 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7455 | return PyInt_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7456 | } |
| 7457 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7458 | PyDoc_STRVAR(rjust__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7459 | "S.rjust(width[, fillchar]) -> unicode\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7460 | \n\ |
Andrew M. Kuchling | efeb43e | 2008-10-04 01:05:56 +0000 | [diff] [blame] | 7461 | 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] | 7462 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7463 | |
| 7464 | static PyObject * |
| 7465 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 7466 | { |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7467 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7468 | Py_UNICODE fillchar = ' '; |
| 7469 | |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7470 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7471 | return NULL; |
| 7472 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7473 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7474 | Py_INCREF(self); |
| 7475 | return (PyObject*) self; |
| 7476 | } |
| 7477 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7478 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7479 | } |
| 7480 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7481 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7482 | 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] | 7483 | { |
| 7484 | /* standard clamping */ |
| 7485 | if (start < 0) |
| 7486 | start = 0; |
| 7487 | if (end < 0) |
| 7488 | end = 0; |
| 7489 | if (end > self->length) |
| 7490 | end = self->length; |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7491 | if (start == 0 && end == self->length && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7492 | /* full slice, return original string */ |
| 7493 | Py_INCREF(self); |
| 7494 | return (PyObject*) self; |
| 7495 | } |
| 7496 | if (start > end) |
| 7497 | start = end; |
| 7498 | /* copy slice */ |
| 7499 | return (PyObject*) PyUnicode_FromUnicode(self->str + start, |
| 7500 | end - start); |
| 7501 | } |
| 7502 | |
| 7503 | PyObject *PyUnicode_Split(PyObject *s, |
| 7504 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7505 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7506 | { |
| 7507 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7508 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7509 | s = PyUnicode_FromObject(s); |
| 7510 | if (s == NULL) |
| 7511 | return NULL; |
| 7512 | if (sep != NULL) { |
| 7513 | sep = PyUnicode_FromObject(sep); |
| 7514 | if (sep == NULL) { |
| 7515 | Py_DECREF(s); |
| 7516 | return NULL; |
| 7517 | } |
| 7518 | } |
| 7519 | |
| 7520 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 7521 | |
| 7522 | Py_DECREF(s); |
| 7523 | Py_XDECREF(sep); |
| 7524 | return result; |
| 7525 | } |
| 7526 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7527 | PyDoc_STRVAR(split__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7528 | "S.split([sep [,maxsplit]]) -> list of strings\n\ |
| 7529 | \n\ |
| 7530 | Return a list of the words in S, using sep as the\n\ |
| 7531 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Georg Brandl | dfb77db | 2008-05-11 09:11:40 +0000 | [diff] [blame] | 7532 | 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] | 7533 | whitespace string is a separator and empty strings are\n\ |
| 7534 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7535 | |
| 7536 | static PyObject* |
| 7537 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 7538 | { |
| 7539 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7540 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7541 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7542 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7543 | return NULL; |
| 7544 | |
| 7545 | if (substring == Py_None) |
| 7546 | return split(self, NULL, maxcount); |
| 7547 | else if (PyUnicode_Check(substring)) |
| 7548 | return split(self, (PyUnicodeObject *)substring, maxcount); |
| 7549 | else |
| 7550 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
| 7551 | } |
| 7552 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7553 | PyObject * |
| 7554 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 7555 | { |
| 7556 | PyObject* str_obj; |
| 7557 | PyObject* sep_obj; |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7558 | PyObject* out; |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 7559 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7560 | str_obj = PyUnicode_FromObject(str_in); |
| 7561 | if (!str_obj) |
| 7562 | return NULL; |
| 7563 | sep_obj = PyUnicode_FromObject(sep_in); |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 7564 | if (!sep_obj) { |
| 7565 | Py_DECREF(str_obj); |
| 7566 | return NULL; |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7567 | } |
| 7568 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 7569 | out = stringlib_partition( |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 7570 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 7571 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 7572 | ); |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7573 | |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 7574 | Py_DECREF(sep_obj); |
| 7575 | Py_DECREF(str_obj); |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7576 | |
| 7577 | return out; |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7578 | } |
| 7579 | |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7580 | |
| 7581 | PyObject * |
| 7582 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 7583 | { |
| 7584 | PyObject* str_obj; |
| 7585 | PyObject* sep_obj; |
| 7586 | PyObject* out; |
| 7587 | |
| 7588 | str_obj = PyUnicode_FromObject(str_in); |
| 7589 | if (!str_obj) |
| 7590 | return NULL; |
| 7591 | sep_obj = PyUnicode_FromObject(sep_in); |
| 7592 | if (!sep_obj) { |
| 7593 | Py_DECREF(str_obj); |
| 7594 | return NULL; |
| 7595 | } |
| 7596 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 7597 | out = stringlib_rpartition( |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7598 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 7599 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 7600 | ); |
| 7601 | |
| 7602 | Py_DECREF(sep_obj); |
| 7603 | Py_DECREF(str_obj); |
| 7604 | |
| 7605 | return out; |
| 7606 | } |
| 7607 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7608 | PyDoc_STRVAR(partition__doc__, |
| 7609 | "S.partition(sep) -> (head, sep, tail)\n\ |
| 7610 | \n\ |
Andrew M. Kuchling | efeb43e | 2008-10-04 01:05:56 +0000 | [diff] [blame] | 7611 | Search for the separator sep in S, and return the part before it,\n\ |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7612 | the separator itself, and the part after it. If the separator is not\n\ |
Andrew M. Kuchling | efeb43e | 2008-10-04 01:05:56 +0000 | [diff] [blame] | 7613 | found, return S and two empty strings."); |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7614 | |
| 7615 | static PyObject* |
Fredrik Lundh | 450277f | 2006-05-26 09:46:59 +0000 | [diff] [blame] | 7616 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7617 | { |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 7618 | return PyUnicode_Partition((PyObject *)self, separator); |
| 7619 | } |
| 7620 | |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7621 | PyDoc_STRVAR(rpartition__doc__, |
Raymond Hettinger | a0c95fa | 2006-09-04 15:32:48 +0000 | [diff] [blame] | 7622 | "S.rpartition(sep) -> (tail, sep, head)\n\ |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7623 | \n\ |
Andrew M. Kuchling | efeb43e | 2008-10-04 01:05:56 +0000 | [diff] [blame] | 7624 | Search for the separator sep in S, starting at the end of S, and return\n\ |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7625 | the part before it, the separator itself, and the part after it. If the\n\ |
Andrew M. Kuchling | efeb43e | 2008-10-04 01:05:56 +0000 | [diff] [blame] | 7626 | separator is not found, return two empty strings and S."); |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7627 | |
| 7628 | static PyObject* |
| 7629 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 7630 | { |
| 7631 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 7632 | } |
| 7633 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7634 | PyObject *PyUnicode_RSplit(PyObject *s, |
| 7635 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7636 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7637 | { |
| 7638 | PyObject *result; |
| 7639 | |
| 7640 | s = PyUnicode_FromObject(s); |
| 7641 | if (s == NULL) |
| 7642 | return NULL; |
| 7643 | if (sep != NULL) { |
| 7644 | sep = PyUnicode_FromObject(sep); |
| 7645 | if (sep == NULL) { |
| 7646 | Py_DECREF(s); |
| 7647 | return NULL; |
| 7648 | } |
| 7649 | } |
| 7650 | |
| 7651 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 7652 | |
| 7653 | Py_DECREF(s); |
| 7654 | Py_XDECREF(sep); |
| 7655 | return result; |
| 7656 | } |
| 7657 | |
| 7658 | PyDoc_STRVAR(rsplit__doc__, |
| 7659 | "S.rsplit([sep [,maxsplit]]) -> list of strings\n\ |
| 7660 | \n\ |
| 7661 | Return a list of the words in S, using sep as the\n\ |
| 7662 | delimiter string, starting at the end of the string and\n\ |
| 7663 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 7664 | splits are done. If sep is not specified, any whitespace string\n\ |
| 7665 | is a separator."); |
| 7666 | |
| 7667 | static PyObject* |
| 7668 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 7669 | { |
| 7670 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7671 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7672 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7673 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7674 | return NULL; |
| 7675 | |
| 7676 | if (substring == Py_None) |
| 7677 | return rsplit(self, NULL, maxcount); |
| 7678 | else if (PyUnicode_Check(substring)) |
| 7679 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
| 7680 | else |
| 7681 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
| 7682 | } |
| 7683 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7684 | PyDoc_STRVAR(splitlines__doc__, |
Amaury Forgeot d'Arc | 2a1fd05 | 2008-11-29 02:03:32 +0000 | [diff] [blame] | 7685 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7686 | \n\ |
| 7687 | 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] | 7688 | 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] | 7689 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7690 | |
| 7691 | static PyObject* |
| 7692 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 7693 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7694 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7695 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7696 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7697 | return NULL; |
| 7698 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 7699 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7700 | } |
| 7701 | |
| 7702 | static |
| 7703 | PyObject *unicode_str(PyUnicodeObject *self) |
| 7704 | { |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 7705 | return PyUnicode_AsEncodedString((PyObject *)self, NULL, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7706 | } |
| 7707 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7708 | PyDoc_STRVAR(swapcase__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7709 | "S.swapcase() -> unicode\n\ |
| 7710 | \n\ |
| 7711 | 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] | 7712 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7713 | |
| 7714 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7715 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7716 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7717 | return fixup(self, fixswapcase); |
| 7718 | } |
| 7719 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7720 | PyDoc_STRVAR(translate__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7721 | "S.translate(table) -> unicode\n\ |
| 7722 | \n\ |
| 7723 | Return a copy of the string S, where all characters have been mapped\n\ |
| 7724 | 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] | 7725 | Unicode ordinals to Unicode ordinals, Unicode strings or None.\n\ |
| 7726 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 7727 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7728 | |
| 7729 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7730 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7731 | { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7732 | return PyUnicode_TranslateCharmap(self->str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7733 | self->length, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7734 | table, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7735 | "ignore"); |
| 7736 | } |
| 7737 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7738 | PyDoc_STRVAR(upper__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7739 | "S.upper() -> unicode\n\ |
| 7740 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7741 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7742 | |
| 7743 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7744 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7745 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7746 | return fixup(self, fixupper); |
| 7747 | } |
| 7748 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7749 | PyDoc_STRVAR(zfill__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7750 | "S.zfill(width) -> unicode\n\ |
| 7751 | \n\ |
Georg Brandl | 9806407 | 2008-09-09 19:26:00 +0000 | [diff] [blame] | 7752 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 7753 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7754 | |
| 7755 | static PyObject * |
| 7756 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 7757 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7758 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7759 | PyUnicodeObject *u; |
| 7760 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7761 | Py_ssize_t width; |
| 7762 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7763 | return NULL; |
| 7764 | |
| 7765 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 7766 | if (PyUnicode_CheckExact(self)) { |
| 7767 | Py_INCREF(self); |
| 7768 | return (PyObject*) self; |
| 7769 | } |
| 7770 | else |
| 7771 | return PyUnicode_FromUnicode( |
| 7772 | PyUnicode_AS_UNICODE(self), |
| 7773 | PyUnicode_GET_SIZE(self) |
| 7774 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7775 | } |
| 7776 | |
| 7777 | fill = width - self->length; |
| 7778 | |
| 7779 | u = pad(self, fill, 0, '0'); |
| 7780 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 7781 | if (u == NULL) |
| 7782 | return NULL; |
| 7783 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7784 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 7785 | /* move sign to beginning of string */ |
| 7786 | u->str[0] = u->str[fill]; |
| 7787 | u->str[fill] = '0'; |
| 7788 | } |
| 7789 | |
| 7790 | return (PyObject*) u; |
| 7791 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7792 | |
| 7793 | #if 0 |
| 7794 | static PyObject* |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 7795 | free_listsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7796 | { |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 7797 | return PyInt_FromLong(numfree); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7798 | } |
| 7799 | #endif |
| 7800 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7801 | PyDoc_STRVAR(startswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7802 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7803 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 7804 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 7805 | With optional start, test S beginning at that position.\n\ |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7806 | With optional end, stop comparing S at that position.\n\ |
| 7807 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7808 | |
| 7809 | static PyObject * |
| 7810 | unicode_startswith(PyUnicodeObject *self, |
| 7811 | PyObject *args) |
| 7812 | { |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7813 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7814 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7815 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7816 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7817 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7818 | |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7819 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 7820 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7821 | return NULL; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7822 | if (PyTuple_Check(subobj)) { |
| 7823 | Py_ssize_t i; |
| 7824 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 7825 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 7826 | PyTuple_GET_ITEM(subobj, i)); |
| 7827 | if (substring == NULL) |
| 7828 | return NULL; |
| 7829 | result = tailmatch(self, substring, start, end, -1); |
| 7830 | Py_DECREF(substring); |
| 7831 | if (result) { |
| 7832 | Py_RETURN_TRUE; |
| 7833 | } |
| 7834 | } |
| 7835 | /* nothing matched */ |
| 7836 | Py_RETURN_FALSE; |
| 7837 | } |
| 7838 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7839 | if (substring == NULL) |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7840 | return NULL; |
| 7841 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7842 | Py_DECREF(substring); |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7843 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7844 | } |
| 7845 | |
| 7846 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7847 | PyDoc_STRVAR(endswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7848 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7849 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 7850 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 7851 | With optional start, test S beginning at that position.\n\ |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7852 | With optional end, stop comparing S at that position.\n\ |
| 7853 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7854 | |
| 7855 | static PyObject * |
| 7856 | unicode_endswith(PyUnicodeObject *self, |
| 7857 | PyObject *args) |
| 7858 | { |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7859 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7860 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7861 | Py_ssize_t start = 0; |
Martin v. Löwis | 412fb67 | 2006-04-13 06:34:32 +0000 | [diff] [blame] | 7862 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7863 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7864 | |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7865 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
| 7866 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7867 | return NULL; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7868 | if (PyTuple_Check(subobj)) { |
| 7869 | Py_ssize_t i; |
| 7870 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 7871 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 7872 | PyTuple_GET_ITEM(subobj, i)); |
| 7873 | if (substring == NULL) |
| 7874 | return NULL; |
| 7875 | result = tailmatch(self, substring, start, end, +1); |
| 7876 | Py_DECREF(substring); |
| 7877 | if (result) { |
| 7878 | Py_RETURN_TRUE; |
| 7879 | } |
| 7880 | } |
| 7881 | Py_RETURN_FALSE; |
| 7882 | } |
| 7883 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7884 | if (substring == NULL) |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7885 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7886 | |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7887 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7888 | Py_DECREF(substring); |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 7889 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7890 | } |
| 7891 | |
| 7892 | |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 7893 | /* Implements do_string_format, which is unicode because of stringlib */ |
| 7894 | #include "stringlib/string_format.h" |
| 7895 | |
| 7896 | PyDoc_STRVAR(format__doc__, |
| 7897 | "S.format(*args, **kwargs) -> unicode\n\ |
| 7898 | \n\ |
| 7899 | "); |
| 7900 | |
Eric Smith | dc13b79 | 2008-05-30 18:10:04 +0000 | [diff] [blame] | 7901 | static PyObject * |
| 7902 | unicode__format__(PyObject *self, PyObject *args) |
| 7903 | { |
| 7904 | PyObject *format_spec; |
| 7905 | PyObject *result = NULL; |
| 7906 | PyObject *tmp = NULL; |
| 7907 | |
| 7908 | /* If 2.x, convert format_spec to the same type as value */ |
| 7909 | /* This is to allow things like u''.format('') */ |
| 7910 | if (!PyArg_ParseTuple(args, "O:__format__", &format_spec)) |
| 7911 | goto done; |
| 7912 | if (!(PyBytes_Check(format_spec) || PyUnicode_Check(format_spec))) { |
| 7913 | PyErr_Format(PyExc_TypeError, "__format__ arg must be str " |
| 7914 | "or unicode, not %s", Py_TYPE(format_spec)->tp_name); |
| 7915 | goto done; |
| 7916 | } |
| 7917 | tmp = PyObject_Unicode(format_spec); |
| 7918 | if (tmp == NULL) |
| 7919 | goto done; |
| 7920 | format_spec = tmp; |
| 7921 | |
| 7922 | result = _PyUnicode_FormatAdvanced(self, |
| 7923 | PyUnicode_AS_UNICODE(format_spec), |
| 7924 | PyUnicode_GET_SIZE(format_spec)); |
| 7925 | done: |
| 7926 | Py_XDECREF(tmp); |
| 7927 | return result; |
| 7928 | } |
| 7929 | |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 7930 | PyDoc_STRVAR(p_format__doc__, |
| 7931 | "S.__format__(format_spec) -> unicode\n\ |
| 7932 | \n\ |
| 7933 | "); |
| 7934 | |
Robert Schuppenies | 901c997 | 2008-06-10 10:10:31 +0000 | [diff] [blame] | 7935 | static PyObject * |
| 7936 | unicode__sizeof__(PyUnicodeObject *v) |
| 7937 | { |
Robert Schuppenies | 9be2ec1 | 2008-07-10 15:24:04 +0000 | [diff] [blame] | 7938 | return PyInt_FromSsize_t(sizeof(PyUnicodeObject) + |
| 7939 | sizeof(Py_UNICODE) * (v->length + 1)); |
Robert Schuppenies | 901c997 | 2008-06-10 10:10:31 +0000 | [diff] [blame] | 7940 | } |
| 7941 | |
| 7942 | PyDoc_STRVAR(sizeof__doc__, |
| 7943 | "S.__sizeof__() -> size of S in memory, in bytes\n\ |
| 7944 | \n\ |
| 7945 | "); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 7946 | |
| 7947 | static PyObject * |
| 7948 | unicode_getnewargs(PyUnicodeObject *v) |
| 7949 | { |
| 7950 | return Py_BuildValue("(u#)", v->str, v->length); |
| 7951 | } |
| 7952 | |
| 7953 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7954 | static PyMethodDef unicode_methods[] = { |
| 7955 | |
| 7956 | /* Order is according to common usage: often used methods should |
| 7957 | appear first, since lookup is done sequentially. */ |
| 7958 | |
Georg Brandl | ecdc0a9 | 2006-03-30 12:19:07 +0000 | [diff] [blame] | 7959 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS, encode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7960 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 7961 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7962 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7963 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 7964 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 7965 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 7966 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 7967 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 7968 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 7969 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Fredrik Lundh | 450277f | 2006-05-26 09:46:59 +0000 | [diff] [blame] | 7970 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7971 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 7972 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 7973 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7974 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7975 | {"decode", (PyCFunction) unicode_decode, METH_VARARGS, decode__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7976 | /* {"maketrans", (PyCFunction) unicode_maketrans, METH_VARARGS, maketrans__doc__}, */ |
| 7977 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 7978 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 7979 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7980 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 7981 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7982 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7983 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7984 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 7985 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 7986 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 7987 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 7988 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 7989 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 7990 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 7991 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 7992 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 7993 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 7994 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 7995 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 7996 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 7997 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7998 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 7999 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
| 8000 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
| 8001 | {"_formatter_field_name_split", (PyCFunction) formatter_field_name_split, METH_NOARGS}, |
| 8002 | {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS}, |
Robert Schuppenies | 901c997 | 2008-06-10 10:10:31 +0000 | [diff] [blame] | 8003 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8004 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8005 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8006 | #endif |
| 8007 | |
| 8008 | #if 0 |
| 8009 | /* This one is just used for debugging the implementation. */ |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 8010 | {"freelistsize", (PyCFunction) free_listsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8011 | #endif |
| 8012 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8013 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8014 | {NULL, NULL} |
| 8015 | }; |
| 8016 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8017 | static PyObject * |
| 8018 | unicode_mod(PyObject *v, PyObject *w) |
| 8019 | { |
| 8020 | if (!PyUnicode_Check(v)) { |
| 8021 | Py_INCREF(Py_NotImplemented); |
| 8022 | return Py_NotImplemented; |
| 8023 | } |
| 8024 | return PyUnicode_Format(v, w); |
| 8025 | } |
| 8026 | |
| 8027 | static PyNumberMethods unicode_as_number = { |
| 8028 | 0, /*nb_add*/ |
| 8029 | 0, /*nb_subtract*/ |
| 8030 | 0, /*nb_multiply*/ |
| 8031 | 0, /*nb_divide*/ |
| 8032 | unicode_mod, /*nb_remainder*/ |
| 8033 | }; |
| 8034 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8035 | static PySequenceMethods unicode_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8036 | (lenfunc) unicode_length, /* sq_length */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 8037 | PyUnicode_Concat, /* sq_concat */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8038 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 8039 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 8040 | (ssizessizeargfunc) unicode_slice, /* sq_slice */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8041 | 0, /* sq_ass_item */ |
| 8042 | 0, /* sq_ass_slice */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 8043 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8044 | }; |
| 8045 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8046 | static PyObject* |
| 8047 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 8048 | { |
Marc-André Lemburg | 3a45779 | 2006-08-14 12:57:27 +0000 | [diff] [blame] | 8049 | if (PyIndex_Check(item)) { |
| 8050 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8051 | if (i == -1 && PyErr_Occurred()) |
| 8052 | return NULL; |
| 8053 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8054 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8055 | return unicode_getitem(self, i); |
| 8056 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8057 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8058 | Py_UNICODE* source_buf; |
| 8059 | Py_UNICODE* result_buf; |
| 8060 | PyObject* result; |
| 8061 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8062 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8063 | &start, &stop, &step, &slicelength) < 0) { |
| 8064 | return NULL; |
| 8065 | } |
| 8066 | |
| 8067 | if (slicelength <= 0) { |
| 8068 | return PyUnicode_FromUnicode(NULL, 0); |
Thomas Wouters | 3ccec68 | 2007-08-28 15:28:19 +0000 | [diff] [blame] | 8069 | } else if (start == 0 && step == 1 && slicelength == self->length && |
| 8070 | PyUnicode_CheckExact(self)) { |
| 8071 | Py_INCREF(self); |
| 8072 | return (PyObject *)self; |
| 8073 | } else if (step == 1) { |
| 8074 | return PyUnicode_FromUnicode(self->str + start, slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8075 | } else { |
| 8076 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 8077 | result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* |
| 8078 | sizeof(Py_UNICODE)); |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8079 | |
| 8080 | if (result_buf == NULL) |
| 8081 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8082 | |
| 8083 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 8084 | result_buf[i] = source_buf[cur]; |
| 8085 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8086 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8087 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 8088 | PyObject_FREE(result_buf); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8089 | return result; |
| 8090 | } |
| 8091 | } else { |
| 8092 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 8093 | return NULL; |
| 8094 | } |
| 8095 | } |
| 8096 | |
| 8097 | static PyMappingMethods unicode_as_mapping = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8098 | (lenfunc)unicode_length, /* mp_length */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8099 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 8100 | (objobjargproc)0, /* mp_ass_subscript */ |
| 8101 | }; |
| 8102 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8103 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8104 | unicode_buffer_getreadbuf(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8105 | Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8106 | const void **ptr) |
| 8107 | { |
| 8108 | if (index != 0) { |
| 8109 | PyErr_SetString(PyExc_SystemError, |
| 8110 | "accessing non-existent unicode segment"); |
| 8111 | return -1; |
| 8112 | } |
| 8113 | *ptr = (void *) self->str; |
| 8114 | return PyUnicode_GET_DATA_SIZE(self); |
| 8115 | } |
| 8116 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8117 | static Py_ssize_t |
| 8118 | unicode_buffer_getwritebuf(PyUnicodeObject *self, Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8119 | const void **ptr) |
| 8120 | { |
| 8121 | PyErr_SetString(PyExc_TypeError, |
Neal Norwitz | 20e7213 | 2002-06-13 21:25:17 +0000 | [diff] [blame] | 8122 | "cannot use unicode as modifiable buffer"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8123 | return -1; |
| 8124 | } |
| 8125 | |
| 8126 | static int |
| 8127 | unicode_buffer_getsegcount(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8128 | Py_ssize_t *lenp) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8129 | { |
| 8130 | if (lenp) |
| 8131 | *lenp = PyUnicode_GET_DATA_SIZE(self); |
| 8132 | return 1; |
| 8133 | } |
| 8134 | |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 8135 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8136 | unicode_buffer_getcharbuf(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8137 | Py_ssize_t index, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8138 | const void **ptr) |
| 8139 | { |
| 8140 | PyObject *str; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8141 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8142 | if (index != 0) { |
| 8143 | PyErr_SetString(PyExc_SystemError, |
| 8144 | "accessing non-existent unicode segment"); |
| 8145 | return -1; |
| 8146 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 8147 | str = _PyUnicode_AsDefaultEncodedString((PyObject *)self, NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8148 | if (str == NULL) |
| 8149 | return -1; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 8150 | *ptr = (void *) PyString_AS_STRING(str); |
| 8151 | return PyString_GET_SIZE(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8152 | } |
| 8153 | |
| 8154 | /* Helpers for PyUnicode_Format() */ |
| 8155 | |
| 8156 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8157 | 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] | 8158 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8159 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8160 | if (argidx < arglen) { |
| 8161 | (*p_argidx)++; |
| 8162 | if (arglen < 0) |
| 8163 | return args; |
| 8164 | else |
| 8165 | return PyTuple_GetItem(args, argidx); |
| 8166 | } |
| 8167 | PyErr_SetString(PyExc_TypeError, |
| 8168 | "not enough arguments for format string"); |
| 8169 | return NULL; |
| 8170 | } |
| 8171 | |
| 8172 | #define F_LJUST (1<<0) |
| 8173 | #define F_SIGN (1<<1) |
| 8174 | #define F_BLANK (1<<2) |
| 8175 | #define F_ALT (1<<3) |
| 8176 | #define F_ZERO (1<<4) |
| 8177 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8178 | static Py_ssize_t |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8179 | strtounicode(Py_UNICODE *buffer, const char *charbuffer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8180 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8181 | register Py_ssize_t i; |
| 8182 | Py_ssize_t len = strlen(charbuffer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8183 | for (i = len - 1; i >= 0; i--) |
| 8184 | buffer[i] = (Py_UNICODE) charbuffer[i]; |
| 8185 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8186 | return len; |
| 8187 | } |
| 8188 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8189 | static int |
| 8190 | doubletounicode(Py_UNICODE *buffer, size_t len, const char *format, double x) |
| 8191 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8192 | Py_ssize_t result; |
| 8193 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8194 | PyOS_ascii_formatd((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8195 | result = strtounicode(buffer, (char *)buffer); |
| 8196 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8197 | } |
| 8198 | |
| 8199 | static int |
| 8200 | longtounicode(Py_UNICODE *buffer, size_t len, const char *format, long x) |
| 8201 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8202 | Py_ssize_t result; |
| 8203 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8204 | PyOS_snprintf((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8205 | result = strtounicode(buffer, (char *)buffer); |
| 8206 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8207 | } |
| 8208 | |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 8209 | /* XXX To save some code duplication, formatfloat/long/int could have been |
| 8210 | shared with stringobject.c, converting from 8-bit to Unicode after the |
| 8211 | formatting is done. */ |
| 8212 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8213 | static int |
| 8214 | formatfloat(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8215 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8216 | int flags, |
| 8217 | int prec, |
| 8218 | int type, |
| 8219 | PyObject *v) |
| 8220 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8221 | /* fmt = '%#.' + `prec` + `type` |
| 8222 | 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] | 8223 | char fmt[20]; |
| 8224 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8225 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8226 | x = PyFloat_AsDouble(v); |
| 8227 | if (x == -1.0 && PyErr_Occurred()) |
| 8228 | return -1; |
| 8229 | if (prec < 0) |
| 8230 | prec = 6; |
Eric Smith | d6c393a | 2008-07-17 19:49:47 +0000 | [diff] [blame] | 8231 | if (type == 'f' && (fabs(x) / 1e25) >= 1e25) |
| 8232 | type = 'g'; |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8233 | /* Worst case length calc to ensure no buffer overrun: |
| 8234 | |
| 8235 | 'g' formats: |
| 8236 | fmt = %#.<prec>g |
| 8237 | buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
| 8238 | for any double rep.) |
| 8239 | len = 1 + prec + 1 + 2 + 5 = 9 + prec |
| 8240 | |
| 8241 | 'f' formats: |
| 8242 | buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
| 8243 | len = 1 + 50 + 1 + prec = 52 + prec |
| 8244 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8245 | If prec=0 the effective precision is 1 (the leading digit is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8246 | always given), therefore increase the length by one. |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8247 | |
| 8248 | */ |
Georg Brandl | 7c3b50d | 2007-07-12 08:38:00 +0000 | [diff] [blame] | 8249 | if (((type == 'g' || type == 'G') && |
| 8250 | buflen <= (size_t)10 + (size_t)prec) || |
Eric Smith | d6c393a | 2008-07-17 19:49:47 +0000 | [diff] [blame] | 8251 | (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8252 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8253 | "formatted float is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8254 | return -1; |
| 8255 | } |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8256 | PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
| 8257 | (flags&F_ALT) ? "#" : "", |
| 8258 | prec, type); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8259 | return doubletounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8260 | } |
| 8261 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8262 | static PyObject* |
| 8263 | formatlong(PyObject *val, int flags, int prec, int type) |
| 8264 | { |
| 8265 | char *buf; |
| 8266 | int i, len; |
| 8267 | PyObject *str; /* temporary string object. */ |
| 8268 | PyUnicodeObject *result; |
| 8269 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 8270 | str = _PyString_FormatLong(val, flags, prec, type, &buf, &len); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8271 | if (!str) |
| 8272 | return NULL; |
| 8273 | result = _PyUnicode_New(len); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 8274 | if (!result) { |
| 8275 | Py_DECREF(str); |
| 8276 | return NULL; |
| 8277 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8278 | for (i = 0; i < len; i++) |
| 8279 | result->str[i] = buf[i]; |
| 8280 | result->str[len] = 0; |
| 8281 | Py_DECREF(str); |
| 8282 | return (PyObject*)result; |
| 8283 | } |
| 8284 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8285 | static int |
| 8286 | formatint(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8287 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8288 | int flags, |
| 8289 | int prec, |
| 8290 | int type, |
| 8291 | PyObject *v) |
| 8292 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8293 | /* fmt = '%#.' + `prec` + 'l' + `type` |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8294 | * worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
| 8295 | * + 1 + 1 |
| 8296 | * = 24 |
| 8297 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8298 | char fmt[64]; /* plenty big enough! */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8299 | char *sign; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8300 | long x; |
| 8301 | |
| 8302 | x = PyInt_AsLong(v); |
| 8303 | if (x == -1 && PyErr_Occurred()) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8304 | return -1; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8305 | if (x < 0 && type == 'u') { |
| 8306 | type = 'd'; |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 8307 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8308 | if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) |
| 8309 | sign = "-"; |
| 8310 | else |
| 8311 | sign = ""; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8312 | if (prec < 0) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8313 | prec = 1; |
| 8314 | |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8315 | /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) |
| 8316 | * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8317 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8318 | if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8319 | PyErr_SetString(PyExc_OverflowError, |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8320 | "formatted integer is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8321 | return -1; |
| 8322 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8323 | |
| 8324 | if ((flags & F_ALT) && |
| 8325 | (type == 'x' || type == 'X')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8326 | /* When converting under %#x or %#X, there are a number |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8327 | * of issues that cause pain: |
| 8328 | * - when 0 is being converted, the C standard leaves off |
| 8329 | * the '0x' or '0X', which is inconsistent with other |
| 8330 | * %#x/%#X conversions and inconsistent with Python's |
| 8331 | * hex() function |
| 8332 | * - there are platforms that violate the standard and |
| 8333 | * convert 0 with the '0x' or '0X' |
| 8334 | * (Metrowerks, Compaq Tru64) |
| 8335 | * - there are platforms that give '0x' when converting |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8336 | * under %#X, but convert 0 in accordance with the |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8337 | * standard (OS/2 EMX) |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8338 | * |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8339 | * We can achieve the desired consistency by inserting our |
| 8340 | * own '0x' or '0X' prefix, and substituting %x/%X in place |
| 8341 | * of %#x/%#X. |
| 8342 | * |
| 8343 | * Note that this is the same approach as used in |
| 8344 | * formatint() in stringobject.c |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 8345 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8346 | PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", |
| 8347 | sign, type, prec, type); |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 8348 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8349 | else { |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8350 | PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", |
| 8351 | sign, (flags&F_ALT) ? "#" : "", |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8352 | prec, type); |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 8353 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8354 | if (sign[0]) |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8355 | return longtounicode(buf, buflen, fmt, -x); |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8356 | else |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8357 | return longtounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8358 | } |
| 8359 | |
| 8360 | static int |
| 8361 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8362 | size_t buflen, |
| 8363 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8364 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8365 | /* presume that the buffer is at least 2 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8366 | if (PyUnicode_Check(v)) { |
| 8367 | if (PyUnicode_GET_SIZE(v) != 1) |
| 8368 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8369 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8370 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8371 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 8372 | else if (PyString_Check(v)) { |
| 8373 | if (PyString_GET_SIZE(v) != 1) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8374 | goto onError; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 8375 | buf[0] = (Py_UNICODE)PyString_AS_STRING(v)[0]; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8376 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8377 | |
| 8378 | else { |
| 8379 | /* Integer input truncated to a character */ |
| 8380 | long x; |
| 8381 | x = PyInt_AsLong(v); |
| 8382 | if (x == -1 && PyErr_Occurred()) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8383 | goto onError; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8384 | #ifdef Py_UNICODE_WIDE |
| 8385 | if (x < 0 || x > 0x10ffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 8386 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8387 | "%c arg not in range(0x110000) " |
| 8388 | "(wide Python build)"); |
| 8389 | return -1; |
| 8390 | } |
| 8391 | #else |
| 8392 | if (x < 0 || x > 0xffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 8393 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8394 | "%c arg not in range(0x10000) " |
| 8395 | "(narrow Python build)"); |
| 8396 | return -1; |
| 8397 | } |
| 8398 | #endif |
| 8399 | buf[0] = (Py_UNICODE) x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8400 | } |
| 8401 | buf[1] = '\0'; |
| 8402 | return 1; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8403 | |
| 8404 | onError: |
| 8405 | PyErr_SetString(PyExc_TypeError, |
| 8406 | "%c requires int or char"); |
| 8407 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8408 | } |
| 8409 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8410 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 8411 | |
| 8412 | FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
| 8413 | chars are formatted. XXX This is a magic number. Each formatting |
| 8414 | routine does bounds checking to ensure no overflow, but a better |
| 8415 | solution may be to malloc a buffer of appropriate size for each |
| 8416 | format. For now, the current solution is sufficient. |
| 8417 | */ |
| 8418 | #define FORMATBUFLEN (size_t)120 |
| 8419 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8420 | PyObject *PyUnicode_Format(PyObject *format, |
| 8421 | PyObject *args) |
| 8422 | { |
| 8423 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8424 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8425 | int args_owned = 0; |
| 8426 | PyUnicodeObject *result = NULL; |
| 8427 | PyObject *dict = NULL; |
| 8428 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8429 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8430 | if (format == NULL || args == NULL) { |
| 8431 | PyErr_BadInternalCall(); |
| 8432 | return NULL; |
| 8433 | } |
| 8434 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8435 | if (uformat == NULL) |
| 8436 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8437 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 8438 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 8439 | |
| 8440 | reslen = rescnt = fmtcnt + 100; |
| 8441 | result = _PyUnicode_New(reslen); |
| 8442 | if (result == NULL) |
| 8443 | goto onError; |
| 8444 | res = PyUnicode_AS_UNICODE(result); |
| 8445 | |
| 8446 | if (PyTuple_Check(args)) { |
| 8447 | arglen = PyTuple_Size(args); |
| 8448 | argidx = 0; |
| 8449 | } |
| 8450 | else { |
| 8451 | arglen = -1; |
| 8452 | argidx = -2; |
| 8453 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 8454 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Neal Norwitz | 80a1bf4 | 2002-11-12 23:01:12 +0000 | [diff] [blame] | 8455 | !PyObject_TypeCheck(args, &PyBaseString_Type)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8456 | dict = args; |
| 8457 | |
| 8458 | while (--fmtcnt >= 0) { |
| 8459 | if (*fmt != '%') { |
| 8460 | if (--rescnt < 0) { |
| 8461 | rescnt = fmtcnt + 100; |
| 8462 | reslen += rescnt; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8463 | if (_PyUnicode_Resize(&result, reslen) < 0) |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 8464 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8465 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 8466 | --rescnt; |
| 8467 | } |
| 8468 | *res++ = *fmt++; |
| 8469 | } |
| 8470 | else { |
| 8471 | /* Got a format specifier */ |
| 8472 | int flags = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8473 | Py_ssize_t width = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8474 | int prec = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8475 | Py_UNICODE c = '\0'; |
| 8476 | Py_UNICODE fill; |
Facundo Batista | c11cecf | 2008-02-24 03:17:21 +0000 | [diff] [blame] | 8477 | int isnumok; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8478 | PyObject *v = NULL; |
| 8479 | PyObject *temp = NULL; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8480 | Py_UNICODE *pbuf; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8481 | Py_UNICODE sign; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8482 | Py_ssize_t len; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8483 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8484 | |
| 8485 | fmt++; |
| 8486 | if (*fmt == '(') { |
| 8487 | Py_UNICODE *keystart; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8488 | Py_ssize_t keylen; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8489 | PyObject *key; |
| 8490 | int pcount = 1; |
| 8491 | |
| 8492 | if (dict == NULL) { |
| 8493 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8494 | "format requires a mapping"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8495 | goto onError; |
| 8496 | } |
| 8497 | ++fmt; |
| 8498 | --fmtcnt; |
| 8499 | keystart = fmt; |
| 8500 | /* Skip over balanced parentheses */ |
| 8501 | while (pcount > 0 && --fmtcnt >= 0) { |
| 8502 | if (*fmt == ')') |
| 8503 | --pcount; |
| 8504 | else if (*fmt == '(') |
| 8505 | ++pcount; |
| 8506 | fmt++; |
| 8507 | } |
| 8508 | keylen = fmt - keystart - 1; |
| 8509 | if (fmtcnt < 0 || pcount > 0) { |
| 8510 | PyErr_SetString(PyExc_ValueError, |
| 8511 | "incomplete format key"); |
| 8512 | goto onError; |
| 8513 | } |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 8514 | #if 0 |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8515 | /* keys are converted to strings using UTF-8 and |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8516 | then looked up since Python uses strings to hold |
| 8517 | variables names etc. in its namespaces and we |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8518 | wouldn't want to break common idioms. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8519 | key = PyUnicode_EncodeUTF8(keystart, |
| 8520 | keylen, |
| 8521 | NULL); |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 8522 | #else |
| 8523 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 8524 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8525 | if (key == NULL) |
| 8526 | goto onError; |
| 8527 | if (args_owned) { |
| 8528 | Py_DECREF(args); |
| 8529 | args_owned = 0; |
| 8530 | } |
| 8531 | args = PyObject_GetItem(dict, key); |
| 8532 | Py_DECREF(key); |
| 8533 | if (args == NULL) { |
| 8534 | goto onError; |
| 8535 | } |
| 8536 | args_owned = 1; |
| 8537 | arglen = -1; |
| 8538 | argidx = -2; |
| 8539 | } |
| 8540 | while (--fmtcnt >= 0) { |
| 8541 | switch (c = *fmt++) { |
| 8542 | case '-': flags |= F_LJUST; continue; |
| 8543 | case '+': flags |= F_SIGN; continue; |
| 8544 | case ' ': flags |= F_BLANK; continue; |
| 8545 | case '#': flags |= F_ALT; continue; |
| 8546 | case '0': flags |= F_ZERO; continue; |
| 8547 | } |
| 8548 | break; |
| 8549 | } |
| 8550 | if (c == '*') { |
| 8551 | v = getnextarg(args, arglen, &argidx); |
| 8552 | if (v == NULL) |
| 8553 | goto onError; |
| 8554 | if (!PyInt_Check(v)) { |
| 8555 | PyErr_SetString(PyExc_TypeError, |
| 8556 | "* wants int"); |
| 8557 | goto onError; |
| 8558 | } |
| 8559 | width = PyInt_AsLong(v); |
| 8560 | if (width < 0) { |
| 8561 | flags |= F_LJUST; |
| 8562 | width = -width; |
| 8563 | } |
| 8564 | if (--fmtcnt >= 0) |
| 8565 | c = *fmt++; |
| 8566 | } |
| 8567 | else if (c >= '0' && c <= '9') { |
| 8568 | width = c - '0'; |
| 8569 | while (--fmtcnt >= 0) { |
| 8570 | c = *fmt++; |
| 8571 | if (c < '0' || c > '9') |
| 8572 | break; |
| 8573 | if ((width*10) / 10 != width) { |
| 8574 | PyErr_SetString(PyExc_ValueError, |
| 8575 | "width too big"); |
| 8576 | goto onError; |
| 8577 | } |
| 8578 | width = width*10 + (c - '0'); |
| 8579 | } |
| 8580 | } |
| 8581 | if (c == '.') { |
| 8582 | prec = 0; |
| 8583 | if (--fmtcnt >= 0) |
| 8584 | c = *fmt++; |
| 8585 | if (c == '*') { |
| 8586 | v = getnextarg(args, arglen, &argidx); |
| 8587 | if (v == NULL) |
| 8588 | goto onError; |
| 8589 | if (!PyInt_Check(v)) { |
| 8590 | PyErr_SetString(PyExc_TypeError, |
| 8591 | "* wants int"); |
| 8592 | goto onError; |
| 8593 | } |
| 8594 | prec = PyInt_AsLong(v); |
| 8595 | if (prec < 0) |
| 8596 | prec = 0; |
| 8597 | if (--fmtcnt >= 0) |
| 8598 | c = *fmt++; |
| 8599 | } |
| 8600 | else if (c >= '0' && c <= '9') { |
| 8601 | prec = c - '0'; |
| 8602 | while (--fmtcnt >= 0) { |
| 8603 | c = Py_CHARMASK(*fmt++); |
| 8604 | if (c < '0' || c > '9') |
| 8605 | break; |
| 8606 | if ((prec*10) / 10 != prec) { |
| 8607 | PyErr_SetString(PyExc_ValueError, |
| 8608 | "prec too big"); |
| 8609 | goto onError; |
| 8610 | } |
| 8611 | prec = prec*10 + (c - '0'); |
| 8612 | } |
| 8613 | } |
| 8614 | } /* prec */ |
| 8615 | if (fmtcnt >= 0) { |
| 8616 | if (c == 'h' || c == 'l' || c == 'L') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8617 | if (--fmtcnt >= 0) |
| 8618 | c = *fmt++; |
| 8619 | } |
| 8620 | } |
| 8621 | if (fmtcnt < 0) { |
| 8622 | PyErr_SetString(PyExc_ValueError, |
| 8623 | "incomplete format"); |
| 8624 | goto onError; |
| 8625 | } |
| 8626 | if (c != '%') { |
| 8627 | v = getnextarg(args, arglen, &argidx); |
| 8628 | if (v == NULL) |
| 8629 | goto onError; |
| 8630 | } |
| 8631 | sign = 0; |
| 8632 | fill = ' '; |
| 8633 | switch (c) { |
| 8634 | |
| 8635 | case '%': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8636 | pbuf = formatbuf; |
| 8637 | /* presume that buffer length is at least 1 */ |
| 8638 | pbuf[0] = '%'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8639 | len = 1; |
| 8640 | break; |
| 8641 | |
| 8642 | case 's': |
| 8643 | case 'r': |
| 8644 | if (PyUnicode_Check(v) && c == 's') { |
| 8645 | temp = v; |
| 8646 | Py_INCREF(temp); |
| 8647 | } |
| 8648 | else { |
| 8649 | PyObject *unicode; |
| 8650 | if (c == 's') |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8651 | temp = PyObject_Unicode(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8652 | else |
| 8653 | temp = PyObject_Repr(v); |
| 8654 | if (temp == NULL) |
| 8655 | goto onError; |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8656 | if (PyUnicode_Check(temp)) |
| 8657 | /* nothing to do */; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 8658 | else if (PyString_Check(temp)) { |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8659 | /* convert to string to Unicode */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 8660 | unicode = PyUnicode_Decode(PyString_AS_STRING(temp), |
| 8661 | PyString_GET_SIZE(temp), |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8662 | NULL, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8663 | "strict"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8664 | Py_DECREF(temp); |
| 8665 | temp = unicode; |
| 8666 | if (temp == NULL) |
| 8667 | goto onError; |
| 8668 | } |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 8669 | else { |
| 8670 | Py_DECREF(temp); |
| 8671 | PyErr_SetString(PyExc_TypeError, |
| 8672 | "%s argument has non-string str()"); |
| 8673 | goto onError; |
| 8674 | } |
| 8675 | } |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8676 | pbuf = PyUnicode_AS_UNICODE(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8677 | len = PyUnicode_GET_SIZE(temp); |
| 8678 | if (prec >= 0 && len > prec) |
| 8679 | len = prec; |
| 8680 | break; |
| 8681 | |
| 8682 | case 'i': |
| 8683 | case 'd': |
| 8684 | case 'u': |
| 8685 | case 'o': |
| 8686 | case 'x': |
| 8687 | case 'X': |
| 8688 | if (c == 'i') |
| 8689 | c = 'd'; |
Facundo Batista | c11cecf | 2008-02-24 03:17:21 +0000 | [diff] [blame] | 8690 | isnumok = 0; |
| 8691 | if (PyNumber_Check(v)) { |
| 8692 | PyObject *iobj=NULL; |
| 8693 | |
| 8694 | if (PyInt_Check(v) || (PyLong_Check(v))) { |
| 8695 | iobj = v; |
| 8696 | Py_INCREF(iobj); |
| 8697 | } |
| 8698 | else { |
| 8699 | iobj = PyNumber_Int(v); |
| 8700 | if (iobj==NULL) iobj = PyNumber_Long(v); |
| 8701 | } |
| 8702 | if (iobj!=NULL) { |
| 8703 | if (PyInt_Check(iobj)) { |
| 8704 | isnumok = 1; |
| 8705 | pbuf = formatbuf; |
| 8706 | len = formatint(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 8707 | flags, prec, c, iobj); |
| 8708 | Py_DECREF(iobj); |
| 8709 | if (len < 0) |
| 8710 | goto onError; |
| 8711 | sign = 1; |
| 8712 | } |
| 8713 | else if (PyLong_Check(iobj)) { |
| 8714 | isnumok = 1; |
| 8715 | temp = formatlong(iobj, flags, prec, c); |
| 8716 | Py_DECREF(iobj); |
| 8717 | if (!temp) |
| 8718 | goto onError; |
| 8719 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 8720 | len = PyUnicode_GET_SIZE(temp); |
| 8721 | sign = 1; |
| 8722 | } |
| 8723 | else { |
| 8724 | Py_DECREF(iobj); |
| 8725 | } |
| 8726 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8727 | } |
Facundo Batista | c11cecf | 2008-02-24 03:17:21 +0000 | [diff] [blame] | 8728 | if (!isnumok) { |
| 8729 | PyErr_Format(PyExc_TypeError, |
| 8730 | "%%%c format: a number is required, " |
Martin v. Löwis | d918e4e | 2008-04-07 03:08:28 +0000 | [diff] [blame] | 8731 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8732 | goto onError; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8733 | } |
| 8734 | if (flags & F_ZERO) |
| 8735 | fill = '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8736 | break; |
| 8737 | |
| 8738 | case 'e': |
| 8739 | case 'E': |
| 8740 | case 'f': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 8741 | case 'F': |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8742 | case 'g': |
| 8743 | case 'G': |
Eric Smith | d6c393a | 2008-07-17 19:49:47 +0000 | [diff] [blame] | 8744 | if (c == 'F') |
| 8745 | c = 'f'; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8746 | pbuf = formatbuf; |
| 8747 | len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 8748 | flags, prec, c, v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8749 | if (len < 0) |
| 8750 | goto onError; |
| 8751 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8752 | if (flags & F_ZERO) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8753 | fill = '0'; |
| 8754 | break; |
| 8755 | |
| 8756 | case 'c': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8757 | pbuf = formatbuf; |
| 8758 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8759 | if (len < 0) |
| 8760 | goto onError; |
| 8761 | break; |
| 8762 | |
| 8763 | default: |
| 8764 | PyErr_Format(PyExc_ValueError, |
Andrew M. Kuchling | 6ca8917 | 2000-12-15 13:07:46 +0000 | [diff] [blame] | 8765 | "unsupported format character '%c' (0x%x) " |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 8766 | "at index %zd", |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8767 | (31<=c && c<=126) ? (char)c : '?', |
Marc-André Lemburg | 24e53b6 | 2002-09-24 09:32:14 +0000 | [diff] [blame] | 8768 | (int)c, |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 8769 | (Py_ssize_t)(fmt - 1 - |
| 8770 | PyUnicode_AS_UNICODE(uformat))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8771 | goto onError; |
| 8772 | } |
| 8773 | if (sign) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8774 | if (*pbuf == '-' || *pbuf == '+') { |
| 8775 | sign = *pbuf++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8776 | len--; |
| 8777 | } |
| 8778 | else if (flags & F_SIGN) |
| 8779 | sign = '+'; |
| 8780 | else if (flags & F_BLANK) |
| 8781 | sign = ' '; |
| 8782 | else |
| 8783 | sign = 0; |
| 8784 | } |
| 8785 | if (width < len) |
| 8786 | width = len; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8787 | if (rescnt - (sign != 0) < width) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8788 | reslen -= rescnt; |
| 8789 | rescnt = width + fmtcnt + 100; |
| 8790 | reslen += rescnt; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8791 | if (reslen < 0) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 8792 | Py_XDECREF(temp); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8793 | PyErr_NoMemory(); |
| 8794 | goto onError; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 8795 | } |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8796 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 8797 | Py_XDECREF(temp); |
| 8798 | goto onError; |
| 8799 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8800 | res = PyUnicode_AS_UNICODE(result) |
| 8801 | + reslen - rescnt; |
| 8802 | } |
| 8803 | if (sign) { |
| 8804 | if (fill != ' ') |
| 8805 | *res++ = sign; |
| 8806 | rescnt--; |
| 8807 | if (width > len) |
| 8808 | width--; |
| 8809 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8810 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
| 8811 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8812 | assert(pbuf[1] == c); |
| 8813 | if (fill != ' ') { |
| 8814 | *res++ = *pbuf++; |
| 8815 | *res++ = *pbuf++; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8816 | } |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8817 | rescnt -= 2; |
| 8818 | width -= 2; |
| 8819 | if (width < 0) |
| 8820 | width = 0; |
| 8821 | len -= 2; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8822 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8823 | if (width > len && !(flags & F_LJUST)) { |
| 8824 | do { |
| 8825 | --rescnt; |
| 8826 | *res++ = fill; |
| 8827 | } while (--width > len); |
| 8828 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8829 | if (fill == ' ') { |
| 8830 | if (sign) |
| 8831 | *res++ = sign; |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8832 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8833 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 8834 | assert(pbuf[1] == c); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8835 | *res++ = *pbuf++; |
| 8836 | *res++ = *pbuf++; |
| 8837 | } |
| 8838 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8839 | Py_UNICODE_COPY(res, pbuf, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8840 | res += len; |
| 8841 | rescnt -= len; |
| 8842 | while (--width >= len) { |
| 8843 | --rescnt; |
| 8844 | *res++ = ' '; |
| 8845 | } |
| 8846 | if (dict && (argidx < arglen) && c != '%') { |
| 8847 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 8848 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8849 | Py_XDECREF(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8850 | goto onError; |
| 8851 | } |
| 8852 | Py_XDECREF(temp); |
| 8853 | } /* '%' */ |
| 8854 | } /* until end */ |
| 8855 | if (argidx < arglen && !dict) { |
| 8856 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 8857 | "not all arguments converted during string formatting"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8858 | goto onError; |
| 8859 | } |
| 8860 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 8861 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
| 8862 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8863 | if (args_owned) { |
| 8864 | Py_DECREF(args); |
| 8865 | } |
| 8866 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8867 | return (PyObject *)result; |
| 8868 | |
| 8869 | onError: |
| 8870 | Py_XDECREF(result); |
| 8871 | Py_DECREF(uformat); |
| 8872 | if (args_owned) { |
| 8873 | Py_DECREF(args); |
| 8874 | } |
| 8875 | return NULL; |
| 8876 | } |
| 8877 | |
| 8878 | static PyBufferProcs unicode_as_buffer = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8879 | (readbufferproc) unicode_buffer_getreadbuf, |
| 8880 | (writebufferproc) unicode_buffer_getwritebuf, |
| 8881 | (segcountproc) unicode_buffer_getsegcount, |
| 8882 | (charbufferproc) unicode_buffer_getcharbuf, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8883 | }; |
| 8884 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 8885 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8886 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 8887 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8888 | static PyObject * |
| 8889 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 8890 | { |
| 8891 | PyObject *x = NULL; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 8892 | static char *kwlist[] = {"string", "encoding", "errors", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8893 | char *encoding = NULL; |
| 8894 | char *errors = NULL; |
| 8895 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8896 | if (type != &PyUnicode_Type) |
| 8897 | return unicode_subtype_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8898 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:unicode", |
| 8899 | kwlist, &x, &encoding, &errors)) |
| 8900 | return NULL; |
| 8901 | if (x == NULL) |
| 8902 | return (PyObject *)_PyUnicode_New(0); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 8903 | if (encoding == NULL && errors == NULL) |
| 8904 | return PyObject_Unicode(x); |
| 8905 | else |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8906 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
| 8907 | } |
| 8908 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8909 | static PyObject * |
| 8910 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 8911 | { |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8912 | PyUnicodeObject *tmp, *pnew; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8913 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8914 | |
| 8915 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 8916 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 8917 | if (tmp == NULL) |
| 8918 | return NULL; |
| 8919 | assert(PyUnicode_Check(tmp)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8920 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 8921 | if (pnew == NULL) { |
| 8922 | Py_DECREF(tmp); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8923 | return NULL; |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 8924 | } |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 8925 | pnew->str = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8926 | if (pnew->str == NULL) { |
| 8927 | _Py_ForgetReference((PyObject *)pnew); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 8928 | PyObject_Del(pnew); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 8929 | Py_DECREF(tmp); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 8930 | return PyErr_NoMemory(); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8931 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8932 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 8933 | pnew->length = n; |
| 8934 | pnew->hash = tmp->hash; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8935 | Py_DECREF(tmp); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 8936 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 8937 | } |
| 8938 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8939 | PyDoc_STRVAR(unicode_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8940 | "unicode(string [, encoding[, errors]]) -> object\n\ |
| 8941 | \n\ |
| 8942 | Create a new Unicode object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 8943 | encoding defaults to the current default string encoding.\n\ |
| 8944 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8945 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8946 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 8947 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8948 | "unicode", /* tp_name */ |
| 8949 | sizeof(PyUnicodeObject), /* tp_size */ |
| 8950 | 0, /* tp_itemsize */ |
| 8951 | /* Slots */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 8952 | (destructor)unicode_dealloc, /* tp_dealloc */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8953 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8954 | 0, /* tp_getattr */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8955 | 0, /* tp_setattr */ |
Marc-André Lemburg | 040f76b | 2006-08-14 10:55:19 +0000 | [diff] [blame] | 8956 | 0, /* tp_compare */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 8957 | unicode_repr, /* tp_repr */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8958 | &unicode_as_number, /* tp_as_number */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8959 | &unicode_as_sequence, /* tp_as_sequence */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8960 | &unicode_as_mapping, /* tp_as_mapping */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8961 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 8962 | 0, /* tp_call*/ |
| 8963 | (reprfunc) unicode_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8964 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 8965 | 0, /* tp_setattro */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8966 | &unicode_as_buffer, /* tp_as_buffer */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8967 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
Neal Norwitz | ee3a1b5 | 2007-02-25 19:44:48 +0000 | [diff] [blame] | 8968 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8969 | unicode_doc, /* tp_doc */ |
| 8970 | 0, /* tp_traverse */ |
| 8971 | 0, /* tp_clear */ |
Marc-André Lemburg | 040f76b | 2006-08-14 10:55:19 +0000 | [diff] [blame] | 8972 | PyUnicode_RichCompare, /* tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8973 | 0, /* tp_weaklistoffset */ |
| 8974 | 0, /* tp_iter */ |
| 8975 | 0, /* tp_iternext */ |
| 8976 | unicode_methods, /* tp_methods */ |
| 8977 | 0, /* tp_members */ |
| 8978 | 0, /* tp_getset */ |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 8979 | &PyBaseString_Type, /* tp_base */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8980 | 0, /* tp_dict */ |
| 8981 | 0, /* tp_descr_get */ |
| 8982 | 0, /* tp_descr_set */ |
| 8983 | 0, /* tp_dictoffset */ |
| 8984 | 0, /* tp_init */ |
| 8985 | 0, /* tp_alloc */ |
| 8986 | unicode_new, /* tp_new */ |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 8987 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8988 | }; |
| 8989 | |
| 8990 | /* Initialize the Unicode implementation */ |
| 8991 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 8992 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8993 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8994 | int i; |
| 8995 | |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 8996 | /* XXX - move this array to unicodectype.c ? */ |
| 8997 | Py_UNICODE linebreak[] = { |
| 8998 | 0x000A, /* LINE FEED */ |
| 8999 | 0x000D, /* CARRIAGE RETURN */ |
| 9000 | 0x001C, /* FILE SEPARATOR */ |
| 9001 | 0x001D, /* GROUP SEPARATOR */ |
| 9002 | 0x001E, /* RECORD SEPARATOR */ |
| 9003 | 0x0085, /* NEXT LINE */ |
| 9004 | 0x2028, /* LINE SEPARATOR */ |
| 9005 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 9006 | }; |
| 9007 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 9008 | /* Init the implementation */ |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 9009 | free_list = NULL; |
| 9010 | numfree = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9011 | unicode_empty = _PyUnicode_New(0); |
Neal Norwitz | e1fdb32 | 2006-07-21 05:32:28 +0000 | [diff] [blame] | 9012 | if (!unicode_empty) |
| 9013 | return; |
| 9014 | |
Marc-André Lemburg | 90e8147 | 2000-06-07 09:13:21 +0000 | [diff] [blame] | 9015 | strcpy(unicode_default_encoding, "ascii"); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9016 | for (i = 0; i < 256; i++) |
| 9017 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 9018 | if (PyType_Ready(&PyUnicode_Type) < 0) |
| 9019 | Py_FatalError("Can't initialize 'unicode'"); |
Fredrik Lundh | b63588c | 2006-05-23 18:44:25 +0000 | [diff] [blame] | 9020 | |
| 9021 | /* initialize the linebreak bloom filter */ |
| 9022 | bloom_linebreak = make_bloom_mask( |
| 9023 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 9024 | ); |
Neal Norwitz | de4c78a | 2006-06-13 08:28:19 +0000 | [diff] [blame] | 9025 | |
| 9026 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9027 | } |
| 9028 | |
| 9029 | /* Finalize the Unicode implementation */ |
| 9030 | |
Christian Heimes | 3b718a7 | 2008-02-14 12:47:33 +0000 | [diff] [blame] | 9031 | int |
| 9032 | PyUnicode_ClearFreeList(void) |
| 9033 | { |
| 9034 | int freelist_size = numfree; |
| 9035 | PyUnicodeObject *u; |
| 9036 | |
| 9037 | for (u = free_list; u != NULL;) { |
| 9038 | PyUnicodeObject *v = u; |
| 9039 | u = *(PyUnicodeObject **)u; |
| 9040 | if (v->str) |
Neal Norwitz | 419fd49 | 2008-03-17 20:22:43 +0000 | [diff] [blame] | 9041 | PyObject_DEL(v->str); |
Christian Heimes | 3b718a7 | 2008-02-14 12:47:33 +0000 | [diff] [blame] | 9042 | Py_XDECREF(v->defenc); |
| 9043 | PyObject_Del(v); |
| 9044 | numfree--; |
| 9045 | } |
| 9046 | free_list = NULL; |
| 9047 | assert(numfree == 0); |
| 9048 | return freelist_size; |
| 9049 | } |
| 9050 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9051 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9052 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9053 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9054 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9055 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 9056 | Py_XDECREF(unicode_empty); |
| 9057 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 9058 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9059 | for (i = 0; i < 256; i++) { |
| 9060 | if (unicode_latin1[i]) { |
| 9061 | Py_DECREF(unicode_latin1[i]); |
| 9062 | unicode_latin1[i] = NULL; |
| 9063 | } |
| 9064 | } |
Christian Heimes | 3b718a7 | 2008-02-14 12:47:33 +0000 | [diff] [blame] | 9065 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9066 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9067 | |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 9068 | #ifdef __cplusplus |
| 9069 | } |
| 9070 | #endif |
| 9071 | |
| 9072 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9073 | /* |
| 9074 | Local variables: |
| 9075 | c-basic-offset: 4 |
| 9076 | indent-tabs-mode: nil |
| 9077 | End: |
| 9078 | */ |