Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1 | /* |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2 | |
| 3 | Unicode implementation based on original code by Fredrik Lundh, |
Fred Drake | 785d14f | 2000-05-09 19:54:43 +0000 | [diff] [blame] | 4 | modified by Marc-Andre Lemburg <mal@lemburg.com> according to the |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5 | Unicode Integration Proposal (see file Misc/unicode.txt). |
| 6 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7 | Major speed upgrades to the method implementations at the Reykjavik |
| 8 | NeedForSpeed sprint, by Fredrik Lundh and Andrew Dalke. |
| 9 | |
Guido van Rossum | 16b1ad9 | 2000-08-03 16:24:25 +0000 | [diff] [blame] | 10 | Copyright (c) Corporation for National Research Initiatives. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 11 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 12 | -------------------------------------------------------------------- |
| 13 | The original string type implementation is: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 14 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 15 | Copyright (c) 1999 by Secret Labs AB |
| 16 | Copyright (c) 1999 by Fredrik Lundh |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 17 | |
Fredrik Lundh | 0fdb90c | 2001-01-19 09:45:02 +0000 | [diff] [blame] | 18 | By obtaining, using, and/or copying this software and/or its |
| 19 | associated documentation, you agree that you have read, understood, |
| 20 | and will comply with the following terms and conditions: |
| 21 | |
| 22 | Permission to use, copy, modify, and distribute this software and its |
| 23 | associated documentation for any purpose and without fee is hereby |
| 24 | granted, provided that the above copyright notice appears in all |
| 25 | copies, and that both that copyright notice and this permission notice |
| 26 | appear in supporting documentation, and that the name of Secret Labs |
| 27 | AB or the author not be used in advertising or publicity pertaining to |
| 28 | distribution of the software without specific, written prior |
| 29 | permission. |
| 30 | |
| 31 | SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 32 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 33 | FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR |
| 34 | ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 35 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 36 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 37 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 38 | -------------------------------------------------------------------- |
| 39 | |
| 40 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 41 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 42 | #define PY_SSIZE_T_CLEAN |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 43 | #include "Python.h" |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 44 | #include "bytes_methods.h" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 46 | #include "unicodeobject.h" |
Marc-André Lemburg | d49e5b4 | 2000-06-30 14:58:20 +0000 | [diff] [blame] | 47 | #include "ucnhash.h" |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 48 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 49 | #ifdef MS_WINDOWS |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 50 | #include <windows.h> |
| 51 | #endif |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 52 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 53 | /* Limit for the Unicode object free list */ |
| 54 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 55 | #define PyUnicode_MAXFREELIST 1024 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 56 | |
| 57 | /* Limit for the Unicode object free list stay alive optimization. |
| 58 | |
| 59 | The implementation will keep allocated Unicode memory intact for |
| 60 | all objects on the free list having a size less than this |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 61 | limit. This reduces malloc() overhead for small Unicode objects. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 62 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 63 | At worst this will result in PyUnicode_MAXFREELIST * |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 64 | (sizeof(PyUnicodeObject) + KEEPALIVE_SIZE_LIMIT + |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 65 | malloc()-overhead) bytes of unused garbage. |
| 66 | |
| 67 | Setting the limit to 0 effectively turns the feature off. |
| 68 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 69 | Note: This is an experimental feature ! If you get core dumps when |
| 70 | using Unicode objects, turn this feature off. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 71 | |
| 72 | */ |
| 73 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 74 | #define KEEPALIVE_SIZE_LIMIT 9 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 75 | |
| 76 | /* Endianness switches; defaults to little endian */ |
| 77 | |
| 78 | #ifdef WORDS_BIGENDIAN |
| 79 | # define BYTEORDER_IS_BIG_ENDIAN |
| 80 | #else |
| 81 | # define BYTEORDER_IS_LITTLE_ENDIAN |
| 82 | #endif |
| 83 | |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 84 | /* --- Globals ------------------------------------------------------------ |
| 85 | |
| 86 | The globals are initialized by the _PyUnicode_Init() API and should |
| 87 | not be used before calling that API. |
| 88 | |
| 89 | */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 90 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 91 | |
| 92 | #ifdef __cplusplus |
| 93 | extern "C" { |
| 94 | #endif |
| 95 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 96 | /* This dictionary holds all interned unicode strings. Note that references |
| 97 | to strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 98 | When the interned string reaches a refcnt of 0 the string deallocation |
| 99 | function will delete the reference from this dictionary. |
| 100 | |
| 101 | Another way to look at this is that to say that the actual reference |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 102 | count of a string is: s->ob_refcnt + (s->state ? 2 : 0) |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 103 | */ |
| 104 | static PyObject *interned; |
| 105 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 106 | /* Free list for Unicode objects */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 107 | static PyUnicodeObject *free_list; |
| 108 | static int numfree; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 109 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 110 | /* The empty Unicode object is shared to improve performance. */ |
| 111 | static PyUnicodeObject *unicode_empty; |
| 112 | |
| 113 | /* Single character Unicode strings in the Latin-1 range are being |
| 114 | shared as well. */ |
| 115 | static PyUnicodeObject *unicode_latin1[256]; |
| 116 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 117 | /* Default encoding to use and assume when NULL is passed as encoding |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 118 | parameter; it is fixed to "utf-8". Always use the |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 119 | PyUnicode_GetDefaultEncoding() API to access this global. |
| 120 | |
Alexandre Vassalotti | 3d2fd7f | 2007-10-16 00:26:33 +0000 | [diff] [blame] | 121 | Don't forget to alter Py_FileSystemDefaultEncoding if you change the |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 122 | hard coded default! |
| 123 | */ |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 124 | static const char unicode_default_encoding[] = "utf-8"; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 125 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 126 | /* Fast detection of the most frequent whitespace characters */ |
| 127 | const unsigned char _Py_ascii_whitespace[] = { |
| 128 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 129 | /* case 0x0009: * HORIZONTAL TABULATION */ |
| 130 | /* case 0x000A: * LINE FEED */ |
| 131 | /* case 0x000B: * VERTICAL TABULATION */ |
| 132 | /* case 0x000C: * FORM FEED */ |
| 133 | /* case 0x000D: * CARRIAGE RETURN */ |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 134 | 0, 1, 1, 1, 1, 1, 0, 0, |
| 135 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 136 | /* case 0x001C: * FILE SEPARATOR */ |
| 137 | /* case 0x001D: * GROUP SEPARATOR */ |
| 138 | /* case 0x001E: * RECORD SEPARATOR */ |
| 139 | /* case 0x001F: * UNIT SEPARATOR */ |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 140 | 0, 0, 0, 0, 1, 1, 1, 1, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 141 | /* case 0x0020: * SPACE */ |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 142 | 1, 0, 0, 0, 0, 0, 0, 0, |
| 143 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 144 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 145 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 146 | |
| 147 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 148 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 149 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 150 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 151 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 152 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 153 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 154 | 0, 0, 0, 0, 0, 0, 0, 0 |
| 155 | }; |
| 156 | |
| 157 | /* Same for linebreaks */ |
| 158 | static unsigned char ascii_linebreak[] = { |
| 159 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 160 | /* 0x000A, * LINE FEED */ |
| 161 | /* 0x000D, * CARRIAGE RETURN */ |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 162 | 0, 0, 1, 0, 0, 1, 0, 0, |
| 163 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 164 | /* 0x001C, * FILE SEPARATOR */ |
| 165 | /* 0x001D, * GROUP SEPARATOR */ |
| 166 | /* 0x001E, * RECORD SEPARATOR */ |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 167 | 0, 0, 0, 0, 1, 1, 1, 0, |
| 168 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 169 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 170 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 171 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 172 | |
| 173 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 174 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 175 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 176 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 177 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 178 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 179 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 180 | 0, 0, 0, 0, 0, 0, 0, 0 |
| 181 | }; |
| 182 | |
| 183 | |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 184 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 185 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 186 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 187 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 188 | return 0x10FFFF; |
| 189 | #else |
| 190 | /* This is actually an illegal character, so it should |
| 191 | not be passed to unichr. */ |
| 192 | return 0xFFFF; |
| 193 | #endif |
| 194 | } |
| 195 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 196 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 197 | |
| 198 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 199 | to keep things simple, we use a single bitmask, using the least 5 |
| 200 | bits from each unicode characters as the bit index. */ |
| 201 | |
| 202 | /* the linebreak mask is set up by Unicode_Init below */ |
| 203 | |
| 204 | #define BLOOM_MASK unsigned long |
| 205 | |
| 206 | static BLOOM_MASK bloom_linebreak; |
| 207 | |
| 208 | #define BLOOM(mask, ch) ((mask & (1 << ((ch) & 0x1F)))) |
| 209 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 210 | #define BLOOM_LINEBREAK(ch) \ |
| 211 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 212 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 213 | |
| 214 | Py_LOCAL_INLINE(BLOOM_MASK) make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len) |
| 215 | { |
| 216 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 217 | |
| 218 | long mask; |
| 219 | Py_ssize_t i; |
| 220 | |
| 221 | mask = 0; |
| 222 | for (i = 0; i < len; i++) |
| 223 | mask |= (1 << (ptr[i] & 0x1F)); |
| 224 | |
| 225 | return mask; |
| 226 | } |
| 227 | |
| 228 | Py_LOCAL_INLINE(int) unicode_member(Py_UNICODE chr, Py_UNICODE* set, Py_ssize_t setlen) |
| 229 | { |
| 230 | Py_ssize_t i; |
| 231 | |
| 232 | for (i = 0; i < setlen; i++) |
| 233 | if (set[i] == chr) |
| 234 | return 1; |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | #define BLOOM_MEMBER(mask, chr, set, setlen)\ |
| 240 | BLOOM(mask, chr) && unicode_member(chr, set, setlen) |
| 241 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 242 | /* --- Unicode Object ----------------------------------------------------- */ |
| 243 | |
| 244 | static |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 245 | int unicode_resize(register PyUnicodeObject *unicode, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 246 | Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 247 | { |
| 248 | void *oldstr; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 249 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 250 | /* Shortcut if there's nothing much to do. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 251 | if (unicode->length == length) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 252 | goto reset; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 253 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 254 | /* Resizing shared object (unicode_empty or single character |
| 255 | objects) in-place is not allowed. Use PyUnicode_Resize() |
| 256 | instead ! */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 257 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 258 | if (unicode == unicode_empty || |
| 259 | (unicode->length == 1 && |
| 260 | unicode->str[0] < 256U && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 261 | unicode_latin1[unicode->str[0]] == unicode)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 262 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 263 | "can't resize shared str objects"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 264 | return -1; |
| 265 | } |
| 266 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 267 | /* We allocate one more byte to make sure the string is Ux0000 terminated. |
| 268 | The overallocation is also used by fastsearch, which assumes that it's |
| 269 | safe to look at str[length] (without making any assumptions about what |
| 270 | it contains). */ |
| 271 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 272 | oldstr = unicode->str; |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 273 | unicode->str = PyObject_REALLOC(unicode->str, |
| 274 | sizeof(Py_UNICODE) * (length + 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 275 | if (!unicode->str) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 276 | unicode->str = (Py_UNICODE *)oldstr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 277 | PyErr_NoMemory(); |
| 278 | return -1; |
| 279 | } |
| 280 | unicode->str[length] = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 281 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 282 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 283 | reset: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 284 | /* Reset the object caches */ |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 285 | if (unicode->defenc) { |
| 286 | Py_DECREF(unicode->defenc); |
| 287 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 288 | } |
| 289 | unicode->hash = -1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 290 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | /* We allocate one more byte to make sure the string is |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 295 | Ux0000 terminated; some code (e.g. new_identifier) |
| 296 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 297 | |
| 298 | XXX This allocator could further be enhanced by assuring that the |
| 299 | free list never reduces its size below 1. |
| 300 | |
| 301 | */ |
| 302 | |
| 303 | static |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 304 | PyUnicodeObject *_PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 305 | { |
| 306 | register PyUnicodeObject *unicode; |
| 307 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 308 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 309 | if (length == 0 && unicode_empty != NULL) { |
| 310 | Py_INCREF(unicode_empty); |
| 311 | return unicode_empty; |
| 312 | } |
| 313 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 314 | /* Ensure we won't overflow the size. */ |
| 315 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 316 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 317 | } |
| 318 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 319 | /* Unicode freelist & memory allocation */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 320 | if (free_list) { |
| 321 | unicode = free_list; |
| 322 | free_list = *(PyUnicodeObject **)unicode; |
| 323 | numfree--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 324 | if (unicode->str) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 325 | /* Keep-Alive optimization: we only upsize the buffer, |
| 326 | never downsize it. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 327 | if ((unicode->length < length) && |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 328 | unicode_resize(unicode, length) < 0) { |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 329 | PyObject_DEL(unicode->str); |
Amaury Forgeot d'Arc | 7888d08 | 2008-08-01 01:06:32 +0000 | [diff] [blame] | 330 | unicode->str = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 331 | } |
| 332 | } |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 333 | else { |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 334 | size_t new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 335 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 336 | } |
| 337 | PyObject_INIT(unicode, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 338 | } |
| 339 | else { |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 340 | size_t new_size; |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 341 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 342 | if (unicode == NULL) |
| 343 | return NULL; |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 344 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 345 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 348 | if (!unicode->str) { |
| 349 | PyErr_NoMemory(); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 350 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 351 | } |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 352 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 353 | * the caller fails before initializing str -- unicode_resize() |
| 354 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 355 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 356 | * We don't want unicode_resize to read uninitialized memory in |
| 357 | * that case. |
| 358 | */ |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 359 | unicode->str[0] = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 360 | unicode->str[length] = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 361 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 362 | unicode->hash = -1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 363 | unicode->state = 0; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 364 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 365 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 366 | |
| 367 | onError: |
Amaury Forgeot d'Arc | 7888d08 | 2008-08-01 01:06:32 +0000 | [diff] [blame] | 368 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 369 | _Py_DEC_REFTOTAL; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 370 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 371 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 372 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | static |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 376 | void unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 377 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 378 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
| 379 | case SSTATE_NOT_INTERNED: |
| 380 | break; |
| 381 | |
| 382 | case SSTATE_INTERNED_MORTAL: |
| 383 | /* revive dead object temporarily for DelItem */ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 384 | Py_REFCNT(unicode) = 3; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 385 | if (PyDict_DelItem(interned, (PyObject *)unicode) != 0) |
| 386 | Py_FatalError( |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 387 | "deletion of interned string failed"); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 388 | break; |
| 389 | |
| 390 | case SSTATE_INTERNED_IMMORTAL: |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 391 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 392 | |
| 393 | default: |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 394 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 397 | if (PyUnicode_CheckExact(unicode) && |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 398 | numfree < PyUnicode_MAXFREELIST) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 399 | /* Keep-Alive optimization */ |
| 400 | if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 401 | PyObject_DEL(unicode->str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 402 | unicode->str = NULL; |
| 403 | unicode->length = 0; |
| 404 | } |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 405 | if (unicode->defenc) { |
| 406 | Py_DECREF(unicode->defenc); |
| 407 | unicode->defenc = NULL; |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 408 | } |
| 409 | /* Add to free list */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 410 | *(PyUnicodeObject **)unicode = free_list; |
| 411 | free_list = unicode; |
| 412 | numfree++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 413 | } |
| 414 | else { |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 415 | PyObject_DEL(unicode->str); |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 416 | Py_XDECREF(unicode->defenc); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 417 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 421 | static |
| 422 | int _PyUnicode_Resize(PyUnicodeObject **unicode, Py_ssize_t length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 423 | { |
| 424 | register PyUnicodeObject *v; |
| 425 | |
| 426 | /* Argument checks */ |
| 427 | if (unicode == NULL) { |
| 428 | PyErr_BadInternalCall(); |
| 429 | return -1; |
| 430 | } |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 431 | v = *unicode; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 432 | 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] | 433 | PyErr_BadInternalCall(); |
| 434 | return -1; |
| 435 | } |
| 436 | |
| 437 | /* Resizing unicode_empty and single character objects is not |
| 438 | possible since these are being shared. We simply return a fresh |
| 439 | copy with the same Unicode content. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 440 | if (v->length != length && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 441 | (v == unicode_empty || v->length == 1)) { |
| 442 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 443 | if (w == NULL) |
| 444 | return -1; |
| 445 | Py_UNICODE_COPY(w->str, v->str, |
| 446 | length < v->length ? length : v->length); |
Raymond Hettinger | c8df578 | 2003-03-09 07:30:43 +0000 | [diff] [blame] | 447 | Py_DECREF(*unicode); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 448 | *unicode = w; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 449 | return 0; |
| 450 | } |
| 451 | |
| 452 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 453 | objects, since we can modify them in-place. */ |
| 454 | return unicode_resize(v, length); |
| 455 | } |
| 456 | |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 457 | int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) |
| 458 | { |
| 459 | return _PyUnicode_Resize((PyUnicodeObject **)unicode, length); |
| 460 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 461 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 462 | PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 463 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 464 | { |
| 465 | PyUnicodeObject *unicode; |
| 466 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 467 | /* If the Unicode data is known at construction time, we can apply |
| 468 | some optimizations which share commonly used objects. */ |
| 469 | if (u != NULL) { |
| 470 | |
| 471 | /* Optimization for empty strings */ |
| 472 | if (size == 0 && unicode_empty != NULL) { |
| 473 | Py_INCREF(unicode_empty); |
| 474 | return (PyObject *)unicode_empty; |
| 475 | } |
| 476 | |
| 477 | /* Single character Unicode objects in the Latin-1 range are |
| 478 | shared when using this constructor */ |
| 479 | if (size == 1 && *u < 256) { |
| 480 | unicode = unicode_latin1[*u]; |
| 481 | if (!unicode) { |
| 482 | unicode = _PyUnicode_New(1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 483 | if (!unicode) |
| 484 | return NULL; |
Marc-André Lemburg | 8879a33 | 2001-06-07 12:26:56 +0000 | [diff] [blame] | 485 | unicode->str[0] = *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 486 | unicode_latin1[*u] = unicode; |
| 487 | } |
| 488 | Py_INCREF(unicode); |
| 489 | return (PyObject *)unicode; |
| 490 | } |
| 491 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 492 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 493 | unicode = _PyUnicode_New(size); |
| 494 | if (!unicode) |
| 495 | return NULL; |
| 496 | |
| 497 | /* Copy the Unicode data into the new object */ |
| 498 | if (u != NULL) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 499 | Py_UNICODE_COPY(unicode->str, u, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 500 | |
| 501 | return (PyObject *)unicode; |
| 502 | } |
| 503 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 504 | PyObject *PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 505 | { |
| 506 | PyUnicodeObject *unicode; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 507 | |
| 508 | if (size < 0) { |
| 509 | PyErr_SetString(PyExc_SystemError, |
| 510 | "Negative size passed to PyUnicode_FromStringAndSize"); |
| 511 | return NULL; |
| 512 | } |
| 513 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 514 | /* If the Unicode data is known at construction time, we can apply |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 515 | some optimizations which share commonly used objects. |
| 516 | Also, this means the input must be UTF-8, so fall back to the |
| 517 | UTF-8 decoder at the end. */ |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 518 | if (u != NULL) { |
| 519 | |
| 520 | /* Optimization for empty strings */ |
| 521 | if (size == 0 && unicode_empty != NULL) { |
| 522 | Py_INCREF(unicode_empty); |
| 523 | return (PyObject *)unicode_empty; |
| 524 | } |
| 525 | |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 526 | /* Single characters are shared when using this constructor. |
| 527 | Restrict to ASCII, since the input must be UTF-8. */ |
| 528 | if (size == 1 && Py_CHARMASK(*u) < 128) { |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 529 | unicode = unicode_latin1[Py_CHARMASK(*u)]; |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 530 | if (!unicode) { |
| 531 | unicode = _PyUnicode_New(1); |
| 532 | if (!unicode) |
| 533 | return NULL; |
Guido van Rossum | 00058aa | 2007-07-19 18:21:28 +0000 | [diff] [blame] | 534 | unicode->str[0] = Py_CHARMASK(*u); |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 535 | unicode_latin1[Py_CHARMASK(*u)] = unicode; |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 536 | } |
| 537 | Py_INCREF(unicode); |
| 538 | return (PyObject *)unicode; |
| 539 | } |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 540 | |
| 541 | return PyUnicode_DecodeUTF8(u, size, NULL); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Walter Dörwald | 5550731 | 2007-05-18 13:12:10 +0000 | [diff] [blame] | 544 | unicode = _PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 545 | if (!unicode) |
| 546 | return NULL; |
| 547 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 548 | return (PyObject *)unicode; |
| 549 | } |
| 550 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 551 | PyObject *PyUnicode_FromString(const char *u) |
| 552 | { |
| 553 | size_t size = strlen(u); |
| 554 | if (size > PY_SSIZE_T_MAX) { |
| 555 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 556 | return NULL; |
| 557 | } |
| 558 | |
| 559 | return PyUnicode_FromStringAndSize(u, size); |
| 560 | } |
| 561 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 562 | #ifdef HAVE_WCHAR_H |
| 563 | |
| 564 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 565 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 566 | { |
| 567 | PyUnicodeObject *unicode; |
| 568 | |
| 569 | if (w == NULL) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 570 | if (size == 0) |
| 571 | return PyUnicode_FromStringAndSize(NULL, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 572 | PyErr_BadInternalCall(); |
| 573 | return NULL; |
| 574 | } |
| 575 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 576 | if (size == -1) { |
| 577 | size = wcslen(w); |
| 578 | } |
| 579 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 580 | unicode = _PyUnicode_New(size); |
| 581 | if (!unicode) |
| 582 | return NULL; |
| 583 | |
| 584 | /* Copy the wchar_t data into the new object */ |
| 585 | #ifdef HAVE_USABLE_WCHAR_T |
| 586 | memcpy(unicode->str, w, size * sizeof(wchar_t)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 587 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 588 | { |
| 589 | register Py_UNICODE *u; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 590 | register Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 591 | u = PyUnicode_AS_UNICODE(unicode); |
Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 592 | for (i = size; i > 0; i--) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 593 | *u++ = *w++; |
| 594 | } |
| 595 | #endif |
| 596 | |
| 597 | return (PyObject *)unicode; |
| 598 | } |
| 599 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 600 | static void |
| 601 | makefmt(char *fmt, int longflag, int size_tflag, int zeropad, int width, int precision, char c) |
| 602 | { |
| 603 | *fmt++ = '%'; |
| 604 | if (width) { |
| 605 | if (zeropad) |
| 606 | *fmt++ = '0'; |
| 607 | fmt += sprintf(fmt, "%d", width); |
| 608 | } |
| 609 | if (precision) |
| 610 | fmt += sprintf(fmt, ".%d", precision); |
| 611 | if (longflag) |
| 612 | *fmt++ = 'l'; |
| 613 | else if (size_tflag) { |
| 614 | char *f = PY_FORMAT_SIZE_T; |
| 615 | while (*f) |
| 616 | *fmt++ = *f++; |
| 617 | } |
| 618 | *fmt++ = c; |
| 619 | *fmt = '\0'; |
| 620 | } |
| 621 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 622 | #define appendstring(string) {for (copy = string;*copy;) *s++ = *copy++;} |
| 623 | |
| 624 | PyObject * |
| 625 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 626 | { |
| 627 | va_list count; |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 628 | Py_ssize_t callcount = 0; |
| 629 | PyObject **callresults = NULL; |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 630 | PyObject **callresult = NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 631 | Py_ssize_t n = 0; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 632 | int width = 0; |
| 633 | int precision = 0; |
| 634 | int zeropad; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 635 | const char* f; |
| 636 | Py_UNICODE *s; |
| 637 | PyObject *string; |
| 638 | /* used by sprintf */ |
| 639 | char buffer[21]; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 640 | /* use abuffer instead of buffer, if we need more space |
| 641 | * (which can happen if there's a format specifier with width). */ |
| 642 | char *abuffer = NULL; |
| 643 | char *realbuffer; |
| 644 | Py_ssize_t abuffersize = 0; |
| 645 | char fmt[60]; /* should be enough for %0width.precisionld */ |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 646 | const char *copy; |
| 647 | |
| 648 | #ifdef VA_LIST_IS_ARRAY |
| 649 | Py_MEMCPY(count, vargs, sizeof(va_list)); |
| 650 | #else |
| 651 | #ifdef __va_copy |
| 652 | __va_copy(count, vargs); |
| 653 | #else |
| 654 | count = vargs; |
| 655 | #endif |
| 656 | #endif |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 657 | /* step 1: count the number of %S/%R/%A format specifications |
| 658 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII() for |
| 659 | * these objects once during step 3 and put the result in |
| 660 | an array) */ |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 661 | for (f = format; *f; f++) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 662 | if (*f == '%' && (*(f+1)=='S' || *(f+1)=='R' || *(f+1)=='A')) |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 663 | ++callcount; |
| 664 | } |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 665 | /* step 2: allocate memory for the results of |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 666 | * PyObject_Str()/PyObject_Repr() calls */ |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 667 | if (callcount) { |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 668 | callresults = PyObject_Malloc(sizeof(PyObject *)*callcount); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 669 | if (!callresults) { |
| 670 | PyErr_NoMemory(); |
| 671 | return NULL; |
| 672 | } |
| 673 | callresult = callresults; |
| 674 | } |
| 675 | /* step 3: figure out how large a buffer we need */ |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 676 | for (f = format; *f; f++) { |
| 677 | if (*f == '%') { |
| 678 | const char* p = f; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 679 | width = 0; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 680 | while (ISDIGIT((unsigned)*f)) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 681 | width = (width*10) + *f++ - '0'; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 682 | while (*++f && *f != '%' && !ISALPHA((unsigned)*f)) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 683 | ; |
| 684 | |
| 685 | /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
| 686 | * they don't affect the amount of space we reserve. |
| 687 | */ |
| 688 | if ((*f == 'l' || *f == 'z') && |
| 689 | (f[1] == 'd' || f[1] == 'u')) |
Eric Smith | ddd2582 | 2007-08-27 11:33:42 +0000 | [diff] [blame] | 690 | ++f; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 691 | |
| 692 | switch (*f) { |
| 693 | case 'c': |
| 694 | (void)va_arg(count, int); |
| 695 | /* fall through... */ |
| 696 | case '%': |
| 697 | n++; |
| 698 | break; |
| 699 | case 'd': case 'u': case 'i': case 'x': |
| 700 | (void) va_arg(count, int); |
| 701 | /* 20 bytes is enough to hold a 64-bit |
| 702 | integer. Decimal takes the most space. |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 703 | This isn't enough for octal. |
| 704 | If a width is specified we need more |
| 705 | (which we allocate later). */ |
| 706 | if (width < 20) |
| 707 | width = 20; |
| 708 | n += width; |
| 709 | if (abuffersize < width) |
| 710 | abuffersize = width; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 711 | break; |
| 712 | case 's': |
Martin v. Löwis | 90d1fcd | 2007-08-31 11:01:23 +0000 | [diff] [blame] | 713 | { |
| 714 | /* UTF-8 */ |
| 715 | unsigned char*s; |
| 716 | s = va_arg(count, unsigned char*); |
| 717 | while (*s) { |
| 718 | if (*s < 128) { |
| 719 | n++; s++; |
| 720 | } else if (*s < 0xc0) { |
| 721 | /* invalid UTF-8 */ |
| 722 | n++; s++; |
| 723 | } else if (*s < 0xc0) { |
| 724 | n++; |
| 725 | s++; if(!*s)break; |
| 726 | s++; |
| 727 | } else if (*s < 0xe0) { |
| 728 | n++; |
| 729 | s++; if(!*s)break; |
| 730 | s++; if(!*s)break; |
| 731 | s++; |
| 732 | } else { |
| 733 | #ifdef Py_UNICODE_WIDE |
| 734 | n++; |
| 735 | #else |
| 736 | n+=2; |
| 737 | #endif |
| 738 | s++; if(!*s)break; |
| 739 | s++; if(!*s)break; |
| 740 | s++; if(!*s)break; |
| 741 | s++; |
| 742 | } |
| 743 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 744 | break; |
Martin v. Löwis | 90d1fcd | 2007-08-31 11:01:23 +0000 | [diff] [blame] | 745 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 746 | case 'U': |
| 747 | { |
| 748 | PyObject *obj = va_arg(count, PyObject *); |
| 749 | assert(obj && PyUnicode_Check(obj)); |
| 750 | n += PyUnicode_GET_SIZE(obj); |
| 751 | break; |
| 752 | } |
Walter Dörwald | d7fb764 | 2007-06-11 16:36:59 +0000 | [diff] [blame] | 753 | case 'V': |
| 754 | { |
| 755 | PyObject *obj = va_arg(count, PyObject *); |
| 756 | const char *str = va_arg(count, const char *); |
| 757 | assert(obj || str); |
| 758 | assert(!obj || PyUnicode_Check(obj)); |
| 759 | if (obj) |
| 760 | n += PyUnicode_GET_SIZE(obj); |
| 761 | else |
| 762 | n += strlen(str); |
| 763 | break; |
| 764 | } |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 765 | case 'S': |
| 766 | { |
| 767 | PyObject *obj = va_arg(count, PyObject *); |
| 768 | PyObject *str; |
| 769 | assert(obj); |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 770 | str = PyObject_Str(obj); |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 771 | if (!str) |
| 772 | goto fail; |
| 773 | n += PyUnicode_GET_SIZE(str); |
| 774 | /* Remember the str and switch to the next slot */ |
| 775 | *callresult++ = str; |
| 776 | break; |
| 777 | } |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 778 | case 'R': |
| 779 | { |
| 780 | PyObject *obj = va_arg(count, PyObject *); |
| 781 | PyObject *repr; |
| 782 | assert(obj); |
| 783 | repr = PyObject_Repr(obj); |
| 784 | if (!repr) |
| 785 | goto fail; |
| 786 | n += PyUnicode_GET_SIZE(repr); |
| 787 | /* Remember the repr and switch to the next slot */ |
| 788 | *callresult++ = repr; |
| 789 | break; |
| 790 | } |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 791 | case 'A': |
| 792 | { |
| 793 | PyObject *obj = va_arg(count, PyObject *); |
| 794 | PyObject *ascii; |
| 795 | assert(obj); |
| 796 | ascii = PyObject_ASCII(obj); |
| 797 | if (!ascii) |
| 798 | goto fail; |
| 799 | n += PyUnicode_GET_SIZE(ascii); |
| 800 | /* Remember the repr and switch to the next slot */ |
| 801 | *callresult++ = ascii; |
| 802 | break; |
| 803 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 804 | case 'p': |
| 805 | (void) va_arg(count, int); |
| 806 | /* maximum 64-bit pointer representation: |
| 807 | * 0xffffffffffffffff |
| 808 | * so 19 characters is enough. |
| 809 | * XXX I count 18 -- what's the extra for? |
| 810 | */ |
| 811 | n += 19; |
| 812 | break; |
| 813 | default: |
| 814 | /* if we stumble upon an unknown |
| 815 | formatting code, copy the rest of |
| 816 | the format string to the output |
| 817 | string. (we cannot just skip the |
| 818 | code, since there's no way to know |
| 819 | what's in the argument list) */ |
| 820 | n += strlen(p); |
| 821 | goto expand; |
| 822 | } |
| 823 | } else |
| 824 | n++; |
| 825 | } |
| 826 | expand: |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 827 | if (abuffersize > 20) { |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 828 | abuffer = PyObject_Malloc(abuffersize); |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 829 | if (!abuffer) { |
| 830 | PyErr_NoMemory(); |
| 831 | goto fail; |
| 832 | } |
| 833 | realbuffer = abuffer; |
| 834 | } |
| 835 | else |
| 836 | realbuffer = buffer; |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 837 | /* step 4: fill the buffer */ |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 838 | /* Since we've analyzed how much space we need for the worst case, |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 839 | we don't have to resize the string. |
| 840 | There can be no errors beyond this point. */ |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 841 | string = PyUnicode_FromUnicode(NULL, n); |
| 842 | if (!string) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 843 | goto fail; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 844 | |
| 845 | s = PyUnicode_AS_UNICODE(string); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 846 | callresult = callresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 847 | |
| 848 | for (f = format; *f; f++) { |
| 849 | if (*f == '%') { |
| 850 | const char* p = f++; |
| 851 | int longflag = 0; |
| 852 | int size_tflag = 0; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 853 | zeropad = (*f == '0'); |
| 854 | /* parse the width.precision part */ |
| 855 | width = 0; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 856 | while (ISDIGIT((unsigned)*f)) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 857 | width = (width*10) + *f++ - '0'; |
| 858 | precision = 0; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 859 | if (*f == '.') { |
| 860 | f++; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 861 | while (ISDIGIT((unsigned)*f)) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 862 | precision = (precision*10) + *f++ - '0'; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 863 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 864 | /* handle the long flag, but only for %ld and %lu. |
| 865 | others can be added when necessary. */ |
| 866 | if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) { |
| 867 | longflag = 1; |
| 868 | ++f; |
| 869 | } |
| 870 | /* handle the size_t flag. */ |
| 871 | if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
| 872 | size_tflag = 1; |
| 873 | ++f; |
| 874 | } |
| 875 | |
| 876 | switch (*f) { |
| 877 | case 'c': |
| 878 | *s++ = va_arg(vargs, int); |
| 879 | break; |
| 880 | case 'd': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 881 | makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'd'); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 882 | if (longflag) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 883 | sprintf(realbuffer, fmt, va_arg(vargs, long)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 884 | else if (size_tflag) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 885 | sprintf(realbuffer, fmt, va_arg(vargs, Py_ssize_t)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 886 | else |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 887 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 888 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 889 | break; |
| 890 | case 'u': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 891 | makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'u'); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 892 | if (longflag) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 893 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned long)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 894 | else if (size_tflag) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 895 | sprintf(realbuffer, fmt, va_arg(vargs, size_t)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 896 | else |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 897 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned int)); |
| 898 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 899 | break; |
| 900 | case 'i': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 901 | makefmt(fmt, 0, 0, zeropad, width, precision, 'i'); |
| 902 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 903 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 904 | break; |
| 905 | case 'x': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 906 | makefmt(fmt, 0, 0, zeropad, width, precision, 'x'); |
| 907 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 908 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 909 | break; |
| 910 | case 's': |
Martin v. Löwis | 90d1fcd | 2007-08-31 11:01:23 +0000 | [diff] [blame] | 911 | { |
| 912 | /* Parameter must be UTF-8 encoded. |
| 913 | In case of encoding errors, use |
| 914 | the replacement character. */ |
| 915 | PyObject *u; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 916 | p = va_arg(vargs, char*); |
Martin v. Löwis | 90d1fcd | 2007-08-31 11:01:23 +0000 | [diff] [blame] | 917 | u = PyUnicode_DecodeUTF8(p, strlen(p), |
| 918 | "replace"); |
| 919 | if (!u) |
| 920 | goto fail; |
| 921 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(u), |
| 922 | PyUnicode_GET_SIZE(u)); |
| 923 | s += PyUnicode_GET_SIZE(u); |
| 924 | Py_DECREF(u); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 925 | break; |
Martin v. Löwis | 90d1fcd | 2007-08-31 11:01:23 +0000 | [diff] [blame] | 926 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 927 | case 'U': |
| 928 | { |
| 929 | PyObject *obj = va_arg(vargs, PyObject *); |
Walter Dörwald | 5c2fab6 | 2007-05-24 19:51:02 +0000 | [diff] [blame] | 930 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 931 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 932 | s += size; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 933 | break; |
| 934 | } |
Walter Dörwald | d7fb764 | 2007-06-11 16:36:59 +0000 | [diff] [blame] | 935 | case 'V': |
| 936 | { |
| 937 | PyObject *obj = va_arg(vargs, PyObject *); |
| 938 | const char *str = va_arg(vargs, const char *); |
| 939 | if (obj) { |
| 940 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 941 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 942 | s += size; |
| 943 | } else { |
| 944 | appendstring(str); |
| 945 | } |
| 946 | break; |
| 947 | } |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 948 | case 'S': |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 949 | case 'R': |
| 950 | { |
Guido van Rossum | 755114a | 2007-06-13 01:04:27 +0000 | [diff] [blame] | 951 | Py_UNICODE *ucopy; |
| 952 | Py_ssize_t usize; |
| 953 | Py_ssize_t upos; |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 954 | /* unused, since we already have the result */ |
| 955 | (void) va_arg(vargs, PyObject *); |
Guido van Rossum | 755114a | 2007-06-13 01:04:27 +0000 | [diff] [blame] | 956 | ucopy = PyUnicode_AS_UNICODE(*callresult); |
| 957 | usize = PyUnicode_GET_SIZE(*callresult); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 958 | for (upos = 0; upos<usize;) |
| 959 | *s++ = ucopy[upos++]; |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 960 | /* We're done with the unicode()/repr() => forget it */ |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 961 | Py_DECREF(*callresult); |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 962 | /* switch to next unicode()/repr() result */ |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 963 | ++callresult; |
| 964 | break; |
| 965 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 966 | case 'p': |
| 967 | sprintf(buffer, "%p", va_arg(vargs, void*)); |
| 968 | /* %p is ill-defined: ensure leading 0x. */ |
| 969 | if (buffer[1] == 'X') |
| 970 | buffer[1] = 'x'; |
| 971 | else if (buffer[1] != 'x') { |
| 972 | memmove(buffer+2, buffer, strlen(buffer)+1); |
| 973 | buffer[0] = '0'; |
| 974 | buffer[1] = 'x'; |
| 975 | } |
| 976 | appendstring(buffer); |
| 977 | break; |
| 978 | case '%': |
| 979 | *s++ = '%'; |
| 980 | break; |
| 981 | default: |
| 982 | appendstring(p); |
| 983 | goto end; |
| 984 | } |
| 985 | } else |
| 986 | *s++ = *f; |
| 987 | } |
| 988 | |
| 989 | end: |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 990 | if (callresults) |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 991 | PyObject_Free(callresults); |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 992 | if (abuffer) |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 993 | PyObject_Free(abuffer); |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 994 | PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 995 | return string; |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 996 | fail: |
| 997 | if (callresults) { |
| 998 | PyObject **callresult2 = callresults; |
Guido van Rossum | 307fa8c | 2007-07-16 20:46:27 +0000 | [diff] [blame] | 999 | while (callresult2 < callresult) { |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 1000 | Py_DECREF(*callresult2); |
| 1001 | ++callresult2; |
| 1002 | } |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 1003 | PyObject_Free(callresults); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 1004 | } |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1005 | if (abuffer) |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 1006 | PyObject_Free(abuffer); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 1007 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | #undef appendstring |
| 1011 | |
| 1012 | PyObject * |
| 1013 | PyUnicode_FromFormat(const char *format, ...) |
| 1014 | { |
| 1015 | PyObject* ret; |
| 1016 | va_list vargs; |
| 1017 | |
| 1018 | #ifdef HAVE_STDARG_PROTOTYPES |
| 1019 | va_start(vargs, format); |
| 1020 | #else |
| 1021 | va_start(vargs); |
| 1022 | #endif |
| 1023 | ret = PyUnicode_FromFormatV(format, vargs); |
| 1024 | va_end(vargs); |
| 1025 | return ret; |
| 1026 | } |
| 1027 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1028 | Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
| 1029 | wchar_t *w, |
| 1030 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1031 | { |
| 1032 | if (unicode == NULL) { |
| 1033 | PyErr_BadInternalCall(); |
| 1034 | return -1; |
| 1035 | } |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1036 | |
| 1037 | /* If possible, try to copy the 0-termination as well */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1038 | if (size > PyUnicode_GET_SIZE(unicode)) |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1039 | size = PyUnicode_GET_SIZE(unicode) + 1; |
| 1040 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1041 | #ifdef HAVE_USABLE_WCHAR_T |
| 1042 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 1043 | #else |
| 1044 | { |
| 1045 | register Py_UNICODE *u; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1046 | register Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1047 | u = PyUnicode_AS_UNICODE(unicode); |
Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 1048 | for (i = size; i > 0; i--) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1049 | *w++ = *u++; |
| 1050 | } |
| 1051 | #endif |
| 1052 | |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1053 | if (size > PyUnicode_GET_SIZE(unicode)) |
| 1054 | return PyUnicode_GET_SIZE(unicode); |
| 1055 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1056 | return size; |
| 1057 | } |
| 1058 | |
| 1059 | #endif |
| 1060 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1061 | PyObject *PyUnicode_FromOrdinal(int ordinal) |
| 1062 | { |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1063 | Py_UNICODE s[2]; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1064 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1065 | if (ordinal < 0 || ordinal > 0x10ffff) { |
| 1066 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1067 | "chr() arg not in range(0x110000)"); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1068 | return NULL; |
| 1069 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1070 | |
| 1071 | #ifndef Py_UNICODE_WIDE |
| 1072 | if (ordinal > 0xffff) { |
| 1073 | ordinal -= 0x10000; |
| 1074 | s[0] = 0xD800 | (ordinal >> 10); |
| 1075 | s[1] = 0xDC00 | (ordinal & 0x3FF); |
| 1076 | return PyUnicode_FromUnicode(s, 2); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1077 | } |
| 1078 | #endif |
| 1079 | |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 1080 | s[0] = (Py_UNICODE)ordinal; |
| 1081 | return PyUnicode_FromUnicode(s, 1); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1084 | PyObject *PyUnicode_FromObject(register PyObject *obj) |
| 1085 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1086 | /* XXX Perhaps we should make this API an alias of |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 1087 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1088 | if (PyUnicode_CheckExact(obj)) { |
| 1089 | Py_INCREF(obj); |
| 1090 | return obj; |
| 1091 | } |
| 1092 | if (PyUnicode_Check(obj)) { |
| 1093 | /* For a Unicode subtype that's not a Unicode object, |
| 1094 | return a true Unicode object with the same data. */ |
| 1095 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 1096 | PyUnicode_GET_SIZE(obj)); |
| 1097 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1098 | PyErr_Format(PyExc_TypeError, |
| 1099 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1100 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1101 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
| 1105 | const char *encoding, |
| 1106 | const char *errors) |
| 1107 | { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1108 | const char *s = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1109 | Py_ssize_t len; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1110 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1111 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1112 | if (obj == NULL) { |
| 1113 | PyErr_BadInternalCall(); |
| 1114 | return NULL; |
| 1115 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1116 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1117 | if (PyUnicode_Check(obj)) { |
| 1118 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1119 | "decoding str is not supported"); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1120 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1121 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1122 | |
| 1123 | /* Coerce object */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1124 | if (PyBytes_Check(obj)) { |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1125 | s = PyBytes_AS_STRING(obj); |
| 1126 | len = PyBytes_GET_SIZE(obj); |
| 1127 | } |
| 1128 | else if (PyByteArray_Check(obj)) { |
| 1129 | s = PyByteArray_AS_STRING(obj); |
| 1130 | len = PyByteArray_GET_SIZE(obj); |
| 1131 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1132 | else if (PyObject_AsCharBuffer(obj, &s, &len)) { |
| 1133 | /* Overwrite the error message with something more useful in |
| 1134 | case of a TypeError. */ |
| 1135 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1136 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1137 | "coercing to str: need string or buffer, " |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1138 | "%.80s found", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1139 | Py_TYPE(obj)->tp_name); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1140 | goto onError; |
| 1141 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1142 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1143 | /* Convert to Unicode */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1144 | if (len == 0) { |
| 1145 | Py_INCREF(unicode_empty); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1146 | v = (PyObject *)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1147 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1148 | else |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1149 | v = PyUnicode_Decode(s, len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 1150 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1151 | return v; |
| 1152 | |
| 1153 | onError: |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1154 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | PyObject *PyUnicode_Decode(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1158 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1159 | const char *encoding, |
| 1160 | const char *errors) |
| 1161 | { |
| 1162 | PyObject *buffer = NULL, *unicode; |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1163 | Py_buffer info; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1164 | char lower[20]; /* Enough for any encoding name we recognize */ |
| 1165 | char *l; |
| 1166 | const char *e; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1167 | |
| 1168 | if (encoding == NULL) |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1169 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1170 | |
| 1171 | /* Convert encoding to lower case and replace '_' with '-' in order to |
| 1172 | catch e.g. UTF_8 */ |
| 1173 | e = encoding; |
| 1174 | l = lower; |
| 1175 | while (*e && l < &lower[(sizeof lower) - 2]) { |
| 1176 | if (ISUPPER(*e)) { |
| 1177 | *l++ = TOLOWER(*e++); |
| 1178 | } |
| 1179 | else if (*e == '_') { |
| 1180 | *l++ = '-'; |
| 1181 | e++; |
| 1182 | } |
| 1183 | else { |
| 1184 | *l++ = *e++; |
| 1185 | } |
| 1186 | } |
| 1187 | *l = '\0'; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1188 | |
| 1189 | /* Shortcuts for common default encodings */ |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1190 | if (strcmp(lower, "utf-8") == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1191 | return PyUnicode_DecodeUTF8(s, size, errors); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1192 | else if ((strcmp(lower, "latin-1") == 0) || |
| 1193 | (strcmp(lower, "iso-8859-1") == 0)) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1194 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1195 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1196 | else if (strcmp(lower, "mbcs") == 0) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1197 | return PyUnicode_DecodeMBCS(s, size, errors); |
| 1198 | #endif |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1199 | else if (strcmp(lower, "ascii") == 0) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1200 | return PyUnicode_DecodeASCII(s, size, errors); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1201 | else if (strcmp(lower, "utf-16") == 0) |
| 1202 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 1203 | else if (strcmp(lower, "utf-32") == 0) |
| 1204 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1205 | |
| 1206 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1207 | buffer = NULL; |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1208 | if (PyBuffer_FillInfo(&info, NULL, (void *)s, size, 1, PyBUF_SIMPLE) < 0) |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1209 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 1210 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1211 | if (buffer == NULL) |
| 1212 | goto onError; |
| 1213 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 1214 | if (unicode == NULL) |
| 1215 | goto onError; |
| 1216 | if (!PyUnicode_Check(unicode)) { |
| 1217 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1218 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1219 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1220 | Py_DECREF(unicode); |
| 1221 | goto onError; |
| 1222 | } |
| 1223 | Py_DECREF(buffer); |
| 1224 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1225 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1226 | onError: |
| 1227 | Py_XDECREF(buffer); |
| 1228 | return NULL; |
| 1229 | } |
| 1230 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1231 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 1232 | const char *encoding, |
| 1233 | const char *errors) |
| 1234 | { |
| 1235 | PyObject *v; |
| 1236 | |
| 1237 | if (!PyUnicode_Check(unicode)) { |
| 1238 | PyErr_BadArgument(); |
| 1239 | goto onError; |
| 1240 | } |
| 1241 | |
| 1242 | if (encoding == NULL) |
| 1243 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1244 | |
| 1245 | /* Decode via the codec registry */ |
| 1246 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1247 | if (v == NULL) |
| 1248 | goto onError; |
| 1249 | return v; |
| 1250 | |
| 1251 | onError: |
| 1252 | return NULL; |
| 1253 | } |
| 1254 | |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1255 | PyObject *PyUnicode_AsDecodedUnicode(PyObject *unicode, |
| 1256 | const char *encoding, |
| 1257 | const char *errors) |
| 1258 | { |
| 1259 | PyObject *v; |
| 1260 | |
| 1261 | if (!PyUnicode_Check(unicode)) { |
| 1262 | PyErr_BadArgument(); |
| 1263 | goto onError; |
| 1264 | } |
| 1265 | |
| 1266 | if (encoding == NULL) |
| 1267 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1268 | |
| 1269 | /* Decode via the codec registry */ |
| 1270 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1271 | if (v == NULL) |
| 1272 | goto onError; |
| 1273 | if (!PyUnicode_Check(v)) { |
| 1274 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1275 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1276 | Py_TYPE(v)->tp_name); |
| 1277 | Py_DECREF(v); |
| 1278 | goto onError; |
| 1279 | } |
| 1280 | return v; |
| 1281 | |
| 1282 | onError: |
| 1283 | return NULL; |
| 1284 | } |
| 1285 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1286 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1287 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1288 | const char *encoding, |
| 1289 | const char *errors) |
| 1290 | { |
| 1291 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1292 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1293 | unicode = PyUnicode_FromUnicode(s, size); |
| 1294 | if (unicode == NULL) |
| 1295 | return NULL; |
| 1296 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 1297 | Py_DECREF(unicode); |
| 1298 | return v; |
| 1299 | } |
| 1300 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1301 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 1302 | const char *encoding, |
| 1303 | const char *errors) |
| 1304 | { |
| 1305 | PyObject *v; |
| 1306 | |
| 1307 | if (!PyUnicode_Check(unicode)) { |
| 1308 | PyErr_BadArgument(); |
| 1309 | goto onError; |
| 1310 | } |
| 1311 | |
| 1312 | if (encoding == NULL) |
| 1313 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1314 | |
| 1315 | /* Encode via the codec registry */ |
| 1316 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1317 | if (v == NULL) |
| 1318 | goto onError; |
| 1319 | return v; |
| 1320 | |
| 1321 | onError: |
| 1322 | return NULL; |
| 1323 | } |
| 1324 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1325 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 1326 | const char *encoding, |
| 1327 | const char *errors) |
| 1328 | { |
| 1329 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1330 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1331 | if (!PyUnicode_Check(unicode)) { |
| 1332 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1333 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1334 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1335 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1336 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1337 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1338 | |
| 1339 | /* Shortcuts for common default encodings */ |
| 1340 | if (errors == NULL) { |
| 1341 | if (strcmp(encoding, "utf-8") == 0) |
Jeremy Hylton | 9cea41c | 2001-05-29 17:13:15 +0000 | [diff] [blame] | 1342 | return PyUnicode_AsUTF8String(unicode); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1343 | else if (strcmp(encoding, "latin-1") == 0) |
| 1344 | return PyUnicode_AsLatin1String(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1345 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 1346 | else if (strcmp(encoding, "mbcs") == 0) |
| 1347 | return PyUnicode_AsMBCSString(unicode); |
| 1348 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1349 | else if (strcmp(encoding, "ascii") == 0) |
| 1350 | return PyUnicode_AsASCIIString(unicode); |
Christian Heimes | 6a27efa | 2008-10-30 21:48:26 +0000 | [diff] [blame] | 1351 | /* During bootstrap, we may need to find the encodings |
| 1352 | package, to load the file system encoding, and require the |
| 1353 | file system encoding in order to load the encodings |
| 1354 | package. |
| 1355 | |
| 1356 | Break out of this dependency by assuming that the path to |
| 1357 | the encodings module is ASCII-only. XXX could try wcstombs |
| 1358 | instead, if the file system encoding is the locale's |
| 1359 | encoding. */ |
| 1360 | else if (Py_FileSystemDefaultEncoding && |
| 1361 | strcmp(encoding, Py_FileSystemDefaultEncoding) == 0 && |
| 1362 | !PyThreadState_GET()->interp->codecs_initialized) |
| 1363 | return PyUnicode_AsASCIIString(unicode); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1364 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1365 | |
| 1366 | /* Encode via the codec registry */ |
| 1367 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1368 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1369 | return NULL; |
| 1370 | |
| 1371 | /* The normal path */ |
| 1372 | if (PyBytes_Check(v)) |
| 1373 | return v; |
| 1374 | |
| 1375 | /* If the codec returns a buffer, raise a warning and convert to bytes */ |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1376 | if (PyByteArray_Check(v)) { |
| 1377 | char msg[100]; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1378 | PyObject *b; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1379 | PyOS_snprintf(msg, sizeof(msg), |
| 1380 | "encoder %s returned buffer instead of bytes", |
| 1381 | encoding); |
| 1382 | if (PyErr_WarnEx(PyExc_RuntimeWarning, msg, 1) < 0) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1383 | Py_DECREF(v); |
| 1384 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1385 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1386 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1387 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 1388 | Py_DECREF(v); |
| 1389 | return b; |
| 1390 | } |
| 1391 | |
| 1392 | PyErr_Format(PyExc_TypeError, |
| 1393 | "encoder did not return a bytes object (type=%.400s)", |
| 1394 | Py_TYPE(v)->tp_name); |
| 1395 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1396 | return NULL; |
| 1397 | } |
| 1398 | |
| 1399 | PyObject *PyUnicode_AsEncodedUnicode(PyObject *unicode, |
| 1400 | const char *encoding, |
| 1401 | const char *errors) |
| 1402 | { |
| 1403 | PyObject *v; |
| 1404 | |
| 1405 | if (!PyUnicode_Check(unicode)) { |
| 1406 | PyErr_BadArgument(); |
| 1407 | goto onError; |
| 1408 | } |
| 1409 | |
| 1410 | if (encoding == NULL) |
| 1411 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1412 | |
| 1413 | /* Encode via the codec registry */ |
| 1414 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1415 | if (v == NULL) |
| 1416 | goto onError; |
| 1417 | if (!PyUnicode_Check(v)) { |
| 1418 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1419 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1420 | Py_TYPE(v)->tp_name); |
| 1421 | Py_DECREF(v); |
| 1422 | goto onError; |
| 1423 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1424 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1425 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1426 | onError: |
| 1427 | return NULL; |
| 1428 | } |
| 1429 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1430 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
| 1431 | const char *errors) |
| 1432 | { |
| 1433 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1434 | if (v) |
| 1435 | return v; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1436 | if (errors != NULL) |
| 1437 | Py_FatalError("non-NULL encoding in _PyUnicode_AsDefaultEncodedString"); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1438 | v = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Guido van Rossum | 0661009 | 2007-08-16 21:02:22 +0000 | [diff] [blame] | 1439 | PyUnicode_GET_SIZE(unicode), |
| 1440 | NULL); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1441 | if (!v) |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1442 | return NULL; |
Guido van Rossum | e7a0d39 | 2007-07-12 07:53:00 +0000 | [diff] [blame] | 1443 | ((PyUnicodeObject *)unicode)->defenc = v; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1444 | return v; |
| 1445 | } |
| 1446 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1447 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1448 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1449 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1450 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 1451 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1452 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1453 | PyObject* |
| 1454 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 1455 | { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1456 | /* During the early bootstrapping process, Py_FileSystemDefaultEncoding |
| 1457 | can be undefined. If it is case, decode using UTF-8. The following assumes |
| 1458 | that Py_FileSystemDefaultEncoding is set to a built-in encoding during the |
| 1459 | bootstrapping process where the codecs aren't ready yet. |
| 1460 | */ |
| 1461 | if (Py_FileSystemDefaultEncoding) { |
| 1462 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1463 | if (strcmp(Py_FileSystemDefaultEncoding, "mbcs") == 0) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1464 | return PyUnicode_DecodeMBCS(s, size, "replace"); |
| 1465 | } |
| 1466 | #elif defined(__APPLE__) |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1467 | if (strcmp(Py_FileSystemDefaultEncoding, "utf-8") == 0) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1468 | return PyUnicode_DecodeUTF8(s, size, "replace"); |
| 1469 | } |
| 1470 | #endif |
| 1471 | return PyUnicode_Decode(s, size, |
| 1472 | Py_FileSystemDefaultEncoding, |
| 1473 | "replace"); |
| 1474 | } |
| 1475 | else { |
| 1476 | return PyUnicode_DecodeUTF8(s, size, "replace"); |
| 1477 | } |
| 1478 | } |
| 1479 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1480 | char* |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1481 | _PyUnicode_AsStringAndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1482 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1483 | PyObject *bytes; |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 1484 | if (!PyUnicode_Check(unicode)) { |
| 1485 | PyErr_BadArgument(); |
| 1486 | return NULL; |
| 1487 | } |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1488 | bytes = _PyUnicode_AsDefaultEncodedString(unicode, NULL); |
| 1489 | if (bytes == NULL) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1490 | return NULL; |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1491 | if (psize != NULL) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1492 | *psize = PyBytes_GET_SIZE(bytes); |
| 1493 | return PyBytes_AS_STRING(bytes); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | char* |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1497 | _PyUnicode_AsString(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1498 | { |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1499 | return _PyUnicode_AsStringAndSize(unicode, NULL); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1502 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 1503 | { |
| 1504 | if (!PyUnicode_Check(unicode)) { |
| 1505 | PyErr_BadArgument(); |
| 1506 | goto onError; |
| 1507 | } |
| 1508 | return PyUnicode_AS_UNICODE(unicode); |
| 1509 | |
| 1510 | onError: |
| 1511 | return NULL; |
| 1512 | } |
| 1513 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1514 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1515 | { |
| 1516 | if (!PyUnicode_Check(unicode)) { |
| 1517 | PyErr_BadArgument(); |
| 1518 | goto onError; |
| 1519 | } |
| 1520 | return PyUnicode_GET_SIZE(unicode); |
| 1521 | |
| 1522 | onError: |
| 1523 | return -1; |
| 1524 | } |
| 1525 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 1526 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1527 | { |
| 1528 | return unicode_default_encoding; |
| 1529 | } |
| 1530 | |
| 1531 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 1532 | { |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1533 | if (strcmp(encoding, unicode_default_encoding) != 0) { |
| 1534 | PyErr_Format(PyExc_ValueError, |
| 1535 | "Can only set default encoding to %s", |
| 1536 | unicode_default_encoding); |
| 1537 | return -1; |
| 1538 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1539 | return 0; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1540 | } |
| 1541 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1542 | /* error handling callback helper: |
| 1543 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 1544 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1545 | and adjust various state variables. |
| 1546 | return 0 on success, -1 on error |
| 1547 | */ |
| 1548 | |
| 1549 | static |
| 1550 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
| 1551 | const char *encoding, const char *reason, |
Walter Dörwald | a651d3d | 2007-08-30 15:29:21 +0000 | [diff] [blame] | 1552 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 1553 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1554 | PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1555 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1556 | static char *argparse = "O!n;decoding error handler must return (str, int) tuple"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1557 | |
| 1558 | PyObject *restuple = NULL; |
| 1559 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1560 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1561 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1562 | Py_ssize_t requiredsize; |
| 1563 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1564 | Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1565 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1566 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1567 | int res = -1; |
| 1568 | |
| 1569 | if (*errorHandler == NULL) { |
| 1570 | *errorHandler = PyCodec_LookupError(errors); |
| 1571 | if (*errorHandler == NULL) |
| 1572 | goto onError; |
| 1573 | } |
| 1574 | |
| 1575 | if (*exceptionObject == NULL) { |
| 1576 | *exceptionObject = PyUnicodeDecodeError_Create( |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1577 | encoding, *input, *inend-*input, *startinpos, *endinpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1578 | if (*exceptionObject == NULL) |
| 1579 | goto onError; |
| 1580 | } |
| 1581 | else { |
| 1582 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 1583 | goto onError; |
| 1584 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
| 1585 | goto onError; |
| 1586 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 1587 | goto onError; |
| 1588 | } |
| 1589 | |
| 1590 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 1591 | if (restuple == NULL) |
| 1592 | goto onError; |
| 1593 | if (!PyTuple_Check(restuple)) { |
| 1594 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 1595 | goto onError; |
| 1596 | } |
| 1597 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
| 1598 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1599 | |
| 1600 | /* Copy back the bytes variables, which might have been modified by the |
| 1601 | callback */ |
| 1602 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 1603 | if (!inputobj) |
| 1604 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1605 | if (!PyBytes_Check(inputobj)) { |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1606 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
| 1607 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1608 | *input = PyBytes_AS_STRING(inputobj); |
| 1609 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1610 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 1611 | /* we can DECREF safely, as the exception has another reference, |
| 1612 | so the object won't go away. */ |
| 1613 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1614 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1615 | if (newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1616 | newpos = insize+newpos; |
| 1617 | if (newpos<0 || newpos>insize) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 1618 | 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] | 1619 | goto onError; |
| 1620 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1621 | |
| 1622 | /* need more space? (at least enough for what we |
| 1623 | have+the replacement+the rest of the string (starting |
| 1624 | at the new input position), so we won't have to check space |
| 1625 | when there are no errors in the rest of the string) */ |
| 1626 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 1627 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 1628 | requiredsize = *outpos + repsize + insize-newpos; |
| 1629 | if (requiredsize > outsize) { |
| 1630 | if (requiredsize<2*outsize) |
| 1631 | requiredsize = 2*outsize; |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1632 | if (_PyUnicode_Resize(output, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1633 | goto onError; |
| 1634 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
| 1635 | } |
| 1636 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1637 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1638 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 1639 | *outptr += repsize; |
| 1640 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1641 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1642 | /* we made it! */ |
| 1643 | res = 0; |
| 1644 | |
| 1645 | onError: |
| 1646 | Py_XDECREF(restuple); |
| 1647 | return res; |
| 1648 | } |
| 1649 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1650 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 1651 | |
| 1652 | /* see RFC2152 for details */ |
| 1653 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1654 | static |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1655 | char utf7_special[128] = { |
| 1656 | /* indicate whether a UTF-7 character is special i.e. cannot be directly |
| 1657 | encoded: |
| 1658 | 0 - not special |
| 1659 | 1 - special |
| 1660 | 2 - whitespace (optional) |
| 1661 | 3 - RFC2152 Set O (optional) */ |
| 1662 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, |
| 1663 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1664 | 2, 3, 3, 3, 3, 3, 3, 0, 0, 0, 3, 1, 0, 0, 0, 1, |
| 1665 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, |
| 1666 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1667 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, |
| 1668 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1669 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 1, 1, |
| 1670 | |
| 1671 | }; |
| 1672 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1673 | /* Note: The comparison (c) <= 0 is a trick to work-around gcc |
| 1674 | warnings about the comparison always being false; since |
| 1675 | utf7_special[0] is 1, we can safely make that one comparison |
| 1676 | true */ |
| 1677 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1678 | #define SPECIAL(c, encodeO, encodeWS) \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1679 | ((c) > 127 || (c) <= 0 || utf7_special[(c)] == 1 || \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1680 | (encodeWS && (utf7_special[(c)] == 2)) || \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1681 | (encodeO && (utf7_special[(c)] == 3))) |
| 1682 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1683 | #define B64(n) \ |
| 1684 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 1685 | #define B64CHAR(c) \ |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1686 | (ISALNUM(c) || (c) == '+' || (c) == '/') |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1687 | #define UB64(c) \ |
| 1688 | ((c) == '+' ? 62 : (c) == '/' ? 63 : (c) >= 'a' ? \ |
| 1689 | (c) - 71 : (c) >= 'A' ? (c) - 65 : (c) + 4 ) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1690 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1691 | #define ENCODE(out, ch, bits) \ |
| 1692 | while (bits >= 6) { \ |
| 1693 | *out++ = B64(ch >> (bits-6)); \ |
| 1694 | bits -= 6; \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1695 | } |
| 1696 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1697 | #define DECODE(out, ch, bits, surrogate) \ |
| 1698 | while (bits >= 16) { \ |
| 1699 | Py_UNICODE outCh = (Py_UNICODE) ((ch >> (bits-16)) & 0xffff); \ |
| 1700 | bits -= 16; \ |
| 1701 | if (surrogate) { \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1702 | /* We have already generated an error for the high surrogate \ |
| 1703 | 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] | 1704 | surrogate = 0; \ |
| 1705 | } else if (0xDC00 <= outCh && outCh <= 0xDFFF) { \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1706 | /* This is a surrogate pair. Unfortunately we can't represent \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1707 | it in a 16-bit character */ \ |
| 1708 | surrogate = 1; \ |
| 1709 | errmsg = "code pairs are not supported"; \ |
| 1710 | goto utf7Error; \ |
| 1711 | } else { \ |
| 1712 | *out++ = outCh; \ |
| 1713 | } \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1714 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1715 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1716 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1717 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1718 | const char *errors) |
| 1719 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1720 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 1721 | } |
| 1722 | |
| 1723 | PyObject *PyUnicode_DecodeUTF7Stateful(const char *s, |
| 1724 | Py_ssize_t size, |
| 1725 | const char *errors, |
| 1726 | Py_ssize_t *consumed) |
| 1727 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1728 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1729 | Py_ssize_t startinpos; |
| 1730 | Py_ssize_t endinpos; |
| 1731 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1732 | const char *e; |
| 1733 | PyUnicodeObject *unicode; |
| 1734 | Py_UNICODE *p; |
| 1735 | const char *errmsg = ""; |
| 1736 | int inShift = 0; |
| 1737 | unsigned int bitsleft = 0; |
| 1738 | unsigned long charsleft = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1739 | int surrogate = 0; |
| 1740 | PyObject *errorHandler = NULL; |
| 1741 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1742 | |
| 1743 | unicode = _PyUnicode_New(size); |
| 1744 | if (!unicode) |
| 1745 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1746 | if (size == 0) { |
| 1747 | if (consumed) |
| 1748 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1749 | return (PyObject *)unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1750 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1751 | |
| 1752 | p = unicode->str; |
| 1753 | e = s + size; |
| 1754 | |
| 1755 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1756 | Py_UNICODE ch; |
| 1757 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 1758 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1759 | |
| 1760 | if (inShift) { |
| 1761 | if ((ch == '-') || !B64CHAR(ch)) { |
| 1762 | inShift = 0; |
| 1763 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1764 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1765 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1766 | if (bitsleft >= 6) { |
| 1767 | /* The shift sequence has a partial character in it. If |
| 1768 | bitsleft < 6 then we could just classify it as padding |
| 1769 | but that is not the case here */ |
| 1770 | |
| 1771 | errmsg = "partial character in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1772 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1773 | } |
| 1774 | /* According to RFC2152 the remaining bits should be zero. We |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1775 | choose to signal an error/insert a replacement character |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1776 | here so indicate the potential of a misencoded character. */ |
| 1777 | |
| 1778 | /* On x86, a << b == a << (b%32) so make sure that bitsleft != 0 */ |
| 1779 | if (bitsleft && charsleft << (sizeof(charsleft) * 8 - bitsleft)) { |
| 1780 | errmsg = "non-zero padding bits in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1781 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1782 | } |
| 1783 | |
| 1784 | if (ch == '-') { |
| 1785 | if ((s < e) && (*(s) == '-')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1786 | *p++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1787 | inShift = 1; |
| 1788 | } |
| 1789 | } else if (SPECIAL(ch,0,0)) { |
| 1790 | errmsg = "unexpected special character"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1791 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1792 | } else { |
| 1793 | *p++ = ch; |
| 1794 | } |
| 1795 | } else { |
| 1796 | charsleft = (charsleft << 6) | UB64(ch); |
| 1797 | bitsleft += 6; |
| 1798 | s++; |
| 1799 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1800 | } |
| 1801 | } |
| 1802 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1803 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1804 | s++; |
| 1805 | if (s < e && *s == '-') { |
| 1806 | s++; |
| 1807 | *p++ = '+'; |
| 1808 | } else |
| 1809 | { |
| 1810 | inShift = 1; |
| 1811 | bitsleft = 0; |
| 1812 | } |
| 1813 | } |
| 1814 | else if (SPECIAL(ch,0,0)) { |
Walter Dörwald | 2b65c75 | 2007-08-30 15:35:26 +0000 | [diff] [blame] | 1815 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1816 | errmsg = "unexpected special character"; |
| 1817 | s++; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1818 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1819 | } |
| 1820 | else { |
| 1821 | *p++ = ch; |
| 1822 | s++; |
| 1823 | } |
| 1824 | continue; |
| 1825 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1826 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1827 | endinpos = s-starts; |
| 1828 | if (unicode_decode_call_errorhandler( |
| 1829 | errors, &errorHandler, |
| 1830 | "utf7", errmsg, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1831 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1832 | &unicode, &outpos, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1833 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1834 | } |
| 1835 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1836 | if (inShift && !consumed) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1837 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1838 | endinpos = size; |
| 1839 | if (unicode_decode_call_errorhandler( |
| 1840 | errors, &errorHandler, |
| 1841 | "utf7", "unterminated shift sequence", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1842 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 1843 | &unicode, &outpos, &p)) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1844 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1845 | if (s < e) |
| 1846 | goto restart; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1847 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1848 | if (consumed) { |
| 1849 | if(inShift) |
| 1850 | *consumed = startinpos; |
| 1851 | else |
| 1852 | *consumed = s-starts; |
| 1853 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1854 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1855 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1856 | goto onError; |
| 1857 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1858 | Py_XDECREF(errorHandler); |
| 1859 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1860 | return (PyObject *)unicode; |
| 1861 | |
| 1862 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1863 | Py_XDECREF(errorHandler); |
| 1864 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1865 | Py_DECREF(unicode); |
| 1866 | return NULL; |
| 1867 | } |
| 1868 | |
| 1869 | |
| 1870 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1871 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1872 | int encodeSetO, |
| 1873 | int encodeWhiteSpace, |
| 1874 | const char *errors) |
| 1875 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 1876 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1877 | /* It might be possible to tighten this worst case */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1878 | Py_ssize_t cbAllocated = 5 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1879 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1880 | Py_ssize_t i = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1881 | unsigned int bitsleft = 0; |
| 1882 | unsigned long charsleft = 0; |
| 1883 | char * out; |
| 1884 | char * start; |
| 1885 | |
| 1886 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1887 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1888 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 1889 | if (cbAllocated / 5 != size) |
| 1890 | return PyErr_NoMemory(); |
| 1891 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 1892 | v = PyBytes_FromStringAndSize(NULL, cbAllocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1893 | if (v == NULL) |
| 1894 | return NULL; |
| 1895 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 1896 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1897 | for (;i < size; ++i) { |
| 1898 | Py_UNICODE ch = s[i]; |
| 1899 | |
| 1900 | if (!inShift) { |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1901 | if (ch == '+') { |
| 1902 | *out++ = '+'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1903 | *out++ = '-'; |
| 1904 | } else if (SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1905 | charsleft = ch; |
| 1906 | bitsleft = 16; |
| 1907 | *out++ = '+'; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1908 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1909 | inShift = bitsleft > 0; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1910 | } else { |
| 1911 | *out++ = (char) ch; |
| 1912 | } |
| 1913 | } else { |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1914 | if (!SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1915 | *out++ = B64(charsleft << (6-bitsleft)); |
| 1916 | charsleft = 0; |
| 1917 | bitsleft = 0; |
| 1918 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 1919 | so no '-' is required, except if the character is itself a '-' */ |
| 1920 | if (B64CHAR(ch) || ch == '-') { |
| 1921 | *out++ = '-'; |
| 1922 | } |
| 1923 | inShift = 0; |
| 1924 | *out++ = (char) ch; |
| 1925 | } else { |
| 1926 | bitsleft += 16; |
| 1927 | charsleft = (charsleft << 16) | ch; |
| 1928 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
| 1929 | |
| 1930 | /* If the next character is special then we dont' need to terminate |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1931 | 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] | 1932 | or '-' then the shift sequence will be terminated implicitly and we |
| 1933 | don't have to insert a '-'. */ |
| 1934 | |
| 1935 | if (bitsleft == 0) { |
| 1936 | if (i + 1 < size) { |
| 1937 | Py_UNICODE ch2 = s[i+1]; |
| 1938 | |
| 1939 | if (SPECIAL(ch2, encodeSetO, encodeWhiteSpace)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1940 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1941 | } else if (B64CHAR(ch2) || ch2 == '-') { |
| 1942 | *out++ = '-'; |
| 1943 | inShift = 0; |
| 1944 | } else { |
| 1945 | inShift = 0; |
| 1946 | } |
| 1947 | |
| 1948 | } |
| 1949 | else { |
| 1950 | *out++ = '-'; |
| 1951 | inShift = 0; |
| 1952 | } |
| 1953 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1954 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1955 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1956 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1957 | if (bitsleft) { |
| 1958 | *out++= B64(charsleft << (6-bitsleft) ); |
| 1959 | *out++ = '-'; |
| 1960 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 1961 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 1962 | return NULL; |
| 1963 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1964 | } |
| 1965 | |
| 1966 | #undef SPECIAL |
| 1967 | #undef B64 |
| 1968 | #undef B64CHAR |
| 1969 | #undef UB64 |
| 1970 | #undef ENCODE |
| 1971 | #undef DECODE |
| 1972 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1973 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 1974 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1975 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1976 | char utf8_code_length[256] = { |
| 1977 | /* Map UTF-8 encoded prefix byte to sequence length. zero means |
| 1978 | illegal prefix. see RFC 2279 for details */ |
| 1979 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1980 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1981 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1982 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1983 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1984 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1985 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1986 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1987 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1988 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1989 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1990 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1991 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1992 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1993 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 1994 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 |
| 1995 | }; |
| 1996 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1997 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1998 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1999 | const char *errors) |
| 2000 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2001 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 2002 | } |
| 2003 | |
| 2004 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2005 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2006 | const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2007 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2008 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2009 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2010 | int n; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2011 | Py_ssize_t startinpos; |
| 2012 | Py_ssize_t endinpos; |
| 2013 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2014 | const char *e; |
| 2015 | PyUnicodeObject *unicode; |
| 2016 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2017 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2018 | PyObject *errorHandler = NULL; |
| 2019 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2020 | |
| 2021 | /* Note: size will always be longer than the resulting Unicode |
| 2022 | character count */ |
| 2023 | unicode = _PyUnicode_New(size); |
| 2024 | if (!unicode) |
| 2025 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2026 | if (size == 0) { |
| 2027 | if (consumed) |
| 2028 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2029 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2030 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2031 | |
| 2032 | /* Unpack UTF-8 encoded data */ |
| 2033 | p = unicode->str; |
| 2034 | e = s + size; |
| 2035 | |
| 2036 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2037 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2038 | |
| 2039 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2040 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2041 | s++; |
| 2042 | continue; |
| 2043 | } |
| 2044 | |
| 2045 | n = utf8_code_length[ch]; |
| 2046 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2047 | if (s + n > e) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2048 | if (consumed) |
| 2049 | break; |
| 2050 | else { |
| 2051 | errmsg = "unexpected end of data"; |
| 2052 | startinpos = s-starts; |
| 2053 | endinpos = size; |
| 2054 | goto utf8Error; |
| 2055 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2056 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2057 | |
| 2058 | switch (n) { |
| 2059 | |
| 2060 | case 0: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2061 | errmsg = "unexpected code byte"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2062 | startinpos = s-starts; |
| 2063 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2064 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2065 | |
| 2066 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2067 | errmsg = "internal error"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2068 | startinpos = s-starts; |
| 2069 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2070 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2071 | |
| 2072 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2073 | if ((s[1] & 0xc0) != 0x80) { |
| 2074 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2075 | startinpos = s-starts; |
| 2076 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2077 | goto utf8Error; |
| 2078 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2079 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2080 | if (ch < 0x80) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2081 | startinpos = s-starts; |
| 2082 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2083 | errmsg = "illegal encoding"; |
| 2084 | goto utf8Error; |
| 2085 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2086 | else |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2087 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2088 | break; |
| 2089 | |
| 2090 | case 3: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2091 | if ((s[1] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2092 | (s[2] & 0xc0) != 0x80) { |
| 2093 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2094 | startinpos = s-starts; |
| 2095 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2096 | goto utf8Error; |
| 2097 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2098 | 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] | 2099 | if (ch < 0x0800) { |
| 2100 | /* Note: UTF-8 encodings of surrogates are considered |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2101 | legal UTF-8 sequences; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2102 | |
| 2103 | XXX For wide builds (UCS-4) we should probably try |
| 2104 | to recombine the surrogates into a single code |
| 2105 | unit. |
| 2106 | */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2107 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2108 | startinpos = s-starts; |
| 2109 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2110 | goto utf8Error; |
| 2111 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2112 | else |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2113 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2114 | break; |
| 2115 | |
| 2116 | case 4: |
| 2117 | if ((s[1] & 0xc0) != 0x80 || |
| 2118 | (s[2] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2119 | (s[3] & 0xc0) != 0x80) { |
| 2120 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2121 | startinpos = s-starts; |
| 2122 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2123 | goto utf8Error; |
| 2124 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2125 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 2126 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 2127 | /* validate and convert to UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2128 | if ((ch < 0x10000) /* minimum value allowed for 4 |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2129 | byte encoding */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2130 | || (ch > 0x10ffff)) /* maximum value allowed for |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2131 | UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2132 | { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2133 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2134 | startinpos = s-starts; |
| 2135 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2136 | goto utf8Error; |
| 2137 | } |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2138 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2139 | *p++ = (Py_UNICODE)ch; |
| 2140 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2141 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2142 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2143 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 2144 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2145 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2146 | /* high surrogate = top 10 bits added to D800 */ |
| 2147 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2148 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2149 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2150 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2151 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2152 | break; |
| 2153 | |
| 2154 | default: |
| 2155 | /* Other sizes are only needed for UCS-4 */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2156 | errmsg = "unsupported Unicode code range"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2157 | startinpos = s-starts; |
| 2158 | endinpos = startinpos+n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2159 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2160 | } |
| 2161 | s += n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2162 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2163 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2164 | utf8Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2165 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2166 | if (unicode_decode_call_errorhandler( |
| 2167 | errors, &errorHandler, |
| 2168 | "utf8", errmsg, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2169 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 2170 | &unicode, &outpos, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2171 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2172 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2173 | if (consumed) |
| 2174 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2175 | |
| 2176 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2177 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2178 | goto onError; |
| 2179 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2180 | Py_XDECREF(errorHandler); |
| 2181 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2182 | return (PyObject *)unicode; |
| 2183 | |
| 2184 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2185 | Py_XDECREF(errorHandler); |
| 2186 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2187 | Py_DECREF(unicode); |
| 2188 | return NULL; |
| 2189 | } |
| 2190 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2191 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 2192 | and allocate exactly as much space needed at the end. Else allocate the |
| 2193 | maximum possible needed (4 result bytes per Unicode character), and return |
| 2194 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2195 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 2196 | PyObject * |
| 2197 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2198 | Py_ssize_t size, |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 2199 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2200 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2201 | #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] | 2202 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2203 | Py_ssize_t i; /* index into s of next input byte */ |
| 2204 | PyObject *result; /* result string object */ |
| 2205 | char *p; /* next free byte in output buffer */ |
| 2206 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 2207 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2208 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2209 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2210 | assert(s != NULL); |
| 2211 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2212 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2213 | if (size <= MAX_SHORT_UNICHARS) { |
| 2214 | /* Write into the stack buffer; nallocated can't overflow. |
| 2215 | * At the end, we'll allocate exactly as much heap space as it |
| 2216 | * turns out we need. |
| 2217 | */ |
| 2218 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2219 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2220 | p = stackbuf; |
| 2221 | } |
| 2222 | else { |
| 2223 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 2224 | nallocated = size * 4; |
| 2225 | if (nallocated / 4 != size) /* overflow! */ |
| 2226 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2227 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2228 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2229 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2230 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2231 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2232 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2233 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2234 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2235 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2236 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2237 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2238 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2239 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2240 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2241 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 2242 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 2243 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2244 | } |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2245 | else { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2246 | /* Encode UCS2 Unicode ordinals */ |
| 2247 | if (ch < 0x10000) { |
| 2248 | /* Special case: check for high surrogate */ |
| 2249 | if (0xD800 <= ch && ch <= 0xDBFF && i != size) { |
| 2250 | Py_UCS4 ch2 = s[i]; |
| 2251 | /* Check for low surrogate and combine the two to |
| 2252 | form a UCS4 value */ |
| 2253 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2254 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2255 | i++; |
| 2256 | goto encodeUCS4; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2257 | } |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2258 | /* Fall through: handles isolated high surrogates */ |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2259 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2260 | *p++ = (char)(0xe0 | (ch >> 12)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2261 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2262 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2263 | continue; |
| 2264 | } |
| 2265 | encodeUCS4: |
| 2266 | /* Encode UCS4 Unicode ordinals */ |
| 2267 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2268 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 2269 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2270 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2271 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2272 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 2273 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2274 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2275 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2276 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2277 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2278 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2279 | } |
| 2280 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 2281 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2282 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2283 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2284 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2285 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2286 | return result; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2287 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2288 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2289 | } |
| 2290 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2291 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 2292 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2293 | if (!PyUnicode_Check(unicode)) { |
| 2294 | PyErr_BadArgument(); |
| 2295 | return NULL; |
| 2296 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 2297 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 2298 | PyUnicode_GET_SIZE(unicode), |
| 2299 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2300 | } |
| 2301 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2302 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 2303 | |
| 2304 | PyObject * |
| 2305 | PyUnicode_DecodeUTF32(const char *s, |
| 2306 | Py_ssize_t size, |
| 2307 | const char *errors, |
| 2308 | int *byteorder) |
| 2309 | { |
| 2310 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 2311 | } |
| 2312 | |
| 2313 | PyObject * |
| 2314 | PyUnicode_DecodeUTF32Stateful(const char *s, |
| 2315 | Py_ssize_t size, |
| 2316 | const char *errors, |
| 2317 | int *byteorder, |
| 2318 | Py_ssize_t *consumed) |
| 2319 | { |
| 2320 | const char *starts = s; |
| 2321 | Py_ssize_t startinpos; |
| 2322 | Py_ssize_t endinpos; |
| 2323 | Py_ssize_t outpos; |
| 2324 | PyUnicodeObject *unicode; |
| 2325 | Py_UNICODE *p; |
| 2326 | #ifndef Py_UNICODE_WIDE |
| 2327 | int i, pairs; |
| 2328 | #else |
| 2329 | const int pairs = 0; |
| 2330 | #endif |
| 2331 | const unsigned char *q, *e; |
| 2332 | int bo = 0; /* assume native ordering by default */ |
| 2333 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2334 | /* Offsets from q for retrieving bytes in the right order. */ |
| 2335 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2336 | int iorder[] = {0, 1, 2, 3}; |
| 2337 | #else |
| 2338 | int iorder[] = {3, 2, 1, 0}; |
| 2339 | #endif |
| 2340 | PyObject *errorHandler = NULL; |
| 2341 | PyObject *exc = NULL; |
Guido van Rossum | 8d991ed | 2007-08-17 15:41:00 +0000 | [diff] [blame] | 2342 | /* On narrow builds we split characters outside the BMP into two |
| 2343 | codepoints => count how much extra space we need. */ |
| 2344 | #ifndef Py_UNICODE_WIDE |
| 2345 | for (i = pairs = 0; i < size/4; i++) |
| 2346 | if (((Py_UCS4 *)s)[i] >= 0x10000) |
| 2347 | pairs++; |
| 2348 | #endif |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2349 | |
| 2350 | /* This might be one to much, because of a BOM */ |
| 2351 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 2352 | if (!unicode) |
| 2353 | return NULL; |
| 2354 | if (size == 0) |
| 2355 | return (PyObject *)unicode; |
| 2356 | |
| 2357 | /* Unpack UTF-32 encoded data */ |
| 2358 | p = unicode->str; |
| 2359 | q = (unsigned char *)s; |
| 2360 | e = q + size; |
| 2361 | |
| 2362 | if (byteorder) |
| 2363 | bo = *byteorder; |
| 2364 | |
| 2365 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2366 | byte order setting accordingly. In native mode, the leading BOM |
| 2367 | mark is skipped, in all other modes, it is copied to the output |
| 2368 | stream as-is (giving a ZWNBSP character). */ |
| 2369 | if (bo == 0) { |
| 2370 | if (size >= 4) { |
| 2371 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2372 | (q[iorder[1]] << 8) | q[iorder[0]]; |
| 2373 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2374 | if (bom == 0x0000FEFF) { |
| 2375 | q += 4; |
| 2376 | bo = -1; |
| 2377 | } |
| 2378 | else if (bom == 0xFFFE0000) { |
| 2379 | q += 4; |
| 2380 | bo = 1; |
| 2381 | } |
| 2382 | #else |
| 2383 | if (bom == 0x0000FEFF) { |
| 2384 | q += 4; |
| 2385 | bo = 1; |
| 2386 | } |
| 2387 | else if (bom == 0xFFFE0000) { |
| 2388 | q += 4; |
| 2389 | bo = -1; |
| 2390 | } |
| 2391 | #endif |
| 2392 | } |
| 2393 | } |
| 2394 | |
| 2395 | if (bo == -1) { |
| 2396 | /* force LE */ |
| 2397 | iorder[0] = 0; |
| 2398 | iorder[1] = 1; |
| 2399 | iorder[2] = 2; |
| 2400 | iorder[3] = 3; |
| 2401 | } |
| 2402 | else if (bo == 1) { |
| 2403 | /* force BE */ |
| 2404 | iorder[0] = 3; |
| 2405 | iorder[1] = 2; |
| 2406 | iorder[2] = 1; |
| 2407 | iorder[3] = 0; |
| 2408 | } |
| 2409 | |
| 2410 | while (q < e) { |
| 2411 | Py_UCS4 ch; |
| 2412 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 2413 | if (e-q<4) { |
| 2414 | if (consumed) |
| 2415 | break; |
| 2416 | errmsg = "truncated data"; |
| 2417 | startinpos = ((const char *)q)-starts; |
| 2418 | endinpos = ((const char *)e)-starts; |
| 2419 | goto utf32Error; |
| 2420 | /* The remaining input chars are ignored if the callback |
| 2421 | chooses to skip the input */ |
| 2422 | } |
| 2423 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2424 | (q[iorder[1]] << 8) | q[iorder[0]]; |
| 2425 | |
| 2426 | if (ch >= 0x110000) |
| 2427 | { |
| 2428 | errmsg = "codepoint not in range(0x110000)"; |
| 2429 | startinpos = ((const char *)q)-starts; |
| 2430 | endinpos = startinpos+4; |
| 2431 | goto utf32Error; |
| 2432 | } |
| 2433 | #ifndef Py_UNICODE_WIDE |
| 2434 | if (ch >= 0x10000) |
| 2435 | { |
| 2436 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 2437 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2438 | } |
| 2439 | else |
| 2440 | #endif |
| 2441 | *p++ = ch; |
| 2442 | q += 4; |
| 2443 | continue; |
| 2444 | utf32Error: |
| 2445 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2446 | if (unicode_decode_call_errorhandler( |
| 2447 | errors, &errorHandler, |
| 2448 | "utf32", errmsg, |
| 2449 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 2450 | &unicode, &outpos, &p)) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2451 | goto onError; |
| 2452 | } |
| 2453 | |
| 2454 | if (byteorder) |
| 2455 | *byteorder = bo; |
| 2456 | |
| 2457 | if (consumed) |
| 2458 | *consumed = (const char *)q-starts; |
| 2459 | |
| 2460 | /* Adjust length */ |
| 2461 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
| 2462 | goto onError; |
| 2463 | |
| 2464 | Py_XDECREF(errorHandler); |
| 2465 | Py_XDECREF(exc); |
| 2466 | return (PyObject *)unicode; |
| 2467 | |
| 2468 | onError: |
| 2469 | Py_DECREF(unicode); |
| 2470 | Py_XDECREF(errorHandler); |
| 2471 | Py_XDECREF(exc); |
| 2472 | return NULL; |
| 2473 | } |
| 2474 | |
| 2475 | PyObject * |
| 2476 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 2477 | Py_ssize_t size, |
| 2478 | const char *errors, |
| 2479 | int byteorder) |
| 2480 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2481 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2482 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2483 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2484 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2485 | Py_ssize_t i, pairs; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2486 | #else |
| 2487 | const int pairs = 0; |
| 2488 | #endif |
| 2489 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2490 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2491 | int iorder[] = {0, 1, 2, 3}; |
| 2492 | #else |
| 2493 | int iorder[] = {3, 2, 1, 0}; |
| 2494 | #endif |
| 2495 | |
| 2496 | #define STORECHAR(CH) \ |
| 2497 | do { \ |
| 2498 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 2499 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 2500 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 2501 | p[iorder[0]] = (CH) & 0xff; \ |
| 2502 | p += 4; \ |
| 2503 | } while(0) |
| 2504 | |
| 2505 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 2506 | so we need less space. */ |
| 2507 | #ifndef Py_UNICODE_WIDE |
| 2508 | for (i = pairs = 0; i < size-1; i++) |
| 2509 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 2510 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 2511 | pairs++; |
| 2512 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2513 | nsize = (size - pairs + (byteorder == 0)); |
| 2514 | bytesize = nsize * 4; |
| 2515 | if (bytesize / 4 != nsize) |
| 2516 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2517 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2518 | if (v == NULL) |
| 2519 | return NULL; |
| 2520 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2521 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2522 | if (byteorder == 0) |
| 2523 | STORECHAR(0xFEFF); |
| 2524 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2525 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2526 | |
| 2527 | if (byteorder == -1) { |
| 2528 | /* force LE */ |
| 2529 | iorder[0] = 0; |
| 2530 | iorder[1] = 1; |
| 2531 | iorder[2] = 2; |
| 2532 | iorder[3] = 3; |
| 2533 | } |
| 2534 | else if (byteorder == 1) { |
| 2535 | /* force BE */ |
| 2536 | iorder[0] = 3; |
| 2537 | iorder[1] = 2; |
| 2538 | iorder[2] = 1; |
| 2539 | iorder[3] = 0; |
| 2540 | } |
| 2541 | |
| 2542 | while (size-- > 0) { |
| 2543 | Py_UCS4 ch = *s++; |
| 2544 | #ifndef Py_UNICODE_WIDE |
| 2545 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 2546 | Py_UCS4 ch2 = *s; |
| 2547 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 2548 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 2549 | s++; |
| 2550 | size--; |
| 2551 | } |
| 2552 | } |
| 2553 | #endif |
| 2554 | STORECHAR(ch); |
| 2555 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2556 | |
| 2557 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2558 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2559 | #undef STORECHAR |
| 2560 | } |
| 2561 | |
| 2562 | PyObject *PyUnicode_AsUTF32String(PyObject *unicode) |
| 2563 | { |
| 2564 | if (!PyUnicode_Check(unicode)) { |
| 2565 | PyErr_BadArgument(); |
| 2566 | return NULL; |
| 2567 | } |
| 2568 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
| 2569 | PyUnicode_GET_SIZE(unicode), |
| 2570 | NULL, |
| 2571 | 0); |
| 2572 | } |
| 2573 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2574 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 2575 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2576 | PyObject * |
| 2577 | PyUnicode_DecodeUTF16(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2578 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2579 | const char *errors, |
| 2580 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2581 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2582 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 2583 | } |
| 2584 | |
| 2585 | PyObject * |
| 2586 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2587 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2588 | const char *errors, |
| 2589 | int *byteorder, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2590 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2591 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2592 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2593 | Py_ssize_t startinpos; |
| 2594 | Py_ssize_t endinpos; |
| 2595 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2596 | PyUnicodeObject *unicode; |
| 2597 | Py_UNICODE *p; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2598 | const unsigned char *q, *e; |
| 2599 | int bo = 0; /* assume native ordering by default */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2600 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2601 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 2602 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2603 | int ihi = 1, ilo = 0; |
| 2604 | #else |
| 2605 | int ihi = 0, ilo = 1; |
| 2606 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2607 | PyObject *errorHandler = NULL; |
| 2608 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2609 | |
| 2610 | /* Note: size will always be longer than the resulting Unicode |
| 2611 | character count */ |
| 2612 | unicode = _PyUnicode_New(size); |
| 2613 | if (!unicode) |
| 2614 | return NULL; |
| 2615 | if (size == 0) |
| 2616 | return (PyObject *)unicode; |
| 2617 | |
| 2618 | /* Unpack UTF-16 encoded data */ |
| 2619 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2620 | q = (unsigned char *)s; |
| 2621 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2622 | |
| 2623 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2624 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2625 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2626 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2627 | byte order setting accordingly. In native mode, the leading BOM |
| 2628 | mark is skipped, in all other modes, it is copied to the output |
| 2629 | stream as-is (giving a ZWNBSP character). */ |
| 2630 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2631 | if (size >= 2) { |
| 2632 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2633 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2634 | if (bom == 0xFEFF) { |
| 2635 | q += 2; |
| 2636 | bo = -1; |
| 2637 | } |
| 2638 | else if (bom == 0xFFFE) { |
| 2639 | q += 2; |
| 2640 | bo = 1; |
| 2641 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2642 | #else |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2643 | if (bom == 0xFEFF) { |
| 2644 | q += 2; |
| 2645 | bo = 1; |
| 2646 | } |
| 2647 | else if (bom == 0xFFFE) { |
| 2648 | q += 2; |
| 2649 | bo = -1; |
| 2650 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2651 | #endif |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2652 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2653 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2654 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2655 | if (bo == -1) { |
| 2656 | /* force LE */ |
| 2657 | ihi = 1; |
| 2658 | ilo = 0; |
| 2659 | } |
| 2660 | else if (bo == 1) { |
| 2661 | /* force BE */ |
| 2662 | ihi = 0; |
| 2663 | ilo = 1; |
| 2664 | } |
| 2665 | |
| 2666 | while (q < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2667 | Py_UNICODE ch; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2668 | /* remaining bytes at the end? (size should be even) */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2669 | if (e-q<2) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2670 | if (consumed) |
| 2671 | break; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2672 | errmsg = "truncated data"; |
| 2673 | startinpos = ((const char *)q)-starts; |
| 2674 | endinpos = ((const char *)e)-starts; |
| 2675 | goto utf16Error; |
| 2676 | /* The remaining input chars are ignored if the callback |
| 2677 | chooses to skip the input */ |
| 2678 | } |
| 2679 | ch = (q[ihi] << 8) | q[ilo]; |
| 2680 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2681 | q += 2; |
| 2682 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2683 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 2684 | *p++ = ch; |
| 2685 | continue; |
| 2686 | } |
| 2687 | |
| 2688 | /* UTF-16 code pair: */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2689 | if (q >= e) { |
| 2690 | errmsg = "unexpected end of data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2691 | startinpos = (((const char *)q)-2)-starts; |
| 2692 | endinpos = ((const char *)e)-starts; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2693 | goto utf16Error; |
| 2694 | } |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 2695 | if (0xD800 <= ch && ch <= 0xDBFF) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2696 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 2697 | q += 2; |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 2698 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2699 | #ifndef Py_UNICODE_WIDE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2700 | *p++ = ch; |
| 2701 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2702 | #else |
| 2703 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2704 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2705 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2706 | } |
| 2707 | else { |
| 2708 | errmsg = "illegal UTF-16 surrogate"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2709 | startinpos = (((const char *)q)-4)-starts; |
| 2710 | endinpos = startinpos+2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2711 | goto utf16Error; |
| 2712 | } |
| 2713 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2714 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2715 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2716 | startinpos = (((const char *)q)-2)-starts; |
| 2717 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2718 | /* Fall through to report the error */ |
| 2719 | |
| 2720 | utf16Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2721 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2722 | if (unicode_decode_call_errorhandler( |
| 2723 | errors, &errorHandler, |
| 2724 | "utf16", errmsg, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2725 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 2726 | &unicode, &outpos, &p)) |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2727 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2728 | } |
| 2729 | |
| 2730 | if (byteorder) |
| 2731 | *byteorder = bo; |
| 2732 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2733 | if (consumed) |
| 2734 | *consumed = (const char *)q-starts; |
| 2735 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2736 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2737 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2738 | goto onError; |
| 2739 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2740 | Py_XDECREF(errorHandler); |
| 2741 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2742 | return (PyObject *)unicode; |
| 2743 | |
| 2744 | onError: |
| 2745 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2746 | Py_XDECREF(errorHandler); |
| 2747 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2748 | return NULL; |
| 2749 | } |
| 2750 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2751 | PyObject * |
| 2752 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2753 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2754 | const char *errors, |
| 2755 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2756 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2757 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2758 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2759 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2760 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2761 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2762 | #else |
| 2763 | const int pairs = 0; |
| 2764 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2765 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2766 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2767 | int ihi = 1, ilo = 0; |
| 2768 | #else |
| 2769 | int ihi = 0, ilo = 1; |
| 2770 | #endif |
| 2771 | |
| 2772 | #define STORECHAR(CH) \ |
| 2773 | do { \ |
| 2774 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 2775 | p[ilo] = (CH) & 0xff; \ |
| 2776 | p += 2; \ |
| 2777 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2778 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2779 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2780 | for (i = pairs = 0; i < size; i++) |
| 2781 | if (s[i] >= 0x10000) |
| 2782 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2783 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2784 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 2785 | if (size > PY_SSIZE_T_MAX || |
| 2786 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
| 2787 | return PyErr_NoMemory(); |
| 2788 | nsize = size + pairs + (byteorder == 0); |
| 2789 | bytesize = nsize * 2; |
| 2790 | if (bytesize / 2 != nsize) |
| 2791 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2792 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2793 | if (v == NULL) |
| 2794 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2795 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2796 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2797 | if (byteorder == 0) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2798 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2799 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2800 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2801 | |
| 2802 | if (byteorder == -1) { |
| 2803 | /* force LE */ |
| 2804 | ihi = 1; |
| 2805 | ilo = 0; |
| 2806 | } |
| 2807 | else if (byteorder == 1) { |
| 2808 | /* force BE */ |
| 2809 | ihi = 0; |
| 2810 | ilo = 1; |
| 2811 | } |
| 2812 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2813 | while (size-- > 0) { |
| 2814 | Py_UNICODE ch = *s++; |
| 2815 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2816 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2817 | if (ch >= 0x10000) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2818 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2819 | ch = 0xD800 | ((ch-0x10000) >> 10); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2820 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2821 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2822 | STORECHAR(ch); |
| 2823 | if (ch2) |
| 2824 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2825 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2826 | |
| 2827 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2828 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2829 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2830 | } |
| 2831 | |
| 2832 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 2833 | { |
| 2834 | if (!PyUnicode_Check(unicode)) { |
| 2835 | PyErr_BadArgument(); |
| 2836 | return NULL; |
| 2837 | } |
| 2838 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
| 2839 | PyUnicode_GET_SIZE(unicode), |
| 2840 | NULL, |
| 2841 | 0); |
| 2842 | } |
| 2843 | |
| 2844 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 2845 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2846 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 2847 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2848 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2849 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2850 | const char *errors) |
| 2851 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2852 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2853 | Py_ssize_t startinpos; |
| 2854 | Py_ssize_t endinpos; |
| 2855 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2856 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2857 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2858 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2859 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2860 | char* message; |
| 2861 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2862 | PyObject *errorHandler = NULL; |
| 2863 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2864 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2865 | /* Escaped strings will always be longer than the resulting |
| 2866 | 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] | 2867 | length after conversion to the true value. |
| 2868 | (but if the error callback returns a long replacement string |
| 2869 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2870 | v = _PyUnicode_New(size); |
| 2871 | if (v == NULL) |
| 2872 | goto onError; |
| 2873 | if (size == 0) |
| 2874 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2875 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2876 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2877 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2878 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2879 | while (s < end) { |
| 2880 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2881 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2882 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2883 | |
| 2884 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 2885 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2886 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2887 | continue; |
| 2888 | } |
| 2889 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2890 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2891 | /* \ - Escapes */ |
| 2892 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2893 | c = *s++; |
| 2894 | if (s > end) |
| 2895 | c = '\0'; /* Invalid after \ */ |
| 2896 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2897 | |
| 2898 | /* \x escapes */ |
| 2899 | case '\n': break; |
| 2900 | case '\\': *p++ = '\\'; break; |
| 2901 | case '\'': *p++ = '\''; break; |
| 2902 | case '\"': *p++ = '\"'; break; |
| 2903 | case 'b': *p++ = '\b'; break; |
| 2904 | case 'f': *p++ = '\014'; break; /* FF */ |
| 2905 | case 't': *p++ = '\t'; break; |
| 2906 | case 'n': *p++ = '\n'; break; |
| 2907 | case 'r': *p++ = '\r'; break; |
| 2908 | case 'v': *p++ = '\013'; break; /* VT */ |
| 2909 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 2910 | |
| 2911 | /* \OOO (octal) escapes */ |
| 2912 | case '0': case '1': case '2': case '3': |
| 2913 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2914 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2915 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2916 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2917 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2918 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2919 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2920 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2921 | break; |
| 2922 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2923 | /* hex escapes */ |
| 2924 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2925 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2926 | digits = 2; |
| 2927 | message = "truncated \\xXX escape"; |
| 2928 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2929 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2930 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2931 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2932 | digits = 4; |
| 2933 | message = "truncated \\uXXXX escape"; |
| 2934 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2935 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2936 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2937 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2938 | digits = 8; |
| 2939 | message = "truncated \\UXXXXXXXX escape"; |
| 2940 | hexescape: |
| 2941 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2942 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2943 | if (s+digits>end) { |
| 2944 | endinpos = size; |
| 2945 | if (unicode_decode_call_errorhandler( |
| 2946 | errors, &errorHandler, |
| 2947 | "unicodeescape", "end of string in escape sequence", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2948 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 2949 | &v, &outpos, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2950 | goto onError; |
| 2951 | goto nextByte; |
| 2952 | } |
| 2953 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2954 | c = (unsigned char) s[i]; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2955 | if (!ISXDIGIT(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2956 | endinpos = (s+i+1)-starts; |
| 2957 | if (unicode_decode_call_errorhandler( |
| 2958 | errors, &errorHandler, |
| 2959 | "unicodeescape", message, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2960 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 2961 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2962 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2963 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2964 | } |
| 2965 | chr = (chr<<4) & ~0xF; |
| 2966 | if (c >= '0' && c <= '9') |
| 2967 | chr += c - '0'; |
| 2968 | else if (c >= 'a' && c <= 'f') |
| 2969 | chr += 10 + c - 'a'; |
| 2970 | else |
| 2971 | chr += 10 + c - 'A'; |
| 2972 | } |
| 2973 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 2974 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2975 | /* _decoding_error will have already written into the |
| 2976 | target buffer. */ |
| 2977 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2978 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2979 | /* when we get here, chr is a 32-bit unicode character */ |
| 2980 | if (chr <= 0xffff) |
| 2981 | /* UCS-2 character */ |
| 2982 | *p++ = (Py_UNICODE) chr; |
| 2983 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2984 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2985 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2986 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2987 | *p++ = chr; |
| 2988 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2989 | chr -= 0x10000L; |
| 2990 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2991 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2992 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2993 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2994 | endinpos = s-starts; |
| 2995 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2996 | if (unicode_decode_call_errorhandler( |
| 2997 | errors, &errorHandler, |
| 2998 | "unicodeescape", "illegal Unicode character", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2999 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 3000 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3001 | goto onError; |
| 3002 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3003 | break; |
| 3004 | |
| 3005 | /* \N{name} */ |
| 3006 | case 'N': |
| 3007 | message = "malformed \\N character escape"; |
| 3008 | if (ucnhash_CAPI == NULL) { |
| 3009 | /* load the unicode data module */ |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3010 | PyObject *m, *api; |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 3011 | m = PyImport_ImportModuleNoBlock("unicodedata"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3012 | if (m == NULL) |
| 3013 | goto ucnhashError; |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3014 | api = PyObject_GetAttrString(m, "ucnhash_CAPI"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3015 | Py_DECREF(m); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3016 | if (api == NULL) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3017 | goto ucnhashError; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3018 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCObject_AsVoidPtr(api); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3019 | Py_DECREF(api); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3020 | if (ucnhash_CAPI == NULL) |
| 3021 | goto ucnhashError; |
| 3022 | } |
| 3023 | if (*s == '{') { |
| 3024 | const char *start = s+1; |
| 3025 | /* look for the closing brace */ |
| 3026 | while (*s != '}' && s < end) |
| 3027 | s++; |
| 3028 | if (s > start && s < end && *s == '}') { |
| 3029 | /* found a name. look it up in the unicode database */ |
| 3030 | message = "unknown Unicode character name"; |
| 3031 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 3032 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3033 | goto store; |
| 3034 | } |
| 3035 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3036 | endinpos = s-starts; |
| 3037 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3038 | if (unicode_decode_call_errorhandler( |
| 3039 | errors, &errorHandler, |
| 3040 | "unicodeescape", message, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3041 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 3042 | &v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3043 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3044 | break; |
| 3045 | |
| 3046 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3047 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3048 | message = "\\ at end of string"; |
| 3049 | s--; |
| 3050 | endinpos = s-starts; |
| 3051 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3052 | if (unicode_decode_call_errorhandler( |
| 3053 | errors, &errorHandler, |
| 3054 | "unicodeescape", message, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3055 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 3056 | &v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3057 | goto onError; |
| 3058 | } |
| 3059 | else { |
| 3060 | *p++ = '\\'; |
| 3061 | *p++ = (unsigned char)s[-1]; |
| 3062 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3063 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3064 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3065 | nextByte: |
| 3066 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3067 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3068 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3069 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 3070 | Py_XDECREF(errorHandler); |
| 3071 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3072 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3073 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3074 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 3075 | PyErr_SetString( |
| 3076 | PyExc_UnicodeError, |
| 3077 | "\\N escapes not supported (can't load unicodedata module)" |
| 3078 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3079 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3080 | Py_XDECREF(errorHandler); |
| 3081 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 3082 | return NULL; |
| 3083 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3084 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3085 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3086 | Py_XDECREF(errorHandler); |
| 3087 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3088 | return NULL; |
| 3089 | } |
| 3090 | |
| 3091 | /* Return a Unicode-Escape string version of the Unicode object. |
| 3092 | |
| 3093 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 3094 | appropriate. |
| 3095 | |
| 3096 | */ |
| 3097 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3098 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
| 3099 | Py_ssize_t size, |
| 3100 | Py_UNICODE ch) |
| 3101 | { |
| 3102 | /* like wcschr, but doesn't stop at NULL characters */ |
| 3103 | |
| 3104 | while (size-- > 0) { |
| 3105 | if (*s == ch) |
| 3106 | return s; |
| 3107 | s++; |
| 3108 | } |
| 3109 | |
| 3110 | return NULL; |
| 3111 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 3112 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3113 | static const char *hexdigits = "0123456789abcdef"; |
| 3114 | |
| 3115 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 3116 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3117 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3118 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3119 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3120 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3121 | #ifdef Py_UNICODE_WIDE |
| 3122 | const Py_ssize_t expandsize = 10; |
| 3123 | #else |
| 3124 | const Py_ssize_t expandsize = 6; |
| 3125 | #endif |
| 3126 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3127 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 3128 | better to choose a different scheme. Perhaps scan the |
| 3129 | first N-chars of the string and allocate based on that size. |
| 3130 | */ |
| 3131 | /* Initial allocation is based on the longest-possible unichr |
| 3132 | escape. |
| 3133 | |
| 3134 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 3135 | unichr, so in this case it's the longest unichr escape. In |
| 3136 | narrow (UTF-16) builds this is five chars per source unichr |
| 3137 | since there are two unichrs in the surrogate pair, so in narrow |
| 3138 | (UTF-16) builds it's not the longest unichr escape. |
| 3139 | |
| 3140 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 3141 | so in the narrow (UTF-16) build case it's the longest unichr |
| 3142 | escape. |
| 3143 | */ |
| 3144 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3145 | if (size == 0) |
| 3146 | return PyBytes_FromStringAndSize(NULL, 0); |
| 3147 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3148 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
| 3149 | return PyErr_NoMemory(); |
| 3150 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3151 | repr = PyBytes_FromStringAndSize(NULL, |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3152 | 2 |
| 3153 | + expandsize*size |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3154 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3155 | if (repr == NULL) |
| 3156 | return NULL; |
| 3157 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3158 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3159 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3160 | while (size-- > 0) { |
| 3161 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3162 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3163 | /* Escape backslashes */ |
| 3164 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3165 | *p++ = '\\'; |
| 3166 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3167 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3168 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3169 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 3170 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3171 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 3172 | else if (ch >= 0x10000) { |
| 3173 | *p++ = '\\'; |
| 3174 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3175 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 3176 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 3177 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 3178 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 3179 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 3180 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 3181 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 3182 | *p++ = hexdigits[ch & 0x0000000F]; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3183 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3184 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3185 | #else |
| 3186 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3187 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 3188 | Py_UNICODE ch2; |
| 3189 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3190 | |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3191 | ch2 = *s++; |
| 3192 | size--; |
| 3193 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 3194 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 3195 | *p++ = '\\'; |
| 3196 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3197 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 3198 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 3199 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 3200 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 3201 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 3202 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 3203 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 3204 | *p++ = hexdigits[ucs & 0x0000000F]; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3205 | continue; |
| 3206 | } |
| 3207 | /* Fall through: isolated surrogates are copied as-is */ |
| 3208 | s--; |
| 3209 | size++; |
| 3210 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3211 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3212 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3213 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3214 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3215 | *p++ = '\\'; |
| 3216 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3217 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 3218 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 3219 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 3220 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3221 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3222 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3223 | /* Map special whitespace to '\t', \n', '\r' */ |
| 3224 | else if (ch == '\t') { |
| 3225 | *p++ = '\\'; |
| 3226 | *p++ = 't'; |
| 3227 | } |
| 3228 | else if (ch == '\n') { |
| 3229 | *p++ = '\\'; |
| 3230 | *p++ = 'n'; |
| 3231 | } |
| 3232 | else if (ch == '\r') { |
| 3233 | *p++ = '\\'; |
| 3234 | *p++ = 'r'; |
| 3235 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3236 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3237 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 3238 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3239 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3240 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3241 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 3242 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3243 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3244 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3245 | /* Copy everything else as-is */ |
| 3246 | else |
| 3247 | *p++ = (char) ch; |
| 3248 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3249 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3250 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 3251 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 3252 | return NULL; |
| 3253 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3254 | } |
| 3255 | |
Alexandre Vassalotti | 2056bed | 2008-12-27 19:46:35 +0000 | [diff] [blame] | 3256 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3257 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3258 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3259 | if (!PyUnicode_Check(unicode)) { |
| 3260 | PyErr_BadArgument(); |
| 3261 | return NULL; |
| 3262 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3263 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3264 | PyUnicode_GET_SIZE(unicode)); |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3265 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3266 | } |
| 3267 | |
| 3268 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 3269 | |
| 3270 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3271 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3272 | const char *errors) |
| 3273 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3274 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3275 | Py_ssize_t startinpos; |
| 3276 | Py_ssize_t endinpos; |
| 3277 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3278 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3279 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3280 | const char *end; |
| 3281 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3282 | PyObject *errorHandler = NULL; |
| 3283 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3284 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3285 | /* Escaped strings will always be longer than the resulting |
| 3286 | 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] | 3287 | length after conversion to the true value. (But decoding error |
| 3288 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3289 | v = _PyUnicode_New(size); |
| 3290 | if (v == NULL) |
| 3291 | goto onError; |
| 3292 | if (size == 0) |
| 3293 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3294 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3295 | end = s + size; |
| 3296 | while (s < end) { |
| 3297 | unsigned char c; |
Martin v. Löwis | 047c05e | 2002-03-21 08:55:28 +0000 | [diff] [blame] | 3298 | Py_UCS4 x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3299 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3300 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3301 | |
| 3302 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3303 | if (*s != '\\') { |
| 3304 | *p++ = (unsigned char)*s++; |
| 3305 | continue; |
| 3306 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3307 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3308 | |
| 3309 | /* \u-escapes are only interpreted iff the number of leading |
| 3310 | backslashes if odd */ |
| 3311 | bs = s; |
| 3312 | for (;s < end;) { |
| 3313 | if (*s != '\\') |
| 3314 | break; |
| 3315 | *p++ = (unsigned char)*s++; |
| 3316 | } |
| 3317 | if (((s - bs) & 1) == 0 || |
| 3318 | s >= end || |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3319 | (*s != 'u' && *s != 'U')) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3320 | continue; |
| 3321 | } |
| 3322 | p--; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3323 | count = *s=='u' ? 4 : 8; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3324 | s++; |
| 3325 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3326 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3327 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3328 | for (x = 0, i = 0; i < count; ++i, ++s) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3329 | c = (unsigned char)*s; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3330 | if (!ISXDIGIT(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3331 | endinpos = s-starts; |
| 3332 | if (unicode_decode_call_errorhandler( |
| 3333 | errors, &errorHandler, |
| 3334 | "rawunicodeescape", "truncated \\uXXXX", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3335 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 3336 | &v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3337 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3338 | goto nextByte; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3339 | } |
| 3340 | x = (x<<4) & ~0xF; |
| 3341 | if (c >= '0' && c <= '9') |
| 3342 | x += c - '0'; |
| 3343 | else if (c >= 'a' && c <= 'f') |
| 3344 | x += 10 + c - 'a'; |
| 3345 | else |
| 3346 | x += 10 + c - 'A'; |
| 3347 | } |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3348 | if (x <= 0xffff) |
| 3349 | /* UCS-2 character */ |
| 3350 | *p++ = (Py_UNICODE) x; |
| 3351 | else if (x <= 0x10ffff) { |
| 3352 | /* UCS-4 character. Either store directly, or as |
| 3353 | surrogate pair. */ |
| 3354 | #ifdef Py_UNICODE_WIDE |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 3355 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3356 | #else |
| 3357 | x -= 0x10000L; |
| 3358 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 3359 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
| 3360 | #endif |
| 3361 | } else { |
| 3362 | endinpos = s-starts; |
| 3363 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3364 | if (unicode_decode_call_errorhandler( |
| 3365 | errors, &errorHandler, |
| 3366 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3367 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 3368 | &v, &outpos, &p)) |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3369 | goto onError; |
| 3370 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3371 | nextByte: |
| 3372 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3373 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3374 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3375 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3376 | Py_XDECREF(errorHandler); |
| 3377 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3378 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3379 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3380 | onError: |
| 3381 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3382 | Py_XDECREF(errorHandler); |
| 3383 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3384 | return NULL; |
| 3385 | } |
| 3386 | |
| 3387 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3388 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3389 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3390 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3391 | char *p; |
| 3392 | char *q; |
| 3393 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3394 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3395 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3396 | #else |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3397 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3398 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3399 | |
| 3400 | if (size > PY_SSIZE_T_MAX / expandsize) |
| 3401 | return PyErr_NoMemory(); |
| 3402 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3403 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3404 | if (repr == NULL) |
| 3405 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3406 | if (size == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3407 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3408 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3409 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3410 | while (size-- > 0) { |
| 3411 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3412 | #ifdef Py_UNICODE_WIDE |
| 3413 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 3414 | if (ch >= 0x10000) { |
| 3415 | *p++ = '\\'; |
| 3416 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 3417 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 3418 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 3419 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 3420 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 3421 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 3422 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 3423 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 3424 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3425 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3426 | else |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3427 | #else |
| 3428 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 3429 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 3430 | Py_UNICODE ch2; |
| 3431 | Py_UCS4 ucs; |
| 3432 | |
| 3433 | ch2 = *s++; |
| 3434 | size--; |
| 3435 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 3436 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 3437 | *p++ = '\\'; |
| 3438 | *p++ = 'U'; |
| 3439 | *p++ = hexdigits[(ucs >> 28) & 0xf]; |
| 3440 | *p++ = hexdigits[(ucs >> 24) & 0xf]; |
| 3441 | *p++ = hexdigits[(ucs >> 20) & 0xf]; |
| 3442 | *p++ = hexdigits[(ucs >> 16) & 0xf]; |
| 3443 | *p++ = hexdigits[(ucs >> 12) & 0xf]; |
| 3444 | *p++ = hexdigits[(ucs >> 8) & 0xf]; |
| 3445 | *p++ = hexdigits[(ucs >> 4) & 0xf]; |
| 3446 | *p++ = hexdigits[ucs & 0xf]; |
| 3447 | continue; |
| 3448 | } |
| 3449 | /* Fall through: isolated surrogates are copied as-is */ |
| 3450 | s--; |
| 3451 | size++; |
| 3452 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3453 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3454 | /* Map 16-bit characters to '\uxxxx' */ |
| 3455 | if (ch >= 256) { |
| 3456 | *p++ = '\\'; |
| 3457 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 3458 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 3459 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 3460 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 3461 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3462 | } |
| 3463 | /* Copy everything else as-is */ |
| 3464 | else |
| 3465 | *p++ = (char) ch; |
| 3466 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3467 | size = p - q; |
| 3468 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3469 | assert(size > 0); |
| 3470 | if (_PyBytes_Resize(&repr, size) < 0) |
| 3471 | return NULL; |
| 3472 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3473 | } |
| 3474 | |
| 3475 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 3476 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3477 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3478 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3479 | PyErr_BadArgument(); |
| 3480 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3481 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3482 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3483 | PyUnicode_GET_SIZE(unicode)); |
| 3484 | |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3485 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3486 | } |
| 3487 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3488 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 3489 | |
| 3490 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3491 | Py_ssize_t size, |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3492 | const char *errors) |
| 3493 | { |
| 3494 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3495 | Py_ssize_t startinpos; |
| 3496 | Py_ssize_t endinpos; |
| 3497 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3498 | PyUnicodeObject *v; |
| 3499 | Py_UNICODE *p; |
| 3500 | const char *end; |
| 3501 | const char *reason; |
| 3502 | PyObject *errorHandler = NULL; |
| 3503 | PyObject *exc = NULL; |
| 3504 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 3505 | #ifdef Py_UNICODE_WIDE |
| 3506 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 3507 | #endif |
| 3508 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3509 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3510 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 3511 | if (v == NULL) |
| 3512 | goto onError; |
| 3513 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
| 3514 | return (PyObject *)v; |
| 3515 | p = PyUnicode_AS_UNICODE(v); |
| 3516 | end = s + size; |
| 3517 | |
| 3518 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3519 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3520 | /* We have to sanity check the raw data, otherwise doom looms for |
| 3521 | some malformed UCS-4 data. */ |
| 3522 | if ( |
| 3523 | #ifdef Py_UNICODE_WIDE |
| 3524 | *p > unimax || *p < 0 || |
| 3525 | #endif |
| 3526 | end-s < Py_UNICODE_SIZE |
| 3527 | ) |
| 3528 | { |
| 3529 | startinpos = s - starts; |
| 3530 | if (end-s < Py_UNICODE_SIZE) { |
| 3531 | endinpos = end-starts; |
| 3532 | reason = "truncated input"; |
| 3533 | } |
| 3534 | else { |
| 3535 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 3536 | reason = "illegal code point (> 0x10FFFF)"; |
| 3537 | } |
| 3538 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 3539 | if (unicode_decode_call_errorhandler( |
| 3540 | errors, &errorHandler, |
| 3541 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3542 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 3543 | &v, &outpos, &p)) { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3544 | goto onError; |
| 3545 | } |
| 3546 | } |
| 3547 | else { |
| 3548 | p++; |
| 3549 | s += Py_UNICODE_SIZE; |
| 3550 | } |
| 3551 | } |
| 3552 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3553 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3554 | goto onError; |
| 3555 | Py_XDECREF(errorHandler); |
| 3556 | Py_XDECREF(exc); |
| 3557 | return (PyObject *)v; |
| 3558 | |
| 3559 | onError: |
| 3560 | Py_XDECREF(v); |
| 3561 | Py_XDECREF(errorHandler); |
| 3562 | Py_XDECREF(exc); |
| 3563 | return NULL; |
| 3564 | } |
| 3565 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3566 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 3567 | |
| 3568 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3569 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3570 | const char *errors) |
| 3571 | { |
| 3572 | PyUnicodeObject *v; |
| 3573 | Py_UNICODE *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3574 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3575 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3576 | if (size == 1) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3577 | Py_UNICODE r = *(unsigned char*)s; |
| 3578 | return PyUnicode_FromUnicode(&r, 1); |
| 3579 | } |
| 3580 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3581 | v = _PyUnicode_New(size); |
| 3582 | if (v == NULL) |
| 3583 | goto onError; |
| 3584 | if (size == 0) |
| 3585 | return (PyObject *)v; |
| 3586 | p = PyUnicode_AS_UNICODE(v); |
| 3587 | while (size-- > 0) |
| 3588 | *p++ = (unsigned char)*s++; |
| 3589 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3590 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3591 | onError: |
| 3592 | Py_XDECREF(v); |
| 3593 | return NULL; |
| 3594 | } |
| 3595 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3596 | /* create or adjust a UnicodeEncodeError */ |
| 3597 | static void make_encode_exception(PyObject **exceptionObject, |
| 3598 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3599 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3600 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3601 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3602 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3603 | if (*exceptionObject == NULL) { |
| 3604 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 3605 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3606 | } |
| 3607 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3608 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 3609 | goto onError; |
| 3610 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 3611 | goto onError; |
| 3612 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 3613 | goto onError; |
| 3614 | return; |
| 3615 | onError: |
| 3616 | Py_DECREF(*exceptionObject); |
| 3617 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3618 | } |
| 3619 | } |
| 3620 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3621 | /* raises a UnicodeEncodeError */ |
| 3622 | static void raise_encode_exception(PyObject **exceptionObject, |
| 3623 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3624 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3625 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3626 | const char *reason) |
| 3627 | { |
| 3628 | make_encode_exception(exceptionObject, |
| 3629 | encoding, unicode, size, startpos, endpos, reason); |
| 3630 | if (*exceptionObject != NULL) |
| 3631 | PyCodec_StrictErrors(*exceptionObject); |
| 3632 | } |
| 3633 | |
| 3634 | /* error handling callback helper: |
| 3635 | build arguments, call the callback and check the arguments, |
| 3636 | put the result into newpos and return the replacement string, which |
| 3637 | has to be freed by the caller */ |
| 3638 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
| 3639 | PyObject **errorHandler, |
| 3640 | const char *encoding, const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3641 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 3642 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3643 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3644 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3645 | static char *argparse = "O!n;encoding error handler must return (str, int) tuple"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3646 | |
| 3647 | PyObject *restuple; |
| 3648 | PyObject *resunicode; |
| 3649 | |
| 3650 | if (*errorHandler == NULL) { |
| 3651 | *errorHandler = PyCodec_LookupError(errors); |
| 3652 | if (*errorHandler == NULL) |
| 3653 | return NULL; |
| 3654 | } |
| 3655 | |
| 3656 | make_encode_exception(exceptionObject, |
| 3657 | encoding, unicode, size, startpos, endpos, reason); |
| 3658 | if (*exceptionObject == NULL) |
| 3659 | return NULL; |
| 3660 | |
| 3661 | restuple = PyObject_CallFunctionObjArgs( |
| 3662 | *errorHandler, *exceptionObject, NULL); |
| 3663 | if (restuple == NULL) |
| 3664 | return NULL; |
| 3665 | if (!PyTuple_Check(restuple)) { |
| 3666 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 3667 | Py_DECREF(restuple); |
| 3668 | return NULL; |
| 3669 | } |
| 3670 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
| 3671 | &resunicode, newpos)) { |
| 3672 | Py_DECREF(restuple); |
| 3673 | return NULL; |
| 3674 | } |
| 3675 | if (*newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3676 | *newpos = size+*newpos; |
| 3677 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 3678 | 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] | 3679 | Py_DECREF(restuple); |
| 3680 | return NULL; |
| 3681 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3682 | Py_INCREF(resunicode); |
| 3683 | Py_DECREF(restuple); |
| 3684 | return resunicode; |
| 3685 | } |
| 3686 | |
| 3687 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3688 | Py_ssize_t size, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3689 | const char *errors, |
| 3690 | int limit) |
| 3691 | { |
| 3692 | /* output object */ |
| 3693 | PyObject *res; |
| 3694 | /* pointers to the beginning and end+1 of input */ |
| 3695 | const Py_UNICODE *startp = p; |
| 3696 | const Py_UNICODE *endp = p + size; |
| 3697 | /* pointer to the beginning of the unencodable characters */ |
| 3698 | /* const Py_UNICODE *badp = NULL; */ |
| 3699 | /* pointer into the output */ |
| 3700 | char *str; |
| 3701 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3702 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3703 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 3704 | 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] | 3705 | PyObject *errorHandler = NULL; |
| 3706 | PyObject *exc = NULL; |
| 3707 | /* the following variable is used for caching string comparisons |
| 3708 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 3709 | int known_errorHandler = -1; |
| 3710 | |
| 3711 | /* allocate enough for a simple encoding without |
| 3712 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3713 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3714 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3715 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3716 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3717 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3718 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3719 | ressize = size; |
| 3720 | |
| 3721 | while (p<endp) { |
| 3722 | Py_UNICODE c = *p; |
| 3723 | |
| 3724 | /* can we encode this? */ |
| 3725 | if (c<limit) { |
| 3726 | /* no overflow check, because we know that the space is enough */ |
| 3727 | *str++ = (char)c; |
| 3728 | ++p; |
| 3729 | } |
| 3730 | else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3731 | Py_ssize_t unicodepos = p-startp; |
| 3732 | Py_ssize_t requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3733 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3734 | Py_ssize_t repsize; |
| 3735 | Py_ssize_t newpos; |
| 3736 | Py_ssize_t respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3737 | Py_UNICODE *uni2; |
| 3738 | /* startpos for collecting unencodable chars */ |
| 3739 | const Py_UNICODE *collstart = p; |
| 3740 | const Py_UNICODE *collend = p; |
| 3741 | /* find all unecodable characters */ |
| 3742 | while ((collend < endp) && ((*collend)>=limit)) |
| 3743 | ++collend; |
| 3744 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 3745 | if (known_errorHandler==-1) { |
| 3746 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3747 | known_errorHandler = 1; |
| 3748 | else if (!strcmp(errors, "replace")) |
| 3749 | known_errorHandler = 2; |
| 3750 | else if (!strcmp(errors, "ignore")) |
| 3751 | known_errorHandler = 3; |
| 3752 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3753 | known_errorHandler = 4; |
| 3754 | else |
| 3755 | known_errorHandler = 0; |
| 3756 | } |
| 3757 | switch (known_errorHandler) { |
| 3758 | case 1: /* strict */ |
| 3759 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 3760 | goto onError; |
| 3761 | case 2: /* replace */ |
| 3762 | while (collstart++<collend) |
| 3763 | *str++ = '?'; /* fall through */ |
| 3764 | case 3: /* ignore */ |
| 3765 | p = collend; |
| 3766 | break; |
| 3767 | case 4: /* xmlcharrefreplace */ |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3768 | respos = str - PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3769 | /* determine replacement size (temporarily (mis)uses p) */ |
| 3770 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 3771 | if (*p<10) |
| 3772 | repsize += 2+1+1; |
| 3773 | else if (*p<100) |
| 3774 | repsize += 2+2+1; |
| 3775 | else if (*p<1000) |
| 3776 | repsize += 2+3+1; |
| 3777 | else if (*p<10000) |
| 3778 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 3779 | #ifndef Py_UNICODE_WIDE |
| 3780 | else |
| 3781 | repsize += 2+5+1; |
| 3782 | #else |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3783 | else if (*p<100000) |
| 3784 | repsize += 2+5+1; |
| 3785 | else if (*p<1000000) |
| 3786 | repsize += 2+6+1; |
| 3787 | else |
| 3788 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3789 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3790 | } |
| 3791 | requiredsize = respos+repsize+(endp-collend); |
| 3792 | if (requiredsize > ressize) { |
| 3793 | if (requiredsize<2*ressize) |
| 3794 | requiredsize = 2*ressize; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3795 | if (_PyBytes_Resize(&res, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3796 | goto onError; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3797 | str = PyBytes_AS_STRING(res) + respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3798 | ressize = requiredsize; |
| 3799 | } |
| 3800 | /* generate replacement (temporarily (mis)uses p) */ |
| 3801 | for (p = collstart; p < collend; ++p) { |
| 3802 | str += sprintf(str, "&#%d;", (int)*p); |
| 3803 | } |
| 3804 | p = collend; |
| 3805 | break; |
| 3806 | default: |
| 3807 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 3808 | encoding, reason, startp, size, &exc, |
| 3809 | collstart-startp, collend-startp, &newpos); |
| 3810 | if (repunicode == NULL) |
| 3811 | goto onError; |
| 3812 | /* need more space? (at least enough for what we |
| 3813 | have+the replacement+the rest of the string, so |
| 3814 | we won't have to check space for encodable characters) */ |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3815 | respos = str - PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3816 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3817 | requiredsize = respos+repsize+(endp-collend); |
| 3818 | if (requiredsize > ressize) { |
| 3819 | if (requiredsize<2*ressize) |
| 3820 | requiredsize = 2*ressize; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3821 | if (_PyBytes_Resize(&res, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3822 | Py_DECREF(repunicode); |
| 3823 | goto onError; |
| 3824 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3825 | str = PyBytes_AS_STRING(res) + respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3826 | ressize = requiredsize; |
| 3827 | } |
| 3828 | /* check if there is anything unencodable in the replacement |
| 3829 | and copy it to the output */ |
| 3830 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 3831 | c = *uni2; |
| 3832 | if (c >= limit) { |
| 3833 | raise_encode_exception(&exc, encoding, startp, size, |
| 3834 | unicodepos, unicodepos+1, reason); |
| 3835 | Py_DECREF(repunicode); |
| 3836 | goto onError; |
| 3837 | } |
| 3838 | *str = (char)c; |
| 3839 | } |
| 3840 | p = startp + newpos; |
| 3841 | Py_DECREF(repunicode); |
| 3842 | } |
| 3843 | } |
| 3844 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3845 | /* Resize if we allocated to much */ |
| 3846 | size = str - PyBytes_AS_STRING(res); |
| 3847 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 3848 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3849 | if (_PyBytes_Resize(&res, size) < 0) |
| 3850 | goto onError; |
| 3851 | } |
| 3852 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3853 | Py_XDECREF(errorHandler); |
| 3854 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3855 | return res; |
| 3856 | |
| 3857 | onError: |
| 3858 | Py_XDECREF(res); |
| 3859 | Py_XDECREF(errorHandler); |
| 3860 | Py_XDECREF(exc); |
| 3861 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3862 | } |
| 3863 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3864 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3865 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3866 | const char *errors) |
| 3867 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3868 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3869 | } |
| 3870 | |
| 3871 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 3872 | { |
| 3873 | if (!PyUnicode_Check(unicode)) { |
| 3874 | PyErr_BadArgument(); |
| 3875 | return NULL; |
| 3876 | } |
| 3877 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 3878 | PyUnicode_GET_SIZE(unicode), |
| 3879 | NULL); |
| 3880 | } |
| 3881 | |
| 3882 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 3883 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3884 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3885 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3886 | const char *errors) |
| 3887 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3888 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3889 | PyUnicodeObject *v; |
| 3890 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3891 | Py_ssize_t startinpos; |
| 3892 | Py_ssize_t endinpos; |
| 3893 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3894 | const char *e; |
| 3895 | PyObject *errorHandler = NULL; |
| 3896 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3897 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3898 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3899 | if (size == 1 && *(unsigned char*)s < 128) { |
| 3900 | Py_UNICODE r = *(unsigned char*)s; |
| 3901 | return PyUnicode_FromUnicode(&r, 1); |
| 3902 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3903 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3904 | v = _PyUnicode_New(size); |
| 3905 | if (v == NULL) |
| 3906 | goto onError; |
| 3907 | if (size == 0) |
| 3908 | return (PyObject *)v; |
| 3909 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3910 | e = s + size; |
| 3911 | while (s < e) { |
| 3912 | register unsigned char c = (unsigned char)*s; |
| 3913 | if (c < 128) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3914 | *p++ = c; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3915 | ++s; |
| 3916 | } |
| 3917 | else { |
| 3918 | startinpos = s-starts; |
| 3919 | endinpos = startinpos + 1; |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 3920 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3921 | if (unicode_decode_call_errorhandler( |
| 3922 | errors, &errorHandler, |
| 3923 | "ascii", "ordinal not in range(128)", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3924 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 3925 | &v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3926 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3927 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3928 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3929 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3930 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3931 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3932 | Py_XDECREF(errorHandler); |
| 3933 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3934 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3935 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3936 | onError: |
| 3937 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3938 | Py_XDECREF(errorHandler); |
| 3939 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3940 | return NULL; |
| 3941 | } |
| 3942 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3943 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3944 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3945 | const char *errors) |
| 3946 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3947 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3948 | } |
| 3949 | |
| 3950 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 3951 | { |
| 3952 | if (!PyUnicode_Check(unicode)) { |
| 3953 | PyErr_BadArgument(); |
| 3954 | return NULL; |
| 3955 | } |
| 3956 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 3957 | PyUnicode_GET_SIZE(unicode), |
| 3958 | NULL); |
| 3959 | } |
| 3960 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3961 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3962 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3963 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3964 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3965 | #if SIZEOF_INT < SIZEOF_SSIZE_T |
| 3966 | #define NEED_RETRY |
| 3967 | #endif |
| 3968 | |
| 3969 | /* XXX This code is limited to "true" double-byte encodings, as |
| 3970 | a) it assumes an incomplete character consists of a single byte, and |
| 3971 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
| 3972 | encodings, see IsDBCSLeadByteEx documentation. */ |
| 3973 | |
| 3974 | static int is_dbcs_lead_byte(const char *s, int offset) |
| 3975 | { |
| 3976 | const char *curr = s + offset; |
| 3977 | |
| 3978 | if (IsDBCSLeadByte(*curr)) { |
| 3979 | const char *prev = CharPrev(s, curr); |
| 3980 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
| 3981 | } |
| 3982 | return 0; |
| 3983 | } |
| 3984 | |
| 3985 | /* |
| 3986 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 3987 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 3988 | */ |
| 3989 | static int decode_mbcs(PyUnicodeObject **v, |
| 3990 | const char *s, /* MBCS string */ |
| 3991 | int size, /* sizeof MBCS string */ |
| 3992 | int final) |
| 3993 | { |
| 3994 | Py_UNICODE *p; |
| 3995 | Py_ssize_t n = 0; |
| 3996 | int usize = 0; |
| 3997 | |
| 3998 | assert(size >= 0); |
| 3999 | |
| 4000 | /* Skip trailing lead-byte unless 'final' is set */ |
| 4001 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
| 4002 | --size; |
| 4003 | |
| 4004 | /* First get the size of the result */ |
| 4005 | if (size > 0) { |
| 4006 | usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); |
| 4007 | if (usize == 0) { |
| 4008 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4009 | return -1; |
| 4010 | } |
| 4011 | } |
| 4012 | |
| 4013 | if (*v == NULL) { |
| 4014 | /* Create unicode object */ |
| 4015 | *v = _PyUnicode_New(usize); |
| 4016 | if (*v == NULL) |
| 4017 | return -1; |
| 4018 | } |
| 4019 | else { |
| 4020 | /* Extend unicode object */ |
| 4021 | n = PyUnicode_GET_SIZE(*v); |
| 4022 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 4023 | return -1; |
| 4024 | } |
| 4025 | |
| 4026 | /* Do the conversion */ |
| 4027 | if (size > 0) { |
| 4028 | p = PyUnicode_AS_UNICODE(*v) + n; |
| 4029 | if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { |
| 4030 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4031 | return -1; |
| 4032 | } |
| 4033 | } |
| 4034 | |
| 4035 | return size; |
| 4036 | } |
| 4037 | |
| 4038 | PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
| 4039 | Py_ssize_t size, |
| 4040 | const char *errors, |
| 4041 | Py_ssize_t *consumed) |
| 4042 | { |
| 4043 | PyUnicodeObject *v = NULL; |
| 4044 | int done; |
| 4045 | |
| 4046 | if (consumed) |
| 4047 | *consumed = 0; |
| 4048 | |
| 4049 | #ifdef NEED_RETRY |
| 4050 | retry: |
| 4051 | if (size > INT_MAX) |
| 4052 | done = decode_mbcs(&v, s, INT_MAX, 0); |
| 4053 | else |
| 4054 | #endif |
| 4055 | done = decode_mbcs(&v, s, (int)size, !consumed); |
| 4056 | |
| 4057 | if (done < 0) { |
| 4058 | Py_XDECREF(v); |
| 4059 | return NULL; |
| 4060 | } |
| 4061 | |
| 4062 | if (consumed) |
| 4063 | *consumed += done; |
| 4064 | |
| 4065 | #ifdef NEED_RETRY |
| 4066 | if (size > INT_MAX) { |
| 4067 | s += done; |
| 4068 | size -= done; |
| 4069 | goto retry; |
| 4070 | } |
| 4071 | #endif |
| 4072 | |
| 4073 | return (PyObject *)v; |
| 4074 | } |
| 4075 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4076 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4077 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4078 | const char *errors) |
| 4079 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4080 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 4081 | } |
| 4082 | |
| 4083 | /* |
| 4084 | * Convert unicode into string object (MBCS). |
| 4085 | * Returns 0 if succeed, -1 otherwise. |
| 4086 | */ |
| 4087 | static int encode_mbcs(PyObject **repr, |
| 4088 | const Py_UNICODE *p, /* unicode */ |
| 4089 | int size) /* size of unicode */ |
| 4090 | { |
| 4091 | int mbcssize = 0; |
| 4092 | Py_ssize_t n = 0; |
| 4093 | |
| 4094 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4095 | |
| 4096 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4097 | if (size > 0) { |
| 4098 | mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); |
| 4099 | if (mbcssize == 0) { |
| 4100 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4101 | return -1; |
| 4102 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4103 | } |
| 4104 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4105 | if (*repr == NULL) { |
| 4106 | /* Create string object */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4107 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4108 | if (*repr == NULL) |
| 4109 | return -1; |
| 4110 | } |
| 4111 | else { |
| 4112 | /* Extend string object */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4113 | n = PyBytes_Size(*repr); |
Hirokazu Yamamoto | d88e8fa | 2008-12-27 14:58:17 +0000 | [diff] [blame] | 4114 | if (_PyBytes_Resize(repr, n + mbcssize) < 0) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4115 | return -1; |
| 4116 | } |
| 4117 | |
| 4118 | /* Do the conversion */ |
| 4119 | if (size > 0) { |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4120 | char *s = PyBytes_AS_STRING(*repr) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4121 | if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { |
| 4122 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4123 | return -1; |
| 4124 | } |
| 4125 | } |
| 4126 | |
| 4127 | return 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4128 | } |
| 4129 | |
| 4130 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4131 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4132 | const char *errors) |
| 4133 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4134 | PyObject *repr = NULL; |
| 4135 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 4136 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4137 | #ifdef NEED_RETRY |
| 4138 | retry: |
| 4139 | if (size > INT_MAX) |
| 4140 | ret = encode_mbcs(&repr, p, INT_MAX); |
| 4141 | else |
| 4142 | #endif |
| 4143 | ret = encode_mbcs(&repr, p, (int)size); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4144 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4145 | if (ret < 0) { |
| 4146 | Py_XDECREF(repr); |
| 4147 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4148 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4149 | |
| 4150 | #ifdef NEED_RETRY |
| 4151 | if (size > INT_MAX) { |
| 4152 | p += INT_MAX; |
| 4153 | size -= INT_MAX; |
| 4154 | goto retry; |
| 4155 | } |
| 4156 | #endif |
| 4157 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4158 | return repr; |
| 4159 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4160 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 4161 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 4162 | { |
| 4163 | if (!PyUnicode_Check(unicode)) { |
| 4164 | PyErr_BadArgument(); |
| 4165 | return NULL; |
| 4166 | } |
| 4167 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 4168 | PyUnicode_GET_SIZE(unicode), |
| 4169 | NULL); |
| 4170 | } |
| 4171 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4172 | #undef NEED_RETRY |
| 4173 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4174 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4175 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4176 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 4177 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4178 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4179 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4180 | PyObject *mapping, |
| 4181 | const char *errors) |
| 4182 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4183 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4184 | Py_ssize_t startinpos; |
| 4185 | Py_ssize_t endinpos; |
| 4186 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4187 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4188 | PyUnicodeObject *v; |
| 4189 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4190 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4191 | PyObject *errorHandler = NULL; |
| 4192 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4193 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4194 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4195 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4196 | /* Default to Latin-1 */ |
| 4197 | if (mapping == NULL) |
| 4198 | return PyUnicode_DecodeLatin1(s, size, errors); |
| 4199 | |
| 4200 | v = _PyUnicode_New(size); |
| 4201 | if (v == NULL) |
| 4202 | goto onError; |
| 4203 | if (size == 0) |
| 4204 | return (PyObject *)v; |
| 4205 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4206 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4207 | if (PyUnicode_CheckExact(mapping)) { |
| 4208 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 4209 | maplen = PyUnicode_GET_SIZE(mapping); |
| 4210 | while (s < e) { |
| 4211 | unsigned char ch = *s; |
| 4212 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4213 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4214 | if (ch < maplen) |
| 4215 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4216 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4217 | if (x == 0xfffe) { |
| 4218 | /* undefined mapping */ |
| 4219 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4220 | startinpos = s-starts; |
| 4221 | endinpos = startinpos+1; |
| 4222 | if (unicode_decode_call_errorhandler( |
| 4223 | errors, &errorHandler, |
| 4224 | "charmap", "character maps to <undefined>", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4225 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 4226 | &v, &outpos, &p)) { |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4227 | goto onError; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 4228 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4229 | continue; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 4230 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4231 | *p++ = x; |
| 4232 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4233 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4234 | } |
| 4235 | else { |
| 4236 | while (s < e) { |
| 4237 | unsigned char ch = *s; |
| 4238 | PyObject *w, *x; |
| 4239 | |
| 4240 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4241 | w = PyLong_FromLong((long)ch); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4242 | if (w == NULL) |
| 4243 | goto onError; |
| 4244 | x = PyObject_GetItem(mapping, w); |
| 4245 | Py_DECREF(w); |
| 4246 | if (x == NULL) { |
| 4247 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4248 | /* No mapping found means: mapping is undefined. */ |
| 4249 | PyErr_Clear(); |
| 4250 | x = Py_None; |
| 4251 | Py_INCREF(x); |
| 4252 | } else |
| 4253 | goto onError; |
| 4254 | } |
| 4255 | |
| 4256 | /* Apply mapping */ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4257 | if (PyLong_Check(x)) { |
| 4258 | long value = PyLong_AS_LONG(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4259 | if (value < 0 || value > 65535) { |
| 4260 | PyErr_SetString(PyExc_TypeError, |
| 4261 | "character mapping must be in range(65536)"); |
| 4262 | Py_DECREF(x); |
| 4263 | goto onError; |
| 4264 | } |
| 4265 | *p++ = (Py_UNICODE)value; |
| 4266 | } |
| 4267 | else if (x == Py_None) { |
| 4268 | /* undefined mapping */ |
| 4269 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4270 | startinpos = s-starts; |
| 4271 | endinpos = startinpos+1; |
| 4272 | if (unicode_decode_call_errorhandler( |
| 4273 | errors, &errorHandler, |
| 4274 | "charmap", "character maps to <undefined>", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4275 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 4276 | &v, &outpos, &p)) { |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4277 | Py_DECREF(x); |
| 4278 | goto onError; |
| 4279 | } |
Walter Dörwald | d4fff17 | 2005-11-28 22:15:56 +0000 | [diff] [blame] | 4280 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4281 | continue; |
| 4282 | } |
| 4283 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4284 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4285 | |
| 4286 | if (targetsize == 1) |
| 4287 | /* 1-1 mapping */ |
| 4288 | *p++ = *PyUnicode_AS_UNICODE(x); |
| 4289 | |
| 4290 | else if (targetsize > 1) { |
| 4291 | /* 1-n mapping */ |
| 4292 | if (targetsize > extrachars) { |
| 4293 | /* resize first */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4294 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 4295 | Py_ssize_t needed = (targetsize - extrachars) + \ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4296 | (targetsize << 2); |
| 4297 | extrachars += needed; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4298 | /* XXX overflow detection missing */ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4299 | if (_PyUnicode_Resize(&v, |
| 4300 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 4301 | Py_DECREF(x); |
| 4302 | goto onError; |
| 4303 | } |
| 4304 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 4305 | } |
| 4306 | Py_UNICODE_COPY(p, |
| 4307 | PyUnicode_AS_UNICODE(x), |
| 4308 | targetsize); |
| 4309 | p += targetsize; |
| 4310 | extrachars -= targetsize; |
| 4311 | } |
| 4312 | /* 1-0 mapping: skip the character */ |
| 4313 | } |
| 4314 | else { |
| 4315 | /* wrong return value */ |
| 4316 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 4317 | "character mapping must return integer, None or str"); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4318 | Py_DECREF(x); |
| 4319 | goto onError; |
| 4320 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4321 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4322 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4323 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4324 | } |
| 4325 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4326 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4327 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4328 | Py_XDECREF(errorHandler); |
| 4329 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4330 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4331 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4332 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4333 | Py_XDECREF(errorHandler); |
| 4334 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4335 | Py_XDECREF(v); |
| 4336 | return NULL; |
| 4337 | } |
| 4338 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4339 | /* Charmap encoding: the lookup table */ |
| 4340 | |
| 4341 | struct encoding_map{ |
| 4342 | PyObject_HEAD |
| 4343 | unsigned char level1[32]; |
| 4344 | int count2, count3; |
| 4345 | unsigned char level23[1]; |
| 4346 | }; |
| 4347 | |
| 4348 | static PyObject* |
| 4349 | encoding_map_size(PyObject *obj, PyObject* args) |
| 4350 | { |
| 4351 | struct encoding_map *map = (struct encoding_map*)obj; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4352 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4353 | 128*map->count3); |
| 4354 | } |
| 4355 | |
| 4356 | static PyMethodDef encoding_map_methods[] = { |
| 4357 | {"size", encoding_map_size, METH_NOARGS, |
| 4358 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 4359 | { 0 } |
| 4360 | }; |
| 4361 | |
| 4362 | static void |
| 4363 | encoding_map_dealloc(PyObject* o) |
| 4364 | { |
| 4365 | PyObject_FREE(o); |
| 4366 | } |
| 4367 | |
| 4368 | static PyTypeObject EncodingMapType = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 4369 | PyVarObject_HEAD_INIT(NULL, 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4370 | "EncodingMap", /*tp_name*/ |
| 4371 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 4372 | 0, /*tp_itemsize*/ |
| 4373 | /* methods */ |
| 4374 | encoding_map_dealloc, /*tp_dealloc*/ |
| 4375 | 0, /*tp_print*/ |
| 4376 | 0, /*tp_getattr*/ |
| 4377 | 0, /*tp_setattr*/ |
| 4378 | 0, /*tp_compare*/ |
| 4379 | 0, /*tp_repr*/ |
| 4380 | 0, /*tp_as_number*/ |
| 4381 | 0, /*tp_as_sequence*/ |
| 4382 | 0, /*tp_as_mapping*/ |
| 4383 | 0, /*tp_hash*/ |
| 4384 | 0, /*tp_call*/ |
| 4385 | 0, /*tp_str*/ |
| 4386 | 0, /*tp_getattro*/ |
| 4387 | 0, /*tp_setattro*/ |
| 4388 | 0, /*tp_as_buffer*/ |
| 4389 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 4390 | 0, /*tp_doc*/ |
| 4391 | 0, /*tp_traverse*/ |
| 4392 | 0, /*tp_clear*/ |
| 4393 | 0, /*tp_richcompare*/ |
| 4394 | 0, /*tp_weaklistoffset*/ |
| 4395 | 0, /*tp_iter*/ |
| 4396 | 0, /*tp_iternext*/ |
| 4397 | encoding_map_methods, /*tp_methods*/ |
| 4398 | 0, /*tp_members*/ |
| 4399 | 0, /*tp_getset*/ |
| 4400 | 0, /*tp_base*/ |
| 4401 | 0, /*tp_dict*/ |
| 4402 | 0, /*tp_descr_get*/ |
| 4403 | 0, /*tp_descr_set*/ |
| 4404 | 0, /*tp_dictoffset*/ |
| 4405 | 0, /*tp_init*/ |
| 4406 | 0, /*tp_alloc*/ |
| 4407 | 0, /*tp_new*/ |
| 4408 | 0, /*tp_free*/ |
| 4409 | 0, /*tp_is_gc*/ |
| 4410 | }; |
| 4411 | |
| 4412 | PyObject* |
| 4413 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 4414 | { |
| 4415 | Py_UNICODE *decode; |
| 4416 | PyObject *result; |
| 4417 | struct encoding_map *mresult; |
| 4418 | int i; |
| 4419 | int need_dict = 0; |
| 4420 | unsigned char level1[32]; |
| 4421 | unsigned char level2[512]; |
| 4422 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 4423 | int count2 = 0, count3 = 0; |
| 4424 | |
| 4425 | if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { |
| 4426 | PyErr_BadArgument(); |
| 4427 | return NULL; |
| 4428 | } |
| 4429 | decode = PyUnicode_AS_UNICODE(string); |
| 4430 | memset(level1, 0xFF, sizeof level1); |
| 4431 | memset(level2, 0xFF, sizeof level2); |
| 4432 | |
| 4433 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 4434 | or if there are non-BMP characters, we need to use |
| 4435 | a mapping dictionary. */ |
| 4436 | if (decode[0] != 0) |
| 4437 | need_dict = 1; |
| 4438 | for (i = 1; i < 256; i++) { |
| 4439 | int l1, l2; |
| 4440 | if (decode[i] == 0 |
| 4441 | #ifdef Py_UNICODE_WIDE |
| 4442 | || decode[i] > 0xFFFF |
| 4443 | #endif |
| 4444 | ) { |
| 4445 | need_dict = 1; |
| 4446 | break; |
| 4447 | } |
| 4448 | if (decode[i] == 0xFFFE) |
| 4449 | /* unmapped character */ |
| 4450 | continue; |
| 4451 | l1 = decode[i] >> 11; |
| 4452 | l2 = decode[i] >> 7; |
| 4453 | if (level1[l1] == 0xFF) |
| 4454 | level1[l1] = count2++; |
| 4455 | if (level2[l2] == 0xFF) |
| 4456 | level2[l2] = count3++; |
| 4457 | } |
| 4458 | |
| 4459 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 4460 | need_dict = 1; |
| 4461 | |
| 4462 | if (need_dict) { |
| 4463 | PyObject *result = PyDict_New(); |
| 4464 | PyObject *key, *value; |
| 4465 | if (!result) |
| 4466 | return NULL; |
| 4467 | for (i = 0; i < 256; i++) { |
| 4468 | key = value = NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4469 | key = PyLong_FromLong(decode[i]); |
| 4470 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4471 | if (!key || !value) |
| 4472 | goto failed1; |
| 4473 | if (PyDict_SetItem(result, key, value) == -1) |
| 4474 | goto failed1; |
| 4475 | Py_DECREF(key); |
| 4476 | Py_DECREF(value); |
| 4477 | } |
| 4478 | return result; |
| 4479 | failed1: |
| 4480 | Py_XDECREF(key); |
| 4481 | Py_XDECREF(value); |
| 4482 | Py_DECREF(result); |
| 4483 | return NULL; |
| 4484 | } |
| 4485 | |
| 4486 | /* Create a three-level trie */ |
| 4487 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 4488 | 16*count2 + 128*count3 - 1); |
| 4489 | if (!result) |
| 4490 | return PyErr_NoMemory(); |
| 4491 | PyObject_Init(result, &EncodingMapType); |
| 4492 | mresult = (struct encoding_map*)result; |
| 4493 | mresult->count2 = count2; |
| 4494 | mresult->count3 = count3; |
| 4495 | mlevel1 = mresult->level1; |
| 4496 | mlevel2 = mresult->level23; |
| 4497 | mlevel3 = mresult->level23 + 16*count2; |
| 4498 | memcpy(mlevel1, level1, 32); |
| 4499 | memset(mlevel2, 0xFF, 16*count2); |
| 4500 | memset(mlevel3, 0, 128*count3); |
| 4501 | count3 = 0; |
| 4502 | for (i = 1; i < 256; i++) { |
| 4503 | int o1, o2, o3, i2, i3; |
| 4504 | if (decode[i] == 0xFFFE) |
| 4505 | /* unmapped character */ |
| 4506 | continue; |
| 4507 | o1 = decode[i]>>11; |
| 4508 | o2 = (decode[i]>>7) & 0xF; |
| 4509 | i2 = 16*mlevel1[o1] + o2; |
| 4510 | if (mlevel2[i2] == 0xFF) |
| 4511 | mlevel2[i2] = count3++; |
| 4512 | o3 = decode[i] & 0x7F; |
| 4513 | i3 = 128*mlevel2[i2] + o3; |
| 4514 | mlevel3[i3] = i; |
| 4515 | } |
| 4516 | return result; |
| 4517 | } |
| 4518 | |
| 4519 | static int |
| 4520 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 4521 | { |
| 4522 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 4523 | int l1 = c>>11; |
| 4524 | int l2 = (c>>7) & 0xF; |
| 4525 | int l3 = c & 0x7F; |
| 4526 | int i; |
| 4527 | |
| 4528 | #ifdef Py_UNICODE_WIDE |
| 4529 | if (c > 0xFFFF) { |
| 4530 | return -1; |
| 4531 | } |
| 4532 | #endif |
| 4533 | if (c == 0) |
| 4534 | return 0; |
| 4535 | /* level 1*/ |
| 4536 | i = map->level1[l1]; |
| 4537 | if (i == 0xFF) { |
| 4538 | return -1; |
| 4539 | } |
| 4540 | /* level 2*/ |
| 4541 | i = map->level23[16*i+l2]; |
| 4542 | if (i == 0xFF) { |
| 4543 | return -1; |
| 4544 | } |
| 4545 | /* level 3 */ |
| 4546 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 4547 | if (i == 0) { |
| 4548 | return -1; |
| 4549 | } |
| 4550 | return i; |
| 4551 | } |
| 4552 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4553 | /* Lookup the character ch in the mapping. If the character |
| 4554 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 4555 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4556 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4557 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4558 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4559 | PyObject *x; |
| 4560 | |
| 4561 | if (w == NULL) |
| 4562 | return NULL; |
| 4563 | x = PyObject_GetItem(mapping, w); |
| 4564 | Py_DECREF(w); |
| 4565 | if (x == NULL) { |
| 4566 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4567 | /* No mapping found means: mapping is undefined. */ |
| 4568 | PyErr_Clear(); |
| 4569 | x = Py_None; |
| 4570 | Py_INCREF(x); |
| 4571 | return x; |
| 4572 | } else |
| 4573 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4574 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 4575 | else if (x == Py_None) |
| 4576 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4577 | else if (PyLong_Check(x)) { |
| 4578 | long value = PyLong_AS_LONG(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4579 | if (value < 0 || value > 255) { |
| 4580 | PyErr_SetString(PyExc_TypeError, |
| 4581 | "character mapping must be in range(256)"); |
| 4582 | Py_DECREF(x); |
| 4583 | return NULL; |
| 4584 | } |
| 4585 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4586 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4587 | else if (PyBytes_Check(x)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4588 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4589 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4590 | /* wrong return value */ |
Walter Dörwald | 580ceed | 2007-05-09 10:39:19 +0000 | [diff] [blame] | 4591 | PyErr_Format(PyExc_TypeError, |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 4592 | "character mapping must return integer, bytes or None, not %.400s", |
Walter Dörwald | 580ceed | 2007-05-09 10:39:19 +0000 | [diff] [blame] | 4593 | x->ob_type->tp_name); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4594 | Py_DECREF(x); |
| 4595 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4596 | } |
| 4597 | } |
| 4598 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4599 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4600 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4601 | { |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4602 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4603 | /* exponentially overallocate to minimize reallocations */ |
| 4604 | if (requiredsize < 2*outsize) |
| 4605 | requiredsize = 2*outsize; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4606 | if (_PyBytes_Resize(outobj, requiredsize)) |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4607 | return -1; |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4608 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4609 | } |
| 4610 | |
| 4611 | typedef enum charmapencode_result { |
| 4612 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
| 4613 | }charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4614 | /* lookup the character, put the result in the output string and adjust |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4615 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4616 | space is available. Return a new reference to the object that |
| 4617 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 4618 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 4619 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4620 | static |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4621 | charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4622 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4623 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4624 | PyObject *rep; |
| 4625 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4626 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4627 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 4628 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4629 | int res = encoding_map_lookup(c, mapping); |
| 4630 | Py_ssize_t requiredsize = *outpos+1; |
| 4631 | if (res == -1) |
| 4632 | return enc_FAILED; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4633 | if (outsize<requiredsize) |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4634 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4635 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4636 | outstart = PyBytes_AS_STRING(*outobj); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4637 | outstart[(*outpos)++] = (char)res; |
| 4638 | return enc_SUCCESS; |
| 4639 | } |
| 4640 | |
| 4641 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4642 | if (rep==NULL) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4643 | return enc_EXCEPTION; |
| 4644 | else if (rep==Py_None) { |
| 4645 | Py_DECREF(rep); |
| 4646 | return enc_FAILED; |
| 4647 | } else { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4648 | if (PyLong_Check(rep)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4649 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4650 | if (outsize<requiredsize) |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4651 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4652 | Py_DECREF(rep); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4653 | return enc_EXCEPTION; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4654 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4655 | outstart = PyBytes_AS_STRING(*outobj); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4656 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4657 | } |
| 4658 | else { |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4659 | const char *repchars = PyBytes_AS_STRING(rep); |
| 4660 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4661 | Py_ssize_t requiredsize = *outpos+repsize; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4662 | if (outsize<requiredsize) |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4663 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4664 | Py_DECREF(rep); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4665 | return enc_EXCEPTION; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4666 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4667 | outstart = PyBytes_AS_STRING(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4668 | memcpy(outstart + *outpos, repchars, repsize); |
| 4669 | *outpos += repsize; |
| 4670 | } |
| 4671 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4672 | Py_DECREF(rep); |
| 4673 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4674 | } |
| 4675 | |
| 4676 | /* handle an error in PyUnicode_EncodeCharmap |
| 4677 | Return 0 on success, -1 on error */ |
| 4678 | static |
| 4679 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4680 | 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] | 4681 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4682 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4683 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4684 | { |
| 4685 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4686 | Py_ssize_t repsize; |
| 4687 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4688 | Py_UNICODE *uni2; |
| 4689 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4690 | Py_ssize_t collstartpos = *inpos; |
| 4691 | Py_ssize_t collendpos = *inpos+1; |
| 4692 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4693 | char *encoding = "charmap"; |
| 4694 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4695 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4696 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4697 | /* find all unencodable characters */ |
| 4698 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4699 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 4700 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4701 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 4702 | if (res != -1) |
| 4703 | break; |
| 4704 | ++collendpos; |
| 4705 | continue; |
| 4706 | } |
| 4707 | |
| 4708 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 4709 | if (rep==NULL) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4710 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4711 | else if (rep!=Py_None) { |
| 4712 | Py_DECREF(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4713 | break; |
| 4714 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4715 | Py_DECREF(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4716 | ++collendpos; |
| 4717 | } |
| 4718 | /* cache callback name lookup |
| 4719 | * (if not done yet, i.e. it's the first error) */ |
| 4720 | if (*known_errorHandler==-1) { |
| 4721 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4722 | *known_errorHandler = 1; |
| 4723 | else if (!strcmp(errors, "replace")) |
| 4724 | *known_errorHandler = 2; |
| 4725 | else if (!strcmp(errors, "ignore")) |
| 4726 | *known_errorHandler = 3; |
| 4727 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4728 | *known_errorHandler = 4; |
| 4729 | else |
| 4730 | *known_errorHandler = 0; |
| 4731 | } |
| 4732 | switch (*known_errorHandler) { |
| 4733 | case 1: /* strict */ |
| 4734 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4735 | return -1; |
| 4736 | case 2: /* replace */ |
| 4737 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
| 4738 | x = charmapencode_output('?', mapping, res, respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4739 | if (x==enc_EXCEPTION) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4740 | return -1; |
| 4741 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4742 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4743 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4744 | return -1; |
| 4745 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4746 | } |
| 4747 | /* fall through */ |
| 4748 | case 3: /* ignore */ |
| 4749 | *inpos = collendpos; |
| 4750 | break; |
| 4751 | case 4: /* xmlcharrefreplace */ |
| 4752 | /* generate replacement (temporarily (mis)uses p) */ |
| 4753 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
| 4754 | char buffer[2+29+1+1]; |
| 4755 | char *cp; |
| 4756 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 4757 | for (cp = buffer; *cp; ++cp) { |
| 4758 | x = charmapencode_output(*cp, mapping, res, respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4759 | if (x==enc_EXCEPTION) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4760 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4761 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4762 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4763 | return -1; |
| 4764 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4765 | } |
| 4766 | } |
| 4767 | *inpos = collendpos; |
| 4768 | break; |
| 4769 | default: |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4770 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4771 | encoding, reason, p, size, exceptionObject, |
| 4772 | collstartpos, collendpos, &newpos); |
| 4773 | if (repunicode == NULL) |
| 4774 | return -1; |
| 4775 | /* generate replacement */ |
| 4776 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4777 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 4778 | x = charmapencode_output(*uni2, mapping, res, respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4779 | if (x==enc_EXCEPTION) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4780 | return -1; |
| 4781 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4782 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4783 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4784 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4785 | return -1; |
| 4786 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4787 | } |
| 4788 | *inpos = newpos; |
| 4789 | Py_DECREF(repunicode); |
| 4790 | } |
| 4791 | return 0; |
| 4792 | } |
| 4793 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4794 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4795 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4796 | PyObject *mapping, |
| 4797 | const char *errors) |
| 4798 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4799 | /* output object */ |
| 4800 | PyObject *res = NULL; |
| 4801 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4802 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4803 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4804 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4805 | PyObject *errorHandler = NULL; |
| 4806 | PyObject *exc = NULL; |
| 4807 | /* the following variable is used for caching string comparisons |
| 4808 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 4809 | * 3=ignore, 4=xmlcharrefreplace */ |
| 4810 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4811 | |
| 4812 | /* Default to Latin-1 */ |
| 4813 | if (mapping == NULL) |
| 4814 | return PyUnicode_EncodeLatin1(p, size, errors); |
| 4815 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4816 | /* allocate enough for a simple encoding without |
| 4817 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4818 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4819 | if (res == NULL) |
| 4820 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 4821 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4822 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4823 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4824 | while (inpos<size) { |
| 4825 | /* try to encode it */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4826 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4827 | if (x==enc_EXCEPTION) /* error */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4828 | goto onError; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4829 | if (x==enc_FAILED) { /* unencodable character */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4830 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 4831 | &exc, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4832 | &known_errorHandler, &errorHandler, errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4833 | &res, &respos)) { |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 4834 | goto onError; |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 4835 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4836 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4837 | else |
| 4838 | /* done with this character => adjust input position */ |
| 4839 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4840 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4841 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4842 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4843 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4844 | if (_PyBytes_Resize(&res, respos) < 0) |
| 4845 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4846 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4847 | Py_XDECREF(exc); |
| 4848 | Py_XDECREF(errorHandler); |
| 4849 | return res; |
| 4850 | |
| 4851 | onError: |
| 4852 | Py_XDECREF(res); |
| 4853 | Py_XDECREF(exc); |
| 4854 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4855 | return NULL; |
| 4856 | } |
| 4857 | |
| 4858 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
| 4859 | PyObject *mapping) |
| 4860 | { |
| 4861 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
| 4862 | PyErr_BadArgument(); |
| 4863 | return NULL; |
| 4864 | } |
| 4865 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
| 4866 | PyUnicode_GET_SIZE(unicode), |
| 4867 | mapping, |
| 4868 | NULL); |
| 4869 | } |
| 4870 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4871 | /* create or adjust a UnicodeTranslateError */ |
| 4872 | static void make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4873 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4874 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4875 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4876 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4877 | if (*exceptionObject == NULL) { |
| 4878 | *exceptionObject = PyUnicodeTranslateError_Create( |
| 4879 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4880 | } |
| 4881 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4882 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 4883 | goto onError; |
| 4884 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 4885 | goto onError; |
| 4886 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 4887 | goto onError; |
| 4888 | return; |
| 4889 | onError: |
| 4890 | Py_DECREF(*exceptionObject); |
| 4891 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4892 | } |
| 4893 | } |
| 4894 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4895 | /* raises a UnicodeTranslateError */ |
| 4896 | static void raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4897 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4898 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4899 | const char *reason) |
| 4900 | { |
| 4901 | make_translate_exception(exceptionObject, |
| 4902 | unicode, size, startpos, endpos, reason); |
| 4903 | if (*exceptionObject != NULL) |
| 4904 | PyCodec_StrictErrors(*exceptionObject); |
| 4905 | } |
| 4906 | |
| 4907 | /* error handling callback helper: |
| 4908 | build arguments, call the callback and check the arguments, |
| 4909 | put the result into newpos and return the replacement string, which |
| 4910 | has to be freed by the caller */ |
| 4911 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
| 4912 | PyObject **errorHandler, |
| 4913 | const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4914 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 4915 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4916 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4917 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 4918 | static char *argparse = "O!n;translating error handler must return (str, int) tuple"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4919 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4920 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4921 | PyObject *restuple; |
| 4922 | PyObject *resunicode; |
| 4923 | |
| 4924 | if (*errorHandler == NULL) { |
| 4925 | *errorHandler = PyCodec_LookupError(errors); |
| 4926 | if (*errorHandler == NULL) |
| 4927 | return NULL; |
| 4928 | } |
| 4929 | |
| 4930 | make_translate_exception(exceptionObject, |
| 4931 | unicode, size, startpos, endpos, reason); |
| 4932 | if (*exceptionObject == NULL) |
| 4933 | return NULL; |
| 4934 | |
| 4935 | restuple = PyObject_CallFunctionObjArgs( |
| 4936 | *errorHandler, *exceptionObject, NULL); |
| 4937 | if (restuple == NULL) |
| 4938 | return NULL; |
| 4939 | if (!PyTuple_Check(restuple)) { |
| 4940 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 4941 | Py_DECREF(restuple); |
| 4942 | return NULL; |
| 4943 | } |
| 4944 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4945 | &resunicode, &i_newpos)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4946 | Py_DECREF(restuple); |
| 4947 | return NULL; |
| 4948 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4949 | if (i_newpos<0) |
| 4950 | *newpos = size+i_newpos; |
| 4951 | else |
| 4952 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4953 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 4954 | 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] | 4955 | Py_DECREF(restuple); |
| 4956 | return NULL; |
| 4957 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4958 | Py_INCREF(resunicode); |
| 4959 | Py_DECREF(restuple); |
| 4960 | return resunicode; |
| 4961 | } |
| 4962 | |
| 4963 | /* Lookup the character ch in the mapping and put the result in result, |
| 4964 | which must be decrefed by the caller. |
| 4965 | Return 0 on success, -1 on error */ |
| 4966 | static |
| 4967 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 4968 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4969 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4970 | PyObject *x; |
| 4971 | |
| 4972 | if (w == NULL) |
| 4973 | return -1; |
| 4974 | x = PyObject_GetItem(mapping, w); |
| 4975 | Py_DECREF(w); |
| 4976 | if (x == NULL) { |
| 4977 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4978 | /* No mapping found means: use 1:1 mapping. */ |
| 4979 | PyErr_Clear(); |
| 4980 | *result = NULL; |
| 4981 | return 0; |
| 4982 | } else |
| 4983 | return -1; |
| 4984 | } |
| 4985 | else if (x == Py_None) { |
| 4986 | *result = x; |
| 4987 | return 0; |
| 4988 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4989 | else if (PyLong_Check(x)) { |
| 4990 | long value = PyLong_AS_LONG(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4991 | long max = PyUnicode_GetMax(); |
| 4992 | if (value < 0 || value > max) { |
| 4993 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 4994 | "character mapping must be in range(0x%x)", max+1); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4995 | Py_DECREF(x); |
| 4996 | return -1; |
| 4997 | } |
| 4998 | *result = x; |
| 4999 | return 0; |
| 5000 | } |
| 5001 | else if (PyUnicode_Check(x)) { |
| 5002 | *result = x; |
| 5003 | return 0; |
| 5004 | } |
| 5005 | else { |
| 5006 | /* wrong return value */ |
| 5007 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 5008 | "character mapping must return integer, None or str"); |
Walter Dörwald | 150523e | 2003-08-15 16:52:19 +0000 | [diff] [blame] | 5009 | Py_DECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5010 | return -1; |
| 5011 | } |
| 5012 | } |
| 5013 | /* ensure that *outobj is at least requiredsize characters long, |
| 5014 | if not reallocate and adjust various state variables. |
| 5015 | Return 0 on success, -1 on error */ |
| 5016 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5017 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5018 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5019 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5020 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5021 | if (requiredsize > oldsize) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5022 | /* remember old output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5023 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5024 | /* exponentially overallocate to minimize reallocations */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5025 | if (requiredsize < 2 * oldsize) |
| 5026 | requiredsize = 2 * oldsize; |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 5027 | if (PyUnicode_Resize(outobj, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5028 | return -1; |
| 5029 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5030 | } |
| 5031 | return 0; |
| 5032 | } |
| 5033 | /* lookup the character, put the result in the output string and adjust |
| 5034 | various state variables. Return a new reference to the object that |
| 5035 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 5036 | undefined (in which case no character was written). |
| 5037 | The called must decref result. |
| 5038 | Return 0 on success, -1 on error. */ |
| 5039 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5040 | 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] | 5041 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5042 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5043 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5044 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5045 | return -1; |
| 5046 | if (*res==NULL) { |
| 5047 | /* not found => default to 1:1 mapping */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5048 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5049 | } |
| 5050 | else if (*res==Py_None) |
| 5051 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5052 | else if (PyLong_Check(*res)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5053 | /* no overflow check, because we know that the space is enough */ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5054 | *(*outp)++ = (Py_UNICODE)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5055 | } |
| 5056 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5057 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5058 | if (repsize==1) { |
| 5059 | /* no overflow check, because we know that the space is enough */ |
| 5060 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 5061 | } |
| 5062 | else if (repsize!=0) { |
| 5063 | /* more than one character */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5064 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
Walter Dörwald | cd736e7 | 2004-02-05 17:36:00 +0000 | [diff] [blame] | 5065 | (insize - (curinp-startinp)) + |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5066 | repsize - 1; |
| 5067 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5068 | return -1; |
| 5069 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 5070 | *outp += repsize; |
| 5071 | } |
| 5072 | } |
| 5073 | else |
| 5074 | return -1; |
| 5075 | return 0; |
| 5076 | } |
| 5077 | |
| 5078 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5079 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5080 | PyObject *mapping, |
| 5081 | const char *errors) |
| 5082 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5083 | /* output object */ |
| 5084 | PyObject *res = NULL; |
| 5085 | /* pointers to the beginning and end+1 of input */ |
| 5086 | const Py_UNICODE *startp = p; |
| 5087 | const Py_UNICODE *endp = p + size; |
| 5088 | /* pointer into the output */ |
| 5089 | Py_UNICODE *str; |
| 5090 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5091 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5092 | char *reason = "character maps to <undefined>"; |
| 5093 | PyObject *errorHandler = NULL; |
| 5094 | PyObject *exc = NULL; |
| 5095 | /* the following variable is used for caching string comparisons |
| 5096 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 5097 | * 3=ignore, 4=xmlcharrefreplace */ |
| 5098 | int known_errorHandler = -1; |
| 5099 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5100 | if (mapping == NULL) { |
| 5101 | PyErr_BadArgument(); |
| 5102 | return NULL; |
| 5103 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5104 | |
| 5105 | /* allocate enough for a simple 1:1 translation without |
| 5106 | replacements, if we need more, we'll resize */ |
| 5107 | res = PyUnicode_FromUnicode(NULL, size); |
| 5108 | if (res == NULL) |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5109 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5110 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5111 | return res; |
| 5112 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5113 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5114 | while (p<endp) { |
| 5115 | /* try to encode it */ |
| 5116 | PyObject *x = NULL; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5117 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5118 | Py_XDECREF(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5119 | goto onError; |
| 5120 | } |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 5121 | Py_XDECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5122 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 5123 | ++p; |
| 5124 | else { /* untranslatable character */ |
| 5125 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5126 | Py_ssize_t repsize; |
| 5127 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5128 | Py_UNICODE *uni2; |
| 5129 | /* startpos for collecting untranslatable chars */ |
| 5130 | const Py_UNICODE *collstart = p; |
| 5131 | const Py_UNICODE *collend = p+1; |
| 5132 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5133 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5134 | /* find all untranslatable characters */ |
| 5135 | while (collend < endp) { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5136 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5137 | goto onError; |
| 5138 | Py_XDECREF(x); |
| 5139 | if (x!=Py_None) |
| 5140 | break; |
| 5141 | ++collend; |
| 5142 | } |
| 5143 | /* cache callback name lookup |
| 5144 | * (if not done yet, i.e. it's the first error) */ |
| 5145 | if (known_errorHandler==-1) { |
| 5146 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5147 | known_errorHandler = 1; |
| 5148 | else if (!strcmp(errors, "replace")) |
| 5149 | known_errorHandler = 2; |
| 5150 | else if (!strcmp(errors, "ignore")) |
| 5151 | known_errorHandler = 3; |
| 5152 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5153 | known_errorHandler = 4; |
| 5154 | else |
| 5155 | known_errorHandler = 0; |
| 5156 | } |
| 5157 | switch (known_errorHandler) { |
| 5158 | case 1: /* strict */ |
| 5159 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
| 5160 | goto onError; |
| 5161 | case 2: /* replace */ |
| 5162 | /* No need to check for space, this is a 1:1 replacement */ |
| 5163 | for (coll = collstart; coll<collend; ++coll) |
| 5164 | *str++ = '?'; |
| 5165 | /* fall through */ |
| 5166 | case 3: /* ignore */ |
| 5167 | p = collend; |
| 5168 | break; |
| 5169 | case 4: /* xmlcharrefreplace */ |
| 5170 | /* generate replacement (temporarily (mis)uses p) */ |
| 5171 | for (p = collstart; p < collend; ++p) { |
| 5172 | char buffer[2+29+1+1]; |
| 5173 | char *cp; |
| 5174 | sprintf(buffer, "&#%d;", (int)*p); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5175 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5176 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 5177 | goto onError; |
| 5178 | for (cp = buffer; *cp; ++cp) |
| 5179 | *str++ = *cp; |
| 5180 | } |
| 5181 | p = collend; |
| 5182 | break; |
| 5183 | default: |
| 5184 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 5185 | reason, startp, size, &exc, |
| 5186 | collstart-startp, collend-startp, &newpos); |
| 5187 | if (repunicode == NULL) |
| 5188 | goto onError; |
| 5189 | /* generate replacement */ |
| 5190 | repsize = PyUnicode_GET_SIZE(repunicode); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5191 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5192 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 5193 | Py_DECREF(repunicode); |
| 5194 | goto onError; |
| 5195 | } |
| 5196 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 5197 | *str++ = *uni2; |
| 5198 | p = startp + newpos; |
| 5199 | Py_DECREF(repunicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5200 | } |
| 5201 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5202 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5203 | /* Resize if we allocated to much */ |
| 5204 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5205 | if (respos<PyUnicode_GET_SIZE(res)) { |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 5206 | if (PyUnicode_Resize(&res, respos) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 5207 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5208 | } |
| 5209 | Py_XDECREF(exc); |
| 5210 | Py_XDECREF(errorHandler); |
| 5211 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5212 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5213 | onError: |
| 5214 | Py_XDECREF(res); |
| 5215 | Py_XDECREF(exc); |
| 5216 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5217 | return NULL; |
| 5218 | } |
| 5219 | |
| 5220 | PyObject *PyUnicode_Translate(PyObject *str, |
| 5221 | PyObject *mapping, |
| 5222 | const char *errors) |
| 5223 | { |
| 5224 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5225 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5226 | str = PyUnicode_FromObject(str); |
| 5227 | if (str == NULL) |
| 5228 | goto onError; |
| 5229 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
| 5230 | PyUnicode_GET_SIZE(str), |
| 5231 | mapping, |
| 5232 | errors); |
| 5233 | Py_DECREF(str); |
| 5234 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5235 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5236 | onError: |
| 5237 | Py_XDECREF(str); |
| 5238 | return NULL; |
| 5239 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5240 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5241 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 5242 | |
| 5243 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5244 | Py_ssize_t length, |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5245 | char *output, |
| 5246 | const char *errors) |
| 5247 | { |
| 5248 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5249 | PyObject *errorHandler = NULL; |
| 5250 | PyObject *exc = NULL; |
| 5251 | const char *encoding = "decimal"; |
| 5252 | const char *reason = "invalid decimal Unicode string"; |
| 5253 | /* the following variable is used for caching string comparisons |
| 5254 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 5255 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5256 | |
| 5257 | if (output == NULL) { |
| 5258 | PyErr_BadArgument(); |
| 5259 | return -1; |
| 5260 | } |
| 5261 | |
| 5262 | p = s; |
| 5263 | end = s + length; |
| 5264 | while (p < end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5265 | register Py_UNICODE ch = *p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5266 | int decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5267 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5268 | Py_ssize_t repsize; |
| 5269 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5270 | Py_UNICODE *uni2; |
| 5271 | Py_UNICODE *collstart; |
| 5272 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5273 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5274 | if (Py_UNICODE_ISSPACE(ch)) { |
| 5275 | *output++ = ' '; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5276 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5277 | continue; |
| 5278 | } |
| 5279 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5280 | if (decimal >= 0) { |
| 5281 | *output++ = '0' + decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5282 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5283 | continue; |
| 5284 | } |
Guido van Rossum | ba47704 | 2000-04-06 18:18:10 +0000 | [diff] [blame] | 5285 | if (0 < ch && ch < 256) { |
Guido van Rossum | 42c29aa | 2000-05-03 23:58:29 +0000 | [diff] [blame] | 5286 | *output++ = (char)ch; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5287 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5288 | continue; |
| 5289 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5290 | /* All other characters are considered unencodable */ |
| 5291 | collstart = p; |
| 5292 | collend = p+1; |
| 5293 | while (collend < end) { |
| 5294 | if ((0 < *collend && *collend < 256) || |
| 5295 | !Py_UNICODE_ISSPACE(*collend) || |
| 5296 | Py_UNICODE_TODECIMAL(*collend)) |
| 5297 | break; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5298 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5299 | /* cache callback name lookup |
| 5300 | * (if not done yet, i.e. it's the first error) */ |
| 5301 | if (known_errorHandler==-1) { |
| 5302 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5303 | known_errorHandler = 1; |
| 5304 | else if (!strcmp(errors, "replace")) |
| 5305 | known_errorHandler = 2; |
| 5306 | else if (!strcmp(errors, "ignore")) |
| 5307 | known_errorHandler = 3; |
| 5308 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5309 | known_errorHandler = 4; |
| 5310 | else |
| 5311 | known_errorHandler = 0; |
| 5312 | } |
| 5313 | switch (known_errorHandler) { |
| 5314 | case 1: /* strict */ |
| 5315 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 5316 | goto onError; |
| 5317 | case 2: /* replace */ |
| 5318 | for (p = collstart; p < collend; ++p) |
| 5319 | *output++ = '?'; |
| 5320 | /* fall through */ |
| 5321 | case 3: /* ignore */ |
| 5322 | p = collend; |
| 5323 | break; |
| 5324 | case 4: /* xmlcharrefreplace */ |
| 5325 | /* generate replacement (temporarily (mis)uses p) */ |
| 5326 | for (p = collstart; p < collend; ++p) |
| 5327 | output += sprintf(output, "&#%d;", (int)*p); |
| 5328 | p = collend; |
| 5329 | break; |
| 5330 | default: |
| 5331 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 5332 | encoding, reason, s, length, &exc, |
| 5333 | collstart-s, collend-s, &newpos); |
| 5334 | if (repunicode == NULL) |
| 5335 | goto onError; |
| 5336 | /* generate replacement */ |
| 5337 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5338 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 5339 | Py_UNICODE ch = *uni2; |
| 5340 | if (Py_UNICODE_ISSPACE(ch)) |
| 5341 | *output++ = ' '; |
| 5342 | else { |
| 5343 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5344 | if (decimal >= 0) |
| 5345 | *output++ = '0' + decimal; |
| 5346 | else if (0 < ch && ch < 256) |
| 5347 | *output++ = (char)ch; |
| 5348 | else { |
| 5349 | Py_DECREF(repunicode); |
| 5350 | raise_encode_exception(&exc, encoding, |
| 5351 | s, length, collstart-s, collend-s, reason); |
| 5352 | goto onError; |
| 5353 | } |
| 5354 | } |
| 5355 | } |
| 5356 | p = s + newpos; |
| 5357 | Py_DECREF(repunicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5358 | } |
| 5359 | } |
| 5360 | /* 0-terminate the output string */ |
| 5361 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5362 | Py_XDECREF(exc); |
| 5363 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5364 | return 0; |
| 5365 | |
| 5366 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5367 | Py_XDECREF(exc); |
| 5368 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5369 | return -1; |
| 5370 | } |
| 5371 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5372 | /* --- Helpers ------------------------------------------------------------ */ |
| 5373 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 5374 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5375 | #include "stringlib/fastsearch.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5376 | #include "stringlib/count.h" |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 5377 | /* Include _ParseTupleFinds from find.h */ |
| 5378 | #define FROM_UNICODE |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5379 | #include "stringlib/find.h" |
| 5380 | #include "stringlib/partition.h" |
| 5381 | |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 5382 | #define _Py_InsertThousandsGrouping _PyUnicode_InsertThousandsGrouping |
| 5383 | #include "stringlib/localeutil.h" |
| 5384 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5385 | /* helper macro to fixup start/end slice values */ |
| 5386 | #define FIX_START_END(obj) \ |
| 5387 | if (start < 0) \ |
| 5388 | start += (obj)->length; \ |
| 5389 | if (start < 0) \ |
| 5390 | start = 0; \ |
| 5391 | if (end > (obj)->length) \ |
| 5392 | end = (obj)->length; \ |
| 5393 | if (end < 0) \ |
| 5394 | end += (obj)->length; \ |
| 5395 | if (end < 0) \ |
| 5396 | end = 0; |
| 5397 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5398 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5399 | PyObject *substr, |
| 5400 | Py_ssize_t start, |
| 5401 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5402 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5403 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5404 | PyUnicodeObject* str_obj; |
| 5405 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5406 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5407 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 5408 | if (!str_obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5409 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5410 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 5411 | if (!sub_obj) { |
| 5412 | Py_DECREF(str_obj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5413 | return -1; |
| 5414 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5415 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5416 | FIX_START_END(str_obj); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5417 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5418 | result = stringlib_count( |
| 5419 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length |
| 5420 | ); |
| 5421 | |
| 5422 | Py_DECREF(sub_obj); |
| 5423 | Py_DECREF(str_obj); |
| 5424 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5425 | return result; |
| 5426 | } |
| 5427 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5428 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5429 | PyObject *sub, |
| 5430 | Py_ssize_t start, |
| 5431 | Py_ssize_t end, |
| 5432 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5433 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5434 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5435 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5436 | str = PyUnicode_FromObject(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5437 | if (!str) |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 5438 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5439 | sub = PyUnicode_FromObject(sub); |
| 5440 | if (!sub) { |
Marc-André Lemburg | 4164439 | 2002-05-29 13:46:29 +0000 | [diff] [blame] | 5441 | Py_DECREF(str); |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 5442 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5443 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5444 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5445 | if (direction > 0) |
| 5446 | result = stringlib_find_slice( |
| 5447 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5448 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5449 | start, end |
| 5450 | ); |
| 5451 | else |
| 5452 | result = stringlib_rfind_slice( |
| 5453 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5454 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5455 | start, end |
| 5456 | ); |
| 5457 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5458 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5459 | Py_DECREF(sub); |
| 5460 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5461 | return result; |
| 5462 | } |
| 5463 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5464 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5465 | int tailmatch(PyUnicodeObject *self, |
| 5466 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5467 | Py_ssize_t start, |
| 5468 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5469 | int direction) |
| 5470 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5471 | if (substring->length == 0) |
| 5472 | return 1; |
| 5473 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5474 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5475 | |
| 5476 | end -= substring->length; |
| 5477 | if (end < start) |
| 5478 | return 0; |
| 5479 | |
| 5480 | if (direction > 0) { |
| 5481 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 5482 | return 1; |
| 5483 | } else { |
| 5484 | if (Py_UNICODE_MATCH(self, start, substring)) |
| 5485 | return 1; |
| 5486 | } |
| 5487 | |
| 5488 | return 0; |
| 5489 | } |
| 5490 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5491 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5492 | PyObject *substr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5493 | Py_ssize_t start, |
| 5494 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5495 | int direction) |
| 5496 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5497 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5498 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5499 | str = PyUnicode_FromObject(str); |
| 5500 | if (str == NULL) |
| 5501 | return -1; |
| 5502 | substr = PyUnicode_FromObject(substr); |
| 5503 | if (substr == NULL) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5504 | Py_DECREF(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5505 | return -1; |
| 5506 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5507 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5508 | result = tailmatch((PyUnicodeObject *)str, |
| 5509 | (PyUnicodeObject *)substr, |
| 5510 | start, end, direction); |
| 5511 | Py_DECREF(str); |
| 5512 | Py_DECREF(substr); |
| 5513 | return result; |
| 5514 | } |
| 5515 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5516 | /* Apply fixfct filter to the Unicode object self and return a |
| 5517 | reference to the modified object */ |
| 5518 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5519 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5520 | PyObject *fixup(PyUnicodeObject *self, |
| 5521 | int (*fixfct)(PyUnicodeObject *s)) |
| 5522 | { |
| 5523 | |
| 5524 | PyUnicodeObject *u; |
| 5525 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5526 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5527 | if (u == NULL) |
| 5528 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5529 | |
| 5530 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5531 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5532 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5533 | /* fixfct should return TRUE if it modified the buffer. If |
| 5534 | FALSE, return a reference to the original buffer instead |
| 5535 | (to save space, not time) */ |
| 5536 | Py_INCREF(self); |
| 5537 | Py_DECREF(u); |
| 5538 | return (PyObject*) self; |
| 5539 | } |
| 5540 | return (PyObject*) u; |
| 5541 | } |
| 5542 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5543 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5544 | int fixupper(PyUnicodeObject *self) |
| 5545 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5546 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5547 | Py_UNICODE *s = self->str; |
| 5548 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5549 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5550 | while (len-- > 0) { |
| 5551 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5552 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5553 | ch = Py_UNICODE_TOUPPER(*s); |
| 5554 | if (ch != *s) { |
| 5555 | status = 1; |
| 5556 | *s = ch; |
| 5557 | } |
| 5558 | s++; |
| 5559 | } |
| 5560 | |
| 5561 | return status; |
| 5562 | } |
| 5563 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5564 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5565 | int fixlower(PyUnicodeObject *self) |
| 5566 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5567 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5568 | Py_UNICODE *s = self->str; |
| 5569 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5570 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5571 | while (len-- > 0) { |
| 5572 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5573 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5574 | ch = Py_UNICODE_TOLOWER(*s); |
| 5575 | if (ch != *s) { |
| 5576 | status = 1; |
| 5577 | *s = ch; |
| 5578 | } |
| 5579 | s++; |
| 5580 | } |
| 5581 | |
| 5582 | return status; |
| 5583 | } |
| 5584 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5585 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5586 | int fixswapcase(PyUnicodeObject *self) |
| 5587 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5588 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5589 | Py_UNICODE *s = self->str; |
| 5590 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5591 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5592 | while (len-- > 0) { |
| 5593 | if (Py_UNICODE_ISUPPER(*s)) { |
| 5594 | *s = Py_UNICODE_TOLOWER(*s); |
| 5595 | status = 1; |
| 5596 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 5597 | *s = Py_UNICODE_TOUPPER(*s); |
| 5598 | status = 1; |
| 5599 | } |
| 5600 | s++; |
| 5601 | } |
| 5602 | |
| 5603 | return status; |
| 5604 | } |
| 5605 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5606 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5607 | int fixcapitalize(PyUnicodeObject *self) |
| 5608 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5609 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5610 | Py_UNICODE *s = self->str; |
| 5611 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5612 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5613 | if (len == 0) |
| 5614 | return 0; |
| 5615 | if (Py_UNICODE_ISLOWER(*s)) { |
| 5616 | *s = Py_UNICODE_TOUPPER(*s); |
| 5617 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5618 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5619 | s++; |
| 5620 | while (--len > 0) { |
| 5621 | if (Py_UNICODE_ISUPPER(*s)) { |
| 5622 | *s = Py_UNICODE_TOLOWER(*s); |
| 5623 | status = 1; |
| 5624 | } |
| 5625 | s++; |
| 5626 | } |
| 5627 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5628 | } |
| 5629 | |
| 5630 | static |
| 5631 | int fixtitle(PyUnicodeObject *self) |
| 5632 | { |
| 5633 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5634 | register Py_UNICODE *e; |
| 5635 | int previous_is_cased; |
| 5636 | |
| 5637 | /* Shortcut for single character strings */ |
| 5638 | if (PyUnicode_GET_SIZE(self) == 1) { |
| 5639 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 5640 | if (*p != ch) { |
| 5641 | *p = ch; |
| 5642 | return 1; |
| 5643 | } |
| 5644 | else |
| 5645 | return 0; |
| 5646 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5647 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5648 | e = p + PyUnicode_GET_SIZE(self); |
| 5649 | previous_is_cased = 0; |
| 5650 | for (; p < e; p++) { |
| 5651 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5652 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5653 | if (previous_is_cased) |
| 5654 | *p = Py_UNICODE_TOLOWER(ch); |
| 5655 | else |
| 5656 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5657 | |
| 5658 | if (Py_UNICODE_ISLOWER(ch) || |
| 5659 | Py_UNICODE_ISUPPER(ch) || |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5660 | Py_UNICODE_ISTITLE(ch)) |
| 5661 | previous_is_cased = 1; |
| 5662 | else |
| 5663 | previous_is_cased = 0; |
| 5664 | } |
| 5665 | return 1; |
| 5666 | } |
| 5667 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5668 | PyObject * |
| 5669 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5670 | { |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 5671 | const Py_UNICODE blank = ' '; |
| 5672 | const Py_UNICODE *sep = ␣ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5673 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5674 | PyUnicodeObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5675 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 5676 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5677 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 5678 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5679 | PyObject *item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5680 | Py_ssize_t sz, i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5681 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5682 | fseq = PySequence_Fast(seq, ""); |
| 5683 | if (fseq == NULL) { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5684 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5685 | } |
| 5686 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5687 | /* NOTE: the following code can't call back into Python code, |
| 5688 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5689 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5690 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5691 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 5692 | /* If empty sequence, return u"". */ |
| 5693 | if (seqlen == 0) { |
| 5694 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 5695 | goto Done; |
| 5696 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5697 | items = PySequence_Fast_ITEMS(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5698 | /* If singleton sequence with an exact Unicode, return that. */ |
| 5699 | if (seqlen == 1) { |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5700 | item = items[0]; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5701 | if (PyUnicode_CheckExact(item)) { |
| 5702 | Py_INCREF(item); |
| 5703 | res = (PyUnicodeObject *)item; |
| 5704 | goto Done; |
| 5705 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5706 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5707 | else { |
| 5708 | /* Set up sep and seplen */ |
| 5709 | if (separator == NULL) { |
| 5710 | sep = ␣ |
| 5711 | seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5712 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5713 | else { |
| 5714 | if (!PyUnicode_Check(separator)) { |
| 5715 | PyErr_Format(PyExc_TypeError, |
| 5716 | "separator: expected str instance," |
| 5717 | " %.80s found", |
| 5718 | Py_TYPE(separator)->tp_name); |
| 5719 | goto onError; |
| 5720 | } |
| 5721 | sep = PyUnicode_AS_UNICODE(separator); |
| 5722 | seplen = PyUnicode_GET_SIZE(separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5723 | } |
| 5724 | } |
| 5725 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5726 | /* There are at least two things to join, or else we have a subclass |
| 5727 | * of str in the sequence. |
| 5728 | * Do a pre-pass to figure out the total amount of space we'll |
| 5729 | * need (sz), and see whether all argument are strings. |
| 5730 | */ |
| 5731 | sz = 0; |
| 5732 | for (i = 0; i < seqlen; i++) { |
| 5733 | const Py_ssize_t old_sz = sz; |
| 5734 | item = items[i]; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5735 | if (!PyUnicode_Check(item)) { |
| 5736 | PyErr_Format(PyExc_TypeError, |
| 5737 | "sequence item %zd: expected str instance," |
| 5738 | " %.80s found", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5739 | i, Py_TYPE(item)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5740 | goto onError; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5741 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5742 | sz += PyUnicode_GET_SIZE(item); |
| 5743 | if (i != 0) |
| 5744 | sz += seplen; |
| 5745 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 5746 | PyErr_SetString(PyExc_OverflowError, |
| 5747 | "join() result is too long for a Python string"); |
| 5748 | goto onError; |
| 5749 | } |
| 5750 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5751 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5752 | res = _PyUnicode_New(sz); |
| 5753 | if (res == NULL) |
| 5754 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5755 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5756 | /* Catenate everything. */ |
| 5757 | res_p = PyUnicode_AS_UNICODE(res); |
| 5758 | for (i = 0; i < seqlen; ++i) { |
| 5759 | Py_ssize_t itemlen; |
| 5760 | item = items[i]; |
| 5761 | itemlen = PyUnicode_GET_SIZE(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5762 | /* Copy item, and maybe the separator. */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5763 | if (i) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5764 | Py_UNICODE_COPY(res_p, sep, seplen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5765 | res_p += seplen; |
| 5766 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5767 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
| 5768 | res_p += itemlen; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5769 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5770 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5771 | Done: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5772 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5773 | return (PyObject *)res; |
| 5774 | |
| 5775 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5776 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5777 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5778 | return NULL; |
| 5779 | } |
| 5780 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5781 | static |
| 5782 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5783 | Py_ssize_t left, |
| 5784 | Py_ssize_t right, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5785 | Py_UNICODE fill) |
| 5786 | { |
| 5787 | PyUnicodeObject *u; |
| 5788 | |
| 5789 | if (left < 0) |
| 5790 | left = 0; |
| 5791 | if (right < 0) |
| 5792 | right = 0; |
| 5793 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5794 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5795 | Py_INCREF(self); |
| 5796 | return self; |
| 5797 | } |
| 5798 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5799 | if (left > PY_SSIZE_T_MAX - self->length || |
| 5800 | right > PY_SSIZE_T_MAX - (left + self->length)) { |
| 5801 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 5802 | return NULL; |
| 5803 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5804 | u = _PyUnicode_New(left + self->length + right); |
| 5805 | if (u) { |
| 5806 | if (left) |
| 5807 | Py_UNICODE_FILL(u->str, fill, left); |
| 5808 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 5809 | if (right) |
| 5810 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 5811 | } |
| 5812 | |
| 5813 | return u; |
| 5814 | } |
| 5815 | |
| 5816 | #define SPLIT_APPEND(data, left, right) \ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5817 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5818 | if (!str) \ |
| 5819 | goto onError; \ |
| 5820 | if (PyList_Append(list, str)) { \ |
| 5821 | Py_DECREF(str); \ |
| 5822 | goto onError; \ |
| 5823 | } \ |
| 5824 | else \ |
| 5825 | Py_DECREF(str); |
| 5826 | |
| 5827 | static |
| 5828 | PyObject *split_whitespace(PyUnicodeObject *self, |
| 5829 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5830 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5831 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5832 | register Py_ssize_t i; |
| 5833 | register Py_ssize_t j; |
| 5834 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5835 | PyObject *str; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5836 | register const Py_UNICODE *buf = self->str; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5837 | |
| 5838 | for (i = j = 0; i < len; ) { |
| 5839 | /* find a token */ |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5840 | while (i < len && Py_UNICODE_ISSPACE(buf[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5841 | i++; |
| 5842 | j = i; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5843 | while (i < len && !Py_UNICODE_ISSPACE(buf[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5844 | i++; |
| 5845 | if (j < i) { |
| 5846 | if (maxcount-- <= 0) |
| 5847 | break; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5848 | SPLIT_APPEND(buf, j, i); |
| 5849 | while (i < len && Py_UNICODE_ISSPACE(buf[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5850 | i++; |
| 5851 | j = i; |
| 5852 | } |
| 5853 | } |
| 5854 | if (j < len) { |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5855 | SPLIT_APPEND(buf, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5856 | } |
| 5857 | return list; |
| 5858 | |
| 5859 | onError: |
| 5860 | Py_DECREF(list); |
| 5861 | return NULL; |
| 5862 | } |
| 5863 | |
| 5864 | PyObject *PyUnicode_Splitlines(PyObject *string, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5865 | int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5866 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5867 | register Py_ssize_t i; |
| 5868 | register Py_ssize_t j; |
| 5869 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5870 | PyObject *list; |
| 5871 | PyObject *str; |
| 5872 | Py_UNICODE *data; |
| 5873 | |
| 5874 | string = PyUnicode_FromObject(string); |
| 5875 | if (string == NULL) |
| 5876 | return NULL; |
| 5877 | data = PyUnicode_AS_UNICODE(string); |
| 5878 | len = PyUnicode_GET_SIZE(string); |
| 5879 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5880 | list = PyList_New(0); |
| 5881 | if (!list) |
| 5882 | goto onError; |
| 5883 | |
| 5884 | for (i = j = 0; i < len; ) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5885 | Py_ssize_t eol; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5886 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5887 | /* Find a line and append it */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5888 | while (i < len && !BLOOM_LINEBREAK(data[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5889 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5890 | |
| 5891 | /* Skip the line break reading CRLF as one line break */ |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5892 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5893 | if (i < len) { |
| 5894 | if (data[i] == '\r' && i + 1 < len && |
| 5895 | data[i+1] == '\n') |
| 5896 | i += 2; |
| 5897 | else |
| 5898 | i++; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5899 | if (keepends) |
| 5900 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5901 | } |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5902 | SPLIT_APPEND(data, j, eol); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5903 | j = i; |
| 5904 | } |
| 5905 | if (j < len) { |
| 5906 | SPLIT_APPEND(data, j, len); |
| 5907 | } |
| 5908 | |
| 5909 | Py_DECREF(string); |
| 5910 | return list; |
| 5911 | |
| 5912 | onError: |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5913 | Py_XDECREF(list); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5914 | Py_DECREF(string); |
| 5915 | return NULL; |
| 5916 | } |
| 5917 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5918 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5919 | PyObject *split_char(PyUnicodeObject *self, |
| 5920 | PyObject *list, |
| 5921 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5922 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5923 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5924 | register Py_ssize_t i; |
| 5925 | register Py_ssize_t j; |
| 5926 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5927 | PyObject *str; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5928 | register const Py_UNICODE *buf = self->str; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5929 | |
| 5930 | for (i = j = 0; i < len; ) { |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5931 | if (buf[i] == ch) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5932 | if (maxcount-- <= 0) |
| 5933 | break; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5934 | SPLIT_APPEND(buf, j, i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5935 | i = j = i + 1; |
| 5936 | } else |
| 5937 | i++; |
| 5938 | } |
| 5939 | if (j <= len) { |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5940 | SPLIT_APPEND(buf, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5941 | } |
| 5942 | return list; |
| 5943 | |
| 5944 | onError: |
| 5945 | Py_DECREF(list); |
| 5946 | return NULL; |
| 5947 | } |
| 5948 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5949 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5950 | PyObject *split_substring(PyUnicodeObject *self, |
| 5951 | PyObject *list, |
| 5952 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5953 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5954 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5955 | register Py_ssize_t i; |
| 5956 | register Py_ssize_t j; |
| 5957 | Py_ssize_t len = self->length; |
| 5958 | Py_ssize_t sublen = substring->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5959 | PyObject *str; |
| 5960 | |
Guido van Rossum | cda4f9a | 2000-12-19 02:23:19 +0000 | [diff] [blame] | 5961 | for (i = j = 0; i <= len - sublen; ) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5962 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 5963 | if (maxcount-- <= 0) |
| 5964 | break; |
| 5965 | SPLIT_APPEND(self->str, j, i); |
| 5966 | i = j = i + sublen; |
| 5967 | } else |
| 5968 | i++; |
| 5969 | } |
| 5970 | if (j <= len) { |
| 5971 | SPLIT_APPEND(self->str, j, len); |
| 5972 | } |
| 5973 | return list; |
| 5974 | |
| 5975 | onError: |
| 5976 | Py_DECREF(list); |
| 5977 | return NULL; |
| 5978 | } |
| 5979 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5980 | static |
| 5981 | PyObject *rsplit_whitespace(PyUnicodeObject *self, |
| 5982 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5983 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5984 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5985 | register Py_ssize_t i; |
| 5986 | register Py_ssize_t j; |
| 5987 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5988 | PyObject *str; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5989 | register const Py_UNICODE *buf = self->str; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5990 | |
| 5991 | for (i = j = len - 1; i >= 0; ) { |
| 5992 | /* find a token */ |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5993 | while (i >= 0 && Py_UNICODE_ISSPACE(buf[i])) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5994 | i--; |
| 5995 | j = i; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5996 | while (i >= 0 && !Py_UNICODE_ISSPACE(buf[i])) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5997 | i--; |
| 5998 | if (j > i) { |
| 5999 | if (maxcount-- <= 0) |
| 6000 | break; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6001 | SPLIT_APPEND(buf, i + 1, j + 1); |
| 6002 | while (i >= 0 && Py_UNICODE_ISSPACE(buf[i])) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6003 | i--; |
| 6004 | j = i; |
| 6005 | } |
| 6006 | } |
| 6007 | if (j >= 0) { |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6008 | SPLIT_APPEND(buf, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6009 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6010 | if (PyList_Reverse(list) < 0) |
| 6011 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6012 | return list; |
| 6013 | |
| 6014 | onError: |
| 6015 | Py_DECREF(list); |
| 6016 | return NULL; |
| 6017 | } |
| 6018 | |
| 6019 | static |
| 6020 | PyObject *rsplit_char(PyUnicodeObject *self, |
| 6021 | PyObject *list, |
| 6022 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6023 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6024 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6025 | register Py_ssize_t i; |
| 6026 | register Py_ssize_t j; |
| 6027 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6028 | PyObject *str; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6029 | register const Py_UNICODE *buf = self->str; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6030 | |
| 6031 | for (i = j = len - 1; i >= 0; ) { |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6032 | if (buf[i] == ch) { |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6033 | if (maxcount-- <= 0) |
| 6034 | break; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6035 | SPLIT_APPEND(buf, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6036 | j = i = i - 1; |
| 6037 | } else |
| 6038 | i--; |
| 6039 | } |
Hye-Shik Chang | 7fc4cf5 | 2003-12-23 09:10:16 +0000 | [diff] [blame] | 6040 | if (j >= -1) { |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6041 | SPLIT_APPEND(buf, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6042 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6043 | if (PyList_Reverse(list) < 0) |
| 6044 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6045 | return list; |
| 6046 | |
| 6047 | onError: |
| 6048 | Py_DECREF(list); |
| 6049 | return NULL; |
| 6050 | } |
| 6051 | |
| 6052 | static |
| 6053 | PyObject *rsplit_substring(PyUnicodeObject *self, |
| 6054 | PyObject *list, |
| 6055 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6056 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6057 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6058 | register Py_ssize_t i; |
| 6059 | register Py_ssize_t j; |
| 6060 | Py_ssize_t len = self->length; |
| 6061 | Py_ssize_t sublen = substring->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6062 | PyObject *str; |
| 6063 | |
| 6064 | for (i = len - sublen, j = len; i >= 0; ) { |
| 6065 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 6066 | if (maxcount-- <= 0) |
| 6067 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6068 | SPLIT_APPEND(self->str, i + sublen, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6069 | j = i; |
| 6070 | i -= sublen; |
| 6071 | } else |
| 6072 | i--; |
| 6073 | } |
| 6074 | if (j >= 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6075 | SPLIT_APPEND(self->str, 0, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6076 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6077 | if (PyList_Reverse(list) < 0) |
| 6078 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6079 | return list; |
| 6080 | |
| 6081 | onError: |
| 6082 | Py_DECREF(list); |
| 6083 | return NULL; |
| 6084 | } |
| 6085 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6086 | #undef SPLIT_APPEND |
| 6087 | |
| 6088 | static |
| 6089 | PyObject *split(PyUnicodeObject *self, |
| 6090 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6091 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6092 | { |
| 6093 | PyObject *list; |
| 6094 | |
| 6095 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6096 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6097 | |
| 6098 | list = PyList_New(0); |
| 6099 | if (!list) |
| 6100 | return NULL; |
| 6101 | |
| 6102 | if (substring == NULL) |
| 6103 | return split_whitespace(self,list,maxcount); |
| 6104 | |
| 6105 | else if (substring->length == 1) |
| 6106 | return split_char(self,list,substring->str[0],maxcount); |
| 6107 | |
| 6108 | else if (substring->length == 0) { |
| 6109 | Py_DECREF(list); |
| 6110 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 6111 | return NULL; |
| 6112 | } |
| 6113 | else |
| 6114 | return split_substring(self,list,substring,maxcount); |
| 6115 | } |
| 6116 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6117 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6118 | PyObject *rsplit(PyUnicodeObject *self, |
| 6119 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6120 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6121 | { |
| 6122 | PyObject *list; |
| 6123 | |
| 6124 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6125 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6126 | |
| 6127 | list = PyList_New(0); |
| 6128 | if (!list) |
| 6129 | return NULL; |
| 6130 | |
| 6131 | if (substring == NULL) |
| 6132 | return rsplit_whitespace(self,list,maxcount); |
| 6133 | |
| 6134 | else if (substring->length == 1) |
| 6135 | return rsplit_char(self,list,substring->str[0],maxcount); |
| 6136 | |
| 6137 | else if (substring->length == 0) { |
| 6138 | Py_DECREF(list); |
| 6139 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 6140 | return NULL; |
| 6141 | } |
| 6142 | else |
| 6143 | return rsplit_substring(self,list,substring,maxcount); |
| 6144 | } |
| 6145 | |
| 6146 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6147 | PyObject *replace(PyUnicodeObject *self, |
| 6148 | PyUnicodeObject *str1, |
| 6149 | PyUnicodeObject *str2, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6150 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6151 | { |
| 6152 | PyUnicodeObject *u; |
| 6153 | |
| 6154 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6155 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6156 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6157 | if (str1->length == str2->length) { |
| 6158 | /* same length */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6159 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6160 | if (str1->length == 1) { |
| 6161 | /* replace characters */ |
| 6162 | Py_UNICODE u1, u2; |
| 6163 | if (!findchar(self->str, self->length, str1->str[0])) |
| 6164 | goto nothing; |
| 6165 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6166 | if (!u) |
| 6167 | return NULL; |
| 6168 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6169 | u1 = str1->str[0]; |
| 6170 | u2 = str2->str[0]; |
| 6171 | for (i = 0; i < u->length; i++) |
| 6172 | if (u->str[i] == u1) { |
| 6173 | if (--maxcount < 0) |
| 6174 | break; |
| 6175 | u->str[i] = u2; |
| 6176 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6177 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6178 | i = fastsearch( |
| 6179 | self->str, self->length, str1->str, str1->length, FAST_SEARCH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6180 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6181 | if (i < 0) |
| 6182 | goto nothing; |
| 6183 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6184 | if (!u) |
| 6185 | return NULL; |
| 6186 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6187 | while (i <= self->length - str1->length) |
| 6188 | if (Py_UNICODE_MATCH(self, i, str1)) { |
| 6189 | if (--maxcount < 0) |
| 6190 | break; |
| 6191 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 6192 | i += str1->length; |
| 6193 | } else |
| 6194 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6195 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6196 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6197 | |
| 6198 | Py_ssize_t n, i, j, e; |
| 6199 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6200 | Py_UNICODE *p; |
| 6201 | |
| 6202 | /* replace strings */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6203 | n = stringlib_count(self->str, self->length, str1->str, str1->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6204 | if (n > maxcount) |
| 6205 | n = maxcount; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6206 | if (n == 0) |
| 6207 | goto nothing; |
| 6208 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 6209 | delta = (str2->length - str1->length); |
| 6210 | if (delta == 0) { |
| 6211 | new_size = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6212 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6213 | product = n * (str2->length - str1->length); |
| 6214 | if ((product / (str2->length - str1->length)) != n) { |
| 6215 | PyErr_SetString(PyExc_OverflowError, |
| 6216 | "replace string is too long"); |
| 6217 | return NULL; |
| 6218 | } |
| 6219 | new_size = self->length + product; |
| 6220 | if (new_size < 0) { |
| 6221 | PyErr_SetString(PyExc_OverflowError, |
| 6222 | "replace string is too long"); |
| 6223 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6224 | } |
| 6225 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6226 | u = _PyUnicode_New(new_size); |
| 6227 | if (!u) |
| 6228 | return NULL; |
| 6229 | i = 0; |
| 6230 | p = u->str; |
| 6231 | e = self->length - str1->length; |
| 6232 | if (str1->length > 0) { |
| 6233 | while (n-- > 0) { |
| 6234 | /* look for next match */ |
| 6235 | j = i; |
| 6236 | while (j <= e) { |
| 6237 | if (Py_UNICODE_MATCH(self, j, str1)) |
| 6238 | break; |
| 6239 | j++; |
| 6240 | } |
| 6241 | if (j > i) { |
| 6242 | if (j > e) |
| 6243 | break; |
| 6244 | /* copy unchanged part [i:j] */ |
| 6245 | Py_UNICODE_COPY(p, self->str+i, j-i); |
| 6246 | p += j - i; |
| 6247 | } |
| 6248 | /* copy substitution string */ |
| 6249 | if (str2->length > 0) { |
| 6250 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6251 | p += str2->length; |
| 6252 | } |
| 6253 | i = j + str1->length; |
| 6254 | } |
| 6255 | if (i < self->length) |
| 6256 | /* copy tail [i:] */ |
| 6257 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6258 | } else { |
| 6259 | /* interleave */ |
| 6260 | while (n > 0) { |
| 6261 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6262 | p += str2->length; |
| 6263 | if (--n <= 0) |
| 6264 | break; |
| 6265 | *p++ = self->str[i++]; |
| 6266 | } |
| 6267 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6268 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6269 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6270 | return (PyObject *) u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6271 | |
| 6272 | nothing: |
| 6273 | /* nothing to replace; return original string (when possible) */ |
| 6274 | if (PyUnicode_CheckExact(self)) { |
| 6275 | Py_INCREF(self); |
| 6276 | return (PyObject *) self; |
| 6277 | } |
| 6278 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6279 | } |
| 6280 | |
| 6281 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 6282 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6283 | PyDoc_STRVAR(title__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 6284 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6285 | \n\ |
| 6286 | 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] | 6287 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6288 | |
| 6289 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6290 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6291 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6292 | return fixup(self, fixtitle); |
| 6293 | } |
| 6294 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6295 | PyDoc_STRVAR(capitalize__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 6296 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6297 | \n\ |
| 6298 | 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] | 6299 | have upper case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6300 | |
| 6301 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6302 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6303 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6304 | return fixup(self, fixcapitalize); |
| 6305 | } |
| 6306 | |
| 6307 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6308 | PyDoc_STRVAR(capwords__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 6309 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6310 | \n\ |
| 6311 | 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] | 6312 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6313 | |
| 6314 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6315 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6316 | { |
| 6317 | PyObject *list; |
| 6318 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6319 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6320 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6321 | /* Split into words */ |
| 6322 | list = split(self, NULL, -1); |
| 6323 | if (!list) |
| 6324 | return NULL; |
| 6325 | |
| 6326 | /* Capitalize each word */ |
| 6327 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 6328 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
| 6329 | fixcapitalize); |
| 6330 | if (item == NULL) |
| 6331 | goto onError; |
| 6332 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 6333 | PyList_SET_ITEM(list, i, item); |
| 6334 | } |
| 6335 | |
| 6336 | /* Join the words to form a new string */ |
| 6337 | item = PyUnicode_Join(NULL, list); |
| 6338 | |
| 6339 | onError: |
| 6340 | Py_DECREF(list); |
| 6341 | return (PyObject *)item; |
| 6342 | } |
| 6343 | #endif |
| 6344 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6345 | /* Argument converter. Coerces to a single unicode character */ |
| 6346 | |
| 6347 | static int |
| 6348 | convert_uc(PyObject *obj, void *addr) |
| 6349 | { |
| 6350 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 6351 | PyObject *uniobj; |
| 6352 | Py_UNICODE *unistr; |
| 6353 | |
| 6354 | uniobj = PyUnicode_FromObject(obj); |
| 6355 | if (uniobj == NULL) { |
| 6356 | PyErr_SetString(PyExc_TypeError, |
| 6357 | "The fill character cannot be converted to Unicode"); |
| 6358 | return 0; |
| 6359 | } |
| 6360 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 6361 | PyErr_SetString(PyExc_TypeError, |
| 6362 | "The fill character must be exactly one character long"); |
| 6363 | Py_DECREF(uniobj); |
| 6364 | return 0; |
| 6365 | } |
| 6366 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 6367 | *fillcharloc = unistr[0]; |
| 6368 | Py_DECREF(uniobj); |
| 6369 | return 1; |
| 6370 | } |
| 6371 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6372 | PyDoc_STRVAR(center__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 6373 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6374 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 6375 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6376 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6377 | |
| 6378 | static PyObject * |
| 6379 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 6380 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6381 | Py_ssize_t marg, left; |
| 6382 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6383 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6384 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 6385 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6386 | return NULL; |
| 6387 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6388 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6389 | Py_INCREF(self); |
| 6390 | return (PyObject*) self; |
| 6391 | } |
| 6392 | |
| 6393 | marg = width - self->length; |
| 6394 | left = marg / 2 + (marg & width & 1); |
| 6395 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6396 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6397 | } |
| 6398 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6399 | #if 0 |
| 6400 | |
| 6401 | /* This code should go into some future Unicode collation support |
| 6402 | module. The basic comparison should compare ordinals on a naive |
Trent Mick | 20abf57 | 2000-08-12 22:14:34 +0000 | [diff] [blame] | 6403 | basis (this is what Java does and thus JPython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6404 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6405 | /* speedy UTF-16 code point order comparison */ |
| 6406 | /* gleaned from: */ |
| 6407 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 6408 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6409 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6410 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6411 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6412 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 6413 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6414 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6415 | }; |
| 6416 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6417 | static int |
| 6418 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6419 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6420 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6421 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6422 | Py_UNICODE *s1 = str1->str; |
| 6423 | Py_UNICODE *s2 = str2->str; |
| 6424 | |
| 6425 | len1 = str1->length; |
| 6426 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6427 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6428 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6429 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6430 | |
| 6431 | c1 = *s1++; |
| 6432 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6433 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6434 | if (c1 > (1<<11) * 26) |
| 6435 | c1 += utf16Fixup[c1>>11]; |
| 6436 | if (c2 > (1<<11) * 26) |
| 6437 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6438 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6439 | |
| 6440 | if (c1 != c2) |
| 6441 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6442 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6443 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6444 | } |
| 6445 | |
| 6446 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6447 | } |
| 6448 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6449 | #else |
| 6450 | |
| 6451 | static int |
| 6452 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6453 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6454 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6455 | |
| 6456 | Py_UNICODE *s1 = str1->str; |
| 6457 | Py_UNICODE *s2 = str2->str; |
| 6458 | |
| 6459 | len1 = str1->length; |
| 6460 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6461 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6462 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6463 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6464 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6465 | c1 = *s1++; |
| 6466 | c2 = *s2++; |
| 6467 | |
| 6468 | if (c1 != c2) |
| 6469 | return (c1 < c2) ? -1 : 1; |
| 6470 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6471 | len1--; len2--; |
| 6472 | } |
| 6473 | |
| 6474 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6475 | } |
| 6476 | |
| 6477 | #endif |
| 6478 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6479 | int PyUnicode_Compare(PyObject *left, |
| 6480 | PyObject *right) |
| 6481 | { |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6482 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) |
| 6483 | return unicode_compare((PyUnicodeObject *)left, |
| 6484 | (PyUnicodeObject *)right); |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6485 | PyErr_Format(PyExc_TypeError, |
| 6486 | "Can't compare %.100s and %.100s", |
| 6487 | left->ob_type->tp_name, |
| 6488 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6489 | return -1; |
| 6490 | } |
| 6491 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6492 | int |
| 6493 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 6494 | { |
| 6495 | int i; |
| 6496 | Py_UNICODE *id; |
| 6497 | assert(PyUnicode_Check(uni)); |
| 6498 | id = PyUnicode_AS_UNICODE(uni); |
| 6499 | /* Compare Unicode string and source character set string */ |
| 6500 | for (i = 0; id[i] && str[i]; i++) |
| 6501 | if (id[i] != str[i]) |
| 6502 | return ((int)id[i] < (int)str[i]) ? -1 : 1; |
| 6503 | if (id[i]) |
| 6504 | return 1; /* uni is longer */ |
| 6505 | if (str[i]) |
| 6506 | return -1; /* str is longer */ |
| 6507 | return 0; |
| 6508 | } |
| 6509 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6510 | |
| 6511 | #define TEST_COND(cond) \ |
| 6512 | ((cond) ? Py_True : Py_False) |
| 6513 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6514 | PyObject *PyUnicode_RichCompare(PyObject *left, |
| 6515 | PyObject *right, |
| 6516 | int op) |
| 6517 | { |
| 6518 | int result; |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6519 | |
| 6520 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 6521 | PyObject *v; |
| 6522 | if (((PyUnicodeObject *) left)->length != |
| 6523 | ((PyUnicodeObject *) right)->length) { |
| 6524 | if (op == Py_EQ) { |
| 6525 | Py_INCREF(Py_False); |
| 6526 | return Py_False; |
| 6527 | } |
| 6528 | if (op == Py_NE) { |
| 6529 | Py_INCREF(Py_True); |
| 6530 | return Py_True; |
| 6531 | } |
| 6532 | } |
| 6533 | if (left == right) |
| 6534 | result = 0; |
| 6535 | else |
| 6536 | result = unicode_compare((PyUnicodeObject *)left, |
| 6537 | (PyUnicodeObject *)right); |
| 6538 | |
| 6539 | /* Convert the return value to a Boolean */ |
| 6540 | switch (op) { |
| 6541 | case Py_EQ: |
| 6542 | v = TEST_COND(result == 0); |
| 6543 | break; |
| 6544 | case Py_NE: |
| 6545 | v = TEST_COND(result != 0); |
| 6546 | break; |
| 6547 | case Py_LE: |
| 6548 | v = TEST_COND(result <= 0); |
| 6549 | break; |
| 6550 | case Py_GE: |
| 6551 | v = TEST_COND(result >= 0); |
| 6552 | break; |
| 6553 | case Py_LT: |
| 6554 | v = TEST_COND(result == -1); |
| 6555 | break; |
| 6556 | case Py_GT: |
| 6557 | v = TEST_COND(result == 1); |
| 6558 | break; |
| 6559 | default: |
| 6560 | PyErr_BadArgument(); |
| 6561 | return NULL; |
| 6562 | } |
| 6563 | Py_INCREF(v); |
| 6564 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6565 | } |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6566 | |
| 6567 | Py_INCREF(Py_NotImplemented); |
| 6568 | return Py_NotImplemented; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6569 | } |
| 6570 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6571 | int PyUnicode_Contains(PyObject *container, |
| 6572 | PyObject *element) |
| 6573 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6574 | PyObject *str, *sub; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6575 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6576 | |
| 6577 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6578 | sub = PyUnicode_FromObject(element); |
| 6579 | if (!sub) { |
Walter Dörwald | 26e0f51 | 2007-06-12 16:51:31 +0000 | [diff] [blame] | 6580 | PyErr_Format(PyExc_TypeError, |
| 6581 | "'in <string>' requires string as left operand, not %s", |
| 6582 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6583 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6584 | } |
| 6585 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6586 | str = PyUnicode_FromObject(container); |
| 6587 | if (!str) { |
| 6588 | Py_DECREF(sub); |
| 6589 | return -1; |
| 6590 | } |
| 6591 | |
| 6592 | result = stringlib_contains_obj(str, sub); |
| 6593 | |
| 6594 | Py_DECREF(str); |
| 6595 | Py_DECREF(sub); |
| 6596 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6597 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6598 | } |
| 6599 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6600 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 6601 | |
| 6602 | PyObject *PyUnicode_Concat(PyObject *left, |
| 6603 | PyObject *right) |
| 6604 | { |
| 6605 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 6606 | |
| 6607 | /* Coerce the two arguments */ |
| 6608 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 6609 | if (u == NULL) |
| 6610 | goto onError; |
| 6611 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 6612 | if (v == NULL) |
| 6613 | goto onError; |
| 6614 | |
| 6615 | /* Shortcuts */ |
| 6616 | if (v == unicode_empty) { |
| 6617 | Py_DECREF(v); |
| 6618 | return (PyObject *)u; |
| 6619 | } |
| 6620 | if (u == unicode_empty) { |
| 6621 | Py_DECREF(u); |
| 6622 | return (PyObject *)v; |
| 6623 | } |
| 6624 | |
| 6625 | /* Concat the two Unicode strings */ |
| 6626 | w = _PyUnicode_New(u->length + v->length); |
| 6627 | if (w == NULL) |
| 6628 | goto onError; |
| 6629 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 6630 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 6631 | |
| 6632 | Py_DECREF(u); |
| 6633 | Py_DECREF(v); |
| 6634 | return (PyObject *)w; |
| 6635 | |
| 6636 | onError: |
| 6637 | Py_XDECREF(u); |
| 6638 | Py_XDECREF(v); |
| 6639 | return NULL; |
| 6640 | } |
| 6641 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 6642 | void |
| 6643 | PyUnicode_Append(PyObject **pleft, PyObject *right) |
| 6644 | { |
| 6645 | PyObject *new; |
| 6646 | if (*pleft == NULL) |
| 6647 | return; |
| 6648 | if (right == NULL || !PyUnicode_Check(*pleft)) { |
| 6649 | Py_DECREF(*pleft); |
| 6650 | *pleft = NULL; |
| 6651 | return; |
| 6652 | } |
| 6653 | new = PyUnicode_Concat(*pleft, right); |
| 6654 | Py_DECREF(*pleft); |
| 6655 | *pleft = new; |
| 6656 | } |
| 6657 | |
| 6658 | void |
| 6659 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 6660 | { |
| 6661 | PyUnicode_Append(pleft, right); |
| 6662 | Py_XDECREF(right); |
| 6663 | } |
| 6664 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6665 | PyDoc_STRVAR(count__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6666 | "S.count(sub[, start[, end]]) -> int\n\ |
| 6667 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6668 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 6669 | 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] | 6670 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6671 | |
| 6672 | static PyObject * |
| 6673 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 6674 | { |
| 6675 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6676 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6677 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6678 | PyObject *result; |
| 6679 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6680 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
| 6681 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6682 | return NULL; |
| 6683 | |
| 6684 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6685 | (PyObject *)substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6686 | if (substring == NULL) |
| 6687 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6688 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6689 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6690 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6691 | result = PyLong_FromSsize_t( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6692 | stringlib_count(self->str + start, end - start, |
| 6693 | substring->str, substring->length) |
| 6694 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6695 | |
| 6696 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6697 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6698 | return result; |
| 6699 | } |
| 6700 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6701 | PyDoc_STRVAR(encode__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 6702 | "S.encode([encoding[, errors]]) -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6703 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 6704 | Encode S using the codec registered for encoding. encoding defaults\n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6705 | 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] | 6706 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6707 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 6708 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 6709 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6710 | |
| 6711 | static PyObject * |
| 6712 | unicode_encode(PyUnicodeObject *self, PyObject *args) |
| 6713 | { |
| 6714 | char *encoding = NULL; |
| 6715 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6716 | PyObject *v; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 6717 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6718 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 6719 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 6720 | v = PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6721 | if (v == NULL) |
| 6722 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6723 | if (!PyBytes_Check(v)) { |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6724 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 6725 | "encoder did not return a bytes object " |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6726 | "(type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 6727 | Py_TYPE(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6728 | Py_DECREF(v); |
| 6729 | return NULL; |
| 6730 | } |
| 6731 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6732 | |
| 6733 | onError: |
| 6734 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6735 | } |
| 6736 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6737 | PyDoc_STRVAR(expandtabs__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 6738 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6739 | \n\ |
| 6740 | 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] | 6741 | 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] | 6742 | |
| 6743 | static PyObject* |
| 6744 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 6745 | { |
| 6746 | Py_UNICODE *e; |
| 6747 | Py_UNICODE *p; |
| 6748 | Py_UNICODE *q; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6749 | Py_UNICODE *qe; |
| 6750 | Py_ssize_t i, j, incr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6751 | PyUnicodeObject *u; |
| 6752 | int tabsize = 8; |
| 6753 | |
| 6754 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
| 6755 | return NULL; |
| 6756 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6757 | /* First pass: determine size of output string */ |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6758 | i = 0; /* chars up to and including most recent \n or \r */ |
| 6759 | j = 0; /* chars since most recent \n or \r (use in tab calculations) */ |
| 6760 | e = self->str + self->length; /* end of input */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6761 | for (p = self->str; p < e; p++) |
| 6762 | if (*p == '\t') { |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6763 | if (tabsize > 0) { |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6764 | incr = tabsize - (j % tabsize); /* cannot overflow */ |
| 6765 | if (j > PY_SSIZE_T_MAX - incr) |
| 6766 | goto overflow1; |
| 6767 | j += incr; |
| 6768 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6769 | } |
| 6770 | else { |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6771 | if (j > PY_SSIZE_T_MAX - 1) |
| 6772 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6773 | j++; |
| 6774 | if (*p == '\n' || *p == '\r') { |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6775 | if (i > PY_SSIZE_T_MAX - j) |
| 6776 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6777 | i += j; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6778 | j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6779 | } |
| 6780 | } |
| 6781 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6782 | if (i > PY_SSIZE_T_MAX - j) |
| 6783 | goto overflow1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6784 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6785 | /* Second pass: create output string and fill it */ |
| 6786 | u = _PyUnicode_New(i + j); |
| 6787 | if (!u) |
| 6788 | return NULL; |
| 6789 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6790 | j = 0; /* same as in first pass */ |
| 6791 | q = u->str; /* next output char */ |
| 6792 | qe = u->str + u->length; /* end of output */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6793 | |
| 6794 | for (p = self->str; p < e; p++) |
| 6795 | if (*p == '\t') { |
| 6796 | if (tabsize > 0) { |
| 6797 | i = tabsize - (j % tabsize); |
| 6798 | j += i; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6799 | while (i--) { |
| 6800 | if (q >= qe) |
| 6801 | goto overflow2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6802 | *q++ = ' '; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6803 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6804 | } |
| 6805 | } |
| 6806 | else { |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6807 | if (q >= qe) |
| 6808 | goto overflow2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6809 | *q++ = *p; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6810 | j++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6811 | if (*p == '\n' || *p == '\r') |
| 6812 | j = 0; |
| 6813 | } |
| 6814 | |
| 6815 | return (PyObject*) u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6816 | |
| 6817 | overflow2: |
| 6818 | Py_DECREF(u); |
| 6819 | overflow1: |
| 6820 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 6821 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6822 | } |
| 6823 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6824 | PyDoc_STRVAR(find__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 6825 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6826 | \n\ |
| 6827 | Return the lowest index in S where substring sub is found,\n\ |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 6828 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6829 | arguments start and end are interpreted as in slice notation.\n\ |
| 6830 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6831 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6832 | |
| 6833 | static PyObject * |
| 6834 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 6835 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6836 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 6837 | Py_ssize_t start; |
| 6838 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6839 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6840 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 6841 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6842 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6843 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6844 | result = stringlib_find_slice( |
| 6845 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6846 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6847 | start, end |
| 6848 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6849 | |
| 6850 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6851 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6852 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6853 | } |
| 6854 | |
| 6855 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6856 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6857 | { |
| 6858 | if (index < 0 || index >= self->length) { |
| 6859 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 6860 | return NULL; |
| 6861 | } |
| 6862 | |
| 6863 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 6864 | } |
| 6865 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 6866 | /* Believe it or not, this produces the same value for ASCII strings |
| 6867 | as string_hash(). */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6868 | static long |
Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 6869 | unicode_hash(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6870 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 6871 | Py_ssize_t len; |
| 6872 | Py_UNICODE *p; |
| 6873 | long x; |
| 6874 | |
| 6875 | if (self->hash != -1) |
| 6876 | return self->hash; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 6877 | len = Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 6878 | p = self->str; |
| 6879 | x = *p << 7; |
| 6880 | while (--len >= 0) |
| 6881 | x = (1000003*x) ^ *p++; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 6882 | x ^= Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 6883 | if (x == -1) |
| 6884 | x = -2; |
| 6885 | self->hash = x; |
| 6886 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6887 | } |
| 6888 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6889 | PyDoc_STRVAR(index__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 6890 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6891 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6892 | 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] | 6893 | |
| 6894 | static PyObject * |
| 6895 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 6896 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6897 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6898 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 6899 | Py_ssize_t start; |
| 6900 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6901 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 6902 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6903 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6904 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6905 | result = stringlib_find_slice( |
| 6906 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6907 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6908 | start, end |
| 6909 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6910 | |
| 6911 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6912 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6913 | if (result < 0) { |
| 6914 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 6915 | return NULL; |
| 6916 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6917 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6918 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6919 | } |
| 6920 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6921 | PyDoc_STRVAR(islower__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6922 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6923 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6924 | 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] | 6925 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6926 | |
| 6927 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6928 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6929 | { |
| 6930 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6931 | register const Py_UNICODE *e; |
| 6932 | int cased; |
| 6933 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6934 | /* Shortcut for single character strings */ |
| 6935 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6936 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6937 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6938 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6939 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6940 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6941 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6942 | e = p + PyUnicode_GET_SIZE(self); |
| 6943 | cased = 0; |
| 6944 | for (; p < e; p++) { |
| 6945 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6946 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6947 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6948 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6949 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 6950 | cased = 1; |
| 6951 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6952 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6953 | } |
| 6954 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6955 | PyDoc_STRVAR(isupper__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6956 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6957 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6958 | 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] | 6959 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6960 | |
| 6961 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6962 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6963 | { |
| 6964 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6965 | register const Py_UNICODE *e; |
| 6966 | int cased; |
| 6967 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6968 | /* Shortcut for single character strings */ |
| 6969 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6970 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6971 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6972 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6973 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6974 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6975 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6976 | e = p + PyUnicode_GET_SIZE(self); |
| 6977 | cased = 0; |
| 6978 | for (; p < e; p++) { |
| 6979 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6980 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6981 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6982 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6983 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 6984 | cased = 1; |
| 6985 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6986 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6987 | } |
| 6988 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6989 | PyDoc_STRVAR(istitle__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6990 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6991 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6992 | Return True if S is a titlecased string and there is at least one\n\ |
| 6993 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 6994 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 6995 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6996 | |
| 6997 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6998 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6999 | { |
| 7000 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7001 | register const Py_UNICODE *e; |
| 7002 | int cased, previous_is_cased; |
| 7003 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7004 | /* Shortcut for single character strings */ |
| 7005 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7006 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 7007 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7008 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7009 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7010 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7011 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7012 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7013 | e = p + PyUnicode_GET_SIZE(self); |
| 7014 | cased = 0; |
| 7015 | previous_is_cased = 0; |
| 7016 | for (; p < e; p++) { |
| 7017 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7018 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7019 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 7020 | if (previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7021 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7022 | previous_is_cased = 1; |
| 7023 | cased = 1; |
| 7024 | } |
| 7025 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 7026 | if (!previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7027 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7028 | previous_is_cased = 1; |
| 7029 | cased = 1; |
| 7030 | } |
| 7031 | else |
| 7032 | previous_is_cased = 0; |
| 7033 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7034 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7035 | } |
| 7036 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7037 | PyDoc_STRVAR(isspace__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7038 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7039 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7040 | Return True if all characters in S are whitespace\n\ |
| 7041 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7042 | |
| 7043 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7044 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7045 | { |
| 7046 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7047 | register const Py_UNICODE *e; |
| 7048 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7049 | /* Shortcut for single character strings */ |
| 7050 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 7051 | Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7052 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7053 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7054 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7055 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7056 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7057 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7058 | e = p + PyUnicode_GET_SIZE(self); |
| 7059 | for (; p < e; p++) { |
| 7060 | if (!Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7061 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7062 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7063 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7064 | } |
| 7065 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7066 | PyDoc_STRVAR(isalpha__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7067 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7068 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7069 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7070 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7071 | |
| 7072 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7073 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7074 | { |
| 7075 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7076 | register const Py_UNICODE *e; |
| 7077 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7078 | /* Shortcut for single character strings */ |
| 7079 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 7080 | Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7081 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7082 | |
| 7083 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7084 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7085 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7086 | |
| 7087 | e = p + PyUnicode_GET_SIZE(self); |
| 7088 | for (; p < e; p++) { |
| 7089 | if (!Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7090 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7091 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7092 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7093 | } |
| 7094 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7095 | PyDoc_STRVAR(isalnum__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7096 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7097 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7098 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7099 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7100 | |
| 7101 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7102 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7103 | { |
| 7104 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7105 | register const Py_UNICODE *e; |
| 7106 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7107 | /* Shortcut for single character strings */ |
| 7108 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 7109 | Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7110 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7111 | |
| 7112 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7113 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7114 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7115 | |
| 7116 | e = p + PyUnicode_GET_SIZE(self); |
| 7117 | for (; p < e; p++) { |
| 7118 | if (!Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7119 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7120 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7121 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7122 | } |
| 7123 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7124 | PyDoc_STRVAR(isdecimal__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7125 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7126 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7127 | 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] | 7128 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7129 | |
| 7130 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7131 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7132 | { |
| 7133 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7134 | register const Py_UNICODE *e; |
| 7135 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7136 | /* Shortcut for single character strings */ |
| 7137 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 7138 | Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7139 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7140 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7141 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7142 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7143 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7144 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7145 | e = p + PyUnicode_GET_SIZE(self); |
| 7146 | for (; p < e; p++) { |
| 7147 | if (!Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7148 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7149 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7150 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7151 | } |
| 7152 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7153 | PyDoc_STRVAR(isdigit__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7154 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7155 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7156 | Return True if all characters in S are digits\n\ |
| 7157 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7158 | |
| 7159 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7160 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7161 | { |
| 7162 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7163 | register const Py_UNICODE *e; |
| 7164 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7165 | /* Shortcut for single character strings */ |
| 7166 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 7167 | Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7168 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7169 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7170 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7171 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7172 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7173 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7174 | e = p + PyUnicode_GET_SIZE(self); |
| 7175 | for (; p < e; p++) { |
| 7176 | if (!Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7177 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7178 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7179 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7180 | } |
| 7181 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7182 | PyDoc_STRVAR(isnumeric__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7183 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7184 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7185 | 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] | 7186 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7187 | |
| 7188 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7189 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7190 | { |
| 7191 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7192 | register const Py_UNICODE *e; |
| 7193 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7194 | /* Shortcut for single character strings */ |
| 7195 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 7196 | Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7197 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7198 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7199 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7200 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7201 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7202 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7203 | e = p + PyUnicode_GET_SIZE(self); |
| 7204 | for (; p < e; p++) { |
| 7205 | if (!Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7206 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7207 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7208 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7209 | } |
| 7210 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7211 | int |
| 7212 | PyUnicode_IsIdentifier(PyObject *self) |
| 7213 | { |
| 7214 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE((PyUnicodeObject*)self); |
| 7215 | register const Py_UNICODE *e; |
| 7216 | |
| 7217 | /* Special case for empty strings */ |
| 7218 | if (PyUnicode_GET_SIZE(self) == 0) |
| 7219 | return 0; |
| 7220 | |
| 7221 | /* PEP 3131 says that the first character must be in |
| 7222 | XID_Start and subsequent characters in XID_Continue, |
| 7223 | and for the ASCII range, the 2.x rules apply (i.e |
| 7224 | start with letters and underscore, continue with |
| 7225 | letters, digits, underscore). However, given the current |
| 7226 | definition of XID_Start and XID_Continue, it is sufficient |
| 7227 | to check just for these, except that _ must be allowed |
| 7228 | as starting an identifier. */ |
| 7229 | if (!_PyUnicode_IsXidStart(*p) && *p != 0x5F /* LOW LINE */) |
| 7230 | return 0; |
| 7231 | |
| 7232 | e = p + PyUnicode_GET_SIZE(self); |
| 7233 | for (p++; p < e; p++) { |
| 7234 | if (!_PyUnicode_IsXidContinue(*p)) |
| 7235 | return 0; |
| 7236 | } |
| 7237 | return 1; |
| 7238 | } |
| 7239 | |
| 7240 | PyDoc_STRVAR(isidentifier__doc__, |
| 7241 | "S.isidentifier() -> bool\n\ |
| 7242 | \n\ |
| 7243 | Return True if S is a valid identifier according\n\ |
| 7244 | to the language definition."); |
| 7245 | |
| 7246 | static PyObject* |
| 7247 | unicode_isidentifier(PyObject *self) |
| 7248 | { |
| 7249 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 7250 | } |
| 7251 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7252 | PyDoc_STRVAR(isprintable__doc__, |
| 7253 | "S.isprintable() -> bool\n\ |
| 7254 | \n\ |
| 7255 | Return True if all characters in S are considered\n\ |
| 7256 | printable in repr() or S is empty, False otherwise."); |
| 7257 | |
| 7258 | static PyObject* |
| 7259 | unicode_isprintable(PyObject *self) |
| 7260 | { |
| 7261 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7262 | register const Py_UNICODE *e; |
| 7263 | |
| 7264 | /* Shortcut for single character strings */ |
| 7265 | if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISPRINTABLE(*p)) { |
| 7266 | Py_RETURN_TRUE; |
| 7267 | } |
| 7268 | |
| 7269 | e = p + PyUnicode_GET_SIZE(self); |
| 7270 | for (; p < e; p++) { |
| 7271 | if (!Py_UNICODE_ISPRINTABLE(*p)) { |
| 7272 | Py_RETURN_FALSE; |
| 7273 | } |
| 7274 | } |
| 7275 | Py_RETURN_TRUE; |
| 7276 | } |
| 7277 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7278 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7279 | "S.join(sequence) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7280 | \n\ |
| 7281 | 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] | 7282 | sequence. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7283 | |
| 7284 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7285 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7286 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7287 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7288 | } |
| 7289 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7290 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7291 | unicode_length(PyUnicodeObject *self) |
| 7292 | { |
| 7293 | return self->length; |
| 7294 | } |
| 7295 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7296 | PyDoc_STRVAR(ljust__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7297 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7298 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 7299 | 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] | 7300 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7301 | |
| 7302 | static PyObject * |
| 7303 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 7304 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7305 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7306 | Py_UNICODE fillchar = ' '; |
| 7307 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7308 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7309 | return NULL; |
| 7310 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7311 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7312 | Py_INCREF(self); |
| 7313 | return (PyObject*) self; |
| 7314 | } |
| 7315 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7316 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7317 | } |
| 7318 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7319 | PyDoc_STRVAR(lower__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7320 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7321 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7322 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7323 | |
| 7324 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7325 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7326 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7327 | return fixup(self, fixlower); |
| 7328 | } |
| 7329 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7330 | #define LEFTSTRIP 0 |
| 7331 | #define RIGHTSTRIP 1 |
| 7332 | #define BOTHSTRIP 2 |
| 7333 | |
| 7334 | /* Arrays indexed by above */ |
| 7335 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 7336 | |
| 7337 | #define STRIPNAME(i) (stripformat[i]+3) |
| 7338 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7339 | /* externally visible for str.strip(unicode) */ |
| 7340 | PyObject * |
| 7341 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 7342 | { |
| 7343 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7344 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7345 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7346 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 7347 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7348 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7349 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
| 7350 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7351 | i = 0; |
| 7352 | if (striptype != RIGHTSTRIP) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7353 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 7354 | i++; |
| 7355 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7356 | } |
| 7357 | |
| 7358 | j = len; |
| 7359 | if (striptype != LEFTSTRIP) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7360 | do { |
| 7361 | j--; |
| 7362 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 7363 | j++; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7364 | } |
| 7365 | |
| 7366 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7367 | Py_INCREF(self); |
| 7368 | return (PyObject*)self; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7369 | } |
| 7370 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7371 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7372 | } |
| 7373 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7374 | |
| 7375 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7376 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7377 | { |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7378 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7379 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7380 | |
| 7381 | i = 0; |
| 7382 | if (striptype != RIGHTSTRIP) { |
| 7383 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 7384 | i++; |
| 7385 | } |
| 7386 | } |
| 7387 | |
| 7388 | j = len; |
| 7389 | if (striptype != LEFTSTRIP) { |
| 7390 | do { |
| 7391 | j--; |
| 7392 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 7393 | j++; |
| 7394 | } |
| 7395 | |
| 7396 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 7397 | Py_INCREF(self); |
| 7398 | return (PyObject*)self; |
| 7399 | } |
| 7400 | else |
| 7401 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7402 | } |
| 7403 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7404 | |
| 7405 | static PyObject * |
| 7406 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 7407 | { |
| 7408 | PyObject *sep = NULL; |
| 7409 | |
| 7410 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 7411 | return NULL; |
| 7412 | |
| 7413 | if (sep != NULL && sep != Py_None) { |
| 7414 | if (PyUnicode_Check(sep)) |
| 7415 | return _PyUnicode_XStrip(self, striptype, sep); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7416 | else { |
| 7417 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7418 | "%s arg must be None or str", |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7419 | STRIPNAME(striptype)); |
| 7420 | return NULL; |
| 7421 | } |
| 7422 | } |
| 7423 | |
| 7424 | return do_strip(self, striptype); |
| 7425 | } |
| 7426 | |
| 7427 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7428 | PyDoc_STRVAR(strip__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7429 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7430 | \n\ |
| 7431 | Return a copy of the string S with leading and trailing\n\ |
| 7432 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7433 | If chars is given and not None, remove characters in chars instead."); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7434 | |
| 7435 | static PyObject * |
| 7436 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 7437 | { |
| 7438 | if (PyTuple_GET_SIZE(args) == 0) |
| 7439 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 7440 | else |
| 7441 | return do_argstrip(self, BOTHSTRIP, args); |
| 7442 | } |
| 7443 | |
| 7444 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7445 | PyDoc_STRVAR(lstrip__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7446 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7447 | \n\ |
| 7448 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7449 | If chars is given and not None, remove characters in chars instead."); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7450 | |
| 7451 | static PyObject * |
| 7452 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 7453 | { |
| 7454 | if (PyTuple_GET_SIZE(args) == 0) |
| 7455 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 7456 | else |
| 7457 | return do_argstrip(self, LEFTSTRIP, args); |
| 7458 | } |
| 7459 | |
| 7460 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7461 | PyDoc_STRVAR(rstrip__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7462 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7463 | \n\ |
| 7464 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7465 | If chars is given and not None, remove characters in chars instead."); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7466 | |
| 7467 | static PyObject * |
| 7468 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 7469 | { |
| 7470 | if (PyTuple_GET_SIZE(args) == 0) |
| 7471 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 7472 | else |
| 7473 | return do_argstrip(self, RIGHTSTRIP, args); |
| 7474 | } |
| 7475 | |
| 7476 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7477 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7478 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7479 | { |
| 7480 | PyUnicodeObject *u; |
| 7481 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7482 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7483 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7484 | |
| 7485 | if (len < 0) |
| 7486 | len = 0; |
| 7487 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7488 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7489 | /* no repeat, return original string */ |
| 7490 | Py_INCREF(str); |
| 7491 | return (PyObject*) str; |
| 7492 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7493 | |
| 7494 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 7495 | * needed doesn't overflow size_t |
| 7496 | */ |
| 7497 | nchars = len * str->length; |
| 7498 | if (len && nchars / len != str->length) { |
| 7499 | PyErr_SetString(PyExc_OverflowError, |
| 7500 | "repeated string is too long"); |
| 7501 | return NULL; |
| 7502 | } |
| 7503 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 7504 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 7505 | PyErr_SetString(PyExc_OverflowError, |
| 7506 | "repeated string is too long"); |
| 7507 | return NULL; |
| 7508 | } |
| 7509 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7510 | if (!u) |
| 7511 | return NULL; |
| 7512 | |
| 7513 | p = u->str; |
| 7514 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7515 | if (str->length == 1 && len > 0) { |
| 7516 | Py_UNICODE_FILL(p, str->str[0], len); |
| 7517 | } else { |
| 7518 | Py_ssize_t done = 0; /* number of characters copied this far */ |
| 7519 | if (done < nchars) { |
| 7520 | Py_UNICODE_COPY(p, str->str, str->length); |
| 7521 | done = str->length; |
| 7522 | } |
| 7523 | while (done < nchars) { |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 7524 | Py_ssize_t n = (done <= nchars-done) ? done : nchars-done; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7525 | Py_UNICODE_COPY(p+done, p, n); |
| 7526 | done += n; |
| 7527 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7528 | } |
| 7529 | |
| 7530 | return (PyObject*) u; |
| 7531 | } |
| 7532 | |
| 7533 | PyObject *PyUnicode_Replace(PyObject *obj, |
| 7534 | PyObject *subobj, |
| 7535 | PyObject *replobj, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7536 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7537 | { |
| 7538 | PyObject *self; |
| 7539 | PyObject *str1; |
| 7540 | PyObject *str2; |
| 7541 | PyObject *result; |
| 7542 | |
| 7543 | self = PyUnicode_FromObject(obj); |
| 7544 | if (self == NULL) |
| 7545 | return NULL; |
| 7546 | str1 = PyUnicode_FromObject(subobj); |
| 7547 | if (str1 == NULL) { |
| 7548 | Py_DECREF(self); |
| 7549 | return NULL; |
| 7550 | } |
| 7551 | str2 = PyUnicode_FromObject(replobj); |
| 7552 | if (str2 == NULL) { |
| 7553 | Py_DECREF(self); |
| 7554 | Py_DECREF(str1); |
| 7555 | return NULL; |
| 7556 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7557 | result = replace((PyUnicodeObject *)self, |
| 7558 | (PyUnicodeObject *)str1, |
| 7559 | (PyUnicodeObject *)str2, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7560 | maxcount); |
| 7561 | Py_DECREF(self); |
| 7562 | Py_DECREF(str1); |
| 7563 | Py_DECREF(str2); |
| 7564 | return result; |
| 7565 | } |
| 7566 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7567 | PyDoc_STRVAR(replace__doc__, |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 7568 | "S.replace (old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7569 | \n\ |
| 7570 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 7571 | old replaced by new. If the optional argument count is\n\ |
| 7572 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7573 | |
| 7574 | static PyObject* |
| 7575 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 7576 | { |
| 7577 | PyUnicodeObject *str1; |
| 7578 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7579 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7580 | PyObject *result; |
| 7581 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7582 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7583 | return NULL; |
| 7584 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 7585 | if (str1 == NULL) |
| 7586 | return NULL; |
| 7587 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7588 | if (str2 == NULL) { |
| 7589 | Py_DECREF(str1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7590 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7591 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7592 | |
| 7593 | result = replace(self, str1, str2, maxcount); |
| 7594 | |
| 7595 | Py_DECREF(str1); |
| 7596 | Py_DECREF(str2); |
| 7597 | return result; |
| 7598 | } |
| 7599 | |
| 7600 | static |
| 7601 | PyObject *unicode_repr(PyObject *unicode) |
| 7602 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7603 | PyObject *repr; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7604 | Py_UNICODE *p; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7605 | Py_UNICODE *s = PyUnicode_AS_UNICODE(unicode); |
| 7606 | Py_ssize_t size = PyUnicode_GET_SIZE(unicode); |
| 7607 | |
| 7608 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 7609 | better to choose a different scheme. Perhaps scan the |
| 7610 | first N-chars of the string and allocate based on that size. |
| 7611 | */ |
| 7612 | /* Initial allocation is based on the longest-possible unichr |
| 7613 | escape. |
| 7614 | |
| 7615 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 7616 | unichr, so in this case it's the longest unichr escape. In |
| 7617 | narrow (UTF-16) builds this is five chars per source unichr |
| 7618 | since there are two unichrs in the surrogate pair, so in narrow |
| 7619 | (UTF-16) builds it's not the longest unichr escape. |
| 7620 | |
| 7621 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 7622 | so in the narrow (UTF-16) build case it's the longest unichr |
| 7623 | escape. |
| 7624 | */ |
| 7625 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7626 | repr = PyUnicode_FromUnicode(NULL, |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7627 | 2 /* quotes */ |
| 7628 | #ifdef Py_UNICODE_WIDE |
| 7629 | + 10*size |
| 7630 | #else |
| 7631 | + 6*size |
| 7632 | #endif |
| 7633 | + 1); |
| 7634 | if (repr == NULL) |
| 7635 | return NULL; |
| 7636 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7637 | p = PyUnicode_AS_UNICODE(repr); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7638 | |
| 7639 | /* Add quote */ |
| 7640 | *p++ = (findchar(s, size, '\'') && |
| 7641 | !findchar(s, size, '"')) ? '"' : '\''; |
| 7642 | while (size-- > 0) { |
| 7643 | Py_UNICODE ch = *s++; |
| 7644 | |
| 7645 | /* Escape quotes and backslashes */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7646 | if ((ch == PyUnicode_AS_UNICODE(repr)[0]) || (ch == '\\')) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7647 | *p++ = '\\'; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7648 | *p++ = ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7649 | continue; |
| 7650 | } |
| 7651 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7652 | /* Map special whitespace to '\t', \n', '\r' */ |
| 7653 | if (ch == '\t') { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7654 | *p++ = '\\'; |
| 7655 | *p++ = 't'; |
| 7656 | } |
| 7657 | else if (ch == '\n') { |
| 7658 | *p++ = '\\'; |
| 7659 | *p++ = 'n'; |
| 7660 | } |
| 7661 | else if (ch == '\r') { |
| 7662 | *p++ = '\\'; |
| 7663 | *p++ = 'r'; |
| 7664 | } |
| 7665 | |
| 7666 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7667 | else if (ch < ' ' || ch == 0x7F) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7668 | *p++ = '\\'; |
| 7669 | *p++ = 'x'; |
| 7670 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 7671 | *p++ = hexdigits[ch & 0x000F]; |
| 7672 | } |
| 7673 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7674 | /* Copy ASCII characters as-is */ |
| 7675 | else if (ch < 0x7F) { |
| 7676 | *p++ = ch; |
| 7677 | } |
| 7678 | |
| 7679 | /* Non-ASCII characters */ |
| 7680 | else { |
| 7681 | Py_UCS4 ucs = ch; |
| 7682 | |
| 7683 | #ifndef Py_UNICODE_WIDE |
| 7684 | Py_UNICODE ch2 = 0; |
| 7685 | /* Get code point from surrogate pair */ |
| 7686 | if (size > 0) { |
| 7687 | ch2 = *s; |
| 7688 | if (ch >= 0xD800 && ch < 0xDC00 && ch2 >= 0xDC00 |
| 7689 | && ch2 <= 0xDFFF) { |
| 7690 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) |
| 7691 | + 0x00010000; |
| 7692 | s++; |
| 7693 | size--; |
| 7694 | } |
| 7695 | } |
| 7696 | #endif |
| 7697 | /* Map Unicode whitespace and control characters |
| 7698 | (categories Z* and C* except ASCII space) |
| 7699 | */ |
| 7700 | if (!Py_UNICODE_ISPRINTABLE(ucs)) { |
| 7701 | /* Map 8-bit characters to '\xhh' */ |
| 7702 | if (ucs <= 0xff) { |
| 7703 | *p++ = '\\'; |
| 7704 | *p++ = 'x'; |
| 7705 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 7706 | *p++ = hexdigits[ch & 0x000F]; |
| 7707 | } |
| 7708 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 7709 | else if (ucs >= 0x10000) { |
| 7710 | *p++ = '\\'; |
| 7711 | *p++ = 'U'; |
| 7712 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 7713 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 7714 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 7715 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 7716 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 7717 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 7718 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 7719 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 7720 | } |
| 7721 | /* Map 16-bit characters to '\uxxxx' */ |
| 7722 | else { |
| 7723 | *p++ = '\\'; |
| 7724 | *p++ = 'u'; |
| 7725 | *p++ = hexdigits[(ucs >> 12) & 0x000F]; |
| 7726 | *p++ = hexdigits[(ucs >> 8) & 0x000F]; |
| 7727 | *p++ = hexdigits[(ucs >> 4) & 0x000F]; |
| 7728 | *p++ = hexdigits[ucs & 0x000F]; |
| 7729 | } |
| 7730 | } |
| 7731 | /* Copy characters as-is */ |
| 7732 | else { |
| 7733 | *p++ = ch; |
| 7734 | #ifndef Py_UNICODE_WIDE |
| 7735 | if (ucs >= 0x10000) |
| 7736 | *p++ = ch2; |
| 7737 | #endif |
| 7738 | } |
| 7739 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7740 | } |
| 7741 | /* Add quote */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7742 | *p++ = PyUnicode_AS_UNICODE(repr)[0]; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7743 | |
| 7744 | *p = '\0'; |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 7745 | PyUnicode_Resize(&repr, p - PyUnicode_AS_UNICODE(repr)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7746 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7747 | } |
| 7748 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7749 | PyDoc_STRVAR(rfind__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7750 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7751 | \n\ |
| 7752 | Return the highest index in S where substring sub is found,\n\ |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 7753 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7754 | arguments start and end are interpreted as in slice notation.\n\ |
| 7755 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7756 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7757 | |
| 7758 | static PyObject * |
| 7759 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 7760 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7761 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7762 | Py_ssize_t start; |
| 7763 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7764 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7765 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7766 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
| 7767 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7768 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7769 | result = stringlib_rfind_slice( |
| 7770 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7771 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7772 | start, end |
| 7773 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7774 | |
| 7775 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7776 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7777 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7778 | } |
| 7779 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7780 | PyDoc_STRVAR(rindex__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7781 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7782 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7783 | 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] | 7784 | |
| 7785 | static PyObject * |
| 7786 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 7787 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7788 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7789 | Py_ssize_t start; |
| 7790 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7791 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7792 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7793 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
| 7794 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7795 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7796 | result = stringlib_rfind_slice( |
| 7797 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7798 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7799 | start, end |
| 7800 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7801 | |
| 7802 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7803 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7804 | if (result < 0) { |
| 7805 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 7806 | return NULL; |
| 7807 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7808 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7809 | } |
| 7810 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7811 | PyDoc_STRVAR(rjust__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7812 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7813 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 7814 | Return S right-justified in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7815 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7816 | |
| 7817 | static PyObject * |
| 7818 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 7819 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7820 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7821 | Py_UNICODE fillchar = ' '; |
| 7822 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7823 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7824 | return NULL; |
| 7825 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7826 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7827 | Py_INCREF(self); |
| 7828 | return (PyObject*) self; |
| 7829 | } |
| 7830 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7831 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7832 | } |
| 7833 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7834 | PyObject *PyUnicode_Split(PyObject *s, |
| 7835 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7836 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7837 | { |
| 7838 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7839 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7840 | s = PyUnicode_FromObject(s); |
| 7841 | if (s == NULL) |
| 7842 | return NULL; |
| 7843 | if (sep != NULL) { |
| 7844 | sep = PyUnicode_FromObject(sep); |
| 7845 | if (sep == NULL) { |
| 7846 | Py_DECREF(s); |
| 7847 | return NULL; |
| 7848 | } |
| 7849 | } |
| 7850 | |
| 7851 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 7852 | |
| 7853 | Py_DECREF(s); |
| 7854 | Py_XDECREF(sep); |
| 7855 | return result; |
| 7856 | } |
| 7857 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7858 | PyDoc_STRVAR(split__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7859 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7860 | \n\ |
| 7861 | Return a list of the words in S, using sep as the\n\ |
| 7862 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 7863 | splits are done. If sep is not specified or is None, any\n\ |
Alexandre Vassalotti | 8ae3e05 | 2008-05-16 00:41:41 +0000 | [diff] [blame] | 7864 | whitespace string is a separator and empty strings are\n\ |
| 7865 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7866 | |
| 7867 | static PyObject* |
| 7868 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 7869 | { |
| 7870 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7871 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7872 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7873 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7874 | return NULL; |
| 7875 | |
| 7876 | if (substring == Py_None) |
| 7877 | return split(self, NULL, maxcount); |
| 7878 | else if (PyUnicode_Check(substring)) |
| 7879 | return split(self, (PyUnicodeObject *)substring, maxcount); |
| 7880 | else |
| 7881 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
| 7882 | } |
| 7883 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7884 | PyObject * |
| 7885 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 7886 | { |
| 7887 | PyObject* str_obj; |
| 7888 | PyObject* sep_obj; |
| 7889 | PyObject* out; |
| 7890 | |
| 7891 | str_obj = PyUnicode_FromObject(str_in); |
| 7892 | if (!str_obj) |
| 7893 | return NULL; |
| 7894 | sep_obj = PyUnicode_FromObject(sep_in); |
| 7895 | if (!sep_obj) { |
| 7896 | Py_DECREF(str_obj); |
| 7897 | return NULL; |
| 7898 | } |
| 7899 | |
| 7900 | out = stringlib_partition( |
| 7901 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 7902 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 7903 | ); |
| 7904 | |
| 7905 | Py_DECREF(sep_obj); |
| 7906 | Py_DECREF(str_obj); |
| 7907 | |
| 7908 | return out; |
| 7909 | } |
| 7910 | |
| 7911 | |
| 7912 | PyObject * |
| 7913 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 7914 | { |
| 7915 | PyObject* str_obj; |
| 7916 | PyObject* sep_obj; |
| 7917 | PyObject* out; |
| 7918 | |
| 7919 | str_obj = PyUnicode_FromObject(str_in); |
| 7920 | if (!str_obj) |
| 7921 | return NULL; |
| 7922 | sep_obj = PyUnicode_FromObject(sep_in); |
| 7923 | if (!sep_obj) { |
| 7924 | Py_DECREF(str_obj); |
| 7925 | return NULL; |
| 7926 | } |
| 7927 | |
| 7928 | out = stringlib_rpartition( |
| 7929 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 7930 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 7931 | ); |
| 7932 | |
| 7933 | Py_DECREF(sep_obj); |
| 7934 | Py_DECREF(str_obj); |
| 7935 | |
| 7936 | return out; |
| 7937 | } |
| 7938 | |
| 7939 | PyDoc_STRVAR(partition__doc__, |
| 7940 | "S.partition(sep) -> (head, sep, tail)\n\ |
| 7941 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7942 | Search for the separator sep in S, and return the part before it,\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7943 | the separator itself, and the part after it. If the separator is not\n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 7944 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7945 | |
| 7946 | static PyObject* |
| 7947 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
| 7948 | { |
| 7949 | return PyUnicode_Partition((PyObject *)self, separator); |
| 7950 | } |
| 7951 | |
| 7952 | PyDoc_STRVAR(rpartition__doc__, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7953 | "S.rpartition(sep) -> (tail, sep, head)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7954 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7955 | Search for the separator sep in S, starting at the end of S, and return\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7956 | the part before it, the separator itself, and the part after it. If the\n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 7957 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7958 | |
| 7959 | static PyObject* |
| 7960 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 7961 | { |
| 7962 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 7963 | } |
| 7964 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7965 | PyObject *PyUnicode_RSplit(PyObject *s, |
| 7966 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7967 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7968 | { |
| 7969 | PyObject *result; |
| 7970 | |
| 7971 | s = PyUnicode_FromObject(s); |
| 7972 | if (s == NULL) |
| 7973 | return NULL; |
| 7974 | if (sep != NULL) { |
| 7975 | sep = PyUnicode_FromObject(sep); |
| 7976 | if (sep == NULL) { |
| 7977 | Py_DECREF(s); |
| 7978 | return NULL; |
| 7979 | } |
| 7980 | } |
| 7981 | |
| 7982 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 7983 | |
| 7984 | Py_DECREF(s); |
| 7985 | Py_XDECREF(sep); |
| 7986 | return result; |
| 7987 | } |
| 7988 | |
| 7989 | PyDoc_STRVAR(rsplit__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7990 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7991 | \n\ |
| 7992 | Return a list of the words in S, using sep as the\n\ |
| 7993 | delimiter string, starting at the end of the string and\n\ |
| 7994 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 7995 | splits are done. If sep is not specified, any whitespace string\n\ |
| 7996 | is a separator."); |
| 7997 | |
| 7998 | static PyObject* |
| 7999 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 8000 | { |
| 8001 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8002 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8003 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8004 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8005 | return NULL; |
| 8006 | |
| 8007 | if (substring == Py_None) |
| 8008 | return rsplit(self, NULL, maxcount); |
| 8009 | else if (PyUnicode_Check(substring)) |
| 8010 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
| 8011 | else |
| 8012 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
| 8013 | } |
| 8014 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8015 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 4469d0c | 2008-11-30 22:46:23 +0000 | [diff] [blame] | 8016 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8017 | \n\ |
| 8018 | 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] | 8019 | 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] | 8020 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8021 | |
| 8022 | static PyObject* |
| 8023 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 8024 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8025 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8026 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8027 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8028 | return NULL; |
| 8029 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8030 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8031 | } |
| 8032 | |
| 8033 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 8034 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8035 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 8036 | if (PyUnicode_CheckExact(self)) { |
| 8037 | Py_INCREF(self); |
| 8038 | return self; |
| 8039 | } else |
| 8040 | /* Subtype -- return genuine unicode string with the same value. */ |
| 8041 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(self), |
| 8042 | PyUnicode_GET_SIZE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8043 | } |
| 8044 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8045 | PyDoc_STRVAR(swapcase__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8046 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8047 | \n\ |
| 8048 | 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] | 8049 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8050 | |
| 8051 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8052 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8053 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8054 | return fixup(self, fixswapcase); |
| 8055 | } |
| 8056 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8057 | PyDoc_STRVAR(maketrans__doc__, |
| 8058 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
| 8059 | \n\ |
| 8060 | Return a translation table usable for str.translate().\n\ |
| 8061 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 8062 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8063 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8064 | If there are two arguments, they must be strings of equal length, and\n\ |
| 8065 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 8066 | character at the same position in y. If there is a third argument, it\n\ |
| 8067 | must be a string, whose characters will be mapped to None in the result."); |
| 8068 | |
| 8069 | static PyObject* |
| 8070 | unicode_maketrans(PyUnicodeObject *null, PyObject *args) |
| 8071 | { |
| 8072 | PyObject *x, *y = NULL, *z = NULL; |
| 8073 | PyObject *new = NULL, *key, *value; |
| 8074 | Py_ssize_t i = 0; |
| 8075 | int res; |
| 8076 | |
| 8077 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 8078 | return NULL; |
| 8079 | new = PyDict_New(); |
| 8080 | if (!new) |
| 8081 | return NULL; |
| 8082 | if (y != NULL) { |
| 8083 | /* x must be a string too, of equal length */ |
| 8084 | Py_ssize_t ylen = PyUnicode_GET_SIZE(y); |
| 8085 | if (!PyUnicode_Check(x)) { |
| 8086 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 8087 | "be a string if there is a second argument"); |
| 8088 | goto err; |
| 8089 | } |
| 8090 | if (PyUnicode_GET_SIZE(x) != ylen) { |
| 8091 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 8092 | "arguments must have equal length"); |
| 8093 | goto err; |
| 8094 | } |
| 8095 | /* create entries for translating chars in x to those in y */ |
| 8096 | for (i = 0; i < PyUnicode_GET_SIZE(x); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8097 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(x)[i]); |
| 8098 | value = PyLong_FromLong(PyUnicode_AS_UNICODE(y)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8099 | if (!key || !value) |
| 8100 | goto err; |
| 8101 | res = PyDict_SetItem(new, key, value); |
| 8102 | Py_DECREF(key); |
| 8103 | Py_DECREF(value); |
| 8104 | if (res < 0) |
| 8105 | goto err; |
| 8106 | } |
| 8107 | /* create entries for deleting chars in z */ |
| 8108 | if (z != NULL) { |
| 8109 | for (i = 0; i < PyUnicode_GET_SIZE(z); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8110 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(z)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8111 | if (!key) |
| 8112 | goto err; |
| 8113 | res = PyDict_SetItem(new, key, Py_None); |
| 8114 | Py_DECREF(key); |
| 8115 | if (res < 0) |
| 8116 | goto err; |
| 8117 | } |
| 8118 | } |
| 8119 | } else { |
| 8120 | /* x must be a dict */ |
| 8121 | if (!PyDict_Check(x)) { |
| 8122 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 8123 | "to maketrans it must be a dict"); |
| 8124 | goto err; |
| 8125 | } |
| 8126 | /* copy entries into the new dict, converting string keys to int keys */ |
| 8127 | while (PyDict_Next(x, &i, &key, &value)) { |
| 8128 | if (PyUnicode_Check(key)) { |
| 8129 | /* convert string keys to integer keys */ |
| 8130 | PyObject *newkey; |
| 8131 | if (PyUnicode_GET_SIZE(key) != 1) { |
| 8132 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 8133 | "table must be of length 1"); |
| 8134 | goto err; |
| 8135 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8136 | newkey = PyLong_FromLong(PyUnicode_AS_UNICODE(key)[0]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8137 | if (!newkey) |
| 8138 | goto err; |
| 8139 | res = PyDict_SetItem(new, newkey, value); |
| 8140 | Py_DECREF(newkey); |
| 8141 | if (res < 0) |
| 8142 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8143 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8144 | /* just keep integer keys */ |
| 8145 | if (PyDict_SetItem(new, key, value) < 0) |
| 8146 | goto err; |
| 8147 | } else { |
| 8148 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 8149 | "be strings or integers"); |
| 8150 | goto err; |
| 8151 | } |
| 8152 | } |
| 8153 | } |
| 8154 | return new; |
| 8155 | err: |
| 8156 | Py_DECREF(new); |
| 8157 | return NULL; |
| 8158 | } |
| 8159 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8160 | PyDoc_STRVAR(translate__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8161 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8162 | \n\ |
| 8163 | Return a copy of the string S, where all characters have been mapped\n\ |
| 8164 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8165 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 8166 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 8167 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8168 | |
| 8169 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8170 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8171 | { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8172 | return PyUnicode_TranslateCharmap(self->str, self->length, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8173 | } |
| 8174 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8175 | PyDoc_STRVAR(upper__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8176 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8177 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8178 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8179 | |
| 8180 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8181 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8182 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8183 | return fixup(self, fixupper); |
| 8184 | } |
| 8185 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8186 | PyDoc_STRVAR(zfill__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8187 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8188 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 8189 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 8190 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8191 | |
| 8192 | static PyObject * |
| 8193 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 8194 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8195 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8196 | PyUnicodeObject *u; |
| 8197 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8198 | Py_ssize_t width; |
| 8199 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8200 | return NULL; |
| 8201 | |
| 8202 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 8203 | if (PyUnicode_CheckExact(self)) { |
| 8204 | Py_INCREF(self); |
| 8205 | return (PyObject*) self; |
| 8206 | } |
| 8207 | else |
| 8208 | return PyUnicode_FromUnicode( |
| 8209 | PyUnicode_AS_UNICODE(self), |
| 8210 | PyUnicode_GET_SIZE(self) |
| 8211 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8212 | } |
| 8213 | |
| 8214 | fill = width - self->length; |
| 8215 | |
| 8216 | u = pad(self, fill, 0, '0'); |
| 8217 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8218 | if (u == NULL) |
| 8219 | return NULL; |
| 8220 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8221 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 8222 | /* move sign to beginning of string */ |
| 8223 | u->str[0] = u->str[fill]; |
| 8224 | u->str[fill] = '0'; |
| 8225 | } |
| 8226 | |
| 8227 | return (PyObject*) u; |
| 8228 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8229 | |
| 8230 | #if 0 |
| 8231 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8232 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8233 | { |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 8234 | return PyLong_FromLong(numfree); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8235 | } |
| 8236 | #endif |
| 8237 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8238 | PyDoc_STRVAR(startswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 8239 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8240 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 8241 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 8242 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8243 | With optional end, stop comparing S at that position.\n\ |
| 8244 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8245 | |
| 8246 | static PyObject * |
| 8247 | unicode_startswith(PyUnicodeObject *self, |
| 8248 | PyObject *args) |
| 8249 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8250 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8251 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8252 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8253 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8254 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8255 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8256 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 8257 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8258 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8259 | if (PyTuple_Check(subobj)) { |
| 8260 | Py_ssize_t i; |
| 8261 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 8262 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 8263 | PyTuple_GET_ITEM(subobj, i)); |
| 8264 | if (substring == NULL) |
| 8265 | return NULL; |
| 8266 | result = tailmatch(self, substring, start, end, -1); |
| 8267 | Py_DECREF(substring); |
| 8268 | if (result) { |
| 8269 | Py_RETURN_TRUE; |
| 8270 | } |
| 8271 | } |
| 8272 | /* nothing matched */ |
| 8273 | Py_RETURN_FALSE; |
| 8274 | } |
| 8275 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8276 | if (substring == NULL) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8277 | return NULL; |
| 8278 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8279 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8280 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8281 | } |
| 8282 | |
| 8283 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8284 | PyDoc_STRVAR(endswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 8285 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8286 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 8287 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 8288 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8289 | With optional end, stop comparing S at that position.\n\ |
| 8290 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8291 | |
| 8292 | static PyObject * |
| 8293 | unicode_endswith(PyUnicodeObject *self, |
| 8294 | PyObject *args) |
| 8295 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8296 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8297 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8298 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8299 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8300 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8301 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8302 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
| 8303 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8304 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8305 | if (PyTuple_Check(subobj)) { |
| 8306 | Py_ssize_t i; |
| 8307 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 8308 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 8309 | PyTuple_GET_ITEM(subobj, i)); |
| 8310 | if (substring == NULL) |
| 8311 | return NULL; |
| 8312 | result = tailmatch(self, substring, start, end, +1); |
| 8313 | Py_DECREF(substring); |
| 8314 | if (result) { |
| 8315 | Py_RETURN_TRUE; |
| 8316 | } |
| 8317 | } |
| 8318 | Py_RETURN_FALSE; |
| 8319 | } |
| 8320 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8321 | if (substring == NULL) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8322 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8323 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8324 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8325 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8326 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8327 | } |
| 8328 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8329 | #include "stringlib/string_format.h" |
| 8330 | |
| 8331 | PyDoc_STRVAR(format__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8332 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8333 | \n\ |
| 8334 | "); |
| 8335 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 8336 | static PyObject * |
| 8337 | unicode__format__(PyObject* self, PyObject* args) |
| 8338 | { |
| 8339 | PyObject *format_spec; |
| 8340 | |
| 8341 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 8342 | return NULL; |
| 8343 | |
| 8344 | return _PyUnicode_FormatAdvanced(self, |
| 8345 | PyUnicode_AS_UNICODE(format_spec), |
| 8346 | PyUnicode_GET_SIZE(format_spec)); |
| 8347 | } |
| 8348 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8349 | PyDoc_STRVAR(p_format__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8350 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8351 | \n\ |
| 8352 | "); |
| 8353 | |
| 8354 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8355 | unicode__sizeof__(PyUnicodeObject *v) |
| 8356 | { |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 8357 | return PyLong_FromSsize_t(sizeof(PyUnicodeObject) + |
| 8358 | sizeof(Py_UNICODE) * (v->length + 1)); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8359 | } |
| 8360 | |
| 8361 | PyDoc_STRVAR(sizeof__doc__, |
| 8362 | "S.__sizeof__() -> size of S in memory, in bytes"); |
| 8363 | |
| 8364 | static PyObject * |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8365 | unicode_getnewargs(PyUnicodeObject *v) |
| 8366 | { |
| 8367 | return Py_BuildValue("(u#)", v->str, v->length); |
| 8368 | } |
| 8369 | |
| 8370 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8371 | static PyMethodDef unicode_methods[] = { |
| 8372 | |
| 8373 | /* Order is according to common usage: often used methods should |
| 8374 | appear first, since lookup is done sequentially. */ |
| 8375 | |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8376 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS, encode__doc__}, |
| 8377 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 8378 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8379 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8380 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 8381 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 8382 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 8383 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 8384 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 8385 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 8386 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8387 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8388 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 8389 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 8390 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8391 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8392 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 8393 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 8394 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8395 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8396 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8397 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8398 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8399 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 8400 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 8401 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 8402 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 8403 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 8404 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 8405 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 8406 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 8407 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 8408 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 8409 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 8410 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 8411 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 8412 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 8413 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8414 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8415 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 8416 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 8417 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Eric Smith | f6db409 | 2007-08-27 23:52:26 +0000 | [diff] [blame] | 8418 | {"_formatter_field_name_split", (PyCFunction) formatter_field_name_split, METH_NOARGS}, |
| 8419 | {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8420 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 8421 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8422 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8423 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8424 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8425 | #endif |
| 8426 | |
| 8427 | #if 0 |
| 8428 | /* This one is just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8429 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8430 | #endif |
| 8431 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8432 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8433 | {NULL, NULL} |
| 8434 | }; |
| 8435 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8436 | static PyObject * |
| 8437 | unicode_mod(PyObject *v, PyObject *w) |
| 8438 | { |
| 8439 | if (!PyUnicode_Check(v)) { |
| 8440 | Py_INCREF(Py_NotImplemented); |
| 8441 | return Py_NotImplemented; |
| 8442 | } |
| 8443 | return PyUnicode_Format(v, w); |
| 8444 | } |
| 8445 | |
| 8446 | static PyNumberMethods unicode_as_number = { |
| 8447 | 0, /*nb_add*/ |
| 8448 | 0, /*nb_subtract*/ |
| 8449 | 0, /*nb_multiply*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8450 | unicode_mod, /*nb_remainder*/ |
| 8451 | }; |
| 8452 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8453 | static PySequenceMethods unicode_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8454 | (lenfunc) unicode_length, /* sq_length */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8455 | PyUnicode_Concat, /* sq_concat */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8456 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 8457 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
Thomas Wouters | d2cf20e | 2007-08-30 22:57:53 +0000 | [diff] [blame] | 8458 | 0, /* sq_slice */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8459 | 0, /* sq_ass_item */ |
| 8460 | 0, /* sq_ass_slice */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8461 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8462 | }; |
| 8463 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8464 | static PyObject* |
| 8465 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 8466 | { |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 8467 | if (PyIndex_Check(item)) { |
| 8468 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8469 | if (i == -1 && PyErr_Occurred()) |
| 8470 | return NULL; |
| 8471 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8472 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8473 | return unicode_getitem(self, i); |
| 8474 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8475 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8476 | Py_UNICODE* source_buf; |
| 8477 | Py_UNICODE* result_buf; |
| 8478 | PyObject* result; |
| 8479 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8480 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8481 | &start, &stop, &step, &slicelength) < 0) { |
| 8482 | return NULL; |
| 8483 | } |
| 8484 | |
| 8485 | if (slicelength <= 0) { |
| 8486 | return PyUnicode_FromUnicode(NULL, 0); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 8487 | } else if (start == 0 && step == 1 && slicelength == self->length && |
| 8488 | PyUnicode_CheckExact(self)) { |
| 8489 | Py_INCREF(self); |
| 8490 | return (PyObject *)self; |
| 8491 | } else if (step == 1) { |
| 8492 | return PyUnicode_FromUnicode(self->str + start, slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8493 | } else { |
| 8494 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 8495 | result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* |
| 8496 | sizeof(Py_UNICODE)); |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8497 | |
| 8498 | if (result_buf == NULL) |
| 8499 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8500 | |
| 8501 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 8502 | result_buf[i] = source_buf[cur]; |
| 8503 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8504 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8505 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 8506 | PyObject_FREE(result_buf); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8507 | return result; |
| 8508 | } |
| 8509 | } else { |
| 8510 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 8511 | return NULL; |
| 8512 | } |
| 8513 | } |
| 8514 | |
| 8515 | static PyMappingMethods unicode_as_mapping = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8516 | (lenfunc)unicode_length, /* mp_length */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8517 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 8518 | (objobjargproc)0, /* mp_ass_subscript */ |
| 8519 | }; |
| 8520 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8521 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8522 | /* Helpers for PyUnicode_Format() */ |
| 8523 | |
| 8524 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8525 | 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] | 8526 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8527 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8528 | if (argidx < arglen) { |
| 8529 | (*p_argidx)++; |
| 8530 | if (arglen < 0) |
| 8531 | return args; |
| 8532 | else |
| 8533 | return PyTuple_GetItem(args, argidx); |
| 8534 | } |
| 8535 | PyErr_SetString(PyExc_TypeError, |
| 8536 | "not enough arguments for format string"); |
| 8537 | return NULL; |
| 8538 | } |
| 8539 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8540 | static Py_ssize_t |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8541 | strtounicode(Py_UNICODE *buffer, const char *charbuffer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8542 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8543 | register Py_ssize_t i; |
| 8544 | Py_ssize_t len = strlen(charbuffer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8545 | for (i = len - 1; i >= 0; i--) |
| 8546 | buffer[i] = (Py_UNICODE) charbuffer[i]; |
| 8547 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8548 | return len; |
| 8549 | } |
| 8550 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8551 | static int |
| 8552 | doubletounicode(Py_UNICODE *buffer, size_t len, const char *format, double x) |
| 8553 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8554 | Py_ssize_t result; |
| 8555 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8556 | PyOS_ascii_formatd((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8557 | result = strtounicode(buffer, (char *)buffer); |
| 8558 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8559 | } |
| 8560 | |
Christian Heimes | 3fd1399 | 2008-03-21 01:05:49 +0000 | [diff] [blame] | 8561 | #if 0 |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8562 | static int |
| 8563 | longtounicode(Py_UNICODE *buffer, size_t len, const char *format, long x) |
| 8564 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8565 | Py_ssize_t result; |
| 8566 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8567 | PyOS_snprintf((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8568 | result = strtounicode(buffer, (char *)buffer); |
| 8569 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8570 | } |
Christian Heimes | 3fd1399 | 2008-03-21 01:05:49 +0000 | [diff] [blame] | 8571 | #endif |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8572 | |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 8573 | /* XXX To save some code duplication, formatfloat/long/int could have been |
| 8574 | shared with stringobject.c, converting from 8-bit to Unicode after the |
| 8575 | formatting is done. */ |
| 8576 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8577 | static int |
| 8578 | formatfloat(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8579 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8580 | int flags, |
| 8581 | int prec, |
| 8582 | int type, |
| 8583 | PyObject *v) |
| 8584 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8585 | /* fmt = '%#.' + `prec` + `type` |
| 8586 | 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] | 8587 | char fmt[20]; |
| 8588 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8589 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8590 | x = PyFloat_AsDouble(v); |
| 8591 | if (x == -1.0 && PyErr_Occurred()) |
| 8592 | return -1; |
| 8593 | if (prec < 0) |
| 8594 | prec = 6; |
Eric Smith | 22b85b3 | 2008-07-17 19:18:29 +0000 | [diff] [blame] | 8595 | if (type == 'f' && (fabs(x) / 1e25) >= 1e25) |
| 8596 | type = 'g'; |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8597 | /* Worst case length calc to ensure no buffer overrun: |
| 8598 | |
| 8599 | 'g' formats: |
| 8600 | fmt = %#.<prec>g |
| 8601 | buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
| 8602 | for any double rep.) |
| 8603 | len = 1 + prec + 1 + 2 + 5 = 9 + prec |
| 8604 | |
| 8605 | 'f' formats: |
| 8606 | buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
| 8607 | len = 1 + 50 + 1 + prec = 52 + prec |
| 8608 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8609 | If prec=0 the effective precision is 1 (the leading digit is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8610 | always given), therefore increase the length by one. |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8611 | |
| 8612 | */ |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 8613 | if (((type == 'g' || type == 'G') && |
| 8614 | buflen <= (size_t)10 + (size_t)prec) || |
Eric Smith | 22b85b3 | 2008-07-17 19:18:29 +0000 | [diff] [blame] | 8615 | (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8616 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8617 | "formatted float is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8618 | return -1; |
| 8619 | } |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8620 | PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
| 8621 | (flags&F_ALT) ? "#" : "", |
| 8622 | prec, type); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8623 | return doubletounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8624 | } |
| 8625 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8626 | static PyObject* |
| 8627 | formatlong(PyObject *val, int flags, int prec, int type) |
| 8628 | { |
| 8629 | char *buf; |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8630 | int len; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8631 | PyObject *str; /* temporary string object. */ |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8632 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8633 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8634 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8635 | if (!str) |
| 8636 | return NULL; |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8637 | result = PyUnicode_FromStringAndSize(buf, len); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8638 | Py_DECREF(str); |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8639 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8640 | } |
| 8641 | |
Christian Heimes | 3fd1399 | 2008-03-21 01:05:49 +0000 | [diff] [blame] | 8642 | #if 0 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8643 | static int |
| 8644 | formatint(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8645 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8646 | int flags, |
| 8647 | int prec, |
| 8648 | int type, |
| 8649 | PyObject *v) |
| 8650 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8651 | /* fmt = '%#.' + `prec` + 'l' + `type` |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8652 | * worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
| 8653 | * + 1 + 1 |
| 8654 | * = 24 |
| 8655 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8656 | char fmt[64]; /* plenty big enough! */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8657 | char *sign; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8658 | long x; |
| 8659 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8660 | x = PyLong_AsLong(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8661 | if (x == -1 && PyErr_Occurred()) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8662 | return -1; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8663 | if (x < 0 && type == 'u') { |
| 8664 | type = 'd'; |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 8665 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8666 | if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) |
| 8667 | sign = "-"; |
| 8668 | else |
| 8669 | sign = ""; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8670 | if (prec < 0) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8671 | prec = 1; |
| 8672 | |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8673 | /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) |
| 8674 | * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8675 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8676 | if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8677 | PyErr_SetString(PyExc_OverflowError, |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8678 | "formatted integer is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8679 | return -1; |
| 8680 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8681 | |
| 8682 | if ((flags & F_ALT) && |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 8683 | (type == 'x' || type == 'X' || type == 'o')) { |
| 8684 | /* When converting under %#o, %#x or %#X, there are a number |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8685 | * of issues that cause pain: |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 8686 | * - for %#o, we want a different base marker than C |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8687 | * - when 0 is being converted, the C standard leaves off |
| 8688 | * the '0x' or '0X', which is inconsistent with other |
| 8689 | * %#x/%#X conversions and inconsistent with Python's |
| 8690 | * hex() function |
| 8691 | * - there are platforms that violate the standard and |
| 8692 | * convert 0 with the '0x' or '0X' |
| 8693 | * (Metrowerks, Compaq Tru64) |
| 8694 | * - there are platforms that give '0x' when converting |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8695 | * under %#X, but convert 0 in accordance with the |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8696 | * standard (OS/2 EMX) |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8697 | * |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8698 | * We can achieve the desired consistency by inserting our |
| 8699 | * own '0x' or '0X' prefix, and substituting %x/%X in place |
| 8700 | * of %#x/%#X. |
| 8701 | * |
| 8702 | * Note that this is the same approach as used in |
| 8703 | * formatint() in stringobject.c |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 8704 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8705 | PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", |
| 8706 | sign, type, prec, type); |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 8707 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8708 | else { |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8709 | PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", |
| 8710 | sign, (flags&F_ALT) ? "#" : "", |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8711 | prec, type); |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 8712 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8713 | if (sign[0]) |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8714 | return longtounicode(buf, buflen, fmt, -x); |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8715 | else |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8716 | return longtounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8717 | } |
Christian Heimes | 3fd1399 | 2008-03-21 01:05:49 +0000 | [diff] [blame] | 8718 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8719 | |
| 8720 | static int |
| 8721 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8722 | size_t buflen, |
| 8723 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8724 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 8725 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8726 | if (PyUnicode_Check(v)) { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 8727 | if (PyUnicode_GET_SIZE(v) == 1) { |
| 8728 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
| 8729 | buf[1] = '\0'; |
| 8730 | return 1; |
| 8731 | } |
| 8732 | #ifndef Py_UNICODE_WIDE |
| 8733 | if (PyUnicode_GET_SIZE(v) == 2) { |
| 8734 | /* Decode a valid surrogate pair */ |
| 8735 | int c0 = PyUnicode_AS_UNICODE(v)[0]; |
| 8736 | int c1 = PyUnicode_AS_UNICODE(v)[1]; |
| 8737 | if (0xD800 <= c0 && c0 <= 0xDBFF && |
| 8738 | 0xDC00 <= c1 && c1 <= 0xDFFF) { |
| 8739 | buf[0] = c0; |
| 8740 | buf[1] = c1; |
| 8741 | buf[2] = '\0'; |
| 8742 | return 2; |
| 8743 | } |
| 8744 | } |
| 8745 | #endif |
| 8746 | goto onError; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8747 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8748 | else { |
| 8749 | /* Integer input truncated to a character */ |
| 8750 | long x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8751 | x = PyLong_AsLong(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8752 | if (x == -1 && PyErr_Occurred()) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8753 | goto onError; |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 8754 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8755 | if (x < 0 || x > 0x10ffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 8756 | PyErr_SetString(PyExc_OverflowError, |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 8757 | "%c arg not in range(0x110000)"); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8758 | return -1; |
| 8759 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 8760 | |
| 8761 | #ifndef Py_UNICODE_WIDE |
| 8762 | if (x > 0xffff) { |
| 8763 | x -= 0x10000; |
| 8764 | buf[0] = (Py_UNICODE)(0xD800 | (x >> 10)); |
| 8765 | buf[1] = (Py_UNICODE)(0xDC00 | (x & 0x3FF)); |
| 8766 | return 2; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8767 | } |
| 8768 | #endif |
| 8769 | buf[0] = (Py_UNICODE) x; |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 8770 | buf[1] = '\0'; |
| 8771 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8772 | } |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8773 | |
| 8774 | onError: |
| 8775 | PyErr_SetString(PyExc_TypeError, |
| 8776 | "%c requires int or char"); |
| 8777 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8778 | } |
| 8779 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8780 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 8781 | |
| 8782 | FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
| 8783 | chars are formatted. XXX This is a magic number. Each formatting |
| 8784 | routine does bounds checking to ensure no overflow, but a better |
| 8785 | solution may be to malloc a buffer of appropriate size for each |
| 8786 | format. For now, the current solution is sufficient. |
| 8787 | */ |
| 8788 | #define FORMATBUFLEN (size_t)120 |
| 8789 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8790 | PyObject *PyUnicode_Format(PyObject *format, |
| 8791 | PyObject *args) |
| 8792 | { |
| 8793 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8794 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8795 | int args_owned = 0; |
| 8796 | PyUnicodeObject *result = NULL; |
| 8797 | PyObject *dict = NULL; |
| 8798 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8799 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8800 | if (format == NULL || args == NULL) { |
| 8801 | PyErr_BadInternalCall(); |
| 8802 | return NULL; |
| 8803 | } |
| 8804 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8805 | if (uformat == NULL) |
| 8806 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8807 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 8808 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 8809 | |
| 8810 | reslen = rescnt = fmtcnt + 100; |
| 8811 | result = _PyUnicode_New(reslen); |
| 8812 | if (result == NULL) |
| 8813 | goto onError; |
| 8814 | res = PyUnicode_AS_UNICODE(result); |
| 8815 | |
| 8816 | if (PyTuple_Check(args)) { |
| 8817 | arglen = PyTuple_Size(args); |
| 8818 | argidx = 0; |
| 8819 | } |
| 8820 | else { |
| 8821 | arglen = -1; |
| 8822 | argidx = -2; |
| 8823 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8824 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 8825 | !PyUnicode_Check(args)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8826 | dict = args; |
| 8827 | |
| 8828 | while (--fmtcnt >= 0) { |
| 8829 | if (*fmt != '%') { |
| 8830 | if (--rescnt < 0) { |
| 8831 | rescnt = fmtcnt + 100; |
| 8832 | reslen += rescnt; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8833 | if (_PyUnicode_Resize(&result, reslen) < 0) |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 8834 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8835 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 8836 | --rescnt; |
| 8837 | } |
| 8838 | *res++ = *fmt++; |
| 8839 | } |
| 8840 | else { |
| 8841 | /* Got a format specifier */ |
| 8842 | int flags = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8843 | Py_ssize_t width = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8844 | int prec = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8845 | Py_UNICODE c = '\0'; |
| 8846 | Py_UNICODE fill; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 8847 | int isnumok; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8848 | PyObject *v = NULL; |
| 8849 | PyObject *temp = NULL; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8850 | Py_UNICODE *pbuf; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8851 | Py_UNICODE sign; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8852 | Py_ssize_t len; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8853 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8854 | |
| 8855 | fmt++; |
| 8856 | if (*fmt == '(') { |
| 8857 | Py_UNICODE *keystart; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8858 | Py_ssize_t keylen; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8859 | PyObject *key; |
| 8860 | int pcount = 1; |
| 8861 | |
| 8862 | if (dict == NULL) { |
| 8863 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8864 | "format requires a mapping"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8865 | goto onError; |
| 8866 | } |
| 8867 | ++fmt; |
| 8868 | --fmtcnt; |
| 8869 | keystart = fmt; |
| 8870 | /* Skip over balanced parentheses */ |
| 8871 | while (pcount > 0 && --fmtcnt >= 0) { |
| 8872 | if (*fmt == ')') |
| 8873 | --pcount; |
| 8874 | else if (*fmt == '(') |
| 8875 | ++pcount; |
| 8876 | fmt++; |
| 8877 | } |
| 8878 | keylen = fmt - keystart - 1; |
| 8879 | if (fmtcnt < 0 || pcount > 0) { |
| 8880 | PyErr_SetString(PyExc_ValueError, |
| 8881 | "incomplete format key"); |
| 8882 | goto onError; |
| 8883 | } |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 8884 | #if 0 |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8885 | /* keys are converted to strings using UTF-8 and |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8886 | then looked up since Python uses strings to hold |
| 8887 | variables names etc. in its namespaces and we |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8888 | wouldn't want to break common idioms. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8889 | key = PyUnicode_EncodeUTF8(keystart, |
| 8890 | keylen, |
| 8891 | NULL); |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 8892 | #else |
| 8893 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 8894 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8895 | if (key == NULL) |
| 8896 | goto onError; |
| 8897 | if (args_owned) { |
| 8898 | Py_DECREF(args); |
| 8899 | args_owned = 0; |
| 8900 | } |
| 8901 | args = PyObject_GetItem(dict, key); |
| 8902 | Py_DECREF(key); |
| 8903 | if (args == NULL) { |
| 8904 | goto onError; |
| 8905 | } |
| 8906 | args_owned = 1; |
| 8907 | arglen = -1; |
| 8908 | argidx = -2; |
| 8909 | } |
| 8910 | while (--fmtcnt >= 0) { |
| 8911 | switch (c = *fmt++) { |
| 8912 | case '-': flags |= F_LJUST; continue; |
| 8913 | case '+': flags |= F_SIGN; continue; |
| 8914 | case ' ': flags |= F_BLANK; continue; |
| 8915 | case '#': flags |= F_ALT; continue; |
| 8916 | case '0': flags |= F_ZERO; continue; |
| 8917 | } |
| 8918 | break; |
| 8919 | } |
| 8920 | if (c == '*') { |
| 8921 | v = getnextarg(args, arglen, &argidx); |
| 8922 | if (v == NULL) |
| 8923 | goto onError; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8924 | if (!PyLong_Check(v)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8925 | PyErr_SetString(PyExc_TypeError, |
| 8926 | "* wants int"); |
| 8927 | goto onError; |
| 8928 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8929 | width = PyLong_AsLong(v); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 8930 | if (width == -1 && PyErr_Occurred()) |
| 8931 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8932 | if (width < 0) { |
| 8933 | flags |= F_LJUST; |
| 8934 | width = -width; |
| 8935 | } |
| 8936 | if (--fmtcnt >= 0) |
| 8937 | c = *fmt++; |
| 8938 | } |
| 8939 | else if (c >= '0' && c <= '9') { |
| 8940 | width = c - '0'; |
| 8941 | while (--fmtcnt >= 0) { |
| 8942 | c = *fmt++; |
| 8943 | if (c < '0' || c > '9') |
| 8944 | break; |
| 8945 | if ((width*10) / 10 != width) { |
| 8946 | PyErr_SetString(PyExc_ValueError, |
| 8947 | "width too big"); |
| 8948 | goto onError; |
| 8949 | } |
| 8950 | width = width*10 + (c - '0'); |
| 8951 | } |
| 8952 | } |
| 8953 | if (c == '.') { |
| 8954 | prec = 0; |
| 8955 | if (--fmtcnt >= 0) |
| 8956 | c = *fmt++; |
| 8957 | if (c == '*') { |
| 8958 | v = getnextarg(args, arglen, &argidx); |
| 8959 | if (v == NULL) |
| 8960 | goto onError; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8961 | if (!PyLong_Check(v)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8962 | PyErr_SetString(PyExc_TypeError, |
| 8963 | "* wants int"); |
| 8964 | goto onError; |
| 8965 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8966 | prec = PyLong_AsLong(v); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 8967 | if (prec == -1 && PyErr_Occurred()) |
| 8968 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8969 | if (prec < 0) |
| 8970 | prec = 0; |
| 8971 | if (--fmtcnt >= 0) |
| 8972 | c = *fmt++; |
| 8973 | } |
| 8974 | else if (c >= '0' && c <= '9') { |
| 8975 | prec = c - '0'; |
| 8976 | while (--fmtcnt >= 0) { |
| 8977 | c = Py_CHARMASK(*fmt++); |
| 8978 | if (c < '0' || c > '9') |
| 8979 | break; |
| 8980 | if ((prec*10) / 10 != prec) { |
| 8981 | PyErr_SetString(PyExc_ValueError, |
| 8982 | "prec too big"); |
| 8983 | goto onError; |
| 8984 | } |
| 8985 | prec = prec*10 + (c - '0'); |
| 8986 | } |
| 8987 | } |
| 8988 | } /* prec */ |
| 8989 | if (fmtcnt >= 0) { |
| 8990 | if (c == 'h' || c == 'l' || c == 'L') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8991 | if (--fmtcnt >= 0) |
| 8992 | c = *fmt++; |
| 8993 | } |
| 8994 | } |
| 8995 | if (fmtcnt < 0) { |
| 8996 | PyErr_SetString(PyExc_ValueError, |
| 8997 | "incomplete format"); |
| 8998 | goto onError; |
| 8999 | } |
| 9000 | if (c != '%') { |
| 9001 | v = getnextarg(args, arglen, &argidx); |
| 9002 | if (v == NULL) |
| 9003 | goto onError; |
| 9004 | } |
| 9005 | sign = 0; |
| 9006 | fill = ' '; |
| 9007 | switch (c) { |
| 9008 | |
| 9009 | case '%': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9010 | pbuf = formatbuf; |
| 9011 | /* presume that buffer length is at least 1 */ |
| 9012 | pbuf[0] = '%'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9013 | len = 1; |
| 9014 | break; |
| 9015 | |
| 9016 | case 's': |
| 9017 | case 'r': |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 9018 | case 'a': |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9019 | if (PyUnicode_Check(v) && c == 's') { |
| 9020 | temp = v; |
| 9021 | Py_INCREF(temp); |
| 9022 | } |
| 9023 | else { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9024 | if (c == 's') |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 9025 | temp = PyObject_Str(v); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 9026 | else if (c == 'r') |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9027 | temp = PyObject_Repr(v); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 9028 | else |
| 9029 | temp = PyObject_ASCII(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9030 | if (temp == NULL) |
| 9031 | goto onError; |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 9032 | if (PyUnicode_Check(temp)) |
| 9033 | /* nothing to do */; |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 9034 | else { |
| 9035 | Py_DECREF(temp); |
| 9036 | PyErr_SetString(PyExc_TypeError, |
| 9037 | "%s argument has non-string str()"); |
| 9038 | goto onError; |
| 9039 | } |
| 9040 | } |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9041 | pbuf = PyUnicode_AS_UNICODE(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9042 | len = PyUnicode_GET_SIZE(temp); |
| 9043 | if (prec >= 0 && len > prec) |
| 9044 | len = prec; |
| 9045 | break; |
| 9046 | |
| 9047 | case 'i': |
| 9048 | case 'd': |
| 9049 | case 'u': |
| 9050 | case 'o': |
| 9051 | case 'x': |
| 9052 | case 'X': |
| 9053 | if (c == 'i') |
| 9054 | c = 'd'; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 9055 | isnumok = 0; |
| 9056 | if (PyNumber_Check(v)) { |
| 9057 | PyObject *iobj=NULL; |
| 9058 | |
| 9059 | if (PyLong_Check(v)) { |
| 9060 | iobj = v; |
| 9061 | Py_INCREF(iobj); |
| 9062 | } |
| 9063 | else { |
| 9064 | iobj = PyNumber_Long(v); |
| 9065 | } |
| 9066 | if (iobj!=NULL) { |
| 9067 | if (PyLong_Check(iobj)) { |
| 9068 | isnumok = 1; |
| 9069 | temp = formatlong(iobj, flags, prec, c); |
| 9070 | Py_DECREF(iobj); |
| 9071 | if (!temp) |
| 9072 | goto onError; |
| 9073 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9074 | len = PyUnicode_GET_SIZE(temp); |
| 9075 | sign = 1; |
| 9076 | } |
| 9077 | else { |
| 9078 | Py_DECREF(iobj); |
| 9079 | } |
| 9080 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9081 | } |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 9082 | if (!isnumok) { |
| 9083 | PyErr_Format(PyExc_TypeError, |
| 9084 | "%%%c format: a number is required, " |
Martin v. Löwis | 5a6f458 | 2008-04-07 03:22:07 +0000 | [diff] [blame] | 9085 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9086 | goto onError; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9087 | } |
| 9088 | if (flags & F_ZERO) |
| 9089 | fill = '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9090 | break; |
| 9091 | |
| 9092 | case 'e': |
| 9093 | case 'E': |
| 9094 | case 'f': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 9095 | case 'F': |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9096 | case 'g': |
| 9097 | case 'G': |
Eric Smith | 22b85b3 | 2008-07-17 19:18:29 +0000 | [diff] [blame] | 9098 | if (c == 'F') |
| 9099 | c = 'f'; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9100 | pbuf = formatbuf; |
| 9101 | len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 9102 | flags, prec, c, v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9103 | if (len < 0) |
| 9104 | goto onError; |
| 9105 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9106 | if (flags & F_ZERO) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9107 | fill = '0'; |
| 9108 | break; |
| 9109 | |
| 9110 | case 'c': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9111 | pbuf = formatbuf; |
| 9112 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9113 | if (len < 0) |
| 9114 | goto onError; |
| 9115 | break; |
| 9116 | |
| 9117 | default: |
| 9118 | PyErr_Format(PyExc_ValueError, |
Andrew M. Kuchling | 6ca8917 | 2000-12-15 13:07:46 +0000 | [diff] [blame] | 9119 | "unsupported format character '%c' (0x%x) " |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 9120 | "at index %zd", |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9121 | (31<=c && c<=126) ? (char)c : '?', |
Marc-André Lemburg | 24e53b6 | 2002-09-24 09:32:14 +0000 | [diff] [blame] | 9122 | (int)c, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 9123 | (Py_ssize_t)(fmt - 1 - |
| 9124 | PyUnicode_AS_UNICODE(uformat))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9125 | goto onError; |
| 9126 | } |
| 9127 | if (sign) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9128 | if (*pbuf == '-' || *pbuf == '+') { |
| 9129 | sign = *pbuf++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9130 | len--; |
| 9131 | } |
| 9132 | else if (flags & F_SIGN) |
| 9133 | sign = '+'; |
| 9134 | else if (flags & F_BLANK) |
| 9135 | sign = ' '; |
| 9136 | else |
| 9137 | sign = 0; |
| 9138 | } |
| 9139 | if (width < len) |
| 9140 | width = len; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 9141 | if (rescnt - (sign != 0) < width) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9142 | reslen -= rescnt; |
| 9143 | rescnt = width + fmtcnt + 100; |
| 9144 | reslen += rescnt; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 9145 | if (reslen < 0) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 9146 | Py_XDECREF(temp); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9147 | PyErr_NoMemory(); |
| 9148 | goto onError; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 9149 | } |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9150 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 9151 | Py_XDECREF(temp); |
| 9152 | goto onError; |
| 9153 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9154 | res = PyUnicode_AS_UNICODE(result) |
| 9155 | + reslen - rescnt; |
| 9156 | } |
| 9157 | if (sign) { |
| 9158 | if (fill != ' ') |
| 9159 | *res++ = sign; |
| 9160 | rescnt--; |
| 9161 | if (width > len) |
| 9162 | width--; |
| 9163 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 9164 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9165 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 9166 | assert(pbuf[1] == c); |
| 9167 | if (fill != ' ') { |
| 9168 | *res++ = *pbuf++; |
| 9169 | *res++ = *pbuf++; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9170 | } |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 9171 | rescnt -= 2; |
| 9172 | width -= 2; |
| 9173 | if (width < 0) |
| 9174 | width = 0; |
| 9175 | len -= 2; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9176 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9177 | if (width > len && !(flags & F_LJUST)) { |
| 9178 | do { |
| 9179 | --rescnt; |
| 9180 | *res++ = fill; |
| 9181 | } while (--width > len); |
| 9182 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9183 | if (fill == ' ') { |
| 9184 | if (sign) |
| 9185 | *res++ = sign; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 9186 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9187 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 9188 | assert(pbuf[1] == c); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9189 | *res++ = *pbuf++; |
| 9190 | *res++ = *pbuf++; |
| 9191 | } |
| 9192 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9193 | Py_UNICODE_COPY(res, pbuf, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9194 | res += len; |
| 9195 | rescnt -= len; |
| 9196 | while (--width >= len) { |
| 9197 | --rescnt; |
| 9198 | *res++ = ' '; |
| 9199 | } |
| 9200 | if (dict && (argidx < arglen) && c != '%') { |
| 9201 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 9202 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9203 | Py_XDECREF(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9204 | goto onError; |
| 9205 | } |
| 9206 | Py_XDECREF(temp); |
| 9207 | } /* '%' */ |
| 9208 | } /* until end */ |
| 9209 | if (argidx < arglen && !dict) { |
| 9210 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 9211 | "not all arguments converted during string formatting"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9212 | goto onError; |
| 9213 | } |
| 9214 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9215 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
| 9216 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9217 | if (args_owned) { |
| 9218 | Py_DECREF(args); |
| 9219 | } |
| 9220 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9221 | return (PyObject *)result; |
| 9222 | |
| 9223 | onError: |
| 9224 | Py_XDECREF(result); |
| 9225 | Py_DECREF(uformat); |
| 9226 | if (args_owned) { |
| 9227 | Py_DECREF(args); |
| 9228 | } |
| 9229 | return NULL; |
| 9230 | } |
| 9231 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 9232 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9233 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 9234 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9235 | static PyObject * |
| 9236 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9237 | { |
| 9238 | PyObject *x = NULL; |
Guido van Rossum | 55b4a7b | 2007-07-11 09:28:11 +0000 | [diff] [blame] | 9239 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9240 | char *encoding = NULL; |
| 9241 | char *errors = NULL; |
| 9242 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9243 | if (type != &PyUnicode_Type) |
| 9244 | return unicode_subtype_new(type, args, kwds); |
Alexandre Vassalotti | 999679a | 2008-05-03 04:42:16 +0000 | [diff] [blame] | 9245 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9246 | kwlist, &x, &encoding, &errors)) |
| 9247 | return NULL; |
| 9248 | if (x == NULL) |
| 9249 | return (PyObject *)_PyUnicode_New(0); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 9250 | if (encoding == NULL && errors == NULL) |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 9251 | return PyObject_Str(x); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 9252 | else |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9253 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
| 9254 | } |
| 9255 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9256 | static PyObject * |
| 9257 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9258 | { |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 9259 | PyUnicodeObject *tmp, *pnew; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9260 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9261 | |
| 9262 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 9263 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 9264 | if (tmp == NULL) |
| 9265 | return NULL; |
| 9266 | assert(PyUnicode_Check(tmp)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 9267 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 9268 | if (pnew == NULL) { |
| 9269 | Py_DECREF(tmp); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9270 | return NULL; |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 9271 | } |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 9272 | pnew->str = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 9273 | if (pnew->str == NULL) { |
| 9274 | _Py_ForgetReference((PyObject *)pnew); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 9275 | PyObject_Del(pnew); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 9276 | Py_DECREF(tmp); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 9277 | return PyErr_NoMemory(); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9278 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 9279 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 9280 | pnew->length = n; |
| 9281 | pnew->hash = tmp->hash; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9282 | Py_DECREF(tmp); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 9283 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9284 | } |
| 9285 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9286 | PyDoc_STRVAR(unicode_doc, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 9287 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9288 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 9289 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 9290 | encoding defaults to the current default string encoding.\n\ |
| 9291 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9292 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9293 | static PyObject *unicode_iter(PyObject *seq); |
| 9294 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9295 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 9296 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | 84fc66d | 2007-05-03 17:18:26 +0000 | [diff] [blame] | 9297 | "str", /* tp_name */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9298 | sizeof(PyUnicodeObject), /* tp_size */ |
| 9299 | 0, /* tp_itemsize */ |
| 9300 | /* Slots */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 9301 | (destructor)unicode_dealloc, /* tp_dealloc */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9302 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9303 | 0, /* tp_getattr */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9304 | 0, /* tp_setattr */ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9305 | 0, /* tp_compare */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9306 | unicode_repr, /* tp_repr */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 9307 | &unicode_as_number, /* tp_as_number */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9308 | &unicode_as_sequence, /* tp_as_sequence */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9309 | &unicode_as_mapping, /* tp_as_mapping */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9310 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 9311 | 0, /* tp_call*/ |
| 9312 | (reprfunc) unicode_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9313 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9314 | 0, /* tp_setattro */ |
Alexandre Vassalotti | 70a2371 | 2007-10-14 02:05:51 +0000 | [diff] [blame] | 9315 | 0, /* tp_as_buffer */ |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 9316 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 9317 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9318 | unicode_doc, /* tp_doc */ |
| 9319 | 0, /* tp_traverse */ |
| 9320 | 0, /* tp_clear */ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9321 | PyUnicode_RichCompare, /* tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9322 | 0, /* tp_weaklistoffset */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9323 | unicode_iter, /* tp_iter */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9324 | 0, /* tp_iternext */ |
| 9325 | unicode_methods, /* tp_methods */ |
| 9326 | 0, /* tp_members */ |
| 9327 | 0, /* tp_getset */ |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 9328 | &PyBaseObject_Type, /* tp_base */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9329 | 0, /* tp_dict */ |
| 9330 | 0, /* tp_descr_get */ |
| 9331 | 0, /* tp_descr_set */ |
| 9332 | 0, /* tp_dictoffset */ |
| 9333 | 0, /* tp_init */ |
| 9334 | 0, /* tp_alloc */ |
| 9335 | unicode_new, /* tp_new */ |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 9336 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9337 | }; |
| 9338 | |
| 9339 | /* Initialize the Unicode implementation */ |
| 9340 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9341 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9342 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9343 | int i; |
| 9344 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9345 | /* XXX - move this array to unicodectype.c ? */ |
| 9346 | Py_UNICODE linebreak[] = { |
| 9347 | 0x000A, /* LINE FEED */ |
| 9348 | 0x000D, /* CARRIAGE RETURN */ |
| 9349 | 0x001C, /* FILE SEPARATOR */ |
| 9350 | 0x001D, /* GROUP SEPARATOR */ |
| 9351 | 0x001E, /* RECORD SEPARATOR */ |
| 9352 | 0x0085, /* NEXT LINE */ |
| 9353 | 0x2028, /* LINE SEPARATOR */ |
| 9354 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 9355 | }; |
| 9356 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 9357 | /* Init the implementation */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 9358 | free_list = NULL; |
| 9359 | numfree = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9360 | unicode_empty = _PyUnicode_New(0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9361 | if (!unicode_empty) |
| 9362 | return; |
| 9363 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9364 | for (i = 0; i < 256; i++) |
| 9365 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 9366 | if (PyType_Ready(&PyUnicode_Type) < 0) |
| 9367 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9368 | |
| 9369 | /* initialize the linebreak bloom filter */ |
| 9370 | bloom_linebreak = make_bloom_mask( |
| 9371 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 9372 | ); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9373 | |
| 9374 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9375 | } |
| 9376 | |
| 9377 | /* Finalize the Unicode implementation */ |
| 9378 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9379 | int |
| 9380 | PyUnicode_ClearFreeList(void) |
| 9381 | { |
| 9382 | int freelist_size = numfree; |
| 9383 | PyUnicodeObject *u; |
| 9384 | |
| 9385 | for (u = free_list; u != NULL;) { |
| 9386 | PyUnicodeObject *v = u; |
| 9387 | u = *(PyUnicodeObject **)u; |
| 9388 | if (v->str) |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 9389 | PyObject_DEL(v->str); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9390 | Py_XDECREF(v->defenc); |
| 9391 | PyObject_Del(v); |
| 9392 | numfree--; |
| 9393 | } |
| 9394 | free_list = NULL; |
| 9395 | assert(numfree == 0); |
| 9396 | return freelist_size; |
| 9397 | } |
| 9398 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9399 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9400 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9401 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9402 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9403 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 9404 | Py_XDECREF(unicode_empty); |
| 9405 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 9406 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9407 | for (i = 0; i < 256; i++) { |
| 9408 | if (unicode_latin1[i]) { |
| 9409 | Py_DECREF(unicode_latin1[i]); |
| 9410 | unicode_latin1[i] = NULL; |
| 9411 | } |
| 9412 | } |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9413 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9414 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9415 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9416 | void |
| 9417 | PyUnicode_InternInPlace(PyObject **p) |
| 9418 | { |
| 9419 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 9420 | PyObject *t; |
| 9421 | if (s == NULL || !PyUnicode_Check(s)) |
| 9422 | Py_FatalError( |
| 9423 | "PyUnicode_InternInPlace: unicode strings only please!"); |
| 9424 | /* If it's a subclass, we don't really know what putting |
| 9425 | it in the interned dict might do. */ |
| 9426 | if (!PyUnicode_CheckExact(s)) |
| 9427 | return; |
| 9428 | if (PyUnicode_CHECK_INTERNED(s)) |
| 9429 | return; |
| 9430 | if (interned == NULL) { |
| 9431 | interned = PyDict_New(); |
| 9432 | if (interned == NULL) { |
| 9433 | PyErr_Clear(); /* Don't leave an exception */ |
| 9434 | return; |
| 9435 | } |
| 9436 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9437 | /* It might be that the GetItem call fails even |
| 9438 | though the key is present in the dictionary, |
| 9439 | namely when this happens during a stack overflow. */ |
| 9440 | Py_ALLOW_RECURSION |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9441 | t = PyDict_GetItem(interned, (PyObject *)s); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9442 | Py_END_ALLOW_RECURSION |
| 9443 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9444 | if (t) { |
| 9445 | Py_INCREF(t); |
| 9446 | Py_DECREF(*p); |
| 9447 | *p = t; |
| 9448 | return; |
| 9449 | } |
| 9450 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9451 | PyThreadState_GET()->recursion_critical = 1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9452 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 9453 | PyErr_Clear(); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9454 | PyThreadState_GET()->recursion_critical = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9455 | return; |
| 9456 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9457 | PyThreadState_GET()->recursion_critical = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9458 | /* The two references in interned are not counted by refcnt. |
| 9459 | The deallocator will take care of this */ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 9460 | Py_REFCNT(s) -= 2; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9461 | PyUnicode_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL; |
| 9462 | } |
| 9463 | |
| 9464 | void |
| 9465 | PyUnicode_InternImmortal(PyObject **p) |
| 9466 | { |
| 9467 | PyUnicode_InternInPlace(p); |
| 9468 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
| 9469 | PyUnicode_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL; |
| 9470 | Py_INCREF(*p); |
| 9471 | } |
| 9472 | } |
| 9473 | |
| 9474 | PyObject * |
| 9475 | PyUnicode_InternFromString(const char *cp) |
| 9476 | { |
| 9477 | PyObject *s = PyUnicode_FromString(cp); |
| 9478 | if (s == NULL) |
| 9479 | return NULL; |
| 9480 | PyUnicode_InternInPlace(&s); |
| 9481 | return s; |
| 9482 | } |
| 9483 | |
| 9484 | void _Py_ReleaseInternedUnicodeStrings(void) |
| 9485 | { |
| 9486 | PyObject *keys; |
| 9487 | PyUnicodeObject *s; |
| 9488 | Py_ssize_t i, n; |
| 9489 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
| 9490 | |
| 9491 | if (interned == NULL || !PyDict_Check(interned)) |
| 9492 | return; |
| 9493 | keys = PyDict_Keys(interned); |
| 9494 | if (keys == NULL || !PyList_Check(keys)) { |
| 9495 | PyErr_Clear(); |
| 9496 | return; |
| 9497 | } |
| 9498 | |
| 9499 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 9500 | detector, interned unicode strings are not forcibly deallocated; |
| 9501 | rather, we give them their stolen references back, and then clear |
| 9502 | and DECREF the interned dict. */ |
| 9503 | |
| 9504 | n = PyList_GET_SIZE(keys); |
| 9505 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
| 9506 | n); |
| 9507 | for (i = 0; i < n; i++) { |
| 9508 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
| 9509 | switch (s->state) { |
| 9510 | case SSTATE_NOT_INTERNED: |
| 9511 | /* XXX Shouldn't happen */ |
| 9512 | break; |
| 9513 | case SSTATE_INTERNED_IMMORTAL: |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 9514 | Py_REFCNT(s) += 1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9515 | immortal_size += s->length; |
| 9516 | break; |
| 9517 | case SSTATE_INTERNED_MORTAL: |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 9518 | Py_REFCNT(s) += 2; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9519 | mortal_size += s->length; |
| 9520 | break; |
| 9521 | default: |
| 9522 | Py_FatalError("Inconsistent interned string state."); |
| 9523 | } |
| 9524 | s->state = SSTATE_NOT_INTERNED; |
| 9525 | } |
| 9526 | fprintf(stderr, "total size of all interned strings: " |
| 9527 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 9528 | "mortal/immortal\n", mortal_size, immortal_size); |
| 9529 | Py_DECREF(keys); |
| 9530 | PyDict_Clear(interned); |
| 9531 | Py_DECREF(interned); |
| 9532 | interned = NULL; |
| 9533 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9534 | |
| 9535 | |
| 9536 | /********************* Unicode Iterator **************************/ |
| 9537 | |
| 9538 | typedef struct { |
| 9539 | PyObject_HEAD |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9540 | Py_ssize_t it_index; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9541 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
| 9542 | } unicodeiterobject; |
| 9543 | |
| 9544 | static void |
| 9545 | unicodeiter_dealloc(unicodeiterobject *it) |
| 9546 | { |
| 9547 | _PyObject_GC_UNTRACK(it); |
| 9548 | Py_XDECREF(it->it_seq); |
| 9549 | PyObject_GC_Del(it); |
| 9550 | } |
| 9551 | |
| 9552 | static int |
| 9553 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 9554 | { |
| 9555 | Py_VISIT(it->it_seq); |
| 9556 | return 0; |
| 9557 | } |
| 9558 | |
| 9559 | static PyObject * |
| 9560 | unicodeiter_next(unicodeiterobject *it) |
| 9561 | { |
| 9562 | PyUnicodeObject *seq; |
| 9563 | PyObject *item; |
| 9564 | |
| 9565 | assert(it != NULL); |
| 9566 | seq = it->it_seq; |
| 9567 | if (seq == NULL) |
| 9568 | return NULL; |
| 9569 | assert(PyUnicode_Check(seq)); |
| 9570 | |
| 9571 | if (it->it_index < PyUnicode_GET_SIZE(seq)) { |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9572 | item = PyUnicode_FromUnicode( |
| 9573 | PyUnicode_AS_UNICODE(seq)+it->it_index, 1); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9574 | if (item != NULL) |
| 9575 | ++it->it_index; |
| 9576 | return item; |
| 9577 | } |
| 9578 | |
| 9579 | Py_DECREF(seq); |
| 9580 | it->it_seq = NULL; |
| 9581 | return NULL; |
| 9582 | } |
| 9583 | |
| 9584 | static PyObject * |
| 9585 | unicodeiter_len(unicodeiterobject *it) |
| 9586 | { |
| 9587 | Py_ssize_t len = 0; |
| 9588 | if (it->it_seq) |
| 9589 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9590 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9591 | } |
| 9592 | |
| 9593 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 9594 | |
| 9595 | static PyMethodDef unicodeiter_methods[] = { |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9596 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
| 9597 | length_hint_doc}, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9598 | {NULL, NULL} /* sentinel */ |
| 9599 | }; |
| 9600 | |
| 9601 | PyTypeObject PyUnicodeIter_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 9602 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Christian Heimes | f83be4e | 2007-11-28 09:44:38 +0000 | [diff] [blame] | 9603 | "str_iterator", /* tp_name */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9604 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 9605 | 0, /* tp_itemsize */ |
| 9606 | /* methods */ |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9607 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9608 | 0, /* tp_print */ |
| 9609 | 0, /* tp_getattr */ |
| 9610 | 0, /* tp_setattr */ |
| 9611 | 0, /* tp_compare */ |
| 9612 | 0, /* tp_repr */ |
| 9613 | 0, /* tp_as_number */ |
| 9614 | 0, /* tp_as_sequence */ |
| 9615 | 0, /* tp_as_mapping */ |
| 9616 | 0, /* tp_hash */ |
| 9617 | 0, /* tp_call */ |
| 9618 | 0, /* tp_str */ |
| 9619 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9620 | 0, /* tp_setattro */ |
| 9621 | 0, /* tp_as_buffer */ |
| 9622 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 9623 | 0, /* tp_doc */ |
| 9624 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 9625 | 0, /* tp_clear */ |
| 9626 | 0, /* tp_richcompare */ |
| 9627 | 0, /* tp_weaklistoffset */ |
| 9628 | PyObject_SelfIter, /* tp_iter */ |
| 9629 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 9630 | unicodeiter_methods, /* tp_methods */ |
| 9631 | 0, |
| 9632 | }; |
| 9633 | |
| 9634 | static PyObject * |
| 9635 | unicode_iter(PyObject *seq) |
| 9636 | { |
| 9637 | unicodeiterobject *it; |
| 9638 | |
| 9639 | if (!PyUnicode_Check(seq)) { |
| 9640 | PyErr_BadInternalCall(); |
| 9641 | return NULL; |
| 9642 | } |
| 9643 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 9644 | if (it == NULL) |
| 9645 | return NULL; |
| 9646 | it->it_index = 0; |
| 9647 | Py_INCREF(seq); |
| 9648 | it->it_seq = (PyUnicodeObject *)seq; |
| 9649 | _PyObject_GC_TRACK(it); |
| 9650 | return (PyObject *)it; |
| 9651 | } |
| 9652 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9653 | size_t |
| 9654 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 9655 | { |
| 9656 | int res = 0; |
| 9657 | while(*u++) |
| 9658 | res++; |
| 9659 | return res; |
| 9660 | } |
| 9661 | |
| 9662 | Py_UNICODE* |
| 9663 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9664 | { |
| 9665 | Py_UNICODE *u = s1; |
| 9666 | while ((*u++ = *s2++)); |
| 9667 | return s1; |
| 9668 | } |
| 9669 | |
| 9670 | Py_UNICODE* |
| 9671 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 9672 | { |
| 9673 | Py_UNICODE *u = s1; |
| 9674 | while ((*u++ = *s2++)) |
| 9675 | if (n-- == 0) |
| 9676 | break; |
| 9677 | return s1; |
| 9678 | } |
| 9679 | |
| 9680 | int |
| 9681 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9682 | { |
| 9683 | while (*s1 && *s2 && *s1 == *s2) |
| 9684 | s1++, s2++; |
| 9685 | if (*s1 && *s2) |
| 9686 | return (*s1 < *s2) ? -1 : +1; |
| 9687 | if (*s1) |
| 9688 | return 1; |
| 9689 | if (*s2) |
| 9690 | return -1; |
| 9691 | return 0; |
| 9692 | } |
| 9693 | |
| 9694 | Py_UNICODE* |
| 9695 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 9696 | { |
| 9697 | const Py_UNICODE *p; |
| 9698 | for (p = s; *p; p++) |
| 9699 | if (*p == c) |
| 9700 | return (Py_UNICODE*)p; |
| 9701 | return NULL; |
| 9702 | } |
| 9703 | |
| 9704 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9705 | #ifdef __cplusplus |
| 9706 | } |
| 9707 | #endif |
| 9708 | |
| 9709 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9710 | /* |
| 9711 | Local variables: |
| 9712 | c-basic-offset: 4 |
| 9713 | indent-tabs-mode: nil |
| 9714 | End: |
| 9715 | */ |