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 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 421 | int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 422 | { |
| 423 | register PyUnicodeObject *v; |
| 424 | |
| 425 | /* Argument checks */ |
| 426 | if (unicode == NULL) { |
| 427 | PyErr_BadInternalCall(); |
| 428 | return -1; |
| 429 | } |
| 430 | v = (PyUnicodeObject *)*unicode; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 431 | 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] | 432 | PyErr_BadInternalCall(); |
| 433 | return -1; |
| 434 | } |
| 435 | |
| 436 | /* Resizing unicode_empty and single character objects is not |
| 437 | possible since these are being shared. We simply return a fresh |
| 438 | copy with the same Unicode content. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 439 | if (v->length != length && |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 440 | (v == unicode_empty || v->length == 1)) { |
| 441 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 442 | if (w == NULL) |
| 443 | return -1; |
| 444 | Py_UNICODE_COPY(w->str, v->str, |
| 445 | length < v->length ? length : v->length); |
Raymond Hettinger | c8df578 | 2003-03-09 07:30:43 +0000 | [diff] [blame] | 446 | Py_DECREF(*unicode); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 447 | *unicode = (PyObject *)w; |
| 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 452 | objects, since we can modify them in-place. */ |
| 453 | return unicode_resize(v, length); |
| 454 | } |
| 455 | |
| 456 | /* Internal API for use in unicodeobject.c only ! */ |
| 457 | #define _PyUnicode_Resize(unicodevar, length) \ |
| 458 | PyUnicode_Resize(((PyObject **)(unicodevar)), length) |
| 459 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 460 | PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 461 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 462 | { |
| 463 | PyUnicodeObject *unicode; |
| 464 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 465 | /* If the Unicode data is known at construction time, we can apply |
| 466 | some optimizations which share commonly used objects. */ |
| 467 | if (u != NULL) { |
| 468 | |
| 469 | /* Optimization for empty strings */ |
| 470 | if (size == 0 && unicode_empty != NULL) { |
| 471 | Py_INCREF(unicode_empty); |
| 472 | return (PyObject *)unicode_empty; |
| 473 | } |
| 474 | |
| 475 | /* Single character Unicode objects in the Latin-1 range are |
| 476 | shared when using this constructor */ |
| 477 | if (size == 1 && *u < 256) { |
| 478 | unicode = unicode_latin1[*u]; |
| 479 | if (!unicode) { |
| 480 | unicode = _PyUnicode_New(1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 481 | if (!unicode) |
| 482 | return NULL; |
Marc-André Lemburg | 8879a33 | 2001-06-07 12:26:56 +0000 | [diff] [blame] | 483 | unicode->str[0] = *u; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 484 | unicode_latin1[*u] = unicode; |
| 485 | } |
| 486 | Py_INCREF(unicode); |
| 487 | return (PyObject *)unicode; |
| 488 | } |
| 489 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 490 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 491 | unicode = _PyUnicode_New(size); |
| 492 | if (!unicode) |
| 493 | return NULL; |
| 494 | |
| 495 | /* Copy the Unicode data into the new object */ |
| 496 | if (u != NULL) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 497 | Py_UNICODE_COPY(unicode->str, u, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 498 | |
| 499 | return (PyObject *)unicode; |
| 500 | } |
| 501 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 502 | PyObject *PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 503 | { |
| 504 | PyUnicodeObject *unicode; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 505 | |
| 506 | if (size < 0) { |
| 507 | PyErr_SetString(PyExc_SystemError, |
| 508 | "Negative size passed to PyUnicode_FromStringAndSize"); |
| 509 | return NULL; |
| 510 | } |
| 511 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 512 | /* 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] | 513 | some optimizations which share commonly used objects. |
| 514 | Also, this means the input must be UTF-8, so fall back to the |
| 515 | UTF-8 decoder at the end. */ |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 516 | if (u != NULL) { |
| 517 | |
| 518 | /* Optimization for empty strings */ |
| 519 | if (size == 0 && unicode_empty != NULL) { |
| 520 | Py_INCREF(unicode_empty); |
| 521 | return (PyObject *)unicode_empty; |
| 522 | } |
| 523 | |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 524 | /* Single characters are shared when using this constructor. |
| 525 | Restrict to ASCII, since the input must be UTF-8. */ |
| 526 | if (size == 1 && Py_CHARMASK(*u) < 128) { |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 527 | unicode = unicode_latin1[Py_CHARMASK(*u)]; |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 528 | if (!unicode) { |
| 529 | unicode = _PyUnicode_New(1); |
| 530 | if (!unicode) |
| 531 | return NULL; |
Guido van Rossum | 00058aa | 2007-07-19 18:21:28 +0000 | [diff] [blame] | 532 | unicode->str[0] = Py_CHARMASK(*u); |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 533 | unicode_latin1[Py_CHARMASK(*u)] = unicode; |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 534 | } |
| 535 | Py_INCREF(unicode); |
| 536 | return (PyObject *)unicode; |
| 537 | } |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 538 | |
| 539 | return PyUnicode_DecodeUTF8(u, size, NULL); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Walter Dörwald | 5550731 | 2007-05-18 13:12:10 +0000 | [diff] [blame] | 542 | unicode = _PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 543 | if (!unicode) |
| 544 | return NULL; |
| 545 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 546 | return (PyObject *)unicode; |
| 547 | } |
| 548 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 549 | PyObject *PyUnicode_FromString(const char *u) |
| 550 | { |
| 551 | size_t size = strlen(u); |
| 552 | if (size > PY_SSIZE_T_MAX) { |
| 553 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 554 | return NULL; |
| 555 | } |
| 556 | |
| 557 | return PyUnicode_FromStringAndSize(u, size); |
| 558 | } |
| 559 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 560 | #ifdef HAVE_WCHAR_H |
| 561 | |
| 562 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 563 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 564 | { |
| 565 | PyUnicodeObject *unicode; |
| 566 | |
| 567 | if (w == NULL) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 568 | if (size == 0) |
| 569 | return PyUnicode_FromStringAndSize(NULL, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 570 | PyErr_BadInternalCall(); |
| 571 | return NULL; |
| 572 | } |
| 573 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 574 | if (size == -1) { |
| 575 | size = wcslen(w); |
| 576 | } |
| 577 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 578 | unicode = _PyUnicode_New(size); |
| 579 | if (!unicode) |
| 580 | return NULL; |
| 581 | |
| 582 | /* Copy the wchar_t data into the new object */ |
| 583 | #ifdef HAVE_USABLE_WCHAR_T |
| 584 | memcpy(unicode->str, w, size * sizeof(wchar_t)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 585 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 586 | { |
| 587 | register Py_UNICODE *u; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 588 | register Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 589 | u = PyUnicode_AS_UNICODE(unicode); |
Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 590 | for (i = size; i > 0; i--) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 591 | *u++ = *w++; |
| 592 | } |
| 593 | #endif |
| 594 | |
| 595 | return (PyObject *)unicode; |
| 596 | } |
| 597 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 598 | static void |
| 599 | makefmt(char *fmt, int longflag, int size_tflag, int zeropad, int width, int precision, char c) |
| 600 | { |
| 601 | *fmt++ = '%'; |
| 602 | if (width) { |
| 603 | if (zeropad) |
| 604 | *fmt++ = '0'; |
| 605 | fmt += sprintf(fmt, "%d", width); |
| 606 | } |
| 607 | if (precision) |
| 608 | fmt += sprintf(fmt, ".%d", precision); |
| 609 | if (longflag) |
| 610 | *fmt++ = 'l'; |
| 611 | else if (size_tflag) { |
| 612 | char *f = PY_FORMAT_SIZE_T; |
| 613 | while (*f) |
| 614 | *fmt++ = *f++; |
| 615 | } |
| 616 | *fmt++ = c; |
| 617 | *fmt = '\0'; |
| 618 | } |
| 619 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 620 | #define appendstring(string) {for (copy = string;*copy;) *s++ = *copy++;} |
| 621 | |
| 622 | PyObject * |
| 623 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 624 | { |
| 625 | va_list count; |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 626 | Py_ssize_t callcount = 0; |
| 627 | PyObject **callresults = NULL; |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 628 | PyObject **callresult = NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 629 | Py_ssize_t n = 0; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 630 | int width = 0; |
| 631 | int precision = 0; |
| 632 | int zeropad; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 633 | const char* f; |
| 634 | Py_UNICODE *s; |
| 635 | PyObject *string; |
| 636 | /* used by sprintf */ |
| 637 | char buffer[21]; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 638 | /* use abuffer instead of buffer, if we need more space |
| 639 | * (which can happen if there's a format specifier with width). */ |
| 640 | char *abuffer = NULL; |
| 641 | char *realbuffer; |
| 642 | Py_ssize_t abuffersize = 0; |
| 643 | char fmt[60]; /* should be enough for %0width.precisionld */ |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 644 | const char *copy; |
| 645 | |
| 646 | #ifdef VA_LIST_IS_ARRAY |
| 647 | Py_MEMCPY(count, vargs, sizeof(va_list)); |
| 648 | #else |
| 649 | #ifdef __va_copy |
| 650 | __va_copy(count, vargs); |
| 651 | #else |
| 652 | count = vargs; |
| 653 | #endif |
| 654 | #endif |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 655 | /* step 1: count the number of %S/%R/%A format specifications |
| 656 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII() for |
| 657 | * these objects once during step 3 and put the result in |
| 658 | an array) */ |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 659 | for (f = format; *f; f++) { |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 660 | if (*f == '%' && (*(f+1)=='S' || *(f+1)=='R' || *(f+1)=='A')) |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 661 | ++callcount; |
| 662 | } |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 663 | /* step 2: allocate memory for the results of |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 664 | * PyObject_Str()/PyObject_Repr() calls */ |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 665 | if (callcount) { |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 666 | callresults = PyObject_Malloc(sizeof(PyObject *)*callcount); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 667 | if (!callresults) { |
| 668 | PyErr_NoMemory(); |
| 669 | return NULL; |
| 670 | } |
| 671 | callresult = callresults; |
| 672 | } |
| 673 | /* step 3: figure out how large a buffer we need */ |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 674 | for (f = format; *f; f++) { |
| 675 | if (*f == '%') { |
| 676 | const char* p = f; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 677 | width = 0; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 678 | while (ISDIGIT((unsigned)*f)) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 679 | width = (width*10) + *f++ - '0'; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 680 | while (*++f && *f != '%' && !ISALPHA((unsigned)*f)) |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 681 | ; |
| 682 | |
| 683 | /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
| 684 | * they don't affect the amount of space we reserve. |
| 685 | */ |
| 686 | if ((*f == 'l' || *f == 'z') && |
| 687 | (f[1] == 'd' || f[1] == 'u')) |
Eric Smith | ddd2582 | 2007-08-27 11:33:42 +0000 | [diff] [blame] | 688 | ++f; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 689 | |
| 690 | switch (*f) { |
| 691 | case 'c': |
| 692 | (void)va_arg(count, int); |
| 693 | /* fall through... */ |
| 694 | case '%': |
| 695 | n++; |
| 696 | break; |
| 697 | case 'd': case 'u': case 'i': case 'x': |
| 698 | (void) va_arg(count, int); |
| 699 | /* 20 bytes is enough to hold a 64-bit |
| 700 | integer. Decimal takes the most space. |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 701 | This isn't enough for octal. |
| 702 | If a width is specified we need more |
| 703 | (which we allocate later). */ |
| 704 | if (width < 20) |
| 705 | width = 20; |
| 706 | n += width; |
| 707 | if (abuffersize < width) |
| 708 | abuffersize = width; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 709 | break; |
| 710 | case 's': |
Martin v. Löwis | 90d1fcd | 2007-08-31 11:01:23 +0000 | [diff] [blame] | 711 | { |
| 712 | /* UTF-8 */ |
| 713 | unsigned char*s; |
| 714 | s = va_arg(count, unsigned char*); |
| 715 | while (*s) { |
| 716 | if (*s < 128) { |
| 717 | n++; s++; |
| 718 | } else if (*s < 0xc0) { |
| 719 | /* invalid UTF-8 */ |
| 720 | n++; s++; |
| 721 | } else if (*s < 0xc0) { |
| 722 | n++; |
| 723 | s++; if(!*s)break; |
| 724 | s++; |
| 725 | } else if (*s < 0xe0) { |
| 726 | n++; |
| 727 | s++; if(!*s)break; |
| 728 | s++; if(!*s)break; |
| 729 | s++; |
| 730 | } else { |
| 731 | #ifdef Py_UNICODE_WIDE |
| 732 | n++; |
| 733 | #else |
| 734 | n+=2; |
| 735 | #endif |
| 736 | s++; if(!*s)break; |
| 737 | s++; if(!*s)break; |
| 738 | s++; if(!*s)break; |
| 739 | s++; |
| 740 | } |
| 741 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 742 | break; |
Martin v. Löwis | 90d1fcd | 2007-08-31 11:01:23 +0000 | [diff] [blame] | 743 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 744 | case 'U': |
| 745 | { |
| 746 | PyObject *obj = va_arg(count, PyObject *); |
| 747 | assert(obj && PyUnicode_Check(obj)); |
| 748 | n += PyUnicode_GET_SIZE(obj); |
| 749 | break; |
| 750 | } |
Walter Dörwald | d7fb764 | 2007-06-11 16:36:59 +0000 | [diff] [blame] | 751 | case 'V': |
| 752 | { |
| 753 | PyObject *obj = va_arg(count, PyObject *); |
| 754 | const char *str = va_arg(count, const char *); |
| 755 | assert(obj || str); |
| 756 | assert(!obj || PyUnicode_Check(obj)); |
| 757 | if (obj) |
| 758 | n += PyUnicode_GET_SIZE(obj); |
| 759 | else |
| 760 | n += strlen(str); |
| 761 | break; |
| 762 | } |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 763 | case 'S': |
| 764 | { |
| 765 | PyObject *obj = va_arg(count, PyObject *); |
| 766 | PyObject *str; |
| 767 | assert(obj); |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 768 | str = PyObject_Str(obj); |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 769 | if (!str) |
| 770 | goto fail; |
| 771 | n += PyUnicode_GET_SIZE(str); |
| 772 | /* Remember the str and switch to the next slot */ |
| 773 | *callresult++ = str; |
| 774 | break; |
| 775 | } |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 776 | case 'R': |
| 777 | { |
| 778 | PyObject *obj = va_arg(count, PyObject *); |
| 779 | PyObject *repr; |
| 780 | assert(obj); |
| 781 | repr = PyObject_Repr(obj); |
| 782 | if (!repr) |
| 783 | goto fail; |
| 784 | n += PyUnicode_GET_SIZE(repr); |
| 785 | /* Remember the repr and switch to the next slot */ |
| 786 | *callresult++ = repr; |
| 787 | break; |
| 788 | } |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 789 | case 'A': |
| 790 | { |
| 791 | PyObject *obj = va_arg(count, PyObject *); |
| 792 | PyObject *ascii; |
| 793 | assert(obj); |
| 794 | ascii = PyObject_ASCII(obj); |
| 795 | if (!ascii) |
| 796 | goto fail; |
| 797 | n += PyUnicode_GET_SIZE(ascii); |
| 798 | /* Remember the repr and switch to the next slot */ |
| 799 | *callresult++ = ascii; |
| 800 | break; |
| 801 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 802 | case 'p': |
| 803 | (void) va_arg(count, int); |
| 804 | /* maximum 64-bit pointer representation: |
| 805 | * 0xffffffffffffffff |
| 806 | * so 19 characters is enough. |
| 807 | * XXX I count 18 -- what's the extra for? |
| 808 | */ |
| 809 | n += 19; |
| 810 | break; |
| 811 | default: |
| 812 | /* if we stumble upon an unknown |
| 813 | formatting code, copy the rest of |
| 814 | the format string to the output |
| 815 | string. (we cannot just skip the |
| 816 | code, since there's no way to know |
| 817 | what's in the argument list) */ |
| 818 | n += strlen(p); |
| 819 | goto expand; |
| 820 | } |
| 821 | } else |
| 822 | n++; |
| 823 | } |
| 824 | expand: |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 825 | if (abuffersize > 20) { |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 826 | abuffer = PyObject_Malloc(abuffersize); |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 827 | if (!abuffer) { |
| 828 | PyErr_NoMemory(); |
| 829 | goto fail; |
| 830 | } |
| 831 | realbuffer = abuffer; |
| 832 | } |
| 833 | else |
| 834 | realbuffer = buffer; |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 835 | /* step 4: fill the buffer */ |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 836 | /* 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] | 837 | we don't have to resize the string. |
| 838 | There can be no errors beyond this point. */ |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 839 | string = PyUnicode_FromUnicode(NULL, n); |
| 840 | if (!string) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 841 | goto fail; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 842 | |
| 843 | s = PyUnicode_AS_UNICODE(string); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 844 | callresult = callresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 845 | |
| 846 | for (f = format; *f; f++) { |
| 847 | if (*f == '%') { |
| 848 | const char* p = f++; |
| 849 | int longflag = 0; |
| 850 | int size_tflag = 0; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 851 | zeropad = (*f == '0'); |
| 852 | /* parse the width.precision part */ |
| 853 | width = 0; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 854 | while (ISDIGIT((unsigned)*f)) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 855 | width = (width*10) + *f++ - '0'; |
| 856 | precision = 0; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 857 | if (*f == '.') { |
| 858 | f++; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 859 | while (ISDIGIT((unsigned)*f)) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 860 | precision = (precision*10) + *f++ - '0'; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 861 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 862 | /* handle the long flag, but only for %ld and %lu. |
| 863 | others can be added when necessary. */ |
| 864 | if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) { |
| 865 | longflag = 1; |
| 866 | ++f; |
| 867 | } |
| 868 | /* handle the size_t flag. */ |
| 869 | if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
| 870 | size_tflag = 1; |
| 871 | ++f; |
| 872 | } |
| 873 | |
| 874 | switch (*f) { |
| 875 | case 'c': |
| 876 | *s++ = va_arg(vargs, int); |
| 877 | break; |
| 878 | case 'd': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 879 | makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'd'); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 880 | if (longflag) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 881 | sprintf(realbuffer, fmt, va_arg(vargs, long)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 882 | else if (size_tflag) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 883 | sprintf(realbuffer, fmt, va_arg(vargs, Py_ssize_t)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 884 | else |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 885 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 886 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 887 | break; |
| 888 | case 'u': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 889 | makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'u'); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 890 | if (longflag) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 891 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned long)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 892 | else if (size_tflag) |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 893 | sprintf(realbuffer, fmt, va_arg(vargs, size_t)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 894 | else |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 895 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned int)); |
| 896 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 897 | break; |
| 898 | case 'i': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 899 | makefmt(fmt, 0, 0, zeropad, width, precision, 'i'); |
| 900 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 901 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 902 | break; |
| 903 | case 'x': |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 904 | makefmt(fmt, 0, 0, zeropad, width, precision, 'x'); |
| 905 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 906 | appendstring(realbuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 907 | break; |
| 908 | case 's': |
Martin v. Löwis | 90d1fcd | 2007-08-31 11:01:23 +0000 | [diff] [blame] | 909 | { |
| 910 | /* Parameter must be UTF-8 encoded. |
| 911 | In case of encoding errors, use |
| 912 | the replacement character. */ |
| 913 | PyObject *u; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 914 | p = va_arg(vargs, char*); |
Martin v. Löwis | 90d1fcd | 2007-08-31 11:01:23 +0000 | [diff] [blame] | 915 | u = PyUnicode_DecodeUTF8(p, strlen(p), |
| 916 | "replace"); |
| 917 | if (!u) |
| 918 | goto fail; |
| 919 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(u), |
| 920 | PyUnicode_GET_SIZE(u)); |
| 921 | s += PyUnicode_GET_SIZE(u); |
| 922 | Py_DECREF(u); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 923 | break; |
Martin v. Löwis | 90d1fcd | 2007-08-31 11:01:23 +0000 | [diff] [blame] | 924 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 925 | case 'U': |
| 926 | { |
| 927 | PyObject *obj = va_arg(vargs, PyObject *); |
Walter Dörwald | 5c2fab6 | 2007-05-24 19:51:02 +0000 | [diff] [blame] | 928 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 929 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 930 | s += size; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 931 | break; |
| 932 | } |
Walter Dörwald | d7fb764 | 2007-06-11 16:36:59 +0000 | [diff] [blame] | 933 | case 'V': |
| 934 | { |
| 935 | PyObject *obj = va_arg(vargs, PyObject *); |
| 936 | const char *str = va_arg(vargs, const char *); |
| 937 | if (obj) { |
| 938 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 939 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 940 | s += size; |
| 941 | } else { |
| 942 | appendstring(str); |
| 943 | } |
| 944 | break; |
| 945 | } |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 946 | case 'S': |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 947 | case 'R': |
| 948 | { |
Guido van Rossum | 755114a | 2007-06-13 01:04:27 +0000 | [diff] [blame] | 949 | Py_UNICODE *ucopy; |
| 950 | Py_ssize_t usize; |
| 951 | Py_ssize_t upos; |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 952 | /* unused, since we already have the result */ |
| 953 | (void) va_arg(vargs, PyObject *); |
Guido van Rossum | 755114a | 2007-06-13 01:04:27 +0000 | [diff] [blame] | 954 | ucopy = PyUnicode_AS_UNICODE(*callresult); |
| 955 | usize = PyUnicode_GET_SIZE(*callresult); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 956 | for (upos = 0; upos<usize;) |
| 957 | *s++ = ucopy[upos++]; |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 958 | /* We're done with the unicode()/repr() => forget it */ |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 959 | Py_DECREF(*callresult); |
Walter Dörwald | 1be7e3f | 2007-05-23 21:02:42 +0000 | [diff] [blame] | 960 | /* switch to next unicode()/repr() result */ |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 961 | ++callresult; |
| 962 | break; |
| 963 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 964 | case 'p': |
| 965 | sprintf(buffer, "%p", va_arg(vargs, void*)); |
| 966 | /* %p is ill-defined: ensure leading 0x. */ |
| 967 | if (buffer[1] == 'X') |
| 968 | buffer[1] = 'x'; |
| 969 | else if (buffer[1] != 'x') { |
| 970 | memmove(buffer+2, buffer, strlen(buffer)+1); |
| 971 | buffer[0] = '0'; |
| 972 | buffer[1] = 'x'; |
| 973 | } |
| 974 | appendstring(buffer); |
| 975 | break; |
| 976 | case '%': |
| 977 | *s++ = '%'; |
| 978 | break; |
| 979 | default: |
| 980 | appendstring(p); |
| 981 | goto end; |
| 982 | } |
| 983 | } else |
| 984 | *s++ = *f; |
| 985 | } |
| 986 | |
| 987 | end: |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 988 | if (callresults) |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 989 | PyObject_Free(callresults); |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 990 | if (abuffer) |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 991 | PyObject_Free(abuffer); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 992 | _PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string)); |
| 993 | return string; |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 994 | fail: |
| 995 | if (callresults) { |
| 996 | PyObject **callresult2 = callresults; |
Guido van Rossum | 307fa8c | 2007-07-16 20:46:27 +0000 | [diff] [blame] | 997 | while (callresult2 < callresult) { |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 998 | Py_DECREF(*callresult2); |
| 999 | ++callresult2; |
| 1000 | } |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 1001 | PyObject_Free(callresults); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 1002 | } |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 1003 | if (abuffer) |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 1004 | PyObject_Free(abuffer); |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 1005 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | #undef appendstring |
| 1009 | |
| 1010 | PyObject * |
| 1011 | PyUnicode_FromFormat(const char *format, ...) |
| 1012 | { |
| 1013 | PyObject* ret; |
| 1014 | va_list vargs; |
| 1015 | |
| 1016 | #ifdef HAVE_STDARG_PROTOTYPES |
| 1017 | va_start(vargs, format); |
| 1018 | #else |
| 1019 | va_start(vargs); |
| 1020 | #endif |
| 1021 | ret = PyUnicode_FromFormatV(format, vargs); |
| 1022 | va_end(vargs); |
| 1023 | return ret; |
| 1024 | } |
| 1025 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1026 | Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
| 1027 | wchar_t *w, |
| 1028 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1029 | { |
| 1030 | if (unicode == NULL) { |
| 1031 | PyErr_BadInternalCall(); |
| 1032 | return -1; |
| 1033 | } |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1034 | |
| 1035 | /* If possible, try to copy the 0-termination as well */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1036 | if (size > PyUnicode_GET_SIZE(unicode)) |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1037 | size = PyUnicode_GET_SIZE(unicode) + 1; |
| 1038 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1039 | #ifdef HAVE_USABLE_WCHAR_T |
| 1040 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 1041 | #else |
| 1042 | { |
| 1043 | register Py_UNICODE *u; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1044 | register Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1045 | u = PyUnicode_AS_UNICODE(unicode); |
Marc-André Lemburg | 204bd6d | 2004-10-15 07:45:05 +0000 | [diff] [blame] | 1046 | for (i = size; i > 0; i--) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1047 | *w++ = *u++; |
| 1048 | } |
| 1049 | #endif |
| 1050 | |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1051 | if (size > PyUnicode_GET_SIZE(unicode)) |
| 1052 | return PyUnicode_GET_SIZE(unicode); |
| 1053 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1054 | return size; |
| 1055 | } |
| 1056 | |
| 1057 | #endif |
| 1058 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1059 | PyObject *PyUnicode_FromOrdinal(int ordinal) |
| 1060 | { |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1061 | Py_UNICODE s[2]; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1062 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1063 | if (ordinal < 0 || ordinal > 0x10ffff) { |
| 1064 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1065 | "chr() arg not in range(0x110000)"); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1066 | return NULL; |
| 1067 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1068 | |
| 1069 | #ifndef Py_UNICODE_WIDE |
| 1070 | if (ordinal > 0xffff) { |
| 1071 | ordinal -= 0x10000; |
| 1072 | s[0] = 0xD800 | (ordinal >> 10); |
| 1073 | s[1] = 0xDC00 | (ordinal & 0x3FF); |
| 1074 | return PyUnicode_FromUnicode(s, 2); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1075 | } |
| 1076 | #endif |
| 1077 | |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 1078 | s[0] = (Py_UNICODE)ordinal; |
| 1079 | return PyUnicode_FromUnicode(s, 1); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1082 | PyObject *PyUnicode_FromObject(register PyObject *obj) |
| 1083 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1084 | /* XXX Perhaps we should make this API an alias of |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 1085 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1086 | if (PyUnicode_CheckExact(obj)) { |
| 1087 | Py_INCREF(obj); |
| 1088 | return obj; |
| 1089 | } |
| 1090 | if (PyUnicode_Check(obj)) { |
| 1091 | /* For a Unicode subtype that's not a Unicode object, |
| 1092 | return a true Unicode object with the same data. */ |
| 1093 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 1094 | PyUnicode_GET_SIZE(obj)); |
| 1095 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1096 | PyErr_Format(PyExc_TypeError, |
| 1097 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1098 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1099 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
| 1103 | const char *encoding, |
| 1104 | const char *errors) |
| 1105 | { |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1106 | const char *s = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1107 | Py_ssize_t len; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1108 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1109 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1110 | if (obj == NULL) { |
| 1111 | PyErr_BadInternalCall(); |
| 1112 | return NULL; |
| 1113 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1114 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1115 | if (PyUnicode_Check(obj)) { |
| 1116 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1117 | "decoding str is not supported"); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1118 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1119 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1120 | |
| 1121 | /* Coerce object */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1122 | if (PyBytes_Check(obj)) { |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1123 | s = PyBytes_AS_STRING(obj); |
| 1124 | len = PyBytes_GET_SIZE(obj); |
| 1125 | } |
| 1126 | else if (PyByteArray_Check(obj)) { |
| 1127 | s = PyByteArray_AS_STRING(obj); |
| 1128 | len = PyByteArray_GET_SIZE(obj); |
| 1129 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1130 | else if (PyObject_AsCharBuffer(obj, &s, &len)) { |
| 1131 | /* Overwrite the error message with something more useful in |
| 1132 | case of a TypeError. */ |
| 1133 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1134 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1135 | "coercing to str: need string or buffer, " |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1136 | "%.80s found", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1137 | Py_TYPE(obj)->tp_name); |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1138 | goto onError; |
| 1139 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1140 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1141 | /* Convert to Unicode */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1142 | if (len == 0) { |
| 1143 | Py_INCREF(unicode_empty); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1144 | v = (PyObject *)unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1145 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1146 | else |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1147 | v = PyUnicode_Decode(s, len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 1148 | |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1149 | return v; |
| 1150 | |
| 1151 | onError: |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1152 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | PyObject *PyUnicode_Decode(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1156 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1157 | const char *encoding, |
| 1158 | const char *errors) |
| 1159 | { |
| 1160 | PyObject *buffer = NULL, *unicode; |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1161 | Py_buffer info; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1162 | char lower[20]; /* Enough for any encoding name we recognize */ |
| 1163 | char *l; |
| 1164 | const char *e; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1165 | |
| 1166 | if (encoding == NULL) |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1167 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1168 | |
| 1169 | /* Convert encoding to lower case and replace '_' with '-' in order to |
| 1170 | catch e.g. UTF_8 */ |
| 1171 | e = encoding; |
| 1172 | l = lower; |
| 1173 | while (*e && l < &lower[(sizeof lower) - 2]) { |
| 1174 | if (ISUPPER(*e)) { |
| 1175 | *l++ = TOLOWER(*e++); |
| 1176 | } |
| 1177 | else if (*e == '_') { |
| 1178 | *l++ = '-'; |
| 1179 | e++; |
| 1180 | } |
| 1181 | else { |
| 1182 | *l++ = *e++; |
| 1183 | } |
| 1184 | } |
| 1185 | *l = '\0'; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1186 | |
| 1187 | /* Shortcuts for common default encodings */ |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1188 | if (strcmp(lower, "utf-8") == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1189 | return PyUnicode_DecodeUTF8(s, size, errors); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1190 | else if ((strcmp(lower, "latin-1") == 0) || |
| 1191 | (strcmp(lower, "iso-8859-1") == 0)) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1192 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1193 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1194 | else if (strcmp(lower, "mbcs") == 0) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1195 | return PyUnicode_DecodeMBCS(s, size, errors); |
| 1196 | #endif |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1197 | else if (strcmp(lower, "ascii") == 0) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1198 | return PyUnicode_DecodeASCII(s, size, errors); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1199 | else if (strcmp(lower, "utf-16") == 0) |
| 1200 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 1201 | else if (strcmp(lower, "utf-32") == 0) |
| 1202 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1203 | |
| 1204 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1205 | buffer = NULL; |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1206 | 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] | 1207 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 1208 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1209 | if (buffer == NULL) |
| 1210 | goto onError; |
| 1211 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 1212 | if (unicode == NULL) |
| 1213 | goto onError; |
| 1214 | if (!PyUnicode_Check(unicode)) { |
| 1215 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1216 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1217 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1218 | Py_DECREF(unicode); |
| 1219 | goto onError; |
| 1220 | } |
| 1221 | Py_DECREF(buffer); |
| 1222 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1223 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1224 | onError: |
| 1225 | Py_XDECREF(buffer); |
| 1226 | return NULL; |
| 1227 | } |
| 1228 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1229 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 1230 | const char *encoding, |
| 1231 | const char *errors) |
| 1232 | { |
| 1233 | PyObject *v; |
| 1234 | |
| 1235 | if (!PyUnicode_Check(unicode)) { |
| 1236 | PyErr_BadArgument(); |
| 1237 | goto onError; |
| 1238 | } |
| 1239 | |
| 1240 | if (encoding == NULL) |
| 1241 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1242 | |
| 1243 | /* Decode via the codec registry */ |
| 1244 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1245 | if (v == NULL) |
| 1246 | goto onError; |
| 1247 | return v; |
| 1248 | |
| 1249 | onError: |
| 1250 | return NULL; |
| 1251 | } |
| 1252 | |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1253 | PyObject *PyUnicode_AsDecodedUnicode(PyObject *unicode, |
| 1254 | const char *encoding, |
| 1255 | const char *errors) |
| 1256 | { |
| 1257 | PyObject *v; |
| 1258 | |
| 1259 | if (!PyUnicode_Check(unicode)) { |
| 1260 | PyErr_BadArgument(); |
| 1261 | goto onError; |
| 1262 | } |
| 1263 | |
| 1264 | if (encoding == NULL) |
| 1265 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1266 | |
| 1267 | /* Decode via the codec registry */ |
| 1268 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1269 | if (v == NULL) |
| 1270 | goto onError; |
| 1271 | if (!PyUnicode_Check(v)) { |
| 1272 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1273 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1274 | Py_TYPE(v)->tp_name); |
| 1275 | Py_DECREF(v); |
| 1276 | goto onError; |
| 1277 | } |
| 1278 | return v; |
| 1279 | |
| 1280 | onError: |
| 1281 | return NULL; |
| 1282 | } |
| 1283 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1284 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1285 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1286 | const char *encoding, |
| 1287 | const char *errors) |
| 1288 | { |
| 1289 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1290 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1291 | unicode = PyUnicode_FromUnicode(s, size); |
| 1292 | if (unicode == NULL) |
| 1293 | return NULL; |
| 1294 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 1295 | Py_DECREF(unicode); |
| 1296 | return v; |
| 1297 | } |
| 1298 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1299 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 1300 | const char *encoding, |
| 1301 | const char *errors) |
| 1302 | { |
| 1303 | PyObject *v; |
| 1304 | |
| 1305 | if (!PyUnicode_Check(unicode)) { |
| 1306 | PyErr_BadArgument(); |
| 1307 | goto onError; |
| 1308 | } |
| 1309 | |
| 1310 | if (encoding == NULL) |
| 1311 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1312 | |
| 1313 | /* Encode via the codec registry */ |
| 1314 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1315 | if (v == NULL) |
| 1316 | goto onError; |
| 1317 | return v; |
| 1318 | |
| 1319 | onError: |
| 1320 | return NULL; |
| 1321 | } |
| 1322 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1323 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 1324 | const char *encoding, |
| 1325 | const char *errors) |
| 1326 | { |
| 1327 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1328 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1329 | if (!PyUnicode_Check(unicode)) { |
| 1330 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1331 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1332 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1333 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1334 | if (encoding == NULL) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1335 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1336 | |
| 1337 | /* Shortcuts for common default encodings */ |
| 1338 | if (errors == NULL) { |
| 1339 | if (strcmp(encoding, "utf-8") == 0) |
Jeremy Hylton | 9cea41c | 2001-05-29 17:13:15 +0000 | [diff] [blame] | 1340 | return PyUnicode_AsUTF8String(unicode); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1341 | else if (strcmp(encoding, "latin-1") == 0) |
| 1342 | return PyUnicode_AsLatin1String(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1343 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
| 1344 | else if (strcmp(encoding, "mbcs") == 0) |
| 1345 | return PyUnicode_AsMBCSString(unicode); |
| 1346 | #endif |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1347 | else if (strcmp(encoding, "ascii") == 0) |
| 1348 | return PyUnicode_AsASCIIString(unicode); |
| 1349 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1350 | |
| 1351 | /* Encode via the codec registry */ |
| 1352 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1353 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1354 | return NULL; |
| 1355 | |
| 1356 | /* The normal path */ |
| 1357 | if (PyBytes_Check(v)) |
| 1358 | return v; |
| 1359 | |
| 1360 | /* 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] | 1361 | if (PyByteArray_Check(v)) { |
| 1362 | char msg[100]; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1363 | PyObject *b; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1364 | PyOS_snprintf(msg, sizeof(msg), |
| 1365 | "encoder %s returned buffer instead of bytes", |
| 1366 | encoding); |
| 1367 | if (PyErr_WarnEx(PyExc_RuntimeWarning, msg, 1) < 0) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1368 | Py_DECREF(v); |
| 1369 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1370 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1371 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1372 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 1373 | Py_DECREF(v); |
| 1374 | return b; |
| 1375 | } |
| 1376 | |
| 1377 | PyErr_Format(PyExc_TypeError, |
| 1378 | "encoder did not return a bytes object (type=%.400s)", |
| 1379 | Py_TYPE(v)->tp_name); |
| 1380 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1381 | return NULL; |
| 1382 | } |
| 1383 | |
| 1384 | PyObject *PyUnicode_AsEncodedUnicode(PyObject *unicode, |
| 1385 | const char *encoding, |
| 1386 | const char *errors) |
| 1387 | { |
| 1388 | PyObject *v; |
| 1389 | |
| 1390 | if (!PyUnicode_Check(unicode)) { |
| 1391 | PyErr_BadArgument(); |
| 1392 | goto onError; |
| 1393 | } |
| 1394 | |
| 1395 | if (encoding == NULL) |
| 1396 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1397 | |
| 1398 | /* Encode via the codec registry */ |
| 1399 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1400 | if (v == NULL) |
| 1401 | goto onError; |
| 1402 | if (!PyUnicode_Check(v)) { |
| 1403 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1404 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1405 | Py_TYPE(v)->tp_name); |
| 1406 | Py_DECREF(v); |
| 1407 | goto onError; |
| 1408 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1409 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1410 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1411 | onError: |
| 1412 | return NULL; |
| 1413 | } |
| 1414 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1415 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
| 1416 | const char *errors) |
| 1417 | { |
| 1418 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1419 | if (v) |
| 1420 | return v; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1421 | if (errors != NULL) |
| 1422 | Py_FatalError("non-NULL encoding in _PyUnicode_AsDefaultEncodedString"); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1423 | v = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Guido van Rossum | 0661009 | 2007-08-16 21:02:22 +0000 | [diff] [blame] | 1424 | PyUnicode_GET_SIZE(unicode), |
| 1425 | NULL); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1426 | if (!v) |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1427 | return NULL; |
Guido van Rossum | e7a0d39 | 2007-07-12 07:53:00 +0000 | [diff] [blame] | 1428 | ((PyUnicodeObject *)unicode)->defenc = v; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1429 | return v; |
| 1430 | } |
| 1431 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1432 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1433 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1434 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1435 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 1436 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1437 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1438 | PyObject* |
| 1439 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 1440 | { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1441 | /* During the early bootstrapping process, Py_FileSystemDefaultEncoding |
| 1442 | can be undefined. If it is case, decode using UTF-8. The following assumes |
| 1443 | that Py_FileSystemDefaultEncoding is set to a built-in encoding during the |
| 1444 | bootstrapping process where the codecs aren't ready yet. |
| 1445 | */ |
| 1446 | if (Py_FileSystemDefaultEncoding) { |
| 1447 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1448 | if (strcmp(Py_FileSystemDefaultEncoding, "mbcs") == 0) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1449 | return PyUnicode_DecodeMBCS(s, size, "replace"); |
| 1450 | } |
| 1451 | #elif defined(__APPLE__) |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1452 | if (strcmp(Py_FileSystemDefaultEncoding, "utf-8") == 0) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1453 | return PyUnicode_DecodeUTF8(s, size, "replace"); |
| 1454 | } |
| 1455 | #endif |
| 1456 | return PyUnicode_Decode(s, size, |
| 1457 | Py_FileSystemDefaultEncoding, |
| 1458 | "replace"); |
| 1459 | } |
| 1460 | else { |
| 1461 | return PyUnicode_DecodeUTF8(s, size, "replace"); |
| 1462 | } |
| 1463 | } |
| 1464 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1465 | char* |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1466 | _PyUnicode_AsStringAndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1467 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1468 | PyObject *bytes; |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 1469 | if (!PyUnicode_Check(unicode)) { |
| 1470 | PyErr_BadArgument(); |
| 1471 | return NULL; |
| 1472 | } |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1473 | bytes = _PyUnicode_AsDefaultEncodedString(unicode, NULL); |
| 1474 | if (bytes == NULL) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1475 | return NULL; |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1476 | if (psize != NULL) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1477 | *psize = PyBytes_GET_SIZE(bytes); |
| 1478 | return PyBytes_AS_STRING(bytes); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1479 | } |
| 1480 | |
| 1481 | char* |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1482 | _PyUnicode_AsString(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1483 | { |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1484 | return _PyUnicode_AsStringAndSize(unicode, NULL); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1487 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 1488 | { |
| 1489 | if (!PyUnicode_Check(unicode)) { |
| 1490 | PyErr_BadArgument(); |
| 1491 | goto onError; |
| 1492 | } |
| 1493 | return PyUnicode_AS_UNICODE(unicode); |
| 1494 | |
| 1495 | onError: |
| 1496 | return NULL; |
| 1497 | } |
| 1498 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1499 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1500 | { |
| 1501 | if (!PyUnicode_Check(unicode)) { |
| 1502 | PyErr_BadArgument(); |
| 1503 | goto onError; |
| 1504 | } |
| 1505 | return PyUnicode_GET_SIZE(unicode); |
| 1506 | |
| 1507 | onError: |
| 1508 | return -1; |
| 1509 | } |
| 1510 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 1511 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1512 | { |
| 1513 | return unicode_default_encoding; |
| 1514 | } |
| 1515 | |
| 1516 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 1517 | { |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1518 | if (strcmp(encoding, unicode_default_encoding) != 0) { |
| 1519 | PyErr_Format(PyExc_ValueError, |
| 1520 | "Can only set default encoding to %s", |
| 1521 | unicode_default_encoding); |
| 1522 | return -1; |
| 1523 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1524 | return 0; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1527 | /* error handling callback helper: |
| 1528 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 1529 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1530 | and adjust various state variables. |
| 1531 | return 0 on success, -1 on error |
| 1532 | */ |
| 1533 | |
| 1534 | static |
| 1535 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
| 1536 | const char *encoding, const char *reason, |
Walter Dörwald | a651d3d | 2007-08-30 15:29:21 +0000 | [diff] [blame] | 1537 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 1538 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1539 | PyObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1540 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1541 | 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] | 1542 | |
| 1543 | PyObject *restuple = NULL; |
| 1544 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1545 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1546 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1547 | Py_ssize_t requiredsize; |
| 1548 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1549 | Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1550 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1551 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1552 | int res = -1; |
| 1553 | |
| 1554 | if (*errorHandler == NULL) { |
| 1555 | *errorHandler = PyCodec_LookupError(errors); |
| 1556 | if (*errorHandler == NULL) |
| 1557 | goto onError; |
| 1558 | } |
| 1559 | |
| 1560 | if (*exceptionObject == NULL) { |
| 1561 | *exceptionObject = PyUnicodeDecodeError_Create( |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1562 | encoding, *input, *inend-*input, *startinpos, *endinpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1563 | if (*exceptionObject == NULL) |
| 1564 | goto onError; |
| 1565 | } |
| 1566 | else { |
| 1567 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 1568 | goto onError; |
| 1569 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
| 1570 | goto onError; |
| 1571 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 1572 | goto onError; |
| 1573 | } |
| 1574 | |
| 1575 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 1576 | if (restuple == NULL) |
| 1577 | goto onError; |
| 1578 | if (!PyTuple_Check(restuple)) { |
| 1579 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 1580 | goto onError; |
| 1581 | } |
| 1582 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
| 1583 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1584 | |
| 1585 | /* Copy back the bytes variables, which might have been modified by the |
| 1586 | callback */ |
| 1587 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 1588 | if (!inputobj) |
| 1589 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1590 | if (!PyBytes_Check(inputobj)) { |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1591 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
| 1592 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1593 | *input = PyBytes_AS_STRING(inputobj); |
| 1594 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1595 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 1596 | /* we can DECREF safely, as the exception has another reference, |
| 1597 | so the object won't go away. */ |
| 1598 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1599 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1600 | if (newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1601 | newpos = insize+newpos; |
| 1602 | if (newpos<0 || newpos>insize) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 1603 | 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] | 1604 | goto onError; |
| 1605 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1606 | |
| 1607 | /* need more space? (at least enough for what we |
| 1608 | have+the replacement+the rest of the string (starting |
| 1609 | at the new input position), so we won't have to check space |
| 1610 | when there are no errors in the rest of the string) */ |
| 1611 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 1612 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 1613 | requiredsize = *outpos + repsize + insize-newpos; |
| 1614 | if (requiredsize > outsize) { |
| 1615 | if (requiredsize<2*outsize) |
| 1616 | requiredsize = 2*outsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1617 | if (PyUnicode_Resize(output, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1618 | goto onError; |
| 1619 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
| 1620 | } |
| 1621 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1622 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1623 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 1624 | *outptr += repsize; |
| 1625 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1626 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1627 | /* we made it! */ |
| 1628 | res = 0; |
| 1629 | |
| 1630 | onError: |
| 1631 | Py_XDECREF(restuple); |
| 1632 | return res; |
| 1633 | } |
| 1634 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1635 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 1636 | |
| 1637 | /* see RFC2152 for details */ |
| 1638 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1639 | static |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1640 | char utf7_special[128] = { |
| 1641 | /* indicate whether a UTF-7 character is special i.e. cannot be directly |
| 1642 | encoded: |
| 1643 | 0 - not special |
| 1644 | 1 - special |
| 1645 | 2 - whitespace (optional) |
| 1646 | 3 - RFC2152 Set O (optional) */ |
| 1647 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, |
| 1648 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1649 | 2, 3, 3, 3, 3, 3, 3, 0, 0, 0, 3, 1, 0, 0, 0, 1, |
| 1650 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, |
| 1651 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1652 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, |
| 1653 | 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1654 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 1, 1, |
| 1655 | |
| 1656 | }; |
| 1657 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1658 | /* Note: The comparison (c) <= 0 is a trick to work-around gcc |
| 1659 | warnings about the comparison always being false; since |
| 1660 | utf7_special[0] is 1, we can safely make that one comparison |
| 1661 | true */ |
| 1662 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1663 | #define SPECIAL(c, encodeO, encodeWS) \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1664 | ((c) > 127 || (c) <= 0 || utf7_special[(c)] == 1 || \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1665 | (encodeWS && (utf7_special[(c)] == 2)) || \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1666 | (encodeO && (utf7_special[(c)] == 3))) |
| 1667 | |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1668 | #define B64(n) \ |
| 1669 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 1670 | #define B64CHAR(c) \ |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1671 | (ISALNUM(c) || (c) == '+' || (c) == '/') |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1672 | #define UB64(c) \ |
| 1673 | ((c) == '+' ? 62 : (c) == '/' ? 63 : (c) >= 'a' ? \ |
| 1674 | (c) - 71 : (c) >= 'A' ? (c) - 65 : (c) + 4 ) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1675 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1676 | #define ENCODE(out, ch, bits) \ |
| 1677 | while (bits >= 6) { \ |
| 1678 | *out++ = B64(ch >> (bits-6)); \ |
| 1679 | bits -= 6; \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1680 | } |
| 1681 | |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1682 | #define DECODE(out, ch, bits, surrogate) \ |
| 1683 | while (bits >= 16) { \ |
| 1684 | Py_UNICODE outCh = (Py_UNICODE) ((ch >> (bits-16)) & 0xffff); \ |
| 1685 | bits -= 16; \ |
| 1686 | if (surrogate) { \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1687 | /* We have already generated an error for the high surrogate \ |
| 1688 | 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] | 1689 | surrogate = 0; \ |
| 1690 | } else if (0xDC00 <= outCh && outCh <= 0xDFFF) { \ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1691 | /* This is a surrogate pair. Unfortunately we can't represent \ |
Marc-André Lemburg | 5c4a9d6 | 2005-10-19 22:39:02 +0000 | [diff] [blame] | 1692 | it in a 16-bit character */ \ |
| 1693 | surrogate = 1; \ |
| 1694 | errmsg = "code pairs are not supported"; \ |
| 1695 | goto utf7Error; \ |
| 1696 | } else { \ |
| 1697 | *out++ = outCh; \ |
| 1698 | } \ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1699 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1700 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1701 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1702 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1703 | const char *errors) |
| 1704 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1705 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 1706 | } |
| 1707 | |
| 1708 | PyObject *PyUnicode_DecodeUTF7Stateful(const char *s, |
| 1709 | Py_ssize_t size, |
| 1710 | const char *errors, |
| 1711 | Py_ssize_t *consumed) |
| 1712 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1713 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1714 | Py_ssize_t startinpos; |
| 1715 | Py_ssize_t endinpos; |
| 1716 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1717 | const char *e; |
| 1718 | PyUnicodeObject *unicode; |
| 1719 | Py_UNICODE *p; |
| 1720 | const char *errmsg = ""; |
| 1721 | int inShift = 0; |
| 1722 | unsigned int bitsleft = 0; |
| 1723 | unsigned long charsleft = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1724 | int surrogate = 0; |
| 1725 | PyObject *errorHandler = NULL; |
| 1726 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1727 | |
| 1728 | unicode = _PyUnicode_New(size); |
| 1729 | if (!unicode) |
| 1730 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1731 | if (size == 0) { |
| 1732 | if (consumed) |
| 1733 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1734 | return (PyObject *)unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1735 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1736 | |
| 1737 | p = unicode->str; |
| 1738 | e = s + size; |
| 1739 | |
| 1740 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1741 | Py_UNICODE ch; |
| 1742 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 1743 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1744 | |
| 1745 | if (inShift) { |
| 1746 | if ((ch == '-') || !B64CHAR(ch)) { |
| 1747 | inShift = 0; |
| 1748 | s++; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1749 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1750 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1751 | if (bitsleft >= 6) { |
| 1752 | /* The shift sequence has a partial character in it. If |
| 1753 | bitsleft < 6 then we could just classify it as padding |
| 1754 | but that is not the case here */ |
| 1755 | |
| 1756 | errmsg = "partial character in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1757 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1758 | } |
| 1759 | /* According to RFC2152 the remaining bits should be zero. We |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1760 | choose to signal an error/insert a replacement character |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1761 | here so indicate the potential of a misencoded character. */ |
| 1762 | |
| 1763 | /* On x86, a << b == a << (b%32) so make sure that bitsleft != 0 */ |
| 1764 | if (bitsleft && charsleft << (sizeof(charsleft) * 8 - bitsleft)) { |
| 1765 | errmsg = "non-zero padding bits in shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1766 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1767 | } |
| 1768 | |
| 1769 | if (ch == '-') { |
| 1770 | if ((s < e) && (*(s) == '-')) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1771 | *p++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1772 | inShift = 1; |
| 1773 | } |
| 1774 | } else if (SPECIAL(ch,0,0)) { |
| 1775 | errmsg = "unexpected special character"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1776 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1777 | } else { |
| 1778 | *p++ = ch; |
| 1779 | } |
| 1780 | } else { |
| 1781 | charsleft = (charsleft << 6) | UB64(ch); |
| 1782 | bitsleft += 6; |
| 1783 | s++; |
| 1784 | /* p, charsleft, bitsleft, surrogate = */ DECODE(p, charsleft, bitsleft, surrogate); |
| 1785 | } |
| 1786 | } |
| 1787 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1788 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1789 | s++; |
| 1790 | if (s < e && *s == '-') { |
| 1791 | s++; |
| 1792 | *p++ = '+'; |
| 1793 | } else |
| 1794 | { |
| 1795 | inShift = 1; |
| 1796 | bitsleft = 0; |
| 1797 | } |
| 1798 | } |
| 1799 | else if (SPECIAL(ch,0,0)) { |
Walter Dörwald | 2b65c75 | 2007-08-30 15:35:26 +0000 | [diff] [blame] | 1800 | startinpos = s-starts; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1801 | errmsg = "unexpected special character"; |
| 1802 | s++; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1803 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1804 | } |
| 1805 | else { |
| 1806 | *p++ = ch; |
| 1807 | s++; |
| 1808 | } |
| 1809 | continue; |
| 1810 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1811 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1812 | endinpos = s-starts; |
| 1813 | if (unicode_decode_call_errorhandler( |
| 1814 | errors, &errorHandler, |
| 1815 | "utf7", errmsg, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1816 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1817 | (PyObject **)&unicode, &outpos, &p)) |
| 1818 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1819 | } |
| 1820 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1821 | if (inShift && !consumed) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1822 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1823 | endinpos = size; |
| 1824 | if (unicode_decode_call_errorhandler( |
| 1825 | errors, &errorHandler, |
| 1826 | "utf7", "unterminated shift sequence", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1827 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1828 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1829 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1830 | if (s < e) |
| 1831 | goto restart; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1832 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1833 | if (consumed) { |
| 1834 | if(inShift) |
| 1835 | *consumed = startinpos; |
| 1836 | else |
| 1837 | *consumed = s-starts; |
| 1838 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1839 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 1840 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1841 | goto onError; |
| 1842 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1843 | Py_XDECREF(errorHandler); |
| 1844 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1845 | return (PyObject *)unicode; |
| 1846 | |
| 1847 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1848 | Py_XDECREF(errorHandler); |
| 1849 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1850 | Py_DECREF(unicode); |
| 1851 | return NULL; |
| 1852 | } |
| 1853 | |
| 1854 | |
| 1855 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1856 | Py_ssize_t size, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1857 | int encodeSetO, |
| 1858 | int encodeWhiteSpace, |
| 1859 | const char *errors) |
| 1860 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1861 | PyObject *v, *result; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1862 | /* It might be possible to tighten this worst case */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1863 | Py_ssize_t cbAllocated = 5 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1864 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1865 | Py_ssize_t i = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1866 | unsigned int bitsleft = 0; |
| 1867 | unsigned long charsleft = 0; |
| 1868 | char * out; |
| 1869 | char * start; |
| 1870 | |
| 1871 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1872 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1873 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 1874 | if (cbAllocated / 5 != size) |
| 1875 | return PyErr_NoMemory(); |
| 1876 | |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 1877 | v = PyByteArray_FromStringAndSize(NULL, cbAllocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1878 | if (v == NULL) |
| 1879 | return NULL; |
| 1880 | |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 1881 | start = out = PyByteArray_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1882 | for (;i < size; ++i) { |
| 1883 | Py_UNICODE ch = s[i]; |
| 1884 | |
| 1885 | if (!inShift) { |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1886 | if (ch == '+') { |
| 1887 | *out++ = '+'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1888 | *out++ = '-'; |
| 1889 | } else if (SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1890 | charsleft = ch; |
| 1891 | bitsleft = 16; |
| 1892 | *out++ = '+'; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1893 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1894 | inShift = bitsleft > 0; |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1895 | } else { |
| 1896 | *out++ = (char) ch; |
| 1897 | } |
| 1898 | } else { |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1899 | if (!SPECIAL(ch, encodeSetO, encodeWhiteSpace)) { |
| 1900 | *out++ = B64(charsleft << (6-bitsleft)); |
| 1901 | charsleft = 0; |
| 1902 | bitsleft = 0; |
| 1903 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 1904 | so no '-' is required, except if the character is itself a '-' */ |
| 1905 | if (B64CHAR(ch) || ch == '-') { |
| 1906 | *out++ = '-'; |
| 1907 | } |
| 1908 | inShift = 0; |
| 1909 | *out++ = (char) ch; |
| 1910 | } else { |
| 1911 | bitsleft += 16; |
| 1912 | charsleft = (charsleft << 16) | ch; |
| 1913 | /* out, charsleft, bitsleft = */ ENCODE(out, charsleft, bitsleft); |
| 1914 | |
| 1915 | /* If the next character is special then we dont' need to terminate |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1916 | 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] | 1917 | or '-' then the shift sequence will be terminated implicitly and we |
| 1918 | don't have to insert a '-'. */ |
| 1919 | |
| 1920 | if (bitsleft == 0) { |
| 1921 | if (i + 1 < size) { |
| 1922 | Py_UNICODE ch2 = s[i+1]; |
| 1923 | |
| 1924 | if (SPECIAL(ch2, encodeSetO, encodeWhiteSpace)) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1925 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1926 | } else if (B64CHAR(ch2) || ch2 == '-') { |
| 1927 | *out++ = '-'; |
| 1928 | inShift = 0; |
| 1929 | } else { |
| 1930 | inShift = 0; |
| 1931 | } |
| 1932 | |
| 1933 | } |
| 1934 | else { |
| 1935 | *out++ = '-'; |
| 1936 | inShift = 0; |
| 1937 | } |
| 1938 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1939 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1940 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 1941 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1942 | if (bitsleft) { |
| 1943 | *out++= B64(charsleft << (6-bitsleft) ); |
| 1944 | *out++ = '-'; |
| 1945 | } |
| 1946 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1947 | result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), out - start); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1948 | Py_DECREF(v); |
| 1949 | return result; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1950 | } |
| 1951 | |
| 1952 | #undef SPECIAL |
| 1953 | #undef B64 |
| 1954 | #undef B64CHAR |
| 1955 | #undef UB64 |
| 1956 | #undef ENCODE |
| 1957 | #undef DECODE |
| 1958 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1959 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 1960 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1961 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1962 | char utf8_code_length[256] = { |
| 1963 | /* Map UTF-8 encoded prefix byte to sequence length. zero means |
| 1964 | illegal prefix. see RFC 2279 for details */ |
| 1965 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1966 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1967 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1968 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1969 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1970 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1971 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1972 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1973 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1974 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1975 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1976 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1977 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1978 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 1979 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 1980 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 |
| 1981 | }; |
| 1982 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1983 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1984 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1985 | const char *errors) |
| 1986 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1987 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 1988 | } |
| 1989 | |
| 1990 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1991 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1992 | const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1993 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1994 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1995 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1996 | int n; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1997 | Py_ssize_t startinpos; |
| 1998 | Py_ssize_t endinpos; |
| 1999 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2000 | const char *e; |
| 2001 | PyUnicodeObject *unicode; |
| 2002 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2003 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2004 | PyObject *errorHandler = NULL; |
| 2005 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2006 | |
| 2007 | /* Note: size will always be longer than the resulting Unicode |
| 2008 | character count */ |
| 2009 | unicode = _PyUnicode_New(size); |
| 2010 | if (!unicode) |
| 2011 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2012 | if (size == 0) { |
| 2013 | if (consumed) |
| 2014 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2015 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2016 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2017 | |
| 2018 | /* Unpack UTF-8 encoded data */ |
| 2019 | p = unicode->str; |
| 2020 | e = s + size; |
| 2021 | |
| 2022 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2023 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2024 | |
| 2025 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2026 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2027 | s++; |
| 2028 | continue; |
| 2029 | } |
| 2030 | |
| 2031 | n = utf8_code_length[ch]; |
| 2032 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2033 | if (s + n > e) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2034 | if (consumed) |
| 2035 | break; |
| 2036 | else { |
| 2037 | errmsg = "unexpected end of data"; |
| 2038 | startinpos = s-starts; |
| 2039 | endinpos = size; |
| 2040 | goto utf8Error; |
| 2041 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2042 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2043 | |
| 2044 | switch (n) { |
| 2045 | |
| 2046 | case 0: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2047 | errmsg = "unexpected code byte"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2048 | startinpos = s-starts; |
| 2049 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2050 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2051 | |
| 2052 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2053 | errmsg = "internal error"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2054 | startinpos = s-starts; |
| 2055 | endinpos = startinpos+1; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2056 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2057 | |
| 2058 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2059 | if ((s[1] & 0xc0) != 0x80) { |
| 2060 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2061 | startinpos = s-starts; |
| 2062 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2063 | goto utf8Error; |
| 2064 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2065 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2066 | if (ch < 0x80) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2067 | startinpos = s-starts; |
| 2068 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2069 | errmsg = "illegal encoding"; |
| 2070 | goto utf8Error; |
| 2071 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2072 | else |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2073 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2074 | break; |
| 2075 | |
| 2076 | case 3: |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2077 | if ((s[1] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2078 | (s[2] & 0xc0) != 0x80) { |
| 2079 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2080 | startinpos = s-starts; |
| 2081 | endinpos = startinpos+3; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2082 | goto utf8Error; |
| 2083 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2084 | 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] | 2085 | if (ch < 0x0800) { |
| 2086 | /* Note: UTF-8 encodings of surrogates are considered |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2087 | legal UTF-8 sequences; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2088 | |
| 2089 | XXX For wide builds (UCS-4) we should probably try |
| 2090 | to recombine the surrogates into a single code |
| 2091 | unit. |
| 2092 | */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2093 | errmsg = "illegal encoding"; |
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 | else |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2099 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2100 | break; |
| 2101 | |
| 2102 | case 4: |
| 2103 | if ((s[1] & 0xc0) != 0x80 || |
| 2104 | (s[2] & 0xc0) != 0x80 || |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2105 | (s[3] & 0xc0) != 0x80) { |
| 2106 | errmsg = "invalid data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2107 | startinpos = s-starts; |
| 2108 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2109 | goto utf8Error; |
| 2110 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2111 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
| 2112 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 2113 | /* validate and convert to UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2114 | if ((ch < 0x10000) /* minimum value allowed for 4 |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2115 | byte encoding */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2116 | || (ch > 0x10ffff)) /* maximum value allowed for |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2117 | UTF-16 */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2118 | { |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2119 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2120 | startinpos = s-starts; |
| 2121 | endinpos = startinpos+4; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2122 | goto utf8Error; |
| 2123 | } |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2124 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2125 | *p++ = (Py_UNICODE)ch; |
| 2126 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2127 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2128 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2129 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 2130 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2131 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2132 | /* high surrogate = top 10 bits added to D800 */ |
| 2133 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2134 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2135 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2136 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2137 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2138 | break; |
| 2139 | |
| 2140 | default: |
| 2141 | /* Other sizes are only needed for UCS-4 */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2142 | errmsg = "unsupported Unicode code range"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2143 | startinpos = s-starts; |
| 2144 | endinpos = startinpos+n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2145 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2146 | } |
| 2147 | s += n; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2148 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2149 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2150 | utf8Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2151 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2152 | if (unicode_decode_call_errorhandler( |
| 2153 | errors, &errorHandler, |
| 2154 | "utf8", errmsg, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2155 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2156 | (PyObject **)&unicode, &outpos, &p)) |
| 2157 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2158 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2159 | if (consumed) |
| 2160 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2161 | |
| 2162 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2163 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2164 | goto onError; |
| 2165 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2166 | Py_XDECREF(errorHandler); |
| 2167 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2168 | return (PyObject *)unicode; |
| 2169 | |
| 2170 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2171 | Py_XDECREF(errorHandler); |
| 2172 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2173 | Py_DECREF(unicode); |
| 2174 | return NULL; |
| 2175 | } |
| 2176 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2177 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 2178 | and allocate exactly as much space needed at the end. Else allocate the |
| 2179 | maximum possible needed (4 result bytes per Unicode character), and return |
| 2180 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2181 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 2182 | PyObject * |
| 2183 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2184 | Py_ssize_t size, |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 2185 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2186 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2187 | #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] | 2188 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2189 | Py_ssize_t i; /* index into s of next input byte */ |
| 2190 | PyObject *result; /* result string object */ |
| 2191 | char *p; /* next free byte in output buffer */ |
| 2192 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 2193 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2194 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2195 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2196 | assert(s != NULL); |
| 2197 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2198 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2199 | if (size <= MAX_SHORT_UNICHARS) { |
| 2200 | /* Write into the stack buffer; nallocated can't overflow. |
| 2201 | * At the end, we'll allocate exactly as much heap space as it |
| 2202 | * turns out we need. |
| 2203 | */ |
| 2204 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2205 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2206 | p = stackbuf; |
| 2207 | } |
| 2208 | else { |
| 2209 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 2210 | nallocated = size * 4; |
| 2211 | if (nallocated / 4 != size) /* overflow! */ |
| 2212 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2213 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2214 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2215 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2216 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2217 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2218 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2219 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2220 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2221 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2222 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2223 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2224 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2225 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2226 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2227 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 2228 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 2229 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2230 | } |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2231 | else { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2232 | /* Encode UCS2 Unicode ordinals */ |
| 2233 | if (ch < 0x10000) { |
| 2234 | /* Special case: check for high surrogate */ |
| 2235 | if (0xD800 <= ch && ch <= 0xDBFF && i != size) { |
| 2236 | Py_UCS4 ch2 = s[i]; |
| 2237 | /* Check for low surrogate and combine the two to |
| 2238 | form a UCS4 value */ |
| 2239 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2240 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2241 | i++; |
| 2242 | goto encodeUCS4; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2243 | } |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2244 | /* Fall through: handles isolated high surrogates */ |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2245 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2246 | *p++ = (char)(0xe0 | (ch >> 12)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2247 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2248 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2249 | continue; |
| 2250 | } |
| 2251 | encodeUCS4: |
| 2252 | /* Encode UCS4 Unicode ordinals */ |
| 2253 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2254 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 2255 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2256 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2257 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2258 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 2259 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2260 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2261 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2262 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2263 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2264 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2265 | } |
| 2266 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 2267 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2268 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2269 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2270 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2271 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2272 | return result; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2273 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2274 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2275 | } |
| 2276 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2277 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 2278 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2279 | if (!PyUnicode_Check(unicode)) { |
| 2280 | PyErr_BadArgument(); |
| 2281 | return NULL; |
| 2282 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 2283 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
| 2284 | PyUnicode_GET_SIZE(unicode), |
| 2285 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2286 | } |
| 2287 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2288 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 2289 | |
| 2290 | PyObject * |
| 2291 | PyUnicode_DecodeUTF32(const char *s, |
| 2292 | Py_ssize_t size, |
| 2293 | const char *errors, |
| 2294 | int *byteorder) |
| 2295 | { |
| 2296 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 2297 | } |
| 2298 | |
| 2299 | PyObject * |
| 2300 | PyUnicode_DecodeUTF32Stateful(const char *s, |
| 2301 | Py_ssize_t size, |
| 2302 | const char *errors, |
| 2303 | int *byteorder, |
| 2304 | Py_ssize_t *consumed) |
| 2305 | { |
| 2306 | const char *starts = s; |
| 2307 | Py_ssize_t startinpos; |
| 2308 | Py_ssize_t endinpos; |
| 2309 | Py_ssize_t outpos; |
| 2310 | PyUnicodeObject *unicode; |
| 2311 | Py_UNICODE *p; |
| 2312 | #ifndef Py_UNICODE_WIDE |
| 2313 | int i, pairs; |
| 2314 | #else |
| 2315 | const int pairs = 0; |
| 2316 | #endif |
| 2317 | const unsigned char *q, *e; |
| 2318 | int bo = 0; /* assume native ordering by default */ |
| 2319 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2320 | /* Offsets from q for retrieving bytes in the right order. */ |
| 2321 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2322 | int iorder[] = {0, 1, 2, 3}; |
| 2323 | #else |
| 2324 | int iorder[] = {3, 2, 1, 0}; |
| 2325 | #endif |
| 2326 | PyObject *errorHandler = NULL; |
| 2327 | PyObject *exc = NULL; |
Guido van Rossum | 8d991ed | 2007-08-17 15:41:00 +0000 | [diff] [blame] | 2328 | /* On narrow builds we split characters outside the BMP into two |
| 2329 | codepoints => count how much extra space we need. */ |
| 2330 | #ifndef Py_UNICODE_WIDE |
| 2331 | for (i = pairs = 0; i < size/4; i++) |
| 2332 | if (((Py_UCS4 *)s)[i] >= 0x10000) |
| 2333 | pairs++; |
| 2334 | #endif |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2335 | |
| 2336 | /* This might be one to much, because of a BOM */ |
| 2337 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 2338 | if (!unicode) |
| 2339 | return NULL; |
| 2340 | if (size == 0) |
| 2341 | return (PyObject *)unicode; |
| 2342 | |
| 2343 | /* Unpack UTF-32 encoded data */ |
| 2344 | p = unicode->str; |
| 2345 | q = (unsigned char *)s; |
| 2346 | e = q + size; |
| 2347 | |
| 2348 | if (byteorder) |
| 2349 | bo = *byteorder; |
| 2350 | |
| 2351 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2352 | byte order setting accordingly. In native mode, the leading BOM |
| 2353 | mark is skipped, in all other modes, it is copied to the output |
| 2354 | stream as-is (giving a ZWNBSP character). */ |
| 2355 | if (bo == 0) { |
| 2356 | if (size >= 4) { |
| 2357 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2358 | (q[iorder[1]] << 8) | q[iorder[0]]; |
| 2359 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2360 | if (bom == 0x0000FEFF) { |
| 2361 | q += 4; |
| 2362 | bo = -1; |
| 2363 | } |
| 2364 | else if (bom == 0xFFFE0000) { |
| 2365 | q += 4; |
| 2366 | bo = 1; |
| 2367 | } |
| 2368 | #else |
| 2369 | if (bom == 0x0000FEFF) { |
| 2370 | q += 4; |
| 2371 | bo = 1; |
| 2372 | } |
| 2373 | else if (bom == 0xFFFE0000) { |
| 2374 | q += 4; |
| 2375 | bo = -1; |
| 2376 | } |
| 2377 | #endif |
| 2378 | } |
| 2379 | } |
| 2380 | |
| 2381 | if (bo == -1) { |
| 2382 | /* force LE */ |
| 2383 | iorder[0] = 0; |
| 2384 | iorder[1] = 1; |
| 2385 | iorder[2] = 2; |
| 2386 | iorder[3] = 3; |
| 2387 | } |
| 2388 | else if (bo == 1) { |
| 2389 | /* force BE */ |
| 2390 | iorder[0] = 3; |
| 2391 | iorder[1] = 2; |
| 2392 | iorder[2] = 1; |
| 2393 | iorder[3] = 0; |
| 2394 | } |
| 2395 | |
| 2396 | while (q < e) { |
| 2397 | Py_UCS4 ch; |
| 2398 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 2399 | if (e-q<4) { |
| 2400 | if (consumed) |
| 2401 | break; |
| 2402 | errmsg = "truncated data"; |
| 2403 | startinpos = ((const char *)q)-starts; |
| 2404 | endinpos = ((const char *)e)-starts; |
| 2405 | goto utf32Error; |
| 2406 | /* The remaining input chars are ignored if the callback |
| 2407 | chooses to skip the input */ |
| 2408 | } |
| 2409 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2410 | (q[iorder[1]] << 8) | q[iorder[0]]; |
| 2411 | |
| 2412 | if (ch >= 0x110000) |
| 2413 | { |
| 2414 | errmsg = "codepoint not in range(0x110000)"; |
| 2415 | startinpos = ((const char *)q)-starts; |
| 2416 | endinpos = startinpos+4; |
| 2417 | goto utf32Error; |
| 2418 | } |
| 2419 | #ifndef Py_UNICODE_WIDE |
| 2420 | if (ch >= 0x10000) |
| 2421 | { |
| 2422 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 2423 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2424 | } |
| 2425 | else |
| 2426 | #endif |
| 2427 | *p++ = ch; |
| 2428 | q += 4; |
| 2429 | continue; |
| 2430 | utf32Error: |
| 2431 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2432 | if (unicode_decode_call_errorhandler( |
| 2433 | errors, &errorHandler, |
| 2434 | "utf32", errmsg, |
| 2435 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 2436 | (PyObject **)&unicode, &outpos, &p)) |
| 2437 | goto onError; |
| 2438 | } |
| 2439 | |
| 2440 | if (byteorder) |
| 2441 | *byteorder = bo; |
| 2442 | |
| 2443 | if (consumed) |
| 2444 | *consumed = (const char *)q-starts; |
| 2445 | |
| 2446 | /* Adjust length */ |
| 2447 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
| 2448 | goto onError; |
| 2449 | |
| 2450 | Py_XDECREF(errorHandler); |
| 2451 | Py_XDECREF(exc); |
| 2452 | return (PyObject *)unicode; |
| 2453 | |
| 2454 | onError: |
| 2455 | Py_DECREF(unicode); |
| 2456 | Py_XDECREF(errorHandler); |
| 2457 | Py_XDECREF(exc); |
| 2458 | return NULL; |
| 2459 | } |
| 2460 | |
| 2461 | PyObject * |
| 2462 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
| 2463 | Py_ssize_t size, |
| 2464 | const char *errors, |
| 2465 | int byteorder) |
| 2466 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2467 | PyObject *v, *result; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2468 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2469 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2470 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2471 | Py_ssize_t i, pairs; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2472 | #else |
| 2473 | const int pairs = 0; |
| 2474 | #endif |
| 2475 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2476 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2477 | int iorder[] = {0, 1, 2, 3}; |
| 2478 | #else |
| 2479 | int iorder[] = {3, 2, 1, 0}; |
| 2480 | #endif |
| 2481 | |
| 2482 | #define STORECHAR(CH) \ |
| 2483 | do { \ |
| 2484 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 2485 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 2486 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 2487 | p[iorder[0]] = (CH) & 0xff; \ |
| 2488 | p += 4; \ |
| 2489 | } while(0) |
| 2490 | |
| 2491 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 2492 | so we need less space. */ |
| 2493 | #ifndef Py_UNICODE_WIDE |
| 2494 | for (i = pairs = 0; i < size-1; i++) |
| 2495 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 2496 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 2497 | pairs++; |
| 2498 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2499 | nsize = (size - pairs + (byteorder == 0)); |
| 2500 | bytesize = nsize * 4; |
| 2501 | if (bytesize / 4 != nsize) |
| 2502 | return PyErr_NoMemory(); |
| 2503 | v = PyByteArray_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2504 | if (v == NULL) |
| 2505 | return NULL; |
| 2506 | |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 2507 | p = (unsigned char *)PyByteArray_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2508 | if (byteorder == 0) |
| 2509 | STORECHAR(0xFEFF); |
| 2510 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2511 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2512 | |
| 2513 | if (byteorder == -1) { |
| 2514 | /* force LE */ |
| 2515 | iorder[0] = 0; |
| 2516 | iorder[1] = 1; |
| 2517 | iorder[2] = 2; |
| 2518 | iorder[3] = 3; |
| 2519 | } |
| 2520 | else if (byteorder == 1) { |
| 2521 | /* force BE */ |
| 2522 | iorder[0] = 3; |
| 2523 | iorder[1] = 2; |
| 2524 | iorder[2] = 1; |
| 2525 | iorder[3] = 0; |
| 2526 | } |
| 2527 | |
| 2528 | while (size-- > 0) { |
| 2529 | Py_UCS4 ch = *s++; |
| 2530 | #ifndef Py_UNICODE_WIDE |
| 2531 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 2532 | Py_UCS4 ch2 = *s; |
| 2533 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 2534 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 2535 | s++; |
| 2536 | size--; |
| 2537 | } |
| 2538 | } |
| 2539 | #endif |
| 2540 | STORECHAR(ch); |
| 2541 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2542 | |
| 2543 | done: |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2544 | result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2545 | Py_DECREF(v); |
| 2546 | return result; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2547 | #undef STORECHAR |
| 2548 | } |
| 2549 | |
| 2550 | PyObject *PyUnicode_AsUTF32String(PyObject *unicode) |
| 2551 | { |
| 2552 | if (!PyUnicode_Check(unicode)) { |
| 2553 | PyErr_BadArgument(); |
| 2554 | return NULL; |
| 2555 | } |
| 2556 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
| 2557 | PyUnicode_GET_SIZE(unicode), |
| 2558 | NULL, |
| 2559 | 0); |
| 2560 | } |
| 2561 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2562 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 2563 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2564 | PyObject * |
| 2565 | PyUnicode_DecodeUTF16(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2566 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2567 | const char *errors, |
| 2568 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2569 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2570 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 2571 | } |
| 2572 | |
| 2573 | PyObject * |
| 2574 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2575 | Py_ssize_t size, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2576 | const char *errors, |
| 2577 | int *byteorder, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2578 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2579 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2580 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2581 | Py_ssize_t startinpos; |
| 2582 | Py_ssize_t endinpos; |
| 2583 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2584 | PyUnicodeObject *unicode; |
| 2585 | Py_UNICODE *p; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2586 | const unsigned char *q, *e; |
| 2587 | int bo = 0; /* assume native ordering by default */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2588 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2589 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 2590 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2591 | int ihi = 1, ilo = 0; |
| 2592 | #else |
| 2593 | int ihi = 0, ilo = 1; |
| 2594 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2595 | PyObject *errorHandler = NULL; |
| 2596 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2597 | |
| 2598 | /* Note: size will always be longer than the resulting Unicode |
| 2599 | character count */ |
| 2600 | unicode = _PyUnicode_New(size); |
| 2601 | if (!unicode) |
| 2602 | return NULL; |
| 2603 | if (size == 0) |
| 2604 | return (PyObject *)unicode; |
| 2605 | |
| 2606 | /* Unpack UTF-16 encoded data */ |
| 2607 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2608 | q = (unsigned char *)s; |
| 2609 | e = q + size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2610 | |
| 2611 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2612 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2613 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2614 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2615 | byte order setting accordingly. In native mode, the leading BOM |
| 2616 | mark is skipped, in all other modes, it is copied to the output |
| 2617 | stream as-is (giving a ZWNBSP character). */ |
| 2618 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2619 | if (size >= 2) { |
| 2620 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2621 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2622 | if (bom == 0xFEFF) { |
| 2623 | q += 2; |
| 2624 | bo = -1; |
| 2625 | } |
| 2626 | else if (bom == 0xFFFE) { |
| 2627 | q += 2; |
| 2628 | bo = 1; |
| 2629 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2630 | #else |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2631 | if (bom == 0xFEFF) { |
| 2632 | q += 2; |
| 2633 | bo = 1; |
| 2634 | } |
| 2635 | else if (bom == 0xFFFE) { |
| 2636 | q += 2; |
| 2637 | bo = -1; |
| 2638 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2639 | #endif |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2640 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2641 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2642 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2643 | if (bo == -1) { |
| 2644 | /* force LE */ |
| 2645 | ihi = 1; |
| 2646 | ilo = 0; |
| 2647 | } |
| 2648 | else if (bo == 1) { |
| 2649 | /* force BE */ |
| 2650 | ihi = 0; |
| 2651 | ilo = 1; |
| 2652 | } |
| 2653 | |
| 2654 | while (q < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2655 | Py_UNICODE ch; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2656 | /* remaining bytes at the end? (size should be even) */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2657 | if (e-q<2) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2658 | if (consumed) |
| 2659 | break; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2660 | errmsg = "truncated data"; |
| 2661 | startinpos = ((const char *)q)-starts; |
| 2662 | endinpos = ((const char *)e)-starts; |
| 2663 | goto utf16Error; |
| 2664 | /* The remaining input chars are ignored if the callback |
| 2665 | chooses to skip the input */ |
| 2666 | } |
| 2667 | ch = (q[ihi] << 8) | q[ilo]; |
| 2668 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2669 | q += 2; |
| 2670 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2671 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 2672 | *p++ = ch; |
| 2673 | continue; |
| 2674 | } |
| 2675 | |
| 2676 | /* UTF-16 code pair: */ |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2677 | if (q >= e) { |
| 2678 | errmsg = "unexpected end of data"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2679 | startinpos = (((const char *)q)-2)-starts; |
| 2680 | endinpos = ((const char *)e)-starts; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2681 | goto utf16Error; |
| 2682 | } |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 2683 | if (0xD800 <= ch && ch <= 0xDBFF) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2684 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 2685 | q += 2; |
Martin v. Löwis | ac93bc2 | 2001-06-26 22:43:40 +0000 | [diff] [blame] | 2686 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2687 | #ifndef Py_UNICODE_WIDE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2688 | *p++ = ch; |
| 2689 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2690 | #else |
| 2691 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2692 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2693 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2694 | } |
| 2695 | else { |
| 2696 | errmsg = "illegal UTF-16 surrogate"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2697 | startinpos = (((const char *)q)-4)-starts; |
| 2698 | endinpos = startinpos+2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2699 | goto utf16Error; |
| 2700 | } |
| 2701 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2702 | } |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2703 | errmsg = "illegal encoding"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2704 | startinpos = (((const char *)q)-2)-starts; |
| 2705 | endinpos = startinpos+2; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2706 | /* Fall through to report the error */ |
| 2707 | |
| 2708 | utf16Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2709 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2710 | if (unicode_decode_call_errorhandler( |
| 2711 | errors, &errorHandler, |
| 2712 | "utf16", errmsg, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2713 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2714 | (PyObject **)&unicode, &outpos, &p)) |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2715 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2716 | } |
| 2717 | |
| 2718 | if (byteorder) |
| 2719 | *byteorder = bo; |
| 2720 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2721 | if (consumed) |
| 2722 | *consumed = (const char *)q-starts; |
| 2723 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2724 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2725 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2726 | goto onError; |
| 2727 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2728 | Py_XDECREF(errorHandler); |
| 2729 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2730 | return (PyObject *)unicode; |
| 2731 | |
| 2732 | onError: |
| 2733 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2734 | Py_XDECREF(errorHandler); |
| 2735 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2736 | return NULL; |
| 2737 | } |
| 2738 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2739 | PyObject * |
| 2740 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2741 | Py_ssize_t size, |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2742 | const char *errors, |
| 2743 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2744 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2745 | PyObject *v, *result; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2746 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2747 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2748 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2749 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2750 | #else |
| 2751 | const int pairs = 0; |
| 2752 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2753 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2754 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2755 | int ihi = 1, ilo = 0; |
| 2756 | #else |
| 2757 | int ihi = 0, ilo = 1; |
| 2758 | #endif |
| 2759 | |
| 2760 | #define STORECHAR(CH) \ |
| 2761 | do { \ |
| 2762 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 2763 | p[ilo] = (CH) & 0xff; \ |
| 2764 | p += 2; \ |
| 2765 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2766 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2767 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2768 | for (i = pairs = 0; i < size; i++) |
| 2769 | if (s[i] >= 0x10000) |
| 2770 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2771 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2772 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 2773 | if (size > PY_SSIZE_T_MAX || |
| 2774 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
| 2775 | return PyErr_NoMemory(); |
| 2776 | nsize = size + pairs + (byteorder == 0); |
| 2777 | bytesize = nsize * 2; |
| 2778 | if (bytesize / 2 != nsize) |
| 2779 | return PyErr_NoMemory(); |
| 2780 | v = PyByteArray_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2781 | if (v == NULL) |
| 2782 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2783 | |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 2784 | p = (unsigned char *)PyByteArray_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2785 | if (byteorder == 0) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2786 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2787 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2788 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2789 | |
| 2790 | if (byteorder == -1) { |
| 2791 | /* force LE */ |
| 2792 | ihi = 1; |
| 2793 | ilo = 0; |
| 2794 | } |
| 2795 | else if (byteorder == 1) { |
| 2796 | /* force BE */ |
| 2797 | ihi = 0; |
| 2798 | ilo = 1; |
| 2799 | } |
| 2800 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2801 | while (size-- > 0) { |
| 2802 | Py_UNICODE ch = *s++; |
| 2803 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2804 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2805 | if (ch >= 0x10000) { |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2806 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2807 | ch = 0xD800 | ((ch-0x10000) >> 10); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2808 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 2809 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2810 | STORECHAR(ch); |
| 2811 | if (ch2) |
| 2812 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2813 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2814 | |
| 2815 | done: |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2816 | result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2817 | Py_DECREF(v); |
| 2818 | return result; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2819 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2820 | } |
| 2821 | |
| 2822 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 2823 | { |
| 2824 | if (!PyUnicode_Check(unicode)) { |
| 2825 | PyErr_BadArgument(); |
| 2826 | return NULL; |
| 2827 | } |
| 2828 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
| 2829 | PyUnicode_GET_SIZE(unicode), |
| 2830 | NULL, |
| 2831 | 0); |
| 2832 | } |
| 2833 | |
| 2834 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 2835 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 2836 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 2837 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2838 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2839 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2840 | const char *errors) |
| 2841 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2842 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2843 | Py_ssize_t startinpos; |
| 2844 | Py_ssize_t endinpos; |
| 2845 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2846 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2847 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2848 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2849 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2850 | char* message; |
| 2851 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2852 | PyObject *errorHandler = NULL; |
| 2853 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2854 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2855 | /* Escaped strings will always be longer than the resulting |
| 2856 | 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] | 2857 | length after conversion to the true value. |
| 2858 | (but if the error callback returns a long replacement string |
| 2859 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2860 | v = _PyUnicode_New(size); |
| 2861 | if (v == NULL) |
| 2862 | goto onError; |
| 2863 | if (size == 0) |
| 2864 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2865 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2866 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2867 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2868 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2869 | while (s < end) { |
| 2870 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 2871 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2872 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2873 | |
| 2874 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 2875 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2876 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2877 | continue; |
| 2878 | } |
| 2879 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2880 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2881 | /* \ - Escapes */ |
| 2882 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2883 | c = *s++; |
| 2884 | if (s > end) |
| 2885 | c = '\0'; /* Invalid after \ */ |
| 2886 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2887 | |
| 2888 | /* \x escapes */ |
| 2889 | case '\n': break; |
| 2890 | case '\\': *p++ = '\\'; break; |
| 2891 | case '\'': *p++ = '\''; break; |
| 2892 | case '\"': *p++ = '\"'; break; |
| 2893 | case 'b': *p++ = '\b'; break; |
| 2894 | case 'f': *p++ = '\014'; break; /* FF */ |
| 2895 | case 't': *p++ = '\t'; break; |
| 2896 | case 'n': *p++ = '\n'; break; |
| 2897 | case 'r': *p++ = '\r'; break; |
| 2898 | case 'v': *p++ = '\013'; break; /* VT */ |
| 2899 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 2900 | |
| 2901 | /* \OOO (octal) escapes */ |
| 2902 | case '0': case '1': case '2': case '3': |
| 2903 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2904 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2905 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2906 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2907 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2908 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2909 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 2910 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2911 | break; |
| 2912 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2913 | /* hex escapes */ |
| 2914 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2915 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2916 | digits = 2; |
| 2917 | message = "truncated \\xXX escape"; |
| 2918 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2919 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2920 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2921 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2922 | digits = 4; |
| 2923 | message = "truncated \\uXXXX escape"; |
| 2924 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2925 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2926 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2927 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2928 | digits = 8; |
| 2929 | message = "truncated \\UXXXXXXXX escape"; |
| 2930 | hexescape: |
| 2931 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2932 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2933 | if (s+digits>end) { |
| 2934 | endinpos = size; |
| 2935 | if (unicode_decode_call_errorhandler( |
| 2936 | errors, &errorHandler, |
| 2937 | "unicodeescape", "end of string in escape sequence", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2938 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2939 | (PyObject **)&v, &outpos, &p)) |
| 2940 | goto onError; |
| 2941 | goto nextByte; |
| 2942 | } |
| 2943 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2944 | c = (unsigned char) s[i]; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 2945 | if (!ISXDIGIT(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2946 | endinpos = (s+i+1)-starts; |
| 2947 | if (unicode_decode_call_errorhandler( |
| 2948 | errors, &errorHandler, |
| 2949 | "unicodeescape", message, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2950 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2951 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2952 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2953 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2954 | } |
| 2955 | chr = (chr<<4) & ~0xF; |
| 2956 | if (c >= '0' && c <= '9') |
| 2957 | chr += c - '0'; |
| 2958 | else if (c >= 'a' && c <= 'f') |
| 2959 | chr += 10 + c - 'a'; |
| 2960 | else |
| 2961 | chr += 10 + c - 'A'; |
| 2962 | } |
| 2963 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 2964 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2965 | /* _decoding_error will have already written into the |
| 2966 | target buffer. */ |
| 2967 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2968 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2969 | /* when we get here, chr is a 32-bit unicode character */ |
| 2970 | if (chr <= 0xffff) |
| 2971 | /* UCS-2 character */ |
| 2972 | *p++ = (Py_UNICODE) chr; |
| 2973 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 2974 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 2975 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2976 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2977 | *p++ = chr; |
| 2978 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2979 | chr -= 0x10000L; |
| 2980 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2981 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2982 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2983 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2984 | endinpos = s-starts; |
| 2985 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 2986 | if (unicode_decode_call_errorhandler( |
| 2987 | errors, &errorHandler, |
| 2988 | "unicodeescape", "illegal Unicode character", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 2989 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2990 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 2991 | goto onError; |
| 2992 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 2993 | break; |
| 2994 | |
| 2995 | /* \N{name} */ |
| 2996 | case 'N': |
| 2997 | message = "malformed \\N character escape"; |
| 2998 | if (ucnhash_CAPI == NULL) { |
| 2999 | /* load the unicode data module */ |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3000 | PyObject *m, *api; |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 3001 | m = PyImport_ImportModuleNoBlock("unicodedata"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3002 | if (m == NULL) |
| 3003 | goto ucnhashError; |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3004 | api = PyObject_GetAttrString(m, "ucnhash_CAPI"); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3005 | Py_DECREF(m); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3006 | if (api == NULL) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3007 | goto ucnhashError; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3008 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCObject_AsVoidPtr(api); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3009 | Py_DECREF(api); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3010 | if (ucnhash_CAPI == NULL) |
| 3011 | goto ucnhashError; |
| 3012 | } |
| 3013 | if (*s == '{') { |
| 3014 | const char *start = s+1; |
| 3015 | /* look for the closing brace */ |
| 3016 | while (*s != '}' && s < end) |
| 3017 | s++; |
| 3018 | if (s > start && s < end && *s == '}') { |
| 3019 | /* found a name. look it up in the unicode database */ |
| 3020 | message = "unknown Unicode character name"; |
| 3021 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 3022 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3023 | goto store; |
| 3024 | } |
| 3025 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3026 | endinpos = s-starts; |
| 3027 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3028 | if (unicode_decode_call_errorhandler( |
| 3029 | errors, &errorHandler, |
| 3030 | "unicodeescape", message, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3031 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3032 | (PyObject **)&v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3033 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3034 | break; |
| 3035 | |
| 3036 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3037 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3038 | message = "\\ at end of string"; |
| 3039 | s--; |
| 3040 | endinpos = s-starts; |
| 3041 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3042 | if (unicode_decode_call_errorhandler( |
| 3043 | errors, &errorHandler, |
| 3044 | "unicodeescape", message, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3045 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3046 | (PyObject **)&v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3047 | goto onError; |
| 3048 | } |
| 3049 | else { |
| 3050 | *p++ = '\\'; |
| 3051 | *p++ = (unsigned char)s[-1]; |
| 3052 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3053 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3054 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3055 | nextByte: |
| 3056 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3057 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3058 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3059 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 3060 | Py_XDECREF(errorHandler); |
| 3061 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3062 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3063 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3064 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 3065 | PyErr_SetString( |
| 3066 | PyExc_UnicodeError, |
| 3067 | "\\N escapes not supported (can't load unicodedata module)" |
| 3068 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3069 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3070 | Py_XDECREF(errorHandler); |
| 3071 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 3072 | return NULL; |
| 3073 | |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3074 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3075 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3076 | Py_XDECREF(errorHandler); |
| 3077 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3078 | return NULL; |
| 3079 | } |
| 3080 | |
| 3081 | /* Return a Unicode-Escape string version of the Unicode object. |
| 3082 | |
| 3083 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 3084 | appropriate. |
| 3085 | |
| 3086 | */ |
| 3087 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3088 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
| 3089 | Py_ssize_t size, |
| 3090 | Py_UNICODE ch) |
| 3091 | { |
| 3092 | /* like wcschr, but doesn't stop at NULL characters */ |
| 3093 | |
| 3094 | while (size-- > 0) { |
| 3095 | if (*s == ch) |
| 3096 | return s; |
| 3097 | s++; |
| 3098 | } |
| 3099 | |
| 3100 | return NULL; |
| 3101 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 3102 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3103 | static const char *hexdigits = "0123456789abcdef"; |
| 3104 | |
| 3105 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
| 3106 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3107 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3108 | PyObject *repr, *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3109 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3110 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3111 | #ifdef Py_UNICODE_WIDE |
| 3112 | const Py_ssize_t expandsize = 10; |
| 3113 | #else |
| 3114 | const Py_ssize_t expandsize = 6; |
| 3115 | #endif |
| 3116 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3117 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 3118 | better to choose a different scheme. Perhaps scan the |
| 3119 | first N-chars of the string and allocate based on that size. |
| 3120 | */ |
| 3121 | /* Initial allocation is based on the longest-possible unichr |
| 3122 | escape. |
| 3123 | |
| 3124 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 3125 | unichr, so in this case it's the longest unichr escape. In |
| 3126 | narrow (UTF-16) builds this is five chars per source unichr |
| 3127 | since there are two unichrs in the surrogate pair, so in narrow |
| 3128 | (UTF-16) builds it's not the longest unichr escape. |
| 3129 | |
| 3130 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 3131 | so in the narrow (UTF-16) build case it's the longest unichr |
| 3132 | escape. |
| 3133 | */ |
| 3134 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3135 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
| 3136 | return PyErr_NoMemory(); |
| 3137 | |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 3138 | repr = PyByteArray_FromStringAndSize(NULL, |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3139 | 2 |
| 3140 | + expandsize*size |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3141 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3142 | if (repr == NULL) |
| 3143 | return NULL; |
| 3144 | |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 3145 | p = PyByteArray_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3146 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3147 | while (size-- > 0) { |
| 3148 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3149 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3150 | /* Escape backslashes */ |
| 3151 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3152 | *p++ = '\\'; |
| 3153 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3154 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3155 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3156 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 3157 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3158 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 3159 | else if (ch >= 0x10000) { |
| 3160 | *p++ = '\\'; |
| 3161 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3162 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 3163 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 3164 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 3165 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 3166 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 3167 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 3168 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 3169 | *p++ = hexdigits[ch & 0x0000000F]; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3170 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3171 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3172 | #else |
| 3173 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3174 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 3175 | Py_UNICODE ch2; |
| 3176 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3177 | |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3178 | ch2 = *s++; |
| 3179 | size--; |
| 3180 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 3181 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 3182 | *p++ = '\\'; |
| 3183 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3184 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 3185 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 3186 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 3187 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 3188 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 3189 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 3190 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 3191 | *p++ = hexdigits[ucs & 0x0000000F]; |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3192 | continue; |
| 3193 | } |
| 3194 | /* Fall through: isolated surrogates are copied as-is */ |
| 3195 | s--; |
| 3196 | size++; |
| 3197 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3198 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3199 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3200 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3201 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3202 | *p++ = '\\'; |
| 3203 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3204 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 3205 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 3206 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 3207 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3208 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3209 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3210 | /* Map special whitespace to '\t', \n', '\r' */ |
| 3211 | else if (ch == '\t') { |
| 3212 | *p++ = '\\'; |
| 3213 | *p++ = 't'; |
| 3214 | } |
| 3215 | else if (ch == '\n') { |
| 3216 | *p++ = '\\'; |
| 3217 | *p++ = 'n'; |
| 3218 | } |
| 3219 | else if (ch == '\r') { |
| 3220 | *p++ = '\\'; |
| 3221 | *p++ = 'r'; |
| 3222 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3223 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3224 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 3225 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3226 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3227 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3228 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 3229 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3230 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3231 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3232 | /* Copy everything else as-is */ |
| 3233 | else |
| 3234 | *p++ = (char) ch; |
| 3235 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3236 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3237 | result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(repr), |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 3238 | p - PyByteArray_AS_STRING(repr)); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3239 | Py_DECREF(repr); |
| 3240 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3241 | } |
| 3242 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3243 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
| 3244 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3245 | PyObject *s, *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3246 | if (!PyUnicode_Check(unicode)) { |
| 3247 | PyErr_BadArgument(); |
| 3248 | return NULL; |
| 3249 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3250 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3251 | PyUnicode_GET_SIZE(unicode)); |
| 3252 | |
| 3253 | if (!s) |
| 3254 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3255 | result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(s), |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 3256 | PyByteArray_GET_SIZE(s)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3257 | Py_DECREF(s); |
| 3258 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3259 | } |
| 3260 | |
| 3261 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 3262 | |
| 3263 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3264 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3265 | const char *errors) |
| 3266 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3267 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3268 | Py_ssize_t startinpos; |
| 3269 | Py_ssize_t endinpos; |
| 3270 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3271 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3272 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3273 | const char *end; |
| 3274 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3275 | PyObject *errorHandler = NULL; |
| 3276 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3277 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3278 | /* Escaped strings will always be longer than the resulting |
| 3279 | 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] | 3280 | length after conversion to the true value. (But decoding error |
| 3281 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3282 | v = _PyUnicode_New(size); |
| 3283 | if (v == NULL) |
| 3284 | goto onError; |
| 3285 | if (size == 0) |
| 3286 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3287 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3288 | end = s + size; |
| 3289 | while (s < end) { |
| 3290 | unsigned char c; |
Martin v. Löwis | 047c05e | 2002-03-21 08:55:28 +0000 | [diff] [blame] | 3291 | Py_UCS4 x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3292 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3293 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3294 | |
| 3295 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3296 | if (*s != '\\') { |
| 3297 | *p++ = (unsigned char)*s++; |
| 3298 | continue; |
| 3299 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3300 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3301 | |
| 3302 | /* \u-escapes are only interpreted iff the number of leading |
| 3303 | backslashes if odd */ |
| 3304 | bs = s; |
| 3305 | for (;s < end;) { |
| 3306 | if (*s != '\\') |
| 3307 | break; |
| 3308 | *p++ = (unsigned char)*s++; |
| 3309 | } |
| 3310 | if (((s - bs) & 1) == 0 || |
| 3311 | s >= end || |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3312 | (*s != 'u' && *s != 'U')) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3313 | continue; |
| 3314 | } |
| 3315 | p--; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3316 | count = *s=='u' ? 4 : 8; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3317 | s++; |
| 3318 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3319 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3320 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3321 | for (x = 0, i = 0; i < count; ++i, ++s) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3322 | c = (unsigned char)*s; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3323 | if (!ISXDIGIT(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3324 | endinpos = s-starts; |
| 3325 | if (unicode_decode_call_errorhandler( |
| 3326 | errors, &errorHandler, |
| 3327 | "rawunicodeescape", "truncated \\uXXXX", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3328 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3329 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3330 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3331 | goto nextByte; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3332 | } |
| 3333 | x = (x<<4) & ~0xF; |
| 3334 | if (c >= '0' && c <= '9') |
| 3335 | x += c - '0'; |
| 3336 | else if (c >= 'a' && c <= 'f') |
| 3337 | x += 10 + c - 'a'; |
| 3338 | else |
| 3339 | x += 10 + c - 'A'; |
| 3340 | } |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3341 | if (x <= 0xffff) |
| 3342 | /* UCS-2 character */ |
| 3343 | *p++ = (Py_UNICODE) x; |
| 3344 | else if (x <= 0x10ffff) { |
| 3345 | /* UCS-4 character. Either store directly, or as |
| 3346 | surrogate pair. */ |
| 3347 | #ifdef Py_UNICODE_WIDE |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 3348 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3349 | #else |
| 3350 | x -= 0x10000L; |
| 3351 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 3352 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
| 3353 | #endif |
| 3354 | } else { |
| 3355 | endinpos = s-starts; |
| 3356 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3357 | if (unicode_decode_call_errorhandler( |
| 3358 | errors, &errorHandler, |
| 3359 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3360 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3361 | (PyObject **)&v, &outpos, &p)) |
| 3362 | goto onError; |
| 3363 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3364 | nextByte: |
| 3365 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3366 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3367 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3368 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3369 | Py_XDECREF(errorHandler); |
| 3370 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3371 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3372 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3373 | onError: |
| 3374 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3375 | Py_XDECREF(errorHandler); |
| 3376 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3377 | return NULL; |
| 3378 | } |
| 3379 | |
| 3380 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3381 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3382 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3383 | PyObject *repr, *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3384 | char *p; |
| 3385 | char *q; |
| 3386 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3387 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3388 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3389 | #else |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3390 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3391 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3392 | |
| 3393 | if (size > PY_SSIZE_T_MAX / expandsize) |
| 3394 | return PyErr_NoMemory(); |
| 3395 | |
| 3396 | repr = PyByteArray_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3397 | if (repr == NULL) |
| 3398 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3399 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3400 | goto done; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3401 | |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 3402 | p = q = PyByteArray_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3403 | while (size-- > 0) { |
| 3404 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3405 | #ifdef Py_UNICODE_WIDE |
| 3406 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 3407 | if (ch >= 0x10000) { |
| 3408 | *p++ = '\\'; |
| 3409 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 3410 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 3411 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 3412 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 3413 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 3414 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 3415 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 3416 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 3417 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3418 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3419 | else |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3420 | #else |
| 3421 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 3422 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 3423 | Py_UNICODE ch2; |
| 3424 | Py_UCS4 ucs; |
| 3425 | |
| 3426 | ch2 = *s++; |
| 3427 | size--; |
| 3428 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
| 3429 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 3430 | *p++ = '\\'; |
| 3431 | *p++ = 'U'; |
| 3432 | *p++ = hexdigits[(ucs >> 28) & 0xf]; |
| 3433 | *p++ = hexdigits[(ucs >> 24) & 0xf]; |
| 3434 | *p++ = hexdigits[(ucs >> 20) & 0xf]; |
| 3435 | *p++ = hexdigits[(ucs >> 16) & 0xf]; |
| 3436 | *p++ = hexdigits[(ucs >> 12) & 0xf]; |
| 3437 | *p++ = hexdigits[(ucs >> 8) & 0xf]; |
| 3438 | *p++ = hexdigits[(ucs >> 4) & 0xf]; |
| 3439 | *p++ = hexdigits[ucs & 0xf]; |
| 3440 | continue; |
| 3441 | } |
| 3442 | /* Fall through: isolated surrogates are copied as-is */ |
| 3443 | s--; |
| 3444 | size++; |
| 3445 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3446 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3447 | /* Map 16-bit characters to '\uxxxx' */ |
| 3448 | if (ch >= 256) { |
| 3449 | *p++ = '\\'; |
| 3450 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 3451 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 3452 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 3453 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 3454 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3455 | } |
| 3456 | /* Copy everything else as-is */ |
| 3457 | else |
| 3458 | *p++ = (char) ch; |
| 3459 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3460 | size = p - q; |
| 3461 | |
| 3462 | done: |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3463 | result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(repr), size); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3464 | Py_DECREF(repr); |
| 3465 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3466 | } |
| 3467 | |
| 3468 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 3469 | { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3470 | PyObject *s, *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3471 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3472 | PyErr_BadArgument(); |
| 3473 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3474 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3475 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3476 | PyUnicode_GET_SIZE(unicode)); |
| 3477 | |
| 3478 | if (!s) |
| 3479 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3480 | result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(s), |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 3481 | PyByteArray_GET_SIZE(s)); |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3482 | Py_DECREF(s); |
| 3483 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3484 | } |
| 3485 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3486 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 3487 | |
| 3488 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3489 | Py_ssize_t size, |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3490 | const char *errors) |
| 3491 | { |
| 3492 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3493 | Py_ssize_t startinpos; |
| 3494 | Py_ssize_t endinpos; |
| 3495 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3496 | PyUnicodeObject *v; |
| 3497 | Py_UNICODE *p; |
| 3498 | const char *end; |
| 3499 | const char *reason; |
| 3500 | PyObject *errorHandler = NULL; |
| 3501 | PyObject *exc = NULL; |
| 3502 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 3503 | #ifdef Py_UNICODE_WIDE |
| 3504 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 3505 | #endif |
| 3506 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3507 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3508 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 3509 | if (v == NULL) |
| 3510 | goto onError; |
| 3511 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
| 3512 | return (PyObject *)v; |
| 3513 | p = PyUnicode_AS_UNICODE(v); |
| 3514 | end = s + size; |
| 3515 | |
| 3516 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3517 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3518 | /* We have to sanity check the raw data, otherwise doom looms for |
| 3519 | some malformed UCS-4 data. */ |
| 3520 | if ( |
| 3521 | #ifdef Py_UNICODE_WIDE |
| 3522 | *p > unimax || *p < 0 || |
| 3523 | #endif |
| 3524 | end-s < Py_UNICODE_SIZE |
| 3525 | ) |
| 3526 | { |
| 3527 | startinpos = s - starts; |
| 3528 | if (end-s < Py_UNICODE_SIZE) { |
| 3529 | endinpos = end-starts; |
| 3530 | reason = "truncated input"; |
| 3531 | } |
| 3532 | else { |
| 3533 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 3534 | reason = "illegal code point (> 0x10FFFF)"; |
| 3535 | } |
| 3536 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 3537 | if (unicode_decode_call_errorhandler( |
| 3538 | errors, &errorHandler, |
| 3539 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3540 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3541 | (PyObject **)&v, &outpos, &p)) { |
| 3542 | goto onError; |
| 3543 | } |
| 3544 | } |
| 3545 | else { |
| 3546 | p++; |
| 3547 | s += Py_UNICODE_SIZE; |
| 3548 | } |
| 3549 | } |
| 3550 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3551 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3552 | goto onError; |
| 3553 | Py_XDECREF(errorHandler); |
| 3554 | Py_XDECREF(exc); |
| 3555 | return (PyObject *)v; |
| 3556 | |
| 3557 | onError: |
| 3558 | Py_XDECREF(v); |
| 3559 | Py_XDECREF(errorHandler); |
| 3560 | Py_XDECREF(exc); |
| 3561 | return NULL; |
| 3562 | } |
| 3563 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3564 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 3565 | |
| 3566 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3567 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3568 | const char *errors) |
| 3569 | { |
| 3570 | PyUnicodeObject *v; |
| 3571 | Py_UNICODE *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3572 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3573 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3574 | if (size == 1) { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3575 | Py_UNICODE r = *(unsigned char*)s; |
| 3576 | return PyUnicode_FromUnicode(&r, 1); |
| 3577 | } |
| 3578 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3579 | v = _PyUnicode_New(size); |
| 3580 | if (v == NULL) |
| 3581 | goto onError; |
| 3582 | if (size == 0) |
| 3583 | return (PyObject *)v; |
| 3584 | p = PyUnicode_AS_UNICODE(v); |
| 3585 | while (size-- > 0) |
| 3586 | *p++ = (unsigned char)*s++; |
| 3587 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3588 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3589 | onError: |
| 3590 | Py_XDECREF(v); |
| 3591 | return NULL; |
| 3592 | } |
| 3593 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3594 | /* create or adjust a UnicodeEncodeError */ |
| 3595 | static void make_encode_exception(PyObject **exceptionObject, |
| 3596 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3597 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3598 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3599 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3600 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3601 | if (*exceptionObject == NULL) { |
| 3602 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 3603 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3604 | } |
| 3605 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3606 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 3607 | goto onError; |
| 3608 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 3609 | goto onError; |
| 3610 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 3611 | goto onError; |
| 3612 | return; |
| 3613 | onError: |
| 3614 | Py_DECREF(*exceptionObject); |
| 3615 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3616 | } |
| 3617 | } |
| 3618 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3619 | /* raises a UnicodeEncodeError */ |
| 3620 | static void raise_encode_exception(PyObject **exceptionObject, |
| 3621 | const char *encoding, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3622 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 3623 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3624 | const char *reason) |
| 3625 | { |
| 3626 | make_encode_exception(exceptionObject, |
| 3627 | encoding, unicode, size, startpos, endpos, reason); |
| 3628 | if (*exceptionObject != NULL) |
| 3629 | PyCodec_StrictErrors(*exceptionObject); |
| 3630 | } |
| 3631 | |
| 3632 | /* error handling callback helper: |
| 3633 | build arguments, call the callback and check the arguments, |
| 3634 | put the result into newpos and return the replacement string, which |
| 3635 | has to be freed by the caller */ |
| 3636 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
| 3637 | PyObject **errorHandler, |
| 3638 | const char *encoding, const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3639 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 3640 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 3641 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3642 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 3643 | 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] | 3644 | |
| 3645 | PyObject *restuple; |
| 3646 | PyObject *resunicode; |
| 3647 | |
| 3648 | if (*errorHandler == NULL) { |
| 3649 | *errorHandler = PyCodec_LookupError(errors); |
| 3650 | if (*errorHandler == NULL) |
| 3651 | return NULL; |
| 3652 | } |
| 3653 | |
| 3654 | make_encode_exception(exceptionObject, |
| 3655 | encoding, unicode, size, startpos, endpos, reason); |
| 3656 | if (*exceptionObject == NULL) |
| 3657 | return NULL; |
| 3658 | |
| 3659 | restuple = PyObject_CallFunctionObjArgs( |
| 3660 | *errorHandler, *exceptionObject, NULL); |
| 3661 | if (restuple == NULL) |
| 3662 | return NULL; |
| 3663 | if (!PyTuple_Check(restuple)) { |
| 3664 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 3665 | Py_DECREF(restuple); |
| 3666 | return NULL; |
| 3667 | } |
| 3668 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
| 3669 | &resunicode, newpos)) { |
| 3670 | Py_DECREF(restuple); |
| 3671 | return NULL; |
| 3672 | } |
| 3673 | if (*newpos<0) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 3674 | *newpos = size+*newpos; |
| 3675 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 3676 | 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] | 3677 | Py_DECREF(restuple); |
| 3678 | return NULL; |
| 3679 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3680 | Py_INCREF(resunicode); |
| 3681 | Py_DECREF(restuple); |
| 3682 | return resunicode; |
| 3683 | } |
| 3684 | |
| 3685 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3686 | Py_ssize_t size, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3687 | const char *errors, |
| 3688 | int limit) |
| 3689 | { |
| 3690 | /* output object */ |
| 3691 | PyObject *res; |
| 3692 | /* pointers to the beginning and end+1 of input */ |
| 3693 | const Py_UNICODE *startp = p; |
| 3694 | const Py_UNICODE *endp = p + size; |
| 3695 | /* pointer to the beginning of the unencodable characters */ |
| 3696 | /* const Py_UNICODE *badp = NULL; */ |
| 3697 | /* pointer into the output */ |
| 3698 | char *str; |
| 3699 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3700 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3701 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 3702 | 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] | 3703 | PyObject *errorHandler = NULL; |
| 3704 | PyObject *exc = NULL; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3705 | PyObject *result = NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3706 | /* the following variable is used for caching string comparisons |
| 3707 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 3708 | int known_errorHandler = -1; |
| 3709 | |
| 3710 | /* allocate enough for a simple encoding without |
| 3711 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3712 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3713 | return PyBytes_FromStringAndSize(NULL, 0); |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 3714 | res = PyByteArray_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3715 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3716 | return NULL; |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 3717 | str = PyByteArray_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3718 | ressize = size; |
| 3719 | |
| 3720 | while (p<endp) { |
| 3721 | Py_UNICODE c = *p; |
| 3722 | |
| 3723 | /* can we encode this? */ |
| 3724 | if (c<limit) { |
| 3725 | /* no overflow check, because we know that the space is enough */ |
| 3726 | *str++ = (char)c; |
| 3727 | ++p; |
| 3728 | } |
| 3729 | else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3730 | Py_ssize_t unicodepos = p-startp; |
| 3731 | Py_ssize_t requiredsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3732 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3733 | Py_ssize_t repsize; |
| 3734 | Py_ssize_t newpos; |
| 3735 | Py_ssize_t respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3736 | Py_UNICODE *uni2; |
| 3737 | /* startpos for collecting unencodable chars */ |
| 3738 | const Py_UNICODE *collstart = p; |
| 3739 | const Py_UNICODE *collend = p; |
| 3740 | /* find all unecodable characters */ |
| 3741 | while ((collend < endp) && ((*collend)>=limit)) |
| 3742 | ++collend; |
| 3743 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 3744 | if (known_errorHandler==-1) { |
| 3745 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 3746 | known_errorHandler = 1; |
| 3747 | else if (!strcmp(errors, "replace")) |
| 3748 | known_errorHandler = 2; |
| 3749 | else if (!strcmp(errors, "ignore")) |
| 3750 | known_errorHandler = 3; |
| 3751 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 3752 | known_errorHandler = 4; |
| 3753 | else |
| 3754 | known_errorHandler = 0; |
| 3755 | } |
| 3756 | switch (known_errorHandler) { |
| 3757 | case 1: /* strict */ |
| 3758 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 3759 | goto onError; |
| 3760 | case 2: /* replace */ |
| 3761 | while (collstart++<collend) |
| 3762 | *str++ = '?'; /* fall through */ |
| 3763 | case 3: /* ignore */ |
| 3764 | p = collend; |
| 3765 | break; |
| 3766 | case 4: /* xmlcharrefreplace */ |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 3767 | respos = str - PyByteArray_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3768 | /* determine replacement size (temporarily (mis)uses p) */ |
| 3769 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 3770 | if (*p<10) |
| 3771 | repsize += 2+1+1; |
| 3772 | else if (*p<100) |
| 3773 | repsize += 2+2+1; |
| 3774 | else if (*p<1000) |
| 3775 | repsize += 2+3+1; |
| 3776 | else if (*p<10000) |
| 3777 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 3778 | #ifndef Py_UNICODE_WIDE |
| 3779 | else |
| 3780 | repsize += 2+5+1; |
| 3781 | #else |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3782 | else if (*p<100000) |
| 3783 | repsize += 2+5+1; |
| 3784 | else if (*p<1000000) |
| 3785 | repsize += 2+6+1; |
| 3786 | else |
| 3787 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3788 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3789 | } |
| 3790 | requiredsize = respos+repsize+(endp-collend); |
| 3791 | if (requiredsize > ressize) { |
| 3792 | if (requiredsize<2*ressize) |
| 3793 | requiredsize = 2*ressize; |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 3794 | if (PyByteArray_Resize(res, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3795 | goto onError; |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 3796 | str = PyByteArray_AS_STRING(res) + respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3797 | ressize = requiredsize; |
| 3798 | } |
| 3799 | /* generate replacement (temporarily (mis)uses p) */ |
| 3800 | for (p = collstart; p < collend; ++p) { |
| 3801 | str += sprintf(str, "&#%d;", (int)*p); |
| 3802 | } |
| 3803 | p = collend; |
| 3804 | break; |
| 3805 | default: |
| 3806 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 3807 | encoding, reason, startp, size, &exc, |
| 3808 | collstart-startp, collend-startp, &newpos); |
| 3809 | if (repunicode == NULL) |
| 3810 | goto onError; |
| 3811 | /* need more space? (at least enough for what we |
| 3812 | have+the replacement+the rest of the string, so |
| 3813 | we won't have to check space for encodable characters) */ |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 3814 | respos = str - PyByteArray_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3815 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 3816 | requiredsize = respos+repsize+(endp-collend); |
| 3817 | if (requiredsize > ressize) { |
| 3818 | if (requiredsize<2*ressize) |
| 3819 | requiredsize = 2*ressize; |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 3820 | if (PyByteArray_Resize(res, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3821 | Py_DECREF(repunicode); |
| 3822 | goto onError; |
| 3823 | } |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 3824 | str = PyByteArray_AS_STRING(res) + respos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3825 | ressize = requiredsize; |
| 3826 | } |
| 3827 | /* check if there is anything unencodable in the replacement |
| 3828 | and copy it to the output */ |
| 3829 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 3830 | c = *uni2; |
| 3831 | if (c >= limit) { |
| 3832 | raise_encode_exception(&exc, encoding, startp, size, |
| 3833 | unicodepos, unicodepos+1, reason); |
| 3834 | Py_DECREF(repunicode); |
| 3835 | goto onError; |
| 3836 | } |
| 3837 | *str = (char)c; |
| 3838 | } |
| 3839 | p = startp + newpos; |
| 3840 | Py_DECREF(repunicode); |
| 3841 | } |
| 3842 | } |
| 3843 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3844 | result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(res), |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 3845 | str - PyByteArray_AS_STRING(res)); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3846 | onError: |
| 3847 | Py_DECREF(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3848 | Py_XDECREF(errorHandler); |
| 3849 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3850 | return result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3851 | } |
| 3852 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3853 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3854 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3855 | const char *errors) |
| 3856 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3857 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3858 | } |
| 3859 | |
| 3860 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 3861 | { |
| 3862 | if (!PyUnicode_Check(unicode)) { |
| 3863 | PyErr_BadArgument(); |
| 3864 | return NULL; |
| 3865 | } |
| 3866 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
| 3867 | PyUnicode_GET_SIZE(unicode), |
| 3868 | NULL); |
| 3869 | } |
| 3870 | |
| 3871 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 3872 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3873 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3874 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3875 | const char *errors) |
| 3876 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3877 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3878 | PyUnicodeObject *v; |
| 3879 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3880 | Py_ssize_t startinpos; |
| 3881 | Py_ssize_t endinpos; |
| 3882 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3883 | const char *e; |
| 3884 | PyObject *errorHandler = NULL; |
| 3885 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3886 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3887 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3888 | if (size == 1 && *(unsigned char*)s < 128) { |
| 3889 | Py_UNICODE r = *(unsigned char*)s; |
| 3890 | return PyUnicode_FromUnicode(&r, 1); |
| 3891 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3892 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3893 | v = _PyUnicode_New(size); |
| 3894 | if (v == NULL) |
| 3895 | goto onError; |
| 3896 | if (size == 0) |
| 3897 | return (PyObject *)v; |
| 3898 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3899 | e = s + size; |
| 3900 | while (s < e) { |
| 3901 | register unsigned char c = (unsigned char)*s; |
| 3902 | if (c < 128) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3903 | *p++ = c; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3904 | ++s; |
| 3905 | } |
| 3906 | else { |
| 3907 | startinpos = s-starts; |
| 3908 | endinpos = startinpos + 1; |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 3909 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3910 | if (unicode_decode_call_errorhandler( |
| 3911 | errors, &errorHandler, |
| 3912 | "ascii", "ordinal not in range(128)", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3913 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3914 | (PyObject **)&v, &outpos, &p)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3915 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3916 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3917 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 3918 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3919 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 3920 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3921 | Py_XDECREF(errorHandler); |
| 3922 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3923 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3924 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3925 | onError: |
| 3926 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3927 | Py_XDECREF(errorHandler); |
| 3928 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3929 | return NULL; |
| 3930 | } |
| 3931 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3932 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3933 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3934 | const char *errors) |
| 3935 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3936 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3937 | } |
| 3938 | |
| 3939 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 3940 | { |
| 3941 | if (!PyUnicode_Check(unicode)) { |
| 3942 | PyErr_BadArgument(); |
| 3943 | return NULL; |
| 3944 | } |
| 3945 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
| 3946 | PyUnicode_GET_SIZE(unicode), |
| 3947 | NULL); |
| 3948 | } |
| 3949 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3950 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3951 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 3952 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 3953 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3954 | #if SIZEOF_INT < SIZEOF_SSIZE_T |
| 3955 | #define NEED_RETRY |
| 3956 | #endif |
| 3957 | |
| 3958 | /* XXX This code is limited to "true" double-byte encodings, as |
| 3959 | a) it assumes an incomplete character consists of a single byte, and |
| 3960 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
| 3961 | encodings, see IsDBCSLeadByteEx documentation. */ |
| 3962 | |
| 3963 | static int is_dbcs_lead_byte(const char *s, int offset) |
| 3964 | { |
| 3965 | const char *curr = s + offset; |
| 3966 | |
| 3967 | if (IsDBCSLeadByte(*curr)) { |
| 3968 | const char *prev = CharPrev(s, curr); |
| 3969 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
| 3970 | } |
| 3971 | return 0; |
| 3972 | } |
| 3973 | |
| 3974 | /* |
| 3975 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 3976 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 3977 | */ |
| 3978 | static int decode_mbcs(PyUnicodeObject **v, |
| 3979 | const char *s, /* MBCS string */ |
| 3980 | int size, /* sizeof MBCS string */ |
| 3981 | int final) |
| 3982 | { |
| 3983 | Py_UNICODE *p; |
| 3984 | Py_ssize_t n = 0; |
| 3985 | int usize = 0; |
| 3986 | |
| 3987 | assert(size >= 0); |
| 3988 | |
| 3989 | /* Skip trailing lead-byte unless 'final' is set */ |
| 3990 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
| 3991 | --size; |
| 3992 | |
| 3993 | /* First get the size of the result */ |
| 3994 | if (size > 0) { |
| 3995 | usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); |
| 3996 | if (usize == 0) { |
| 3997 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 3998 | return -1; |
| 3999 | } |
| 4000 | } |
| 4001 | |
| 4002 | if (*v == NULL) { |
| 4003 | /* Create unicode object */ |
| 4004 | *v = _PyUnicode_New(usize); |
| 4005 | if (*v == NULL) |
| 4006 | return -1; |
| 4007 | } |
| 4008 | else { |
| 4009 | /* Extend unicode object */ |
| 4010 | n = PyUnicode_GET_SIZE(*v); |
| 4011 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 4012 | return -1; |
| 4013 | } |
| 4014 | |
| 4015 | /* Do the conversion */ |
| 4016 | if (size > 0) { |
| 4017 | p = PyUnicode_AS_UNICODE(*v) + n; |
| 4018 | if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { |
| 4019 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4020 | return -1; |
| 4021 | } |
| 4022 | } |
| 4023 | |
| 4024 | return size; |
| 4025 | } |
| 4026 | |
| 4027 | PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
| 4028 | Py_ssize_t size, |
| 4029 | const char *errors, |
| 4030 | Py_ssize_t *consumed) |
| 4031 | { |
| 4032 | PyUnicodeObject *v = NULL; |
| 4033 | int done; |
| 4034 | |
| 4035 | if (consumed) |
| 4036 | *consumed = 0; |
| 4037 | |
| 4038 | #ifdef NEED_RETRY |
| 4039 | retry: |
| 4040 | if (size > INT_MAX) |
| 4041 | done = decode_mbcs(&v, s, INT_MAX, 0); |
| 4042 | else |
| 4043 | #endif |
| 4044 | done = decode_mbcs(&v, s, (int)size, !consumed); |
| 4045 | |
| 4046 | if (done < 0) { |
| 4047 | Py_XDECREF(v); |
| 4048 | return NULL; |
| 4049 | } |
| 4050 | |
| 4051 | if (consumed) |
| 4052 | *consumed += done; |
| 4053 | |
| 4054 | #ifdef NEED_RETRY |
| 4055 | if (size > INT_MAX) { |
| 4056 | s += done; |
| 4057 | size -= done; |
| 4058 | goto retry; |
| 4059 | } |
| 4060 | #endif |
| 4061 | |
| 4062 | return (PyObject *)v; |
| 4063 | } |
| 4064 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4065 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4066 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4067 | const char *errors) |
| 4068 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4069 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 4070 | } |
| 4071 | |
| 4072 | /* |
| 4073 | * Convert unicode into string object (MBCS). |
| 4074 | * Returns 0 if succeed, -1 otherwise. |
| 4075 | */ |
| 4076 | static int encode_mbcs(PyObject **repr, |
| 4077 | const Py_UNICODE *p, /* unicode */ |
| 4078 | int size) /* size of unicode */ |
| 4079 | { |
| 4080 | int mbcssize = 0; |
| 4081 | Py_ssize_t n = 0; |
| 4082 | |
| 4083 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4084 | |
| 4085 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4086 | if (size > 0) { |
| 4087 | mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); |
| 4088 | if (mbcssize == 0) { |
| 4089 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4090 | return -1; |
| 4091 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4092 | } |
| 4093 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4094 | if (*repr == NULL) { |
| 4095 | /* Create string object */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4096 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4097 | if (*repr == NULL) |
| 4098 | return -1; |
| 4099 | } |
| 4100 | else { |
| 4101 | /* Extend string object */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4102 | n = PyBytes_Size(*repr); |
| 4103 | if (_PyBytes_Resize(repr, n + mbcssize) < 0) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4104 | return -1; |
| 4105 | } |
| 4106 | |
| 4107 | /* Do the conversion */ |
| 4108 | if (size > 0) { |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4109 | char *s = PyBytes_AS_STRING(*repr) + n; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4110 | if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { |
| 4111 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4112 | return -1; |
| 4113 | } |
| 4114 | } |
| 4115 | |
| 4116 | return 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4117 | } |
| 4118 | |
| 4119 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4120 | Py_ssize_t size, |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4121 | const char *errors) |
| 4122 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4123 | PyObject *repr = NULL; |
| 4124 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 4125 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4126 | #ifdef NEED_RETRY |
| 4127 | retry: |
| 4128 | if (size > INT_MAX) |
| 4129 | ret = encode_mbcs(&repr, p, INT_MAX); |
| 4130 | else |
| 4131 | #endif |
| 4132 | ret = encode_mbcs(&repr, p, (int)size); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4133 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4134 | if (ret < 0) { |
| 4135 | Py_XDECREF(repr); |
| 4136 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4137 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4138 | |
| 4139 | #ifdef NEED_RETRY |
| 4140 | if (size > INT_MAX) { |
| 4141 | p += INT_MAX; |
| 4142 | size -= INT_MAX; |
| 4143 | goto retry; |
| 4144 | } |
| 4145 | #endif |
| 4146 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4147 | return repr; |
| 4148 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4149 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 4150 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 4151 | { |
| 4152 | if (!PyUnicode_Check(unicode)) { |
| 4153 | PyErr_BadArgument(); |
| 4154 | return NULL; |
| 4155 | } |
| 4156 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
| 4157 | PyUnicode_GET_SIZE(unicode), |
| 4158 | NULL); |
| 4159 | } |
| 4160 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4161 | #undef NEED_RETRY |
| 4162 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4163 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4164 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4165 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 4166 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4167 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4168 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4169 | PyObject *mapping, |
| 4170 | const char *errors) |
| 4171 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4172 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4173 | Py_ssize_t startinpos; |
| 4174 | Py_ssize_t endinpos; |
| 4175 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4176 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4177 | PyUnicodeObject *v; |
| 4178 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4179 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4180 | PyObject *errorHandler = NULL; |
| 4181 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4182 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4183 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4184 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4185 | /* Default to Latin-1 */ |
| 4186 | if (mapping == NULL) |
| 4187 | return PyUnicode_DecodeLatin1(s, size, errors); |
| 4188 | |
| 4189 | v = _PyUnicode_New(size); |
| 4190 | if (v == NULL) |
| 4191 | goto onError; |
| 4192 | if (size == 0) |
| 4193 | return (PyObject *)v; |
| 4194 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4195 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4196 | if (PyUnicode_CheckExact(mapping)) { |
| 4197 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 4198 | maplen = PyUnicode_GET_SIZE(mapping); |
| 4199 | while (s < e) { |
| 4200 | unsigned char ch = *s; |
| 4201 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4202 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4203 | if (ch < maplen) |
| 4204 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4205 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4206 | if (x == 0xfffe) { |
| 4207 | /* undefined mapping */ |
| 4208 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4209 | startinpos = s-starts; |
| 4210 | endinpos = startinpos+1; |
| 4211 | if (unicode_decode_call_errorhandler( |
| 4212 | errors, &errorHandler, |
| 4213 | "charmap", "character maps to <undefined>", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4214 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4215 | (PyObject **)&v, &outpos, &p)) { |
| 4216 | goto onError; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 4217 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4218 | continue; |
Marc-André Lemburg | ec233e5 | 2001-01-06 14:59:58 +0000 | [diff] [blame] | 4219 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4220 | *p++ = x; |
| 4221 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4222 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4223 | } |
| 4224 | else { |
| 4225 | while (s < e) { |
| 4226 | unsigned char ch = *s; |
| 4227 | PyObject *w, *x; |
| 4228 | |
| 4229 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4230 | w = PyLong_FromLong((long)ch); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4231 | if (w == NULL) |
| 4232 | goto onError; |
| 4233 | x = PyObject_GetItem(mapping, w); |
| 4234 | Py_DECREF(w); |
| 4235 | if (x == NULL) { |
| 4236 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4237 | /* No mapping found means: mapping is undefined. */ |
| 4238 | PyErr_Clear(); |
| 4239 | x = Py_None; |
| 4240 | Py_INCREF(x); |
| 4241 | } else |
| 4242 | goto onError; |
| 4243 | } |
| 4244 | |
| 4245 | /* Apply mapping */ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4246 | if (PyLong_Check(x)) { |
| 4247 | long value = PyLong_AS_LONG(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4248 | if (value < 0 || value > 65535) { |
| 4249 | PyErr_SetString(PyExc_TypeError, |
| 4250 | "character mapping must be in range(65536)"); |
| 4251 | Py_DECREF(x); |
| 4252 | goto onError; |
| 4253 | } |
| 4254 | *p++ = (Py_UNICODE)value; |
| 4255 | } |
| 4256 | else if (x == Py_None) { |
| 4257 | /* undefined mapping */ |
| 4258 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4259 | startinpos = s-starts; |
| 4260 | endinpos = startinpos+1; |
| 4261 | if (unicode_decode_call_errorhandler( |
| 4262 | errors, &errorHandler, |
| 4263 | "charmap", "character maps to <undefined>", |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 4264 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4265 | (PyObject **)&v, &outpos, &p)) { |
| 4266 | Py_DECREF(x); |
| 4267 | goto onError; |
| 4268 | } |
Walter Dörwald | d4fff17 | 2005-11-28 22:15:56 +0000 | [diff] [blame] | 4269 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4270 | continue; |
| 4271 | } |
| 4272 | else if (PyUnicode_Check(x)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4273 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4274 | |
| 4275 | if (targetsize == 1) |
| 4276 | /* 1-1 mapping */ |
| 4277 | *p++ = *PyUnicode_AS_UNICODE(x); |
| 4278 | |
| 4279 | else if (targetsize > 1) { |
| 4280 | /* 1-n mapping */ |
| 4281 | if (targetsize > extrachars) { |
| 4282 | /* resize first */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4283 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 4284 | Py_ssize_t needed = (targetsize - extrachars) + \ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4285 | (targetsize << 2); |
| 4286 | extrachars += needed; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4287 | /* XXX overflow detection missing */ |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4288 | if (_PyUnicode_Resize(&v, |
| 4289 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 4290 | Py_DECREF(x); |
| 4291 | goto onError; |
| 4292 | } |
| 4293 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 4294 | } |
| 4295 | Py_UNICODE_COPY(p, |
| 4296 | PyUnicode_AS_UNICODE(x), |
| 4297 | targetsize); |
| 4298 | p += targetsize; |
| 4299 | extrachars -= targetsize; |
| 4300 | } |
| 4301 | /* 1-0 mapping: skip the character */ |
| 4302 | } |
| 4303 | else { |
| 4304 | /* wrong return value */ |
| 4305 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 4306 | "character mapping must return integer, None or str"); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4307 | Py_DECREF(x); |
| 4308 | goto onError; |
| 4309 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4310 | Py_DECREF(x); |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4311 | ++s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4312 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4313 | } |
| 4314 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4315 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4316 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4317 | Py_XDECREF(errorHandler); |
| 4318 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4319 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4320 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4321 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4322 | Py_XDECREF(errorHandler); |
| 4323 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4324 | Py_XDECREF(v); |
| 4325 | return NULL; |
| 4326 | } |
| 4327 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4328 | /* Charmap encoding: the lookup table */ |
| 4329 | |
| 4330 | struct encoding_map{ |
| 4331 | PyObject_HEAD |
| 4332 | unsigned char level1[32]; |
| 4333 | int count2, count3; |
| 4334 | unsigned char level23[1]; |
| 4335 | }; |
| 4336 | |
| 4337 | static PyObject* |
| 4338 | encoding_map_size(PyObject *obj, PyObject* args) |
| 4339 | { |
| 4340 | struct encoding_map *map = (struct encoding_map*)obj; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4341 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4342 | 128*map->count3); |
| 4343 | } |
| 4344 | |
| 4345 | static PyMethodDef encoding_map_methods[] = { |
| 4346 | {"size", encoding_map_size, METH_NOARGS, |
| 4347 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 4348 | { 0 } |
| 4349 | }; |
| 4350 | |
| 4351 | static void |
| 4352 | encoding_map_dealloc(PyObject* o) |
| 4353 | { |
| 4354 | PyObject_FREE(o); |
| 4355 | } |
| 4356 | |
| 4357 | static PyTypeObject EncodingMapType = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 4358 | PyVarObject_HEAD_INIT(NULL, 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4359 | "EncodingMap", /*tp_name*/ |
| 4360 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 4361 | 0, /*tp_itemsize*/ |
| 4362 | /* methods */ |
| 4363 | encoding_map_dealloc, /*tp_dealloc*/ |
| 4364 | 0, /*tp_print*/ |
| 4365 | 0, /*tp_getattr*/ |
| 4366 | 0, /*tp_setattr*/ |
| 4367 | 0, /*tp_compare*/ |
| 4368 | 0, /*tp_repr*/ |
| 4369 | 0, /*tp_as_number*/ |
| 4370 | 0, /*tp_as_sequence*/ |
| 4371 | 0, /*tp_as_mapping*/ |
| 4372 | 0, /*tp_hash*/ |
| 4373 | 0, /*tp_call*/ |
| 4374 | 0, /*tp_str*/ |
| 4375 | 0, /*tp_getattro*/ |
| 4376 | 0, /*tp_setattro*/ |
| 4377 | 0, /*tp_as_buffer*/ |
| 4378 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 4379 | 0, /*tp_doc*/ |
| 4380 | 0, /*tp_traverse*/ |
| 4381 | 0, /*tp_clear*/ |
| 4382 | 0, /*tp_richcompare*/ |
| 4383 | 0, /*tp_weaklistoffset*/ |
| 4384 | 0, /*tp_iter*/ |
| 4385 | 0, /*tp_iternext*/ |
| 4386 | encoding_map_methods, /*tp_methods*/ |
| 4387 | 0, /*tp_members*/ |
| 4388 | 0, /*tp_getset*/ |
| 4389 | 0, /*tp_base*/ |
| 4390 | 0, /*tp_dict*/ |
| 4391 | 0, /*tp_descr_get*/ |
| 4392 | 0, /*tp_descr_set*/ |
| 4393 | 0, /*tp_dictoffset*/ |
| 4394 | 0, /*tp_init*/ |
| 4395 | 0, /*tp_alloc*/ |
| 4396 | 0, /*tp_new*/ |
| 4397 | 0, /*tp_free*/ |
| 4398 | 0, /*tp_is_gc*/ |
| 4399 | }; |
| 4400 | |
| 4401 | PyObject* |
| 4402 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 4403 | { |
| 4404 | Py_UNICODE *decode; |
| 4405 | PyObject *result; |
| 4406 | struct encoding_map *mresult; |
| 4407 | int i; |
| 4408 | int need_dict = 0; |
| 4409 | unsigned char level1[32]; |
| 4410 | unsigned char level2[512]; |
| 4411 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 4412 | int count2 = 0, count3 = 0; |
| 4413 | |
| 4414 | if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { |
| 4415 | PyErr_BadArgument(); |
| 4416 | return NULL; |
| 4417 | } |
| 4418 | decode = PyUnicode_AS_UNICODE(string); |
| 4419 | memset(level1, 0xFF, sizeof level1); |
| 4420 | memset(level2, 0xFF, sizeof level2); |
| 4421 | |
| 4422 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 4423 | or if there are non-BMP characters, we need to use |
| 4424 | a mapping dictionary. */ |
| 4425 | if (decode[0] != 0) |
| 4426 | need_dict = 1; |
| 4427 | for (i = 1; i < 256; i++) { |
| 4428 | int l1, l2; |
| 4429 | if (decode[i] == 0 |
| 4430 | #ifdef Py_UNICODE_WIDE |
| 4431 | || decode[i] > 0xFFFF |
| 4432 | #endif |
| 4433 | ) { |
| 4434 | need_dict = 1; |
| 4435 | break; |
| 4436 | } |
| 4437 | if (decode[i] == 0xFFFE) |
| 4438 | /* unmapped character */ |
| 4439 | continue; |
| 4440 | l1 = decode[i] >> 11; |
| 4441 | l2 = decode[i] >> 7; |
| 4442 | if (level1[l1] == 0xFF) |
| 4443 | level1[l1] = count2++; |
| 4444 | if (level2[l2] == 0xFF) |
| 4445 | level2[l2] = count3++; |
| 4446 | } |
| 4447 | |
| 4448 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 4449 | need_dict = 1; |
| 4450 | |
| 4451 | if (need_dict) { |
| 4452 | PyObject *result = PyDict_New(); |
| 4453 | PyObject *key, *value; |
| 4454 | if (!result) |
| 4455 | return NULL; |
| 4456 | for (i = 0; i < 256; i++) { |
| 4457 | key = value = NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4458 | key = PyLong_FromLong(decode[i]); |
| 4459 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4460 | if (!key || !value) |
| 4461 | goto failed1; |
| 4462 | if (PyDict_SetItem(result, key, value) == -1) |
| 4463 | goto failed1; |
| 4464 | Py_DECREF(key); |
| 4465 | Py_DECREF(value); |
| 4466 | } |
| 4467 | return result; |
| 4468 | failed1: |
| 4469 | Py_XDECREF(key); |
| 4470 | Py_XDECREF(value); |
| 4471 | Py_DECREF(result); |
| 4472 | return NULL; |
| 4473 | } |
| 4474 | |
| 4475 | /* Create a three-level trie */ |
| 4476 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 4477 | 16*count2 + 128*count3 - 1); |
| 4478 | if (!result) |
| 4479 | return PyErr_NoMemory(); |
| 4480 | PyObject_Init(result, &EncodingMapType); |
| 4481 | mresult = (struct encoding_map*)result; |
| 4482 | mresult->count2 = count2; |
| 4483 | mresult->count3 = count3; |
| 4484 | mlevel1 = mresult->level1; |
| 4485 | mlevel2 = mresult->level23; |
| 4486 | mlevel3 = mresult->level23 + 16*count2; |
| 4487 | memcpy(mlevel1, level1, 32); |
| 4488 | memset(mlevel2, 0xFF, 16*count2); |
| 4489 | memset(mlevel3, 0, 128*count3); |
| 4490 | count3 = 0; |
| 4491 | for (i = 1; i < 256; i++) { |
| 4492 | int o1, o2, o3, i2, i3; |
| 4493 | if (decode[i] == 0xFFFE) |
| 4494 | /* unmapped character */ |
| 4495 | continue; |
| 4496 | o1 = decode[i]>>11; |
| 4497 | o2 = (decode[i]>>7) & 0xF; |
| 4498 | i2 = 16*mlevel1[o1] + o2; |
| 4499 | if (mlevel2[i2] == 0xFF) |
| 4500 | mlevel2[i2] = count3++; |
| 4501 | o3 = decode[i] & 0x7F; |
| 4502 | i3 = 128*mlevel2[i2] + o3; |
| 4503 | mlevel3[i3] = i; |
| 4504 | } |
| 4505 | return result; |
| 4506 | } |
| 4507 | |
| 4508 | static int |
| 4509 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 4510 | { |
| 4511 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 4512 | int l1 = c>>11; |
| 4513 | int l2 = (c>>7) & 0xF; |
| 4514 | int l3 = c & 0x7F; |
| 4515 | int i; |
| 4516 | |
| 4517 | #ifdef Py_UNICODE_WIDE |
| 4518 | if (c > 0xFFFF) { |
| 4519 | return -1; |
| 4520 | } |
| 4521 | #endif |
| 4522 | if (c == 0) |
| 4523 | return 0; |
| 4524 | /* level 1*/ |
| 4525 | i = map->level1[l1]; |
| 4526 | if (i == 0xFF) { |
| 4527 | return -1; |
| 4528 | } |
| 4529 | /* level 2*/ |
| 4530 | i = map->level23[16*i+l2]; |
| 4531 | if (i == 0xFF) { |
| 4532 | return -1; |
| 4533 | } |
| 4534 | /* level 3 */ |
| 4535 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 4536 | if (i == 0) { |
| 4537 | return -1; |
| 4538 | } |
| 4539 | return i; |
| 4540 | } |
| 4541 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4542 | /* Lookup the character ch in the mapping. If the character |
| 4543 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 4544 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4545 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4546 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4547 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4548 | PyObject *x; |
| 4549 | |
| 4550 | if (w == NULL) |
| 4551 | return NULL; |
| 4552 | x = PyObject_GetItem(mapping, w); |
| 4553 | Py_DECREF(w); |
| 4554 | if (x == NULL) { |
| 4555 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4556 | /* No mapping found means: mapping is undefined. */ |
| 4557 | PyErr_Clear(); |
| 4558 | x = Py_None; |
| 4559 | Py_INCREF(x); |
| 4560 | return x; |
| 4561 | } else |
| 4562 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4563 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 4564 | else if (x == Py_None) |
| 4565 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4566 | else if (PyLong_Check(x)) { |
| 4567 | long value = PyLong_AS_LONG(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4568 | if (value < 0 || value > 255) { |
| 4569 | PyErr_SetString(PyExc_TypeError, |
| 4570 | "character mapping must be in range(256)"); |
| 4571 | Py_DECREF(x); |
| 4572 | return NULL; |
| 4573 | } |
| 4574 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4575 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4576 | else if (PyBytes_Check(x)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4577 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4578 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4579 | /* wrong return value */ |
Walter Dörwald | 580ceed | 2007-05-09 10:39:19 +0000 | [diff] [blame] | 4580 | PyErr_Format(PyExc_TypeError, |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 4581 | "character mapping must return integer, bytes or None, not %.400s", |
Walter Dörwald | 580ceed | 2007-05-09 10:39:19 +0000 | [diff] [blame] | 4582 | x->ob_type->tp_name); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4583 | Py_DECREF(x); |
| 4584 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4585 | } |
| 4586 | } |
| 4587 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4588 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4589 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4590 | { |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4591 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4592 | /* exponentially overallocate to minimize reallocations */ |
| 4593 | if (requiredsize < 2*outsize) |
| 4594 | requiredsize = 2*outsize; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4595 | if (_PyBytes_Resize(outobj, requiredsize)) |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4596 | return -1; |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4597 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4598 | } |
| 4599 | |
| 4600 | typedef enum charmapencode_result { |
| 4601 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
| 4602 | }charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4603 | /* 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] | 4604 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4605 | space is available. Return a new reference to the object that |
| 4606 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 4607 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 4608 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4609 | static |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4610 | charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4611 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4612 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4613 | PyObject *rep; |
| 4614 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4615 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4616 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 4617 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4618 | int res = encoding_map_lookup(c, mapping); |
| 4619 | Py_ssize_t requiredsize = *outpos+1; |
| 4620 | if (res == -1) |
| 4621 | return enc_FAILED; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4622 | if (outsize<requiredsize) |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4623 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4624 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4625 | outstart = PyBytes_AS_STRING(*outobj); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4626 | outstart[(*outpos)++] = (char)res; |
| 4627 | return enc_SUCCESS; |
| 4628 | } |
| 4629 | |
| 4630 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4631 | if (rep==NULL) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4632 | return enc_EXCEPTION; |
| 4633 | else if (rep==Py_None) { |
| 4634 | Py_DECREF(rep); |
| 4635 | return enc_FAILED; |
| 4636 | } else { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4637 | if (PyLong_Check(rep)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4638 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4639 | if (outsize<requiredsize) |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4640 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4641 | Py_DECREF(rep); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4642 | return enc_EXCEPTION; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4643 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4644 | outstart = PyBytes_AS_STRING(*outobj); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4645 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4646 | } |
| 4647 | else { |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4648 | const char *repchars = PyBytes_AS_STRING(rep); |
| 4649 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4650 | Py_ssize_t requiredsize = *outpos+repsize; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4651 | if (outsize<requiredsize) |
Walter Dörwald | 827b055 | 2007-05-12 13:23:53 +0000 | [diff] [blame] | 4652 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4653 | Py_DECREF(rep); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4654 | return enc_EXCEPTION; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4655 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4656 | outstart = PyBytes_AS_STRING(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4657 | memcpy(outstart + *outpos, repchars, repsize); |
| 4658 | *outpos += repsize; |
| 4659 | } |
| 4660 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4661 | Py_DECREF(rep); |
| 4662 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4663 | } |
| 4664 | |
| 4665 | /* handle an error in PyUnicode_EncodeCharmap |
| 4666 | Return 0 on success, -1 on error */ |
| 4667 | static |
| 4668 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4669 | 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] | 4670 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4671 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4672 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4673 | { |
| 4674 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4675 | Py_ssize_t repsize; |
| 4676 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4677 | Py_UNICODE *uni2; |
| 4678 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4679 | Py_ssize_t collstartpos = *inpos; |
| 4680 | Py_ssize_t collendpos = *inpos+1; |
| 4681 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4682 | char *encoding = "charmap"; |
| 4683 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4684 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4685 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4686 | /* find all unencodable characters */ |
| 4687 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4688 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 4689 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4690 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 4691 | if (res != -1) |
| 4692 | break; |
| 4693 | ++collendpos; |
| 4694 | continue; |
| 4695 | } |
| 4696 | |
| 4697 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 4698 | if (rep==NULL) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4699 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4700 | else if (rep!=Py_None) { |
| 4701 | Py_DECREF(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4702 | break; |
| 4703 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4704 | Py_DECREF(rep); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4705 | ++collendpos; |
| 4706 | } |
| 4707 | /* cache callback name lookup |
| 4708 | * (if not done yet, i.e. it's the first error) */ |
| 4709 | if (*known_errorHandler==-1) { |
| 4710 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4711 | *known_errorHandler = 1; |
| 4712 | else if (!strcmp(errors, "replace")) |
| 4713 | *known_errorHandler = 2; |
| 4714 | else if (!strcmp(errors, "ignore")) |
| 4715 | *known_errorHandler = 3; |
| 4716 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4717 | *known_errorHandler = 4; |
| 4718 | else |
| 4719 | *known_errorHandler = 0; |
| 4720 | } |
| 4721 | switch (*known_errorHandler) { |
| 4722 | case 1: /* strict */ |
| 4723 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4724 | return -1; |
| 4725 | case 2: /* replace */ |
| 4726 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
| 4727 | x = charmapencode_output('?', mapping, res, respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4728 | if (x==enc_EXCEPTION) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4729 | return -1; |
| 4730 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4731 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4732 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4733 | return -1; |
| 4734 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4735 | } |
| 4736 | /* fall through */ |
| 4737 | case 3: /* ignore */ |
| 4738 | *inpos = collendpos; |
| 4739 | break; |
| 4740 | case 4: /* xmlcharrefreplace */ |
| 4741 | /* generate replacement (temporarily (mis)uses p) */ |
| 4742 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
| 4743 | char buffer[2+29+1+1]; |
| 4744 | char *cp; |
| 4745 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 4746 | for (cp = buffer; *cp; ++cp) { |
| 4747 | x = charmapencode_output(*cp, mapping, res, respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4748 | if (x==enc_EXCEPTION) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4749 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4750 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4751 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4752 | return -1; |
| 4753 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4754 | } |
| 4755 | } |
| 4756 | *inpos = collendpos; |
| 4757 | break; |
| 4758 | default: |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4759 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4760 | encoding, reason, p, size, exceptionObject, |
| 4761 | collstartpos, collendpos, &newpos); |
| 4762 | if (repunicode == NULL) |
| 4763 | return -1; |
| 4764 | /* generate replacement */ |
| 4765 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4766 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 4767 | x = charmapencode_output(*uni2, mapping, res, respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4768 | if (x==enc_EXCEPTION) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4769 | return -1; |
| 4770 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4771 | else if (x==enc_FAILED) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4772 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4773 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 4774 | return -1; |
| 4775 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4776 | } |
| 4777 | *inpos = newpos; |
| 4778 | Py_DECREF(repunicode); |
| 4779 | } |
| 4780 | return 0; |
| 4781 | } |
| 4782 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4783 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4784 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4785 | PyObject *mapping, |
| 4786 | const char *errors) |
| 4787 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4788 | /* output object */ |
| 4789 | PyObject *res = NULL; |
| 4790 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4791 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4792 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4793 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4794 | PyObject *errorHandler = NULL; |
| 4795 | PyObject *exc = NULL; |
| 4796 | /* the following variable is used for caching string comparisons |
| 4797 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 4798 | * 3=ignore, 4=xmlcharrefreplace */ |
| 4799 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4800 | |
| 4801 | /* Default to Latin-1 */ |
| 4802 | if (mapping == NULL) |
| 4803 | return PyUnicode_EncodeLatin1(p, size, errors); |
| 4804 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4805 | /* allocate enough for a simple encoding without |
| 4806 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4807 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4808 | if (res == NULL) |
| 4809 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 4810 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4811 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4812 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4813 | while (inpos<size) { |
| 4814 | /* try to encode it */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4815 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4816 | if (x==enc_EXCEPTION) /* error */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4817 | goto onError; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4818 | if (x==enc_FAILED) { /* unencodable character */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4819 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 4820 | &exc, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 4821 | &known_errorHandler, &errorHandler, errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4822 | &res, &respos)) { |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 4823 | goto onError; |
Walter Dörwald | 9b30f20 | 2003-08-15 16:26:34 +0000 | [diff] [blame] | 4824 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4825 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4826 | else |
| 4827 | /* done with this character => adjust input position */ |
| 4828 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4829 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4830 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4831 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4832 | if (respos<PyBytes_GET_SIZE(res)) |
| 4833 | _PyBytes_Resize(&res, respos); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4834 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4835 | Py_XDECREF(exc); |
| 4836 | Py_XDECREF(errorHandler); |
| 4837 | return res; |
| 4838 | |
| 4839 | onError: |
| 4840 | Py_XDECREF(res); |
| 4841 | Py_XDECREF(exc); |
| 4842 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4843 | return NULL; |
| 4844 | } |
| 4845 | |
| 4846 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
| 4847 | PyObject *mapping) |
| 4848 | { |
| 4849 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
| 4850 | PyErr_BadArgument(); |
| 4851 | return NULL; |
| 4852 | } |
| 4853 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
| 4854 | PyUnicode_GET_SIZE(unicode), |
| 4855 | mapping, |
| 4856 | NULL); |
| 4857 | } |
| 4858 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4859 | /* create or adjust a UnicodeTranslateError */ |
| 4860 | static void make_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4861 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4862 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4863 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4864 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4865 | if (*exceptionObject == NULL) { |
| 4866 | *exceptionObject = PyUnicodeTranslateError_Create( |
| 4867 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4868 | } |
| 4869 | else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4870 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 4871 | goto onError; |
| 4872 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 4873 | goto onError; |
| 4874 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 4875 | goto onError; |
| 4876 | return; |
| 4877 | onError: |
| 4878 | Py_DECREF(*exceptionObject); |
| 4879 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4880 | } |
| 4881 | } |
| 4882 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4883 | /* raises a UnicodeTranslateError */ |
| 4884 | static void raise_translate_exception(PyObject **exceptionObject, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4885 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4886 | Py_ssize_t startpos, Py_ssize_t endpos, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4887 | const char *reason) |
| 4888 | { |
| 4889 | make_translate_exception(exceptionObject, |
| 4890 | unicode, size, startpos, endpos, reason); |
| 4891 | if (*exceptionObject != NULL) |
| 4892 | PyCodec_StrictErrors(*exceptionObject); |
| 4893 | } |
| 4894 | |
| 4895 | /* error handling callback helper: |
| 4896 | build arguments, call the callback and check the arguments, |
| 4897 | put the result into newpos and return the replacement string, which |
| 4898 | has to be freed by the caller */ |
| 4899 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
| 4900 | PyObject **errorHandler, |
| 4901 | const char *reason, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4902 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 4903 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4904 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4905 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 4906 | 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] | 4907 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4908 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4909 | PyObject *restuple; |
| 4910 | PyObject *resunicode; |
| 4911 | |
| 4912 | if (*errorHandler == NULL) { |
| 4913 | *errorHandler = PyCodec_LookupError(errors); |
| 4914 | if (*errorHandler == NULL) |
| 4915 | return NULL; |
| 4916 | } |
| 4917 | |
| 4918 | make_translate_exception(exceptionObject, |
| 4919 | unicode, size, startpos, endpos, reason); |
| 4920 | if (*exceptionObject == NULL) |
| 4921 | return NULL; |
| 4922 | |
| 4923 | restuple = PyObject_CallFunctionObjArgs( |
| 4924 | *errorHandler, *exceptionObject, NULL); |
| 4925 | if (restuple == NULL) |
| 4926 | return NULL; |
| 4927 | if (!PyTuple_Check(restuple)) { |
| 4928 | PyErr_Format(PyExc_TypeError, &argparse[4]); |
| 4929 | Py_DECREF(restuple); |
| 4930 | return NULL; |
| 4931 | } |
| 4932 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4933 | &resunicode, &i_newpos)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4934 | Py_DECREF(restuple); |
| 4935 | return NULL; |
| 4936 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4937 | if (i_newpos<0) |
| 4938 | *newpos = size+i_newpos; |
| 4939 | else |
| 4940 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4941 | if (*newpos<0 || *newpos>size) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 4942 | 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] | 4943 | Py_DECREF(restuple); |
| 4944 | return NULL; |
| 4945 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4946 | Py_INCREF(resunicode); |
| 4947 | Py_DECREF(restuple); |
| 4948 | return resunicode; |
| 4949 | } |
| 4950 | |
| 4951 | /* Lookup the character ch in the mapping and put the result in result, |
| 4952 | which must be decrefed by the caller. |
| 4953 | Return 0 on success, -1 on error */ |
| 4954 | static |
| 4955 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 4956 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4957 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4958 | PyObject *x; |
| 4959 | |
| 4960 | if (w == NULL) |
| 4961 | return -1; |
| 4962 | x = PyObject_GetItem(mapping, w); |
| 4963 | Py_DECREF(w); |
| 4964 | if (x == NULL) { |
| 4965 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4966 | /* No mapping found means: use 1:1 mapping. */ |
| 4967 | PyErr_Clear(); |
| 4968 | *result = NULL; |
| 4969 | return 0; |
| 4970 | } else |
| 4971 | return -1; |
| 4972 | } |
| 4973 | else if (x == Py_None) { |
| 4974 | *result = x; |
| 4975 | return 0; |
| 4976 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4977 | else if (PyLong_Check(x)) { |
| 4978 | long value = PyLong_AS_LONG(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4979 | long max = PyUnicode_GetMax(); |
| 4980 | if (value < 0 || value > max) { |
| 4981 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 4982 | "character mapping must be in range(0x%x)", max+1); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4983 | Py_DECREF(x); |
| 4984 | return -1; |
| 4985 | } |
| 4986 | *result = x; |
| 4987 | return 0; |
| 4988 | } |
| 4989 | else if (PyUnicode_Check(x)) { |
| 4990 | *result = x; |
| 4991 | return 0; |
| 4992 | } |
| 4993 | else { |
| 4994 | /* wrong return value */ |
| 4995 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 4996 | "character mapping must return integer, None or str"); |
Walter Dörwald | 150523e | 2003-08-15 16:52:19 +0000 | [diff] [blame] | 4997 | Py_DECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4998 | return -1; |
| 4999 | } |
| 5000 | } |
| 5001 | /* ensure that *outobj is at least requiredsize characters long, |
| 5002 | if not reallocate and adjust various state variables. |
| 5003 | Return 0 on success, -1 on error */ |
| 5004 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5005 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5006 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5007 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5008 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5009 | if (requiredsize > oldsize) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5010 | /* remember old output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5011 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5012 | /* exponentially overallocate to minimize reallocations */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5013 | if (requiredsize < 2 * oldsize) |
| 5014 | requiredsize = 2 * oldsize; |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 5015 | if (_PyUnicode_Resize(outobj, requiredsize) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5016 | return -1; |
| 5017 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5018 | } |
| 5019 | return 0; |
| 5020 | } |
| 5021 | /* lookup the character, put the result in the output string and adjust |
| 5022 | various state variables. Return a new reference to the object that |
| 5023 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 5024 | undefined (in which case no character was written). |
| 5025 | The called must decref result. |
| 5026 | Return 0 on success, -1 on error. */ |
| 5027 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5028 | 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] | 5029 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5030 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5031 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5032 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5033 | return -1; |
| 5034 | if (*res==NULL) { |
| 5035 | /* not found => default to 1:1 mapping */ |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5036 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5037 | } |
| 5038 | else if (*res==Py_None) |
| 5039 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5040 | else if (PyLong_Check(*res)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5041 | /* no overflow check, because we know that the space is enough */ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5042 | *(*outp)++ = (Py_UNICODE)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5043 | } |
| 5044 | else if (PyUnicode_Check(*res)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5045 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5046 | if (repsize==1) { |
| 5047 | /* no overflow check, because we know that the space is enough */ |
| 5048 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 5049 | } |
| 5050 | else if (repsize!=0) { |
| 5051 | /* more than one character */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5052 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
Walter Dörwald | cd736e7 | 2004-02-05 17:36:00 +0000 | [diff] [blame] | 5053 | (insize - (curinp-startinp)) + |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5054 | repsize - 1; |
| 5055 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5056 | return -1; |
| 5057 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 5058 | *outp += repsize; |
| 5059 | } |
| 5060 | } |
| 5061 | else |
| 5062 | return -1; |
| 5063 | return 0; |
| 5064 | } |
| 5065 | |
| 5066 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5067 | Py_ssize_t size, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5068 | PyObject *mapping, |
| 5069 | const char *errors) |
| 5070 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5071 | /* output object */ |
| 5072 | PyObject *res = NULL; |
| 5073 | /* pointers to the beginning and end+1 of input */ |
| 5074 | const Py_UNICODE *startp = p; |
| 5075 | const Py_UNICODE *endp = p + size; |
| 5076 | /* pointer into the output */ |
| 5077 | Py_UNICODE *str; |
| 5078 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5079 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5080 | char *reason = "character maps to <undefined>"; |
| 5081 | PyObject *errorHandler = NULL; |
| 5082 | PyObject *exc = NULL; |
| 5083 | /* the following variable is used for caching string comparisons |
| 5084 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 5085 | * 3=ignore, 4=xmlcharrefreplace */ |
| 5086 | int known_errorHandler = -1; |
| 5087 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5088 | if (mapping == NULL) { |
| 5089 | PyErr_BadArgument(); |
| 5090 | return NULL; |
| 5091 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5092 | |
| 5093 | /* allocate enough for a simple 1:1 translation without |
| 5094 | replacements, if we need more, we'll resize */ |
| 5095 | res = PyUnicode_FromUnicode(NULL, size); |
| 5096 | if (res == NULL) |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5097 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5098 | if (size == 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5099 | return res; |
| 5100 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5101 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5102 | while (p<endp) { |
| 5103 | /* try to encode it */ |
| 5104 | PyObject *x = NULL; |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5105 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5106 | Py_XDECREF(x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5107 | goto onError; |
| 5108 | } |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 5109 | Py_XDECREF(x); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5110 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 5111 | ++p; |
| 5112 | else { /* untranslatable character */ |
| 5113 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5114 | Py_ssize_t repsize; |
| 5115 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5116 | Py_UNICODE *uni2; |
| 5117 | /* startpos for collecting untranslatable chars */ |
| 5118 | const Py_UNICODE *collstart = p; |
| 5119 | const Py_UNICODE *collend = p+1; |
| 5120 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5121 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5122 | /* find all untranslatable characters */ |
| 5123 | while (collend < endp) { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5124 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5125 | goto onError; |
| 5126 | Py_XDECREF(x); |
| 5127 | if (x!=Py_None) |
| 5128 | break; |
| 5129 | ++collend; |
| 5130 | } |
| 5131 | /* cache callback name lookup |
| 5132 | * (if not done yet, i.e. it's the first error) */ |
| 5133 | if (known_errorHandler==-1) { |
| 5134 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5135 | known_errorHandler = 1; |
| 5136 | else if (!strcmp(errors, "replace")) |
| 5137 | known_errorHandler = 2; |
| 5138 | else if (!strcmp(errors, "ignore")) |
| 5139 | known_errorHandler = 3; |
| 5140 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5141 | known_errorHandler = 4; |
| 5142 | else |
| 5143 | known_errorHandler = 0; |
| 5144 | } |
| 5145 | switch (known_errorHandler) { |
| 5146 | case 1: /* strict */ |
| 5147 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
| 5148 | goto onError; |
| 5149 | case 2: /* replace */ |
| 5150 | /* No need to check for space, this is a 1:1 replacement */ |
| 5151 | for (coll = collstart; coll<collend; ++coll) |
| 5152 | *str++ = '?'; |
| 5153 | /* fall through */ |
| 5154 | case 3: /* ignore */ |
| 5155 | p = collend; |
| 5156 | break; |
| 5157 | case 4: /* xmlcharrefreplace */ |
| 5158 | /* generate replacement (temporarily (mis)uses p) */ |
| 5159 | for (p = collstart; p < collend; ++p) { |
| 5160 | char buffer[2+29+1+1]; |
| 5161 | char *cp; |
| 5162 | sprintf(buffer, "&#%d;", (int)*p); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5163 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5164 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 5165 | goto onError; |
| 5166 | for (cp = buffer; *cp; ++cp) |
| 5167 | *str++ = *cp; |
| 5168 | } |
| 5169 | p = collend; |
| 5170 | break; |
| 5171 | default: |
| 5172 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 5173 | reason, startp, size, &exc, |
| 5174 | collstart-startp, collend-startp, &newpos); |
| 5175 | if (repunicode == NULL) |
| 5176 | goto onError; |
| 5177 | /* generate replacement */ |
| 5178 | repsize = PyUnicode_GET_SIZE(repunicode); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5179 | if (charmaptranslate_makespace(&res, &str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5180 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 5181 | Py_DECREF(repunicode); |
| 5182 | goto onError; |
| 5183 | } |
| 5184 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 5185 | *str++ = *uni2; |
| 5186 | p = startp + newpos; |
| 5187 | Py_DECREF(repunicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5188 | } |
| 5189 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5190 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5191 | /* Resize if we allocated to much */ |
| 5192 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5193 | if (respos<PyUnicode_GET_SIZE(res)) { |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 5194 | if (_PyUnicode_Resize(&res, respos) < 0) |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 5195 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5196 | } |
| 5197 | Py_XDECREF(exc); |
| 5198 | Py_XDECREF(errorHandler); |
| 5199 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5200 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5201 | onError: |
| 5202 | Py_XDECREF(res); |
| 5203 | Py_XDECREF(exc); |
| 5204 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5205 | return NULL; |
| 5206 | } |
| 5207 | |
| 5208 | PyObject *PyUnicode_Translate(PyObject *str, |
| 5209 | PyObject *mapping, |
| 5210 | const char *errors) |
| 5211 | { |
| 5212 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5213 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5214 | str = PyUnicode_FromObject(str); |
| 5215 | if (str == NULL) |
| 5216 | goto onError; |
| 5217 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
| 5218 | PyUnicode_GET_SIZE(str), |
| 5219 | mapping, |
| 5220 | errors); |
| 5221 | Py_DECREF(str); |
| 5222 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5223 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5224 | onError: |
| 5225 | Py_XDECREF(str); |
| 5226 | return NULL; |
| 5227 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5228 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5229 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 5230 | |
| 5231 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5232 | Py_ssize_t length, |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5233 | char *output, |
| 5234 | const char *errors) |
| 5235 | { |
| 5236 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5237 | PyObject *errorHandler = NULL; |
| 5238 | PyObject *exc = NULL; |
| 5239 | const char *encoding = "decimal"; |
| 5240 | const char *reason = "invalid decimal Unicode string"; |
| 5241 | /* the following variable is used for caching string comparisons |
| 5242 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 5243 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5244 | |
| 5245 | if (output == NULL) { |
| 5246 | PyErr_BadArgument(); |
| 5247 | return -1; |
| 5248 | } |
| 5249 | |
| 5250 | p = s; |
| 5251 | end = s + length; |
| 5252 | while (p < end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5253 | register Py_UNICODE ch = *p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5254 | int decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5255 | PyObject *repunicode; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5256 | Py_ssize_t repsize; |
| 5257 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5258 | Py_UNICODE *uni2; |
| 5259 | Py_UNICODE *collstart; |
| 5260 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5261 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5262 | if (Py_UNICODE_ISSPACE(ch)) { |
| 5263 | *output++ = ' '; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5264 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5265 | continue; |
| 5266 | } |
| 5267 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5268 | if (decimal >= 0) { |
| 5269 | *output++ = '0' + decimal; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5270 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5271 | continue; |
| 5272 | } |
Guido van Rossum | ba47704 | 2000-04-06 18:18:10 +0000 | [diff] [blame] | 5273 | if (0 < ch && ch < 256) { |
Guido van Rossum | 42c29aa | 2000-05-03 23:58:29 +0000 | [diff] [blame] | 5274 | *output++ = (char)ch; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5275 | ++p; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5276 | continue; |
| 5277 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5278 | /* All other characters are considered unencodable */ |
| 5279 | collstart = p; |
| 5280 | collend = p+1; |
| 5281 | while (collend < end) { |
| 5282 | if ((0 < *collend && *collend < 256) || |
| 5283 | !Py_UNICODE_ISSPACE(*collend) || |
| 5284 | Py_UNICODE_TODECIMAL(*collend)) |
| 5285 | break; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5286 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5287 | /* cache callback name lookup |
| 5288 | * (if not done yet, i.e. it's the first error) */ |
| 5289 | if (known_errorHandler==-1) { |
| 5290 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5291 | known_errorHandler = 1; |
| 5292 | else if (!strcmp(errors, "replace")) |
| 5293 | known_errorHandler = 2; |
| 5294 | else if (!strcmp(errors, "ignore")) |
| 5295 | known_errorHandler = 3; |
| 5296 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5297 | known_errorHandler = 4; |
| 5298 | else |
| 5299 | known_errorHandler = 0; |
| 5300 | } |
| 5301 | switch (known_errorHandler) { |
| 5302 | case 1: /* strict */ |
| 5303 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 5304 | goto onError; |
| 5305 | case 2: /* replace */ |
| 5306 | for (p = collstart; p < collend; ++p) |
| 5307 | *output++ = '?'; |
| 5308 | /* fall through */ |
| 5309 | case 3: /* ignore */ |
| 5310 | p = collend; |
| 5311 | break; |
| 5312 | case 4: /* xmlcharrefreplace */ |
| 5313 | /* generate replacement (temporarily (mis)uses p) */ |
| 5314 | for (p = collstart; p < collend; ++p) |
| 5315 | output += sprintf(output, "&#%d;", (int)*p); |
| 5316 | p = collend; |
| 5317 | break; |
| 5318 | default: |
| 5319 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 5320 | encoding, reason, s, length, &exc, |
| 5321 | collstart-s, collend-s, &newpos); |
| 5322 | if (repunicode == NULL) |
| 5323 | goto onError; |
| 5324 | /* generate replacement */ |
| 5325 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5326 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 5327 | Py_UNICODE ch = *uni2; |
| 5328 | if (Py_UNICODE_ISSPACE(ch)) |
| 5329 | *output++ = ' '; |
| 5330 | else { |
| 5331 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5332 | if (decimal >= 0) |
| 5333 | *output++ = '0' + decimal; |
| 5334 | else if (0 < ch && ch < 256) |
| 5335 | *output++ = (char)ch; |
| 5336 | else { |
| 5337 | Py_DECREF(repunicode); |
| 5338 | raise_encode_exception(&exc, encoding, |
| 5339 | s, length, collstart-s, collend-s, reason); |
| 5340 | goto onError; |
| 5341 | } |
| 5342 | } |
| 5343 | } |
| 5344 | p = s + newpos; |
| 5345 | Py_DECREF(repunicode); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5346 | } |
| 5347 | } |
| 5348 | /* 0-terminate the output string */ |
| 5349 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5350 | Py_XDECREF(exc); |
| 5351 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5352 | return 0; |
| 5353 | |
| 5354 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5355 | Py_XDECREF(exc); |
| 5356 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5357 | return -1; |
| 5358 | } |
| 5359 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5360 | /* --- Helpers ------------------------------------------------------------ */ |
| 5361 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 5362 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5363 | #include "stringlib/fastsearch.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5364 | #include "stringlib/count.h" |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 5365 | /* Include _ParseTupleFinds from find.h */ |
| 5366 | #define FROM_UNICODE |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5367 | #include "stringlib/find.h" |
| 5368 | #include "stringlib/partition.h" |
| 5369 | |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 5370 | #define _Py_InsertThousandsGrouping _PyUnicode_InsertThousandsGrouping |
| 5371 | #include "stringlib/localeutil.h" |
| 5372 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5373 | /* helper macro to fixup start/end slice values */ |
| 5374 | #define FIX_START_END(obj) \ |
| 5375 | if (start < 0) \ |
| 5376 | start += (obj)->length; \ |
| 5377 | if (start < 0) \ |
| 5378 | start = 0; \ |
| 5379 | if (end > (obj)->length) \ |
| 5380 | end = (obj)->length; \ |
| 5381 | if (end < 0) \ |
| 5382 | end += (obj)->length; \ |
| 5383 | if (end < 0) \ |
| 5384 | end = 0; |
| 5385 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5386 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5387 | PyObject *substr, |
| 5388 | Py_ssize_t start, |
| 5389 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5390 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5391 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5392 | PyUnicodeObject* str_obj; |
| 5393 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5394 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5395 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 5396 | if (!str_obj) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5397 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5398 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 5399 | if (!sub_obj) { |
| 5400 | Py_DECREF(str_obj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5401 | return -1; |
| 5402 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5403 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5404 | FIX_START_END(str_obj); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5405 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5406 | result = stringlib_count( |
| 5407 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length |
| 5408 | ); |
| 5409 | |
| 5410 | Py_DECREF(sub_obj); |
| 5411 | Py_DECREF(str_obj); |
| 5412 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5413 | return result; |
| 5414 | } |
| 5415 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5416 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5417 | PyObject *sub, |
| 5418 | Py_ssize_t start, |
| 5419 | Py_ssize_t end, |
| 5420 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5421 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5422 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5423 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5424 | str = PyUnicode_FromObject(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5425 | if (!str) |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 5426 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5427 | sub = PyUnicode_FromObject(sub); |
| 5428 | if (!sub) { |
Marc-André Lemburg | 4164439 | 2002-05-29 13:46:29 +0000 | [diff] [blame] | 5429 | Py_DECREF(str); |
Marc-André Lemburg | 4da6fd6 | 2002-05-29 11:33:13 +0000 | [diff] [blame] | 5430 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5431 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5432 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5433 | if (direction > 0) |
| 5434 | result = stringlib_find_slice( |
| 5435 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5436 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5437 | start, end |
| 5438 | ); |
| 5439 | else |
| 5440 | result = stringlib_rfind_slice( |
| 5441 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5442 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5443 | start, end |
| 5444 | ); |
| 5445 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5446 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5447 | Py_DECREF(sub); |
| 5448 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5449 | return result; |
| 5450 | } |
| 5451 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5452 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5453 | int tailmatch(PyUnicodeObject *self, |
| 5454 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5455 | Py_ssize_t start, |
| 5456 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5457 | int direction) |
| 5458 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5459 | if (substring->length == 0) |
| 5460 | return 1; |
| 5461 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5462 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5463 | |
| 5464 | end -= substring->length; |
| 5465 | if (end < start) |
| 5466 | return 0; |
| 5467 | |
| 5468 | if (direction > 0) { |
| 5469 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 5470 | return 1; |
| 5471 | } else { |
| 5472 | if (Py_UNICODE_MATCH(self, start, substring)) |
| 5473 | return 1; |
| 5474 | } |
| 5475 | |
| 5476 | return 0; |
| 5477 | } |
| 5478 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5479 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5480 | PyObject *substr, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5481 | Py_ssize_t start, |
| 5482 | Py_ssize_t end, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5483 | int direction) |
| 5484 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5485 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5486 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5487 | str = PyUnicode_FromObject(str); |
| 5488 | if (str == NULL) |
| 5489 | return -1; |
| 5490 | substr = PyUnicode_FromObject(substr); |
| 5491 | if (substr == NULL) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5492 | Py_DECREF(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5493 | return -1; |
| 5494 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5495 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5496 | result = tailmatch((PyUnicodeObject *)str, |
| 5497 | (PyUnicodeObject *)substr, |
| 5498 | start, end, direction); |
| 5499 | Py_DECREF(str); |
| 5500 | Py_DECREF(substr); |
| 5501 | return result; |
| 5502 | } |
| 5503 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5504 | /* Apply fixfct filter to the Unicode object self and return a |
| 5505 | reference to the modified object */ |
| 5506 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5507 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5508 | PyObject *fixup(PyUnicodeObject *self, |
| 5509 | int (*fixfct)(PyUnicodeObject *s)) |
| 5510 | { |
| 5511 | |
| 5512 | PyUnicodeObject *u; |
| 5513 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5514 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5515 | if (u == NULL) |
| 5516 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5517 | |
| 5518 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5519 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5520 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5521 | /* fixfct should return TRUE if it modified the buffer. If |
| 5522 | FALSE, return a reference to the original buffer instead |
| 5523 | (to save space, not time) */ |
| 5524 | Py_INCREF(self); |
| 5525 | Py_DECREF(u); |
| 5526 | return (PyObject*) self; |
| 5527 | } |
| 5528 | return (PyObject*) u; |
| 5529 | } |
| 5530 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5531 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5532 | int fixupper(PyUnicodeObject *self) |
| 5533 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5534 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5535 | Py_UNICODE *s = self->str; |
| 5536 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5537 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5538 | while (len-- > 0) { |
| 5539 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5540 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5541 | ch = Py_UNICODE_TOUPPER(*s); |
| 5542 | if (ch != *s) { |
| 5543 | status = 1; |
| 5544 | *s = ch; |
| 5545 | } |
| 5546 | s++; |
| 5547 | } |
| 5548 | |
| 5549 | return status; |
| 5550 | } |
| 5551 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5552 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5553 | int fixlower(PyUnicodeObject *self) |
| 5554 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5555 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5556 | Py_UNICODE *s = self->str; |
| 5557 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5558 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5559 | while (len-- > 0) { |
| 5560 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5561 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5562 | ch = Py_UNICODE_TOLOWER(*s); |
| 5563 | if (ch != *s) { |
| 5564 | status = 1; |
| 5565 | *s = ch; |
| 5566 | } |
| 5567 | s++; |
| 5568 | } |
| 5569 | |
| 5570 | return status; |
| 5571 | } |
| 5572 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5573 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5574 | int fixswapcase(PyUnicodeObject *self) |
| 5575 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5576 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5577 | Py_UNICODE *s = self->str; |
| 5578 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5579 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5580 | while (len-- > 0) { |
| 5581 | if (Py_UNICODE_ISUPPER(*s)) { |
| 5582 | *s = Py_UNICODE_TOLOWER(*s); |
| 5583 | status = 1; |
| 5584 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 5585 | *s = Py_UNICODE_TOUPPER(*s); |
| 5586 | status = 1; |
| 5587 | } |
| 5588 | s++; |
| 5589 | } |
| 5590 | |
| 5591 | return status; |
| 5592 | } |
| 5593 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5594 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5595 | int fixcapitalize(PyUnicodeObject *self) |
| 5596 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5597 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5598 | Py_UNICODE *s = self->str; |
| 5599 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5600 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5601 | if (len == 0) |
| 5602 | return 0; |
| 5603 | if (Py_UNICODE_ISLOWER(*s)) { |
| 5604 | *s = Py_UNICODE_TOUPPER(*s); |
| 5605 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5606 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 5607 | s++; |
| 5608 | while (--len > 0) { |
| 5609 | if (Py_UNICODE_ISUPPER(*s)) { |
| 5610 | *s = Py_UNICODE_TOLOWER(*s); |
| 5611 | status = 1; |
| 5612 | } |
| 5613 | s++; |
| 5614 | } |
| 5615 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5616 | } |
| 5617 | |
| 5618 | static |
| 5619 | int fixtitle(PyUnicodeObject *self) |
| 5620 | { |
| 5621 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 5622 | register Py_UNICODE *e; |
| 5623 | int previous_is_cased; |
| 5624 | |
| 5625 | /* Shortcut for single character strings */ |
| 5626 | if (PyUnicode_GET_SIZE(self) == 1) { |
| 5627 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 5628 | if (*p != ch) { |
| 5629 | *p = ch; |
| 5630 | return 1; |
| 5631 | } |
| 5632 | else |
| 5633 | return 0; |
| 5634 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5635 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5636 | e = p + PyUnicode_GET_SIZE(self); |
| 5637 | previous_is_cased = 0; |
| 5638 | for (; p < e; p++) { |
| 5639 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5640 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5641 | if (previous_is_cased) |
| 5642 | *p = Py_UNICODE_TOLOWER(ch); |
| 5643 | else |
| 5644 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5645 | |
| 5646 | if (Py_UNICODE_ISLOWER(ch) || |
| 5647 | Py_UNICODE_ISUPPER(ch) || |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5648 | Py_UNICODE_ISTITLE(ch)) |
| 5649 | previous_is_cased = 1; |
| 5650 | else |
| 5651 | previous_is_cased = 0; |
| 5652 | } |
| 5653 | return 1; |
| 5654 | } |
| 5655 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5656 | PyObject * |
| 5657 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5658 | { |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 5659 | const Py_UNICODE blank = ' '; |
| 5660 | const Py_UNICODE *sep = ␣ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5661 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5662 | PyUnicodeObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5663 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 5664 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5665 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 5666 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5667 | PyObject *item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5668 | Py_ssize_t sz, i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5669 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5670 | fseq = PySequence_Fast(seq, ""); |
| 5671 | if (fseq == NULL) { |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5672 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5673 | } |
| 5674 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5675 | /* NOTE: the following code can't call back into Python code, |
| 5676 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5677 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5678 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5679 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 5680 | /* If empty sequence, return u"". */ |
| 5681 | if (seqlen == 0) { |
| 5682 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 5683 | goto Done; |
| 5684 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5685 | items = PySequence_Fast_ITEMS(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5686 | /* If singleton sequence with an exact Unicode, return that. */ |
| 5687 | if (seqlen == 1) { |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5688 | item = items[0]; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5689 | if (PyUnicode_CheckExact(item)) { |
| 5690 | Py_INCREF(item); |
| 5691 | res = (PyUnicodeObject *)item; |
| 5692 | goto Done; |
| 5693 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5694 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5695 | else { |
| 5696 | /* Set up sep and seplen */ |
| 5697 | if (separator == NULL) { |
| 5698 | sep = ␣ |
| 5699 | seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5700 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5701 | else { |
| 5702 | if (!PyUnicode_Check(separator)) { |
| 5703 | PyErr_Format(PyExc_TypeError, |
| 5704 | "separator: expected str instance," |
| 5705 | " %.80s found", |
| 5706 | Py_TYPE(separator)->tp_name); |
| 5707 | goto onError; |
| 5708 | } |
| 5709 | sep = PyUnicode_AS_UNICODE(separator); |
| 5710 | seplen = PyUnicode_GET_SIZE(separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5711 | } |
| 5712 | } |
| 5713 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5714 | /* There are at least two things to join, or else we have a subclass |
| 5715 | * of str in the sequence. |
| 5716 | * Do a pre-pass to figure out the total amount of space we'll |
| 5717 | * need (sz), and see whether all argument are strings. |
| 5718 | */ |
| 5719 | sz = 0; |
| 5720 | for (i = 0; i < seqlen; i++) { |
| 5721 | const Py_ssize_t old_sz = sz; |
| 5722 | item = items[i]; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5723 | if (!PyUnicode_Check(item)) { |
| 5724 | PyErr_Format(PyExc_TypeError, |
| 5725 | "sequence item %zd: expected str instance," |
| 5726 | " %.80s found", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5727 | i, Py_TYPE(item)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5728 | goto onError; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5729 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5730 | sz += PyUnicode_GET_SIZE(item); |
| 5731 | if (i != 0) |
| 5732 | sz += seplen; |
| 5733 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 5734 | PyErr_SetString(PyExc_OverflowError, |
| 5735 | "join() result is too long for a Python string"); |
| 5736 | goto onError; |
| 5737 | } |
| 5738 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5739 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5740 | res = _PyUnicode_New(sz); |
| 5741 | if (res == NULL) |
| 5742 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 5743 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5744 | /* Catenate everything. */ |
| 5745 | res_p = PyUnicode_AS_UNICODE(res); |
| 5746 | for (i = 0; i < seqlen; ++i) { |
| 5747 | Py_ssize_t itemlen; |
| 5748 | item = items[i]; |
| 5749 | itemlen = PyUnicode_GET_SIZE(item); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5750 | /* Copy item, and maybe the separator. */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5751 | if (i) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5752 | Py_UNICODE_COPY(res_p, sep, seplen); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5753 | res_p += seplen; |
| 5754 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 5755 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
| 5756 | res_p += itemlen; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5757 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5758 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5759 | Done: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5760 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5761 | return (PyObject *)res; |
| 5762 | |
| 5763 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 5764 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 5765 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5766 | return NULL; |
| 5767 | } |
| 5768 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5769 | static |
| 5770 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5771 | Py_ssize_t left, |
| 5772 | Py_ssize_t right, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5773 | Py_UNICODE fill) |
| 5774 | { |
| 5775 | PyUnicodeObject *u; |
| 5776 | |
| 5777 | if (left < 0) |
| 5778 | left = 0; |
| 5779 | if (right < 0) |
| 5780 | right = 0; |
| 5781 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5782 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5783 | Py_INCREF(self); |
| 5784 | return self; |
| 5785 | } |
| 5786 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 5787 | if (left > PY_SSIZE_T_MAX - self->length || |
| 5788 | right > PY_SSIZE_T_MAX - (left + self->length)) { |
| 5789 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 5790 | return NULL; |
| 5791 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5792 | u = _PyUnicode_New(left + self->length + right); |
| 5793 | if (u) { |
| 5794 | if (left) |
| 5795 | Py_UNICODE_FILL(u->str, fill, left); |
| 5796 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 5797 | if (right) |
| 5798 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 5799 | } |
| 5800 | |
| 5801 | return u; |
| 5802 | } |
| 5803 | |
| 5804 | #define SPLIT_APPEND(data, left, right) \ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5805 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5806 | if (!str) \ |
| 5807 | goto onError; \ |
| 5808 | if (PyList_Append(list, str)) { \ |
| 5809 | Py_DECREF(str); \ |
| 5810 | goto onError; \ |
| 5811 | } \ |
| 5812 | else \ |
| 5813 | Py_DECREF(str); |
| 5814 | |
| 5815 | static |
| 5816 | PyObject *split_whitespace(PyUnicodeObject *self, |
| 5817 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5818 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5819 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5820 | register Py_ssize_t i; |
| 5821 | register Py_ssize_t j; |
| 5822 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5823 | PyObject *str; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5824 | register const Py_UNICODE *buf = self->str; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5825 | |
| 5826 | for (i = j = 0; i < len; ) { |
| 5827 | /* find a token */ |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5828 | while (i < len && Py_UNICODE_ISSPACE(buf[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5829 | i++; |
| 5830 | j = i; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5831 | while (i < len && !Py_UNICODE_ISSPACE(buf[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5832 | i++; |
| 5833 | if (j < i) { |
| 5834 | if (maxcount-- <= 0) |
| 5835 | break; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5836 | SPLIT_APPEND(buf, j, i); |
| 5837 | while (i < len && Py_UNICODE_ISSPACE(buf[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5838 | i++; |
| 5839 | j = i; |
| 5840 | } |
| 5841 | } |
| 5842 | if (j < len) { |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5843 | SPLIT_APPEND(buf, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5844 | } |
| 5845 | return list; |
| 5846 | |
| 5847 | onError: |
| 5848 | Py_DECREF(list); |
| 5849 | return NULL; |
| 5850 | } |
| 5851 | |
| 5852 | PyObject *PyUnicode_Splitlines(PyObject *string, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5853 | int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5854 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5855 | register Py_ssize_t i; |
| 5856 | register Py_ssize_t j; |
| 5857 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5858 | PyObject *list; |
| 5859 | PyObject *str; |
| 5860 | Py_UNICODE *data; |
| 5861 | |
| 5862 | string = PyUnicode_FromObject(string); |
| 5863 | if (string == NULL) |
| 5864 | return NULL; |
| 5865 | data = PyUnicode_AS_UNICODE(string); |
| 5866 | len = PyUnicode_GET_SIZE(string); |
| 5867 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5868 | list = PyList_New(0); |
| 5869 | if (!list) |
| 5870 | goto onError; |
| 5871 | |
| 5872 | for (i = j = 0; i < len; ) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5873 | Py_ssize_t eol; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5874 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5875 | /* Find a line and append it */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5876 | while (i < len && !BLOOM_LINEBREAK(data[i])) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5877 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5878 | |
| 5879 | /* Skip the line break reading CRLF as one line break */ |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5880 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5881 | if (i < len) { |
| 5882 | if (data[i] == '\r' && i + 1 < len && |
| 5883 | data[i+1] == '\n') |
| 5884 | i += 2; |
| 5885 | else |
| 5886 | i++; |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5887 | if (keepends) |
| 5888 | eol = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5889 | } |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 5890 | SPLIT_APPEND(data, j, eol); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5891 | j = i; |
| 5892 | } |
| 5893 | if (j < len) { |
| 5894 | SPLIT_APPEND(data, j, len); |
| 5895 | } |
| 5896 | |
| 5897 | Py_DECREF(string); |
| 5898 | return list; |
| 5899 | |
| 5900 | onError: |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 5901 | Py_XDECREF(list); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5902 | Py_DECREF(string); |
| 5903 | return NULL; |
| 5904 | } |
| 5905 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5906 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5907 | PyObject *split_char(PyUnicodeObject *self, |
| 5908 | PyObject *list, |
| 5909 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5910 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5911 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5912 | register Py_ssize_t i; |
| 5913 | register Py_ssize_t j; |
| 5914 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5915 | PyObject *str; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5916 | register const Py_UNICODE *buf = self->str; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5917 | |
| 5918 | for (i = j = 0; i < len; ) { |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5919 | if (buf[i] == ch) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5920 | if (maxcount-- <= 0) |
| 5921 | break; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5922 | SPLIT_APPEND(buf, j, i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5923 | i = j = i + 1; |
| 5924 | } else |
| 5925 | i++; |
| 5926 | } |
| 5927 | if (j <= len) { |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5928 | SPLIT_APPEND(buf, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5929 | } |
| 5930 | return list; |
| 5931 | |
| 5932 | onError: |
| 5933 | Py_DECREF(list); |
| 5934 | return NULL; |
| 5935 | } |
| 5936 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5937 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5938 | PyObject *split_substring(PyUnicodeObject *self, |
| 5939 | PyObject *list, |
| 5940 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5941 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5942 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5943 | register Py_ssize_t i; |
| 5944 | register Py_ssize_t j; |
| 5945 | Py_ssize_t len = self->length; |
| 5946 | Py_ssize_t sublen = substring->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5947 | PyObject *str; |
| 5948 | |
Guido van Rossum | cda4f9a | 2000-12-19 02:23:19 +0000 | [diff] [blame] | 5949 | for (i = j = 0; i <= len - sublen; ) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5950 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 5951 | if (maxcount-- <= 0) |
| 5952 | break; |
| 5953 | SPLIT_APPEND(self->str, j, i); |
| 5954 | i = j = i + sublen; |
| 5955 | } else |
| 5956 | i++; |
| 5957 | } |
| 5958 | if (j <= len) { |
| 5959 | SPLIT_APPEND(self->str, j, len); |
| 5960 | } |
| 5961 | return list; |
| 5962 | |
| 5963 | onError: |
| 5964 | Py_DECREF(list); |
| 5965 | return NULL; |
| 5966 | } |
| 5967 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5968 | static |
| 5969 | PyObject *rsplit_whitespace(PyUnicodeObject *self, |
| 5970 | PyObject *list, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5971 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5972 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5973 | register Py_ssize_t i; |
| 5974 | register Py_ssize_t j; |
| 5975 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5976 | PyObject *str; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5977 | register const Py_UNICODE *buf = self->str; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5978 | |
| 5979 | for (i = j = len - 1; i >= 0; ) { |
| 5980 | /* find a token */ |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5981 | while (i >= 0 && Py_UNICODE_ISSPACE(buf[i])) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5982 | i--; |
| 5983 | j = i; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5984 | while (i >= 0 && !Py_UNICODE_ISSPACE(buf[i])) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5985 | i--; |
| 5986 | if (j > i) { |
| 5987 | if (maxcount-- <= 0) |
| 5988 | break; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5989 | SPLIT_APPEND(buf, i + 1, j + 1); |
| 5990 | while (i >= 0 && Py_UNICODE_ISSPACE(buf[i])) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5991 | i--; |
| 5992 | j = i; |
| 5993 | } |
| 5994 | } |
| 5995 | if (j >= 0) { |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 5996 | SPLIT_APPEND(buf, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 5997 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5998 | if (PyList_Reverse(list) < 0) |
| 5999 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6000 | return list; |
| 6001 | |
| 6002 | onError: |
| 6003 | Py_DECREF(list); |
| 6004 | return NULL; |
| 6005 | } |
| 6006 | |
| 6007 | static |
| 6008 | PyObject *rsplit_char(PyUnicodeObject *self, |
| 6009 | PyObject *list, |
| 6010 | Py_UNICODE ch, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6011 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6012 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6013 | register Py_ssize_t i; |
| 6014 | register Py_ssize_t j; |
| 6015 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6016 | PyObject *str; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6017 | register const Py_UNICODE *buf = self->str; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6018 | |
| 6019 | for (i = j = len - 1; i >= 0; ) { |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6020 | if (buf[i] == ch) { |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6021 | if (maxcount-- <= 0) |
| 6022 | break; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6023 | SPLIT_APPEND(buf, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6024 | j = i = i - 1; |
| 6025 | } else |
| 6026 | i--; |
| 6027 | } |
Hye-Shik Chang | 7fc4cf5 | 2003-12-23 09:10:16 +0000 | [diff] [blame] | 6028 | if (j >= -1) { |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6029 | SPLIT_APPEND(buf, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6030 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6031 | if (PyList_Reverse(list) < 0) |
| 6032 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6033 | return list; |
| 6034 | |
| 6035 | onError: |
| 6036 | Py_DECREF(list); |
| 6037 | return NULL; |
| 6038 | } |
| 6039 | |
| 6040 | static |
| 6041 | PyObject *rsplit_substring(PyUnicodeObject *self, |
| 6042 | PyObject *list, |
| 6043 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6044 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6045 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6046 | register Py_ssize_t i; |
| 6047 | register Py_ssize_t j; |
| 6048 | Py_ssize_t len = self->length; |
| 6049 | Py_ssize_t sublen = substring->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6050 | PyObject *str; |
| 6051 | |
| 6052 | for (i = len - sublen, j = len; i >= 0; ) { |
| 6053 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 6054 | if (maxcount-- <= 0) |
| 6055 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6056 | SPLIT_APPEND(self->str, i + sublen, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6057 | j = i; |
| 6058 | i -= sublen; |
| 6059 | } else |
| 6060 | i--; |
| 6061 | } |
| 6062 | if (j >= 0) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6063 | SPLIT_APPEND(self->str, 0, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6064 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6065 | if (PyList_Reverse(list) < 0) |
| 6066 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6067 | return list; |
| 6068 | |
| 6069 | onError: |
| 6070 | Py_DECREF(list); |
| 6071 | return NULL; |
| 6072 | } |
| 6073 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6074 | #undef SPLIT_APPEND |
| 6075 | |
| 6076 | static |
| 6077 | PyObject *split(PyUnicodeObject *self, |
| 6078 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6079 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6080 | { |
| 6081 | PyObject *list; |
| 6082 | |
| 6083 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6084 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6085 | |
| 6086 | list = PyList_New(0); |
| 6087 | if (!list) |
| 6088 | return NULL; |
| 6089 | |
| 6090 | if (substring == NULL) |
| 6091 | return split_whitespace(self,list,maxcount); |
| 6092 | |
| 6093 | else if (substring->length == 1) |
| 6094 | return split_char(self,list,substring->str[0],maxcount); |
| 6095 | |
| 6096 | else if (substring->length == 0) { |
| 6097 | Py_DECREF(list); |
| 6098 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 6099 | return NULL; |
| 6100 | } |
| 6101 | else |
| 6102 | return split_substring(self,list,substring,maxcount); |
| 6103 | } |
| 6104 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6105 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6106 | PyObject *rsplit(PyUnicodeObject *self, |
| 6107 | PyUnicodeObject *substring, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6108 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6109 | { |
| 6110 | PyObject *list; |
| 6111 | |
| 6112 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6113 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6114 | |
| 6115 | list = PyList_New(0); |
| 6116 | if (!list) |
| 6117 | return NULL; |
| 6118 | |
| 6119 | if (substring == NULL) |
| 6120 | return rsplit_whitespace(self,list,maxcount); |
| 6121 | |
| 6122 | else if (substring->length == 1) |
| 6123 | return rsplit_char(self,list,substring->str[0],maxcount); |
| 6124 | |
| 6125 | else if (substring->length == 0) { |
| 6126 | Py_DECREF(list); |
| 6127 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 6128 | return NULL; |
| 6129 | } |
| 6130 | else |
| 6131 | return rsplit_substring(self,list,substring,maxcount); |
| 6132 | } |
| 6133 | |
| 6134 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6135 | PyObject *replace(PyUnicodeObject *self, |
| 6136 | PyUnicodeObject *str1, |
| 6137 | PyUnicodeObject *str2, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6138 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6139 | { |
| 6140 | PyUnicodeObject *u; |
| 6141 | |
| 6142 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6143 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6144 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6145 | if (str1->length == str2->length) { |
| 6146 | /* same length */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6147 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6148 | if (str1->length == 1) { |
| 6149 | /* replace characters */ |
| 6150 | Py_UNICODE u1, u2; |
| 6151 | if (!findchar(self->str, self->length, str1->str[0])) |
| 6152 | goto nothing; |
| 6153 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6154 | if (!u) |
| 6155 | return NULL; |
| 6156 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6157 | u1 = str1->str[0]; |
| 6158 | u2 = str2->str[0]; |
| 6159 | for (i = 0; i < u->length; i++) |
| 6160 | if (u->str[i] == u1) { |
| 6161 | if (--maxcount < 0) |
| 6162 | break; |
| 6163 | u->str[i] = u2; |
| 6164 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6165 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6166 | i = fastsearch( |
| 6167 | self->str, self->length, str1->str, str1->length, FAST_SEARCH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6168 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6169 | if (i < 0) |
| 6170 | goto nothing; |
| 6171 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6172 | if (!u) |
| 6173 | return NULL; |
| 6174 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6175 | while (i <= self->length - str1->length) |
| 6176 | if (Py_UNICODE_MATCH(self, i, str1)) { |
| 6177 | if (--maxcount < 0) |
| 6178 | break; |
| 6179 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 6180 | i += str1->length; |
| 6181 | } else |
| 6182 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6183 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6184 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6185 | |
| 6186 | Py_ssize_t n, i, j, e; |
| 6187 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6188 | Py_UNICODE *p; |
| 6189 | |
| 6190 | /* replace strings */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6191 | n = stringlib_count(self->str, self->length, str1->str, str1->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6192 | if (n > maxcount) |
| 6193 | n = maxcount; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6194 | if (n == 0) |
| 6195 | goto nothing; |
| 6196 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 6197 | delta = (str2->length - str1->length); |
| 6198 | if (delta == 0) { |
| 6199 | new_size = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6200 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6201 | product = n * (str2->length - str1->length); |
| 6202 | if ((product / (str2->length - str1->length)) != n) { |
| 6203 | PyErr_SetString(PyExc_OverflowError, |
| 6204 | "replace string is too long"); |
| 6205 | return NULL; |
| 6206 | } |
| 6207 | new_size = self->length + product; |
| 6208 | if (new_size < 0) { |
| 6209 | PyErr_SetString(PyExc_OverflowError, |
| 6210 | "replace string is too long"); |
| 6211 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6212 | } |
| 6213 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6214 | u = _PyUnicode_New(new_size); |
| 6215 | if (!u) |
| 6216 | return NULL; |
| 6217 | i = 0; |
| 6218 | p = u->str; |
| 6219 | e = self->length - str1->length; |
| 6220 | if (str1->length > 0) { |
| 6221 | while (n-- > 0) { |
| 6222 | /* look for next match */ |
| 6223 | j = i; |
| 6224 | while (j <= e) { |
| 6225 | if (Py_UNICODE_MATCH(self, j, str1)) |
| 6226 | break; |
| 6227 | j++; |
| 6228 | } |
| 6229 | if (j > i) { |
| 6230 | if (j > e) |
| 6231 | break; |
| 6232 | /* copy unchanged part [i:j] */ |
| 6233 | Py_UNICODE_COPY(p, self->str+i, j-i); |
| 6234 | p += j - i; |
| 6235 | } |
| 6236 | /* copy substitution string */ |
| 6237 | if (str2->length > 0) { |
| 6238 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6239 | p += str2->length; |
| 6240 | } |
| 6241 | i = j + str1->length; |
| 6242 | } |
| 6243 | if (i < self->length) |
| 6244 | /* copy tail [i:] */ |
| 6245 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6246 | } else { |
| 6247 | /* interleave */ |
| 6248 | while (n > 0) { |
| 6249 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6250 | p += str2->length; |
| 6251 | if (--n <= 0) |
| 6252 | break; |
| 6253 | *p++ = self->str[i++]; |
| 6254 | } |
| 6255 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6256 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6257 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6258 | return (PyObject *) u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6259 | |
| 6260 | nothing: |
| 6261 | /* nothing to replace; return original string (when possible) */ |
| 6262 | if (PyUnicode_CheckExact(self)) { |
| 6263 | Py_INCREF(self); |
| 6264 | return (PyObject *) self; |
| 6265 | } |
| 6266 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6267 | } |
| 6268 | |
| 6269 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 6270 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6271 | PyDoc_STRVAR(title__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 6272 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6273 | \n\ |
| 6274 | 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] | 6275 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6276 | |
| 6277 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6278 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6279 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6280 | return fixup(self, fixtitle); |
| 6281 | } |
| 6282 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6283 | PyDoc_STRVAR(capitalize__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 6284 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6285 | \n\ |
| 6286 | 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] | 6287 | have upper 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_capitalize(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, fixcapitalize); |
| 6293 | } |
| 6294 | |
| 6295 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6296 | PyDoc_STRVAR(capwords__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 6297 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6298 | \n\ |
| 6299 | 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] | 6300 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6301 | |
| 6302 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6303 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6304 | { |
| 6305 | PyObject *list; |
| 6306 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6307 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6308 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6309 | /* Split into words */ |
| 6310 | list = split(self, NULL, -1); |
| 6311 | if (!list) |
| 6312 | return NULL; |
| 6313 | |
| 6314 | /* Capitalize each word */ |
| 6315 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 6316 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
| 6317 | fixcapitalize); |
| 6318 | if (item == NULL) |
| 6319 | goto onError; |
| 6320 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 6321 | PyList_SET_ITEM(list, i, item); |
| 6322 | } |
| 6323 | |
| 6324 | /* Join the words to form a new string */ |
| 6325 | item = PyUnicode_Join(NULL, list); |
| 6326 | |
| 6327 | onError: |
| 6328 | Py_DECREF(list); |
| 6329 | return (PyObject *)item; |
| 6330 | } |
| 6331 | #endif |
| 6332 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6333 | /* Argument converter. Coerces to a single unicode character */ |
| 6334 | |
| 6335 | static int |
| 6336 | convert_uc(PyObject *obj, void *addr) |
| 6337 | { |
| 6338 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 6339 | PyObject *uniobj; |
| 6340 | Py_UNICODE *unistr; |
| 6341 | |
| 6342 | uniobj = PyUnicode_FromObject(obj); |
| 6343 | if (uniobj == NULL) { |
| 6344 | PyErr_SetString(PyExc_TypeError, |
| 6345 | "The fill character cannot be converted to Unicode"); |
| 6346 | return 0; |
| 6347 | } |
| 6348 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 6349 | PyErr_SetString(PyExc_TypeError, |
| 6350 | "The fill character must be exactly one character long"); |
| 6351 | Py_DECREF(uniobj); |
| 6352 | return 0; |
| 6353 | } |
| 6354 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 6355 | *fillcharloc = unistr[0]; |
| 6356 | Py_DECREF(uniobj); |
| 6357 | return 1; |
| 6358 | } |
| 6359 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6360 | PyDoc_STRVAR(center__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 6361 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6362 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 6363 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6364 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6365 | |
| 6366 | static PyObject * |
| 6367 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 6368 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6369 | Py_ssize_t marg, left; |
| 6370 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6371 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6372 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 6373 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6374 | return NULL; |
| 6375 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6376 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6377 | Py_INCREF(self); |
| 6378 | return (PyObject*) self; |
| 6379 | } |
| 6380 | |
| 6381 | marg = width - self->length; |
| 6382 | left = marg / 2 + (marg & width & 1); |
| 6383 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6384 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6385 | } |
| 6386 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6387 | #if 0 |
| 6388 | |
| 6389 | /* This code should go into some future Unicode collation support |
| 6390 | module. The basic comparison should compare ordinals on a naive |
Trent Mick | 20abf57 | 2000-08-12 22:14:34 +0000 | [diff] [blame] | 6391 | basis (this is what Java does and thus JPython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6392 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6393 | /* speedy UTF-16 code point order comparison */ |
| 6394 | /* gleaned from: */ |
| 6395 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 6396 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6397 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6398 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6399 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6400 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 6401 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6402 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6403 | }; |
| 6404 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6405 | static int |
| 6406 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6407 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6408 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6409 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6410 | Py_UNICODE *s1 = str1->str; |
| 6411 | Py_UNICODE *s2 = str2->str; |
| 6412 | |
| 6413 | len1 = str1->length; |
| 6414 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6415 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6416 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6417 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6418 | |
| 6419 | c1 = *s1++; |
| 6420 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6421 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6422 | if (c1 > (1<<11) * 26) |
| 6423 | c1 += utf16Fixup[c1>>11]; |
| 6424 | if (c2 > (1<<11) * 26) |
| 6425 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6426 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6427 | |
| 6428 | if (c1 != c2) |
| 6429 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6430 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6431 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6432 | } |
| 6433 | |
| 6434 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6435 | } |
| 6436 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6437 | #else |
| 6438 | |
| 6439 | static int |
| 6440 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6441 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6442 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6443 | |
| 6444 | Py_UNICODE *s1 = str1->str; |
| 6445 | Py_UNICODE *s2 = str2->str; |
| 6446 | |
| 6447 | len1 = str1->length; |
| 6448 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6449 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6450 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6451 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6452 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6453 | c1 = *s1++; |
| 6454 | c2 = *s2++; |
| 6455 | |
| 6456 | if (c1 != c2) |
| 6457 | return (c1 < c2) ? -1 : 1; |
| 6458 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6459 | len1--; len2--; |
| 6460 | } |
| 6461 | |
| 6462 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6463 | } |
| 6464 | |
| 6465 | #endif |
| 6466 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6467 | int PyUnicode_Compare(PyObject *left, |
| 6468 | PyObject *right) |
| 6469 | { |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6470 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) |
| 6471 | return unicode_compare((PyUnicodeObject *)left, |
| 6472 | (PyUnicodeObject *)right); |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6473 | PyErr_Format(PyExc_TypeError, |
| 6474 | "Can't compare %.100s and %.100s", |
| 6475 | left->ob_type->tp_name, |
| 6476 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6477 | return -1; |
| 6478 | } |
| 6479 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6480 | int |
| 6481 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 6482 | { |
| 6483 | int i; |
| 6484 | Py_UNICODE *id; |
| 6485 | assert(PyUnicode_Check(uni)); |
| 6486 | id = PyUnicode_AS_UNICODE(uni); |
| 6487 | /* Compare Unicode string and source character set string */ |
| 6488 | for (i = 0; id[i] && str[i]; i++) |
| 6489 | if (id[i] != str[i]) |
| 6490 | return ((int)id[i] < (int)str[i]) ? -1 : 1; |
| 6491 | if (id[i]) |
| 6492 | return 1; /* uni is longer */ |
| 6493 | if (str[i]) |
| 6494 | return -1; /* str is longer */ |
| 6495 | return 0; |
| 6496 | } |
| 6497 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6498 | PyObject *PyUnicode_RichCompare(PyObject *left, |
| 6499 | PyObject *right, |
| 6500 | int op) |
| 6501 | { |
| 6502 | int result; |
| 6503 | |
| 6504 | result = PyUnicode_Compare(left, right); |
| 6505 | if (result == -1 && PyErr_Occurred()) |
| 6506 | goto onError; |
| 6507 | |
| 6508 | /* Convert the return value to a Boolean */ |
| 6509 | switch (op) { |
| 6510 | case Py_EQ: |
| 6511 | result = (result == 0); |
| 6512 | break; |
| 6513 | case Py_NE: |
| 6514 | result = (result != 0); |
| 6515 | break; |
| 6516 | case Py_LE: |
| 6517 | result = (result <= 0); |
| 6518 | break; |
| 6519 | case Py_GE: |
| 6520 | result = (result >= 0); |
| 6521 | break; |
| 6522 | case Py_LT: |
| 6523 | result = (result == -1); |
| 6524 | break; |
| 6525 | case Py_GT: |
| 6526 | result = (result == 1); |
| 6527 | break; |
| 6528 | } |
| 6529 | return PyBool_FromLong(result); |
| 6530 | |
| 6531 | onError: |
| 6532 | |
| 6533 | /* Standard case |
| 6534 | |
| 6535 | Type errors mean that PyUnicode_FromObject() could not convert |
| 6536 | one of the arguments (usually the right hand side) to Unicode, |
| 6537 | ie. we can't handle the comparison request. However, it is |
| 6538 | possible that the other object knows a comparison method, which |
| 6539 | is why we return Py_NotImplemented to give the other object a |
| 6540 | chance. |
| 6541 | |
| 6542 | */ |
| 6543 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 6544 | PyErr_Clear(); |
| 6545 | Py_INCREF(Py_NotImplemented); |
| 6546 | return Py_NotImplemented; |
| 6547 | } |
| 6548 | if (op != Py_EQ && op != Py_NE) |
| 6549 | return NULL; |
| 6550 | |
| 6551 | /* Equality comparison. |
| 6552 | |
| 6553 | This is a special case: we silence any PyExc_UnicodeDecodeError |
| 6554 | and instead turn it into a PyErr_UnicodeWarning. |
| 6555 | |
| 6556 | */ |
| 6557 | if (!PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) |
| 6558 | return NULL; |
| 6559 | PyErr_Clear(); |
Skip Montanaro | 46fc337 | 2007-08-12 11:44:53 +0000 | [diff] [blame] | 6560 | if (PyErr_WarnEx(PyExc_UnicodeWarning, |
| 6561 | (op == Py_EQ) ? |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 6562 | "equal comparison " |
| 6563 | "failed to convert both arguments to str - " |
Skip Montanaro | 46fc337 | 2007-08-12 11:44:53 +0000 | [diff] [blame] | 6564 | "interpreting them as being unequal" |
| 6565 | : |
| 6566 | "Unicode unequal comparison " |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 6567 | "failed to convert both arguments to str - " |
Skip Montanaro | 46fc337 | 2007-08-12 11:44:53 +0000 | [diff] [blame] | 6568 | "interpreting them as being unequal", |
| 6569 | 1) < 0) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6570 | return NULL; |
| 6571 | result = (op == Py_NE); |
| 6572 | return PyBool_FromLong(result); |
| 6573 | } |
| 6574 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6575 | int PyUnicode_Contains(PyObject *container, |
| 6576 | PyObject *element) |
| 6577 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6578 | PyObject *str, *sub; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6579 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6580 | |
| 6581 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6582 | sub = PyUnicode_FromObject(element); |
| 6583 | if (!sub) { |
Walter Dörwald | 26e0f51 | 2007-06-12 16:51:31 +0000 | [diff] [blame] | 6584 | PyErr_Format(PyExc_TypeError, |
| 6585 | "'in <string>' requires string as left operand, not %s", |
| 6586 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6587 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6588 | } |
| 6589 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6590 | str = PyUnicode_FromObject(container); |
| 6591 | if (!str) { |
| 6592 | Py_DECREF(sub); |
| 6593 | return -1; |
| 6594 | } |
| 6595 | |
| 6596 | result = stringlib_contains_obj(str, sub); |
| 6597 | |
| 6598 | Py_DECREF(str); |
| 6599 | Py_DECREF(sub); |
| 6600 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6601 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 6602 | } |
| 6603 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6604 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 6605 | |
| 6606 | PyObject *PyUnicode_Concat(PyObject *left, |
| 6607 | PyObject *right) |
| 6608 | { |
| 6609 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 6610 | |
| 6611 | /* Coerce the two arguments */ |
| 6612 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 6613 | if (u == NULL) |
| 6614 | goto onError; |
| 6615 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 6616 | if (v == NULL) |
| 6617 | goto onError; |
| 6618 | |
| 6619 | /* Shortcuts */ |
| 6620 | if (v == unicode_empty) { |
| 6621 | Py_DECREF(v); |
| 6622 | return (PyObject *)u; |
| 6623 | } |
| 6624 | if (u == unicode_empty) { |
| 6625 | Py_DECREF(u); |
| 6626 | return (PyObject *)v; |
| 6627 | } |
| 6628 | |
| 6629 | /* Concat the two Unicode strings */ |
| 6630 | w = _PyUnicode_New(u->length + v->length); |
| 6631 | if (w == NULL) |
| 6632 | goto onError; |
| 6633 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 6634 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 6635 | |
| 6636 | Py_DECREF(u); |
| 6637 | Py_DECREF(v); |
| 6638 | return (PyObject *)w; |
| 6639 | |
| 6640 | onError: |
| 6641 | Py_XDECREF(u); |
| 6642 | Py_XDECREF(v); |
| 6643 | return NULL; |
| 6644 | } |
| 6645 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 6646 | void |
| 6647 | PyUnicode_Append(PyObject **pleft, PyObject *right) |
| 6648 | { |
| 6649 | PyObject *new; |
| 6650 | if (*pleft == NULL) |
| 6651 | return; |
| 6652 | if (right == NULL || !PyUnicode_Check(*pleft)) { |
| 6653 | Py_DECREF(*pleft); |
| 6654 | *pleft = NULL; |
| 6655 | return; |
| 6656 | } |
| 6657 | new = PyUnicode_Concat(*pleft, right); |
| 6658 | Py_DECREF(*pleft); |
| 6659 | *pleft = new; |
| 6660 | } |
| 6661 | |
| 6662 | void |
| 6663 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 6664 | { |
| 6665 | PyUnicode_Append(pleft, right); |
| 6666 | Py_XDECREF(right); |
| 6667 | } |
| 6668 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6669 | PyDoc_STRVAR(count__doc__, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6670 | "S.count(sub[, start[, end]]) -> int\n\ |
| 6671 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6672 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 6673 | 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] | 6674 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6675 | |
| 6676 | static PyObject * |
| 6677 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 6678 | { |
| 6679 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6680 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6681 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6682 | PyObject *result; |
| 6683 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 6684 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
| 6685 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6686 | return NULL; |
| 6687 | |
| 6688 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6689 | (PyObject *)substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6690 | if (substring == NULL) |
| 6691 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6692 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6693 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6694 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6695 | result = PyLong_FromSsize_t( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6696 | stringlib_count(self->str + start, end - start, |
| 6697 | substring->str, substring->length) |
| 6698 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6699 | |
| 6700 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6701 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6702 | return result; |
| 6703 | } |
| 6704 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6705 | PyDoc_STRVAR(encode__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 6706 | "S.encode([encoding[, errors]]) -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6707 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 6708 | Encode S using the codec registered for encoding. encoding defaults\n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6709 | 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] | 6710 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 6711 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 6712 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 6713 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6714 | |
| 6715 | static PyObject * |
| 6716 | unicode_encode(PyUnicodeObject *self, PyObject *args) |
| 6717 | { |
| 6718 | char *encoding = NULL; |
| 6719 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6720 | PyObject *v; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 6721 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6722 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 6723 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 6724 | v = PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6725 | if (v == NULL) |
| 6726 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 6727 | if (!PyBytes_Check(v)) { |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6728 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 6729 | "encoder did not return a bytes object " |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6730 | "(type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 6731 | Py_TYPE(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6732 | Py_DECREF(v); |
| 6733 | return NULL; |
| 6734 | } |
| 6735 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 6736 | |
| 6737 | onError: |
| 6738 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 6739 | } |
| 6740 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6741 | PyDoc_STRVAR(expandtabs__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 6742 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6743 | \n\ |
| 6744 | 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] | 6745 | 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] | 6746 | |
| 6747 | static PyObject* |
| 6748 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 6749 | { |
| 6750 | Py_UNICODE *e; |
| 6751 | Py_UNICODE *p; |
| 6752 | Py_UNICODE *q; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6753 | Py_UNICODE *qe; |
| 6754 | Py_ssize_t i, j, incr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6755 | PyUnicodeObject *u; |
| 6756 | int tabsize = 8; |
| 6757 | |
| 6758 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
| 6759 | return NULL; |
| 6760 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 6761 | /* First pass: determine size of output string */ |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6762 | i = 0; /* chars up to and including most recent \n or \r */ |
| 6763 | j = 0; /* chars since most recent \n or \r (use in tab calculations) */ |
| 6764 | e = self->str + self->length; /* end of input */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6765 | for (p = self->str; p < e; p++) |
| 6766 | if (*p == '\t') { |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6767 | if (tabsize > 0) { |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6768 | incr = tabsize - (j % tabsize); /* cannot overflow */ |
| 6769 | if (j > PY_SSIZE_T_MAX - incr) |
| 6770 | goto overflow1; |
| 6771 | j += incr; |
| 6772 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6773 | } |
| 6774 | else { |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6775 | if (j > PY_SSIZE_T_MAX - 1) |
| 6776 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6777 | j++; |
| 6778 | if (*p == '\n' || *p == '\r') { |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6779 | if (i > PY_SSIZE_T_MAX - j) |
| 6780 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6781 | i += j; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6782 | j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6783 | } |
| 6784 | } |
| 6785 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6786 | if (i > PY_SSIZE_T_MAX - j) |
| 6787 | goto overflow1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6788 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6789 | /* Second pass: create output string and fill it */ |
| 6790 | u = _PyUnicode_New(i + j); |
| 6791 | if (!u) |
| 6792 | return NULL; |
| 6793 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6794 | j = 0; /* same as in first pass */ |
| 6795 | q = u->str; /* next output char */ |
| 6796 | qe = u->str + u->length; /* end of output */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6797 | |
| 6798 | for (p = self->str; p < e; p++) |
| 6799 | if (*p == '\t') { |
| 6800 | if (tabsize > 0) { |
| 6801 | i = tabsize - (j % tabsize); |
| 6802 | j += i; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6803 | while (i--) { |
| 6804 | if (q >= qe) |
| 6805 | goto overflow2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6806 | *q++ = ' '; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6807 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6808 | } |
| 6809 | } |
| 6810 | else { |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6811 | if (q >= qe) |
| 6812 | goto overflow2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6813 | *q++ = *p; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6814 | j++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6815 | if (*p == '\n' || *p == '\r') |
| 6816 | j = 0; |
| 6817 | } |
| 6818 | |
| 6819 | return (PyObject*) u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 6820 | |
| 6821 | overflow2: |
| 6822 | Py_DECREF(u); |
| 6823 | overflow1: |
| 6824 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 6825 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6826 | } |
| 6827 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6828 | PyDoc_STRVAR(find__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 6829 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6830 | \n\ |
| 6831 | 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] | 6832 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6833 | arguments start and end are interpreted as in slice notation.\n\ |
| 6834 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6835 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6836 | |
| 6837 | static PyObject * |
| 6838 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 6839 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6840 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 6841 | Py_ssize_t start; |
| 6842 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6843 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6844 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 6845 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6846 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6847 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6848 | result = stringlib_find_slice( |
| 6849 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6850 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6851 | start, end |
| 6852 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6853 | |
| 6854 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6855 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6856 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6857 | } |
| 6858 | |
| 6859 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6860 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6861 | { |
| 6862 | if (index < 0 || index >= self->length) { |
| 6863 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 6864 | return NULL; |
| 6865 | } |
| 6866 | |
| 6867 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 6868 | } |
| 6869 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 6870 | /* Believe it or not, this produces the same value for ASCII strings |
| 6871 | as string_hash(). */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6872 | static long |
Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 6873 | unicode_hash(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6874 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 6875 | Py_ssize_t len; |
| 6876 | Py_UNICODE *p; |
| 6877 | long x; |
| 6878 | |
| 6879 | if (self->hash != -1) |
| 6880 | return self->hash; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 6881 | len = Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 6882 | p = self->str; |
| 6883 | x = *p << 7; |
| 6884 | while (--len >= 0) |
| 6885 | x = (1000003*x) ^ *p++; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 6886 | x ^= Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 6887 | if (x == -1) |
| 6888 | x = -2; |
| 6889 | self->hash = x; |
| 6890 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6891 | } |
| 6892 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6893 | PyDoc_STRVAR(index__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 6894 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6895 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6896 | 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] | 6897 | |
| 6898 | static PyObject * |
| 6899 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 6900 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6901 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6902 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 6903 | Py_ssize_t start; |
| 6904 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6905 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 6906 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6907 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6908 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6909 | result = stringlib_find_slice( |
| 6910 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 6911 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 6912 | start, end |
| 6913 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6914 | |
| 6915 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6916 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6917 | if (result < 0) { |
| 6918 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 6919 | return NULL; |
| 6920 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6921 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 6922 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6923 | } |
| 6924 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6925 | PyDoc_STRVAR(islower__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6926 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6927 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6928 | 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] | 6929 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6930 | |
| 6931 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6932 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6933 | { |
| 6934 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6935 | register const Py_UNICODE *e; |
| 6936 | int cased; |
| 6937 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6938 | /* Shortcut for single character strings */ |
| 6939 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6940 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6941 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6942 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6943 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6944 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6945 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6946 | e = p + PyUnicode_GET_SIZE(self); |
| 6947 | cased = 0; |
| 6948 | for (; p < e; p++) { |
| 6949 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6950 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6951 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6952 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6953 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 6954 | cased = 1; |
| 6955 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6956 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6957 | } |
| 6958 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6959 | PyDoc_STRVAR(isupper__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6960 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6961 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6962 | 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] | 6963 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6964 | |
| 6965 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6966 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6967 | { |
| 6968 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6969 | register const Py_UNICODE *e; |
| 6970 | int cased; |
| 6971 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6972 | /* Shortcut for single character strings */ |
| 6973 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6974 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6975 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6976 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 6977 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6978 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 6979 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6980 | e = p + PyUnicode_GET_SIZE(self); |
| 6981 | cased = 0; |
| 6982 | for (; p < e; p++) { |
| 6983 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6984 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6985 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6986 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6987 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 6988 | cased = 1; |
| 6989 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6990 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6991 | } |
| 6992 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6993 | PyDoc_STRVAR(istitle__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 6994 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6995 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 6996 | Return True if S is a titlecased string and there is at least one\n\ |
| 6997 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 6998 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 6999 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7000 | |
| 7001 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7002 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7003 | { |
| 7004 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7005 | register const Py_UNICODE *e; |
| 7006 | int cased, previous_is_cased; |
| 7007 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7008 | /* Shortcut for single character strings */ |
| 7009 | if (PyUnicode_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7010 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 7011 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7012 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7013 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7014 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7015 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7016 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7017 | e = p + PyUnicode_GET_SIZE(self); |
| 7018 | cased = 0; |
| 7019 | previous_is_cased = 0; |
| 7020 | for (; p < e; p++) { |
| 7021 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7022 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7023 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 7024 | if (previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7025 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7026 | previous_is_cased = 1; |
| 7027 | cased = 1; |
| 7028 | } |
| 7029 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 7030 | if (!previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7031 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7032 | previous_is_cased = 1; |
| 7033 | cased = 1; |
| 7034 | } |
| 7035 | else |
| 7036 | previous_is_cased = 0; |
| 7037 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7038 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7039 | } |
| 7040 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7041 | PyDoc_STRVAR(isspace__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7042 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7043 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7044 | Return True if all characters in S are whitespace\n\ |
| 7045 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7046 | |
| 7047 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7048 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7049 | { |
| 7050 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7051 | register const Py_UNICODE *e; |
| 7052 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7053 | /* Shortcut for single character strings */ |
| 7054 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 7055 | Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7056 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7057 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7058 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7059 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7060 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7061 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7062 | e = p + PyUnicode_GET_SIZE(self); |
| 7063 | for (; p < e; p++) { |
| 7064 | if (!Py_UNICODE_ISSPACE(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7065 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7066 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7067 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7068 | } |
| 7069 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7070 | PyDoc_STRVAR(isalpha__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7071 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7072 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7073 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7074 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7075 | |
| 7076 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7077 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7078 | { |
| 7079 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7080 | register const Py_UNICODE *e; |
| 7081 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7082 | /* Shortcut for single character strings */ |
| 7083 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 7084 | Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7085 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7086 | |
| 7087 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7088 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7089 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7090 | |
| 7091 | e = p + PyUnicode_GET_SIZE(self); |
| 7092 | for (; p < e; p++) { |
| 7093 | if (!Py_UNICODE_ISALPHA(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7094 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7095 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7096 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7097 | } |
| 7098 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7099 | PyDoc_STRVAR(isalnum__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7100 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7101 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7102 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7103 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7104 | |
| 7105 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7106 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7107 | { |
| 7108 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7109 | register const Py_UNICODE *e; |
| 7110 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7111 | /* Shortcut for single character strings */ |
| 7112 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 7113 | Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7114 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7115 | |
| 7116 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7117 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7118 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7119 | |
| 7120 | e = p + PyUnicode_GET_SIZE(self); |
| 7121 | for (; p < e; p++) { |
| 7122 | if (!Py_UNICODE_ISALNUM(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7123 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7124 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7125 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7126 | } |
| 7127 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7128 | PyDoc_STRVAR(isdecimal__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7129 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7130 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7131 | 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] | 7132 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7133 | |
| 7134 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7135 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7136 | { |
| 7137 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7138 | register const Py_UNICODE *e; |
| 7139 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7140 | /* Shortcut for single character strings */ |
| 7141 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 7142 | Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7143 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7144 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7145 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7146 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7147 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7148 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7149 | e = p + PyUnicode_GET_SIZE(self); |
| 7150 | for (; p < e; p++) { |
| 7151 | if (!Py_UNICODE_ISDECIMAL(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7152 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7153 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7154 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7155 | } |
| 7156 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7157 | PyDoc_STRVAR(isdigit__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7158 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7159 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7160 | Return True if all characters in S are digits\n\ |
| 7161 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7162 | |
| 7163 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7164 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7165 | { |
| 7166 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7167 | register const Py_UNICODE *e; |
| 7168 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7169 | /* Shortcut for single character strings */ |
| 7170 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 7171 | Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7172 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7173 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7174 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7175 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7176 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7177 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7178 | e = p + PyUnicode_GET_SIZE(self); |
| 7179 | for (; p < e; p++) { |
| 7180 | if (!Py_UNICODE_ISDIGIT(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7181 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7182 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7183 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7184 | } |
| 7185 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7186 | PyDoc_STRVAR(isnumeric__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7187 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7188 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7189 | 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] | 7190 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7191 | |
| 7192 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7193 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7194 | { |
| 7195 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7196 | register const Py_UNICODE *e; |
| 7197 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7198 | /* Shortcut for single character strings */ |
| 7199 | if (PyUnicode_GET_SIZE(self) == 1 && |
| 7200 | Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7201 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7202 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7203 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7204 | if (PyUnicode_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7205 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7206 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7207 | e = p + PyUnicode_GET_SIZE(self); |
| 7208 | for (; p < e; p++) { |
| 7209 | if (!Py_UNICODE_ISNUMERIC(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7210 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7211 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7212 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7213 | } |
| 7214 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7215 | int |
| 7216 | PyUnicode_IsIdentifier(PyObject *self) |
| 7217 | { |
| 7218 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE((PyUnicodeObject*)self); |
| 7219 | register const Py_UNICODE *e; |
| 7220 | |
| 7221 | /* Special case for empty strings */ |
| 7222 | if (PyUnicode_GET_SIZE(self) == 0) |
| 7223 | return 0; |
| 7224 | |
| 7225 | /* PEP 3131 says that the first character must be in |
| 7226 | XID_Start and subsequent characters in XID_Continue, |
| 7227 | and for the ASCII range, the 2.x rules apply (i.e |
| 7228 | start with letters and underscore, continue with |
| 7229 | letters, digits, underscore). However, given the current |
| 7230 | definition of XID_Start and XID_Continue, it is sufficient |
| 7231 | to check just for these, except that _ must be allowed |
| 7232 | as starting an identifier. */ |
| 7233 | if (!_PyUnicode_IsXidStart(*p) && *p != 0x5F /* LOW LINE */) |
| 7234 | return 0; |
| 7235 | |
| 7236 | e = p + PyUnicode_GET_SIZE(self); |
| 7237 | for (p++; p < e; p++) { |
| 7238 | if (!_PyUnicode_IsXidContinue(*p)) |
| 7239 | return 0; |
| 7240 | } |
| 7241 | return 1; |
| 7242 | } |
| 7243 | |
| 7244 | PyDoc_STRVAR(isidentifier__doc__, |
| 7245 | "S.isidentifier() -> bool\n\ |
| 7246 | \n\ |
| 7247 | Return True if S is a valid identifier according\n\ |
| 7248 | to the language definition."); |
| 7249 | |
| 7250 | static PyObject* |
| 7251 | unicode_isidentifier(PyObject *self) |
| 7252 | { |
| 7253 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 7254 | } |
| 7255 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7256 | PyDoc_STRVAR(isprintable__doc__, |
| 7257 | "S.isprintable() -> bool\n\ |
| 7258 | \n\ |
| 7259 | Return True if all characters in S are considered\n\ |
| 7260 | printable in repr() or S is empty, False otherwise."); |
| 7261 | |
| 7262 | static PyObject* |
| 7263 | unicode_isprintable(PyObject *self) |
| 7264 | { |
| 7265 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7266 | register const Py_UNICODE *e; |
| 7267 | |
| 7268 | /* Shortcut for single character strings */ |
| 7269 | if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISPRINTABLE(*p)) { |
| 7270 | Py_RETURN_TRUE; |
| 7271 | } |
| 7272 | |
| 7273 | e = p + PyUnicode_GET_SIZE(self); |
| 7274 | for (; p < e; p++) { |
| 7275 | if (!Py_UNICODE_ISPRINTABLE(*p)) { |
| 7276 | Py_RETURN_FALSE; |
| 7277 | } |
| 7278 | } |
| 7279 | Py_RETURN_TRUE; |
| 7280 | } |
| 7281 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7282 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7283 | "S.join(sequence) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7284 | \n\ |
| 7285 | 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] | 7286 | sequence. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7287 | |
| 7288 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7289 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7290 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7291 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7292 | } |
| 7293 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7294 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7295 | unicode_length(PyUnicodeObject *self) |
| 7296 | { |
| 7297 | return self->length; |
| 7298 | } |
| 7299 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7300 | PyDoc_STRVAR(ljust__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7301 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7302 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame^] | 7303 | 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] | 7304 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7305 | |
| 7306 | static PyObject * |
| 7307 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 7308 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7309 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7310 | Py_UNICODE fillchar = ' '; |
| 7311 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7312 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7313 | return NULL; |
| 7314 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7315 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7316 | Py_INCREF(self); |
| 7317 | return (PyObject*) self; |
| 7318 | } |
| 7319 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7320 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7321 | } |
| 7322 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7323 | PyDoc_STRVAR(lower__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7324 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7325 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7326 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7327 | |
| 7328 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7329 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7330 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7331 | return fixup(self, fixlower); |
| 7332 | } |
| 7333 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7334 | #define LEFTSTRIP 0 |
| 7335 | #define RIGHTSTRIP 1 |
| 7336 | #define BOTHSTRIP 2 |
| 7337 | |
| 7338 | /* Arrays indexed by above */ |
| 7339 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 7340 | |
| 7341 | #define STRIPNAME(i) (stripformat[i]+3) |
| 7342 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7343 | /* externally visible for str.strip(unicode) */ |
| 7344 | PyObject * |
| 7345 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 7346 | { |
| 7347 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7348 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7349 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7350 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 7351 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7352 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7353 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
| 7354 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7355 | i = 0; |
| 7356 | if (striptype != RIGHTSTRIP) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7357 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 7358 | i++; |
| 7359 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7360 | } |
| 7361 | |
| 7362 | j = len; |
| 7363 | if (striptype != LEFTSTRIP) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7364 | do { |
| 7365 | j--; |
| 7366 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 7367 | j++; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7368 | } |
| 7369 | |
| 7370 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7371 | Py_INCREF(self); |
| 7372 | return (PyObject*)self; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7373 | } |
| 7374 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7375 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7376 | } |
| 7377 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7378 | |
| 7379 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7380 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7381 | { |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7382 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7383 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7384 | |
| 7385 | i = 0; |
| 7386 | if (striptype != RIGHTSTRIP) { |
| 7387 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 7388 | i++; |
| 7389 | } |
| 7390 | } |
| 7391 | |
| 7392 | j = len; |
| 7393 | if (striptype != LEFTSTRIP) { |
| 7394 | do { |
| 7395 | j--; |
| 7396 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 7397 | j++; |
| 7398 | } |
| 7399 | |
| 7400 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 7401 | Py_INCREF(self); |
| 7402 | return (PyObject*)self; |
| 7403 | } |
| 7404 | else |
| 7405 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7406 | } |
| 7407 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7408 | |
| 7409 | static PyObject * |
| 7410 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 7411 | { |
| 7412 | PyObject *sep = NULL; |
| 7413 | |
| 7414 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 7415 | return NULL; |
| 7416 | |
| 7417 | if (sep != NULL && sep != Py_None) { |
| 7418 | if (PyUnicode_Check(sep)) |
| 7419 | return _PyUnicode_XStrip(self, striptype, sep); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7420 | else { |
| 7421 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7422 | "%s arg must be None or str", |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7423 | STRIPNAME(striptype)); |
| 7424 | return NULL; |
| 7425 | } |
| 7426 | } |
| 7427 | |
| 7428 | return do_strip(self, striptype); |
| 7429 | } |
| 7430 | |
| 7431 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7432 | PyDoc_STRVAR(strip__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7433 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7434 | \n\ |
| 7435 | Return a copy of the string S with leading and trailing\n\ |
| 7436 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7437 | 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] | 7438 | |
| 7439 | static PyObject * |
| 7440 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 7441 | { |
| 7442 | if (PyTuple_GET_SIZE(args) == 0) |
| 7443 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 7444 | else |
| 7445 | return do_argstrip(self, BOTHSTRIP, args); |
| 7446 | } |
| 7447 | |
| 7448 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7449 | PyDoc_STRVAR(lstrip__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7450 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7451 | \n\ |
| 7452 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7453 | 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] | 7454 | |
| 7455 | static PyObject * |
| 7456 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 7457 | { |
| 7458 | if (PyTuple_GET_SIZE(args) == 0) |
| 7459 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 7460 | else |
| 7461 | return do_argstrip(self, LEFTSTRIP, args); |
| 7462 | } |
| 7463 | |
| 7464 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7465 | PyDoc_STRVAR(rstrip__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7466 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7467 | \n\ |
| 7468 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7469 | 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] | 7470 | |
| 7471 | static PyObject * |
| 7472 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 7473 | { |
| 7474 | if (PyTuple_GET_SIZE(args) == 0) |
| 7475 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 7476 | else |
| 7477 | return do_argstrip(self, RIGHTSTRIP, args); |
| 7478 | } |
| 7479 | |
| 7480 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7481 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7482 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7483 | { |
| 7484 | PyUnicodeObject *u; |
| 7485 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7486 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7487 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7488 | |
| 7489 | if (len < 0) |
| 7490 | len = 0; |
| 7491 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7492 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7493 | /* no repeat, return original string */ |
| 7494 | Py_INCREF(str); |
| 7495 | return (PyObject*) str; |
| 7496 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7497 | |
| 7498 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 7499 | * needed doesn't overflow size_t |
| 7500 | */ |
| 7501 | nchars = len * str->length; |
| 7502 | if (len && nchars / len != str->length) { |
| 7503 | PyErr_SetString(PyExc_OverflowError, |
| 7504 | "repeated string is too long"); |
| 7505 | return NULL; |
| 7506 | } |
| 7507 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 7508 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 7509 | PyErr_SetString(PyExc_OverflowError, |
| 7510 | "repeated string is too long"); |
| 7511 | return NULL; |
| 7512 | } |
| 7513 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7514 | if (!u) |
| 7515 | return NULL; |
| 7516 | |
| 7517 | p = u->str; |
| 7518 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7519 | if (str->length == 1 && len > 0) { |
| 7520 | Py_UNICODE_FILL(p, str->str[0], len); |
| 7521 | } else { |
| 7522 | Py_ssize_t done = 0; /* number of characters copied this far */ |
| 7523 | if (done < nchars) { |
| 7524 | Py_UNICODE_COPY(p, str->str, str->length); |
| 7525 | done = str->length; |
| 7526 | } |
| 7527 | while (done < nchars) { |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 7528 | Py_ssize_t n = (done <= nchars-done) ? done : nchars-done; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7529 | Py_UNICODE_COPY(p+done, p, n); |
| 7530 | done += n; |
| 7531 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7532 | } |
| 7533 | |
| 7534 | return (PyObject*) u; |
| 7535 | } |
| 7536 | |
| 7537 | PyObject *PyUnicode_Replace(PyObject *obj, |
| 7538 | PyObject *subobj, |
| 7539 | PyObject *replobj, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7540 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7541 | { |
| 7542 | PyObject *self; |
| 7543 | PyObject *str1; |
| 7544 | PyObject *str2; |
| 7545 | PyObject *result; |
| 7546 | |
| 7547 | self = PyUnicode_FromObject(obj); |
| 7548 | if (self == NULL) |
| 7549 | return NULL; |
| 7550 | str1 = PyUnicode_FromObject(subobj); |
| 7551 | if (str1 == NULL) { |
| 7552 | Py_DECREF(self); |
| 7553 | return NULL; |
| 7554 | } |
| 7555 | str2 = PyUnicode_FromObject(replobj); |
| 7556 | if (str2 == NULL) { |
| 7557 | Py_DECREF(self); |
| 7558 | Py_DECREF(str1); |
| 7559 | return NULL; |
| 7560 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7561 | result = replace((PyUnicodeObject *)self, |
| 7562 | (PyUnicodeObject *)str1, |
| 7563 | (PyUnicodeObject *)str2, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7564 | maxcount); |
| 7565 | Py_DECREF(self); |
| 7566 | Py_DECREF(str1); |
| 7567 | Py_DECREF(str2); |
| 7568 | return result; |
| 7569 | } |
| 7570 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7571 | PyDoc_STRVAR(replace__doc__, |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 7572 | "S.replace (old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7573 | \n\ |
| 7574 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 7575 | old replaced by new. If the optional argument count is\n\ |
| 7576 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7577 | |
| 7578 | static PyObject* |
| 7579 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 7580 | { |
| 7581 | PyUnicodeObject *str1; |
| 7582 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7583 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7584 | PyObject *result; |
| 7585 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7586 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7587 | return NULL; |
| 7588 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 7589 | if (str1 == NULL) |
| 7590 | return NULL; |
| 7591 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7592 | if (str2 == NULL) { |
| 7593 | Py_DECREF(str1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7594 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 7595 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7596 | |
| 7597 | result = replace(self, str1, str2, maxcount); |
| 7598 | |
| 7599 | Py_DECREF(str1); |
| 7600 | Py_DECREF(str2); |
| 7601 | return result; |
| 7602 | } |
| 7603 | |
| 7604 | static |
| 7605 | PyObject *unicode_repr(PyObject *unicode) |
| 7606 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7607 | PyObject *repr; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7608 | Py_UNICODE *p; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7609 | Py_UNICODE *s = PyUnicode_AS_UNICODE(unicode); |
| 7610 | Py_ssize_t size = PyUnicode_GET_SIZE(unicode); |
| 7611 | |
| 7612 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 7613 | better to choose a different scheme. Perhaps scan the |
| 7614 | first N-chars of the string and allocate based on that size. |
| 7615 | */ |
| 7616 | /* Initial allocation is based on the longest-possible unichr |
| 7617 | escape. |
| 7618 | |
| 7619 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 7620 | unichr, so in this case it's the longest unichr escape. In |
| 7621 | narrow (UTF-16) builds this is five chars per source unichr |
| 7622 | since there are two unichrs in the surrogate pair, so in narrow |
| 7623 | (UTF-16) builds it's not the longest unichr escape. |
| 7624 | |
| 7625 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 7626 | so in the narrow (UTF-16) build case it's the longest unichr |
| 7627 | escape. |
| 7628 | */ |
| 7629 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7630 | repr = PyUnicode_FromUnicode(NULL, |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7631 | 2 /* quotes */ |
| 7632 | #ifdef Py_UNICODE_WIDE |
| 7633 | + 10*size |
| 7634 | #else |
| 7635 | + 6*size |
| 7636 | #endif |
| 7637 | + 1); |
| 7638 | if (repr == NULL) |
| 7639 | return NULL; |
| 7640 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7641 | p = PyUnicode_AS_UNICODE(repr); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7642 | |
| 7643 | /* Add quote */ |
| 7644 | *p++ = (findchar(s, size, '\'') && |
| 7645 | !findchar(s, size, '"')) ? '"' : '\''; |
| 7646 | while (size-- > 0) { |
| 7647 | Py_UNICODE ch = *s++; |
| 7648 | |
| 7649 | /* Escape quotes and backslashes */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7650 | if ((ch == PyUnicode_AS_UNICODE(repr)[0]) || (ch == '\\')) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7651 | *p++ = '\\'; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7652 | *p++ = ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7653 | continue; |
| 7654 | } |
| 7655 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7656 | /* Map special whitespace to '\t', \n', '\r' */ |
| 7657 | if (ch == '\t') { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7658 | *p++ = '\\'; |
| 7659 | *p++ = 't'; |
| 7660 | } |
| 7661 | else if (ch == '\n') { |
| 7662 | *p++ = '\\'; |
| 7663 | *p++ = 'n'; |
| 7664 | } |
| 7665 | else if (ch == '\r') { |
| 7666 | *p++ = '\\'; |
| 7667 | *p++ = 'r'; |
| 7668 | } |
| 7669 | |
| 7670 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7671 | else if (ch < ' ' || ch == 0x7F) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7672 | *p++ = '\\'; |
| 7673 | *p++ = 'x'; |
| 7674 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 7675 | *p++ = hexdigits[ch & 0x000F]; |
| 7676 | } |
| 7677 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7678 | /* Copy ASCII characters as-is */ |
| 7679 | else if (ch < 0x7F) { |
| 7680 | *p++ = ch; |
| 7681 | } |
| 7682 | |
| 7683 | /* Non-ASCII characters */ |
| 7684 | else { |
| 7685 | Py_UCS4 ucs = ch; |
| 7686 | |
| 7687 | #ifndef Py_UNICODE_WIDE |
| 7688 | Py_UNICODE ch2 = 0; |
| 7689 | /* Get code point from surrogate pair */ |
| 7690 | if (size > 0) { |
| 7691 | ch2 = *s; |
| 7692 | if (ch >= 0xD800 && ch < 0xDC00 && ch2 >= 0xDC00 |
| 7693 | && ch2 <= 0xDFFF) { |
| 7694 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) |
| 7695 | + 0x00010000; |
| 7696 | s++; |
| 7697 | size--; |
| 7698 | } |
| 7699 | } |
| 7700 | #endif |
| 7701 | /* Map Unicode whitespace and control characters |
| 7702 | (categories Z* and C* except ASCII space) |
| 7703 | */ |
| 7704 | if (!Py_UNICODE_ISPRINTABLE(ucs)) { |
| 7705 | /* Map 8-bit characters to '\xhh' */ |
| 7706 | if (ucs <= 0xff) { |
| 7707 | *p++ = '\\'; |
| 7708 | *p++ = 'x'; |
| 7709 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 7710 | *p++ = hexdigits[ch & 0x000F]; |
| 7711 | } |
| 7712 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 7713 | else if (ucs >= 0x10000) { |
| 7714 | *p++ = '\\'; |
| 7715 | *p++ = 'U'; |
| 7716 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 7717 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 7718 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 7719 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 7720 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 7721 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 7722 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 7723 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 7724 | } |
| 7725 | /* Map 16-bit characters to '\uxxxx' */ |
| 7726 | else { |
| 7727 | *p++ = '\\'; |
| 7728 | *p++ = 'u'; |
| 7729 | *p++ = hexdigits[(ucs >> 12) & 0x000F]; |
| 7730 | *p++ = hexdigits[(ucs >> 8) & 0x000F]; |
| 7731 | *p++ = hexdigits[(ucs >> 4) & 0x000F]; |
| 7732 | *p++ = hexdigits[ucs & 0x000F]; |
| 7733 | } |
| 7734 | } |
| 7735 | /* Copy characters as-is */ |
| 7736 | else { |
| 7737 | *p++ = ch; |
| 7738 | #ifndef Py_UNICODE_WIDE |
| 7739 | if (ucs >= 0x10000) |
| 7740 | *p++ = ch2; |
| 7741 | #endif |
| 7742 | } |
| 7743 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7744 | } |
| 7745 | /* Add quote */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7746 | *p++ = PyUnicode_AS_UNICODE(repr)[0]; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7747 | |
| 7748 | *p = '\0'; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7749 | _PyUnicode_Resize(&repr, p - PyUnicode_AS_UNICODE(repr)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 7750 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7751 | } |
| 7752 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7753 | PyDoc_STRVAR(rfind__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7754 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7755 | \n\ |
| 7756 | 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] | 7757 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7758 | arguments start and end are interpreted as in slice notation.\n\ |
| 7759 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7760 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7761 | |
| 7762 | static PyObject * |
| 7763 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 7764 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7765 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7766 | Py_ssize_t start; |
| 7767 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7768 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7769 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7770 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
| 7771 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7772 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7773 | result = stringlib_rfind_slice( |
| 7774 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7775 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7776 | start, end |
| 7777 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7778 | |
| 7779 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7780 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7781 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7782 | } |
| 7783 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7784 | PyDoc_STRVAR(rindex__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7785 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7786 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7787 | 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] | 7788 | |
| 7789 | static PyObject * |
| 7790 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 7791 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7792 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7793 | Py_ssize_t start; |
| 7794 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7795 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7796 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7797 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
| 7798 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7799 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7800 | result = stringlib_rfind_slice( |
| 7801 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7802 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7803 | start, end |
| 7804 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7805 | |
| 7806 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7807 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7808 | if (result < 0) { |
| 7809 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 7810 | return NULL; |
| 7811 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7812 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7813 | } |
| 7814 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7815 | PyDoc_STRVAR(rjust__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7816 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7817 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame^] | 7818 | 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] | 7819 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7820 | |
| 7821 | static PyObject * |
| 7822 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 7823 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7824 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7825 | Py_UNICODE fillchar = ' '; |
| 7826 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7827 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7828 | return NULL; |
| 7829 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7830 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7831 | Py_INCREF(self); |
| 7832 | return (PyObject*) self; |
| 7833 | } |
| 7834 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7835 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7836 | } |
| 7837 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7838 | PyObject *PyUnicode_Split(PyObject *s, |
| 7839 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7840 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7841 | { |
| 7842 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7843 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7844 | s = PyUnicode_FromObject(s); |
| 7845 | if (s == NULL) |
| 7846 | return NULL; |
| 7847 | if (sep != NULL) { |
| 7848 | sep = PyUnicode_FromObject(sep); |
| 7849 | if (sep == NULL) { |
| 7850 | Py_DECREF(s); |
| 7851 | return NULL; |
| 7852 | } |
| 7853 | } |
| 7854 | |
| 7855 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 7856 | |
| 7857 | Py_DECREF(s); |
| 7858 | Py_XDECREF(sep); |
| 7859 | return result; |
| 7860 | } |
| 7861 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7862 | PyDoc_STRVAR(split__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7863 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7864 | \n\ |
| 7865 | Return a list of the words in S, using sep as the\n\ |
| 7866 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 7867 | 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] | 7868 | whitespace string is a separator and empty strings are\n\ |
| 7869 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7870 | |
| 7871 | static PyObject* |
| 7872 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 7873 | { |
| 7874 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7875 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7876 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7877 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7878 | return NULL; |
| 7879 | |
| 7880 | if (substring == Py_None) |
| 7881 | return split(self, NULL, maxcount); |
| 7882 | else if (PyUnicode_Check(substring)) |
| 7883 | return split(self, (PyUnicodeObject *)substring, maxcount); |
| 7884 | else |
| 7885 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
| 7886 | } |
| 7887 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7888 | PyObject * |
| 7889 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 7890 | { |
| 7891 | PyObject* str_obj; |
| 7892 | PyObject* sep_obj; |
| 7893 | PyObject* out; |
| 7894 | |
| 7895 | str_obj = PyUnicode_FromObject(str_in); |
| 7896 | if (!str_obj) |
| 7897 | return NULL; |
| 7898 | sep_obj = PyUnicode_FromObject(sep_in); |
| 7899 | if (!sep_obj) { |
| 7900 | Py_DECREF(str_obj); |
| 7901 | return NULL; |
| 7902 | } |
| 7903 | |
| 7904 | out = stringlib_partition( |
| 7905 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 7906 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 7907 | ); |
| 7908 | |
| 7909 | Py_DECREF(sep_obj); |
| 7910 | Py_DECREF(str_obj); |
| 7911 | |
| 7912 | return out; |
| 7913 | } |
| 7914 | |
| 7915 | |
| 7916 | PyObject * |
| 7917 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 7918 | { |
| 7919 | PyObject* str_obj; |
| 7920 | PyObject* sep_obj; |
| 7921 | PyObject* out; |
| 7922 | |
| 7923 | str_obj = PyUnicode_FromObject(str_in); |
| 7924 | if (!str_obj) |
| 7925 | return NULL; |
| 7926 | sep_obj = PyUnicode_FromObject(sep_in); |
| 7927 | if (!sep_obj) { |
| 7928 | Py_DECREF(str_obj); |
| 7929 | return NULL; |
| 7930 | } |
| 7931 | |
| 7932 | out = stringlib_rpartition( |
| 7933 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 7934 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 7935 | ); |
| 7936 | |
| 7937 | Py_DECREF(sep_obj); |
| 7938 | Py_DECREF(str_obj); |
| 7939 | |
| 7940 | return out; |
| 7941 | } |
| 7942 | |
| 7943 | PyDoc_STRVAR(partition__doc__, |
| 7944 | "S.partition(sep) -> (head, sep, tail)\n\ |
| 7945 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7946 | 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] | 7947 | 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^] | 7948 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7949 | |
| 7950 | static PyObject* |
| 7951 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
| 7952 | { |
| 7953 | return PyUnicode_Partition((PyObject *)self, separator); |
| 7954 | } |
| 7955 | |
| 7956 | PyDoc_STRVAR(rpartition__doc__, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7957 | "S.rpartition(sep) -> (tail, sep, head)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7958 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7959 | 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] | 7960 | 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^] | 7961 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7962 | |
| 7963 | static PyObject* |
| 7964 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 7965 | { |
| 7966 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 7967 | } |
| 7968 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7969 | PyObject *PyUnicode_RSplit(PyObject *s, |
| 7970 | PyObject *sep, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7971 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7972 | { |
| 7973 | PyObject *result; |
| 7974 | |
| 7975 | s = PyUnicode_FromObject(s); |
| 7976 | if (s == NULL) |
| 7977 | return NULL; |
| 7978 | if (sep != NULL) { |
| 7979 | sep = PyUnicode_FromObject(sep); |
| 7980 | if (sep == NULL) { |
| 7981 | Py_DECREF(s); |
| 7982 | return NULL; |
| 7983 | } |
| 7984 | } |
| 7985 | |
| 7986 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 7987 | |
| 7988 | Py_DECREF(s); |
| 7989 | Py_XDECREF(sep); |
| 7990 | return result; |
| 7991 | } |
| 7992 | |
| 7993 | PyDoc_STRVAR(rsplit__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7994 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 7995 | \n\ |
| 7996 | Return a list of the words in S, using sep as the\n\ |
| 7997 | delimiter string, starting at the end of the string and\n\ |
| 7998 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 7999 | splits are done. If sep is not specified, any whitespace string\n\ |
| 8000 | is a separator."); |
| 8001 | |
| 8002 | static PyObject* |
| 8003 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 8004 | { |
| 8005 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8006 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8007 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8008 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8009 | return NULL; |
| 8010 | |
| 8011 | if (substring == Py_None) |
| 8012 | return rsplit(self, NULL, maxcount); |
| 8013 | else if (PyUnicode_Check(substring)) |
| 8014 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
| 8015 | else |
| 8016 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
| 8017 | } |
| 8018 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8019 | PyDoc_STRVAR(splitlines__doc__, |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8020 | "S.splitlines([keepends]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8021 | \n\ |
| 8022 | 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] | 8023 | 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] | 8024 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8025 | |
| 8026 | static PyObject* |
| 8027 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 8028 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8029 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8030 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8031 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8032 | return NULL; |
| 8033 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8034 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8035 | } |
| 8036 | |
| 8037 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 8038 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8039 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 8040 | if (PyUnicode_CheckExact(self)) { |
| 8041 | Py_INCREF(self); |
| 8042 | return self; |
| 8043 | } else |
| 8044 | /* Subtype -- return genuine unicode string with the same value. */ |
| 8045 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(self), |
| 8046 | PyUnicode_GET_SIZE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8047 | } |
| 8048 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8049 | PyDoc_STRVAR(swapcase__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8050 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8051 | \n\ |
| 8052 | 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] | 8053 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8054 | |
| 8055 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8056 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8057 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8058 | return fixup(self, fixswapcase); |
| 8059 | } |
| 8060 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8061 | PyDoc_STRVAR(maketrans__doc__, |
| 8062 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
| 8063 | \n\ |
| 8064 | Return a translation table usable for str.translate().\n\ |
| 8065 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 8066 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8067 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8068 | If there are two arguments, they must be strings of equal length, and\n\ |
| 8069 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 8070 | character at the same position in y. If there is a third argument, it\n\ |
| 8071 | must be a string, whose characters will be mapped to None in the result."); |
| 8072 | |
| 8073 | static PyObject* |
| 8074 | unicode_maketrans(PyUnicodeObject *null, PyObject *args) |
| 8075 | { |
| 8076 | PyObject *x, *y = NULL, *z = NULL; |
| 8077 | PyObject *new = NULL, *key, *value; |
| 8078 | Py_ssize_t i = 0; |
| 8079 | int res; |
| 8080 | |
| 8081 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 8082 | return NULL; |
| 8083 | new = PyDict_New(); |
| 8084 | if (!new) |
| 8085 | return NULL; |
| 8086 | if (y != NULL) { |
| 8087 | /* x must be a string too, of equal length */ |
| 8088 | Py_ssize_t ylen = PyUnicode_GET_SIZE(y); |
| 8089 | if (!PyUnicode_Check(x)) { |
| 8090 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 8091 | "be a string if there is a second argument"); |
| 8092 | goto err; |
| 8093 | } |
| 8094 | if (PyUnicode_GET_SIZE(x) != ylen) { |
| 8095 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 8096 | "arguments must have equal length"); |
| 8097 | goto err; |
| 8098 | } |
| 8099 | /* create entries for translating chars in x to those in y */ |
| 8100 | for (i = 0; i < PyUnicode_GET_SIZE(x); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8101 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(x)[i]); |
| 8102 | value = PyLong_FromLong(PyUnicode_AS_UNICODE(y)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8103 | if (!key || !value) |
| 8104 | goto err; |
| 8105 | res = PyDict_SetItem(new, key, value); |
| 8106 | Py_DECREF(key); |
| 8107 | Py_DECREF(value); |
| 8108 | if (res < 0) |
| 8109 | goto err; |
| 8110 | } |
| 8111 | /* create entries for deleting chars in z */ |
| 8112 | if (z != NULL) { |
| 8113 | for (i = 0; i < PyUnicode_GET_SIZE(z); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8114 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(z)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8115 | if (!key) |
| 8116 | goto err; |
| 8117 | res = PyDict_SetItem(new, key, Py_None); |
| 8118 | Py_DECREF(key); |
| 8119 | if (res < 0) |
| 8120 | goto err; |
| 8121 | } |
| 8122 | } |
| 8123 | } else { |
| 8124 | /* x must be a dict */ |
| 8125 | if (!PyDict_Check(x)) { |
| 8126 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 8127 | "to maketrans it must be a dict"); |
| 8128 | goto err; |
| 8129 | } |
| 8130 | /* copy entries into the new dict, converting string keys to int keys */ |
| 8131 | while (PyDict_Next(x, &i, &key, &value)) { |
| 8132 | if (PyUnicode_Check(key)) { |
| 8133 | /* convert string keys to integer keys */ |
| 8134 | PyObject *newkey; |
| 8135 | if (PyUnicode_GET_SIZE(key) != 1) { |
| 8136 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 8137 | "table must be of length 1"); |
| 8138 | goto err; |
| 8139 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8140 | newkey = PyLong_FromLong(PyUnicode_AS_UNICODE(key)[0]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8141 | if (!newkey) |
| 8142 | goto err; |
| 8143 | res = PyDict_SetItem(new, newkey, value); |
| 8144 | Py_DECREF(newkey); |
| 8145 | if (res < 0) |
| 8146 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8147 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8148 | /* just keep integer keys */ |
| 8149 | if (PyDict_SetItem(new, key, value) < 0) |
| 8150 | goto err; |
| 8151 | } else { |
| 8152 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 8153 | "be strings or integers"); |
| 8154 | goto err; |
| 8155 | } |
| 8156 | } |
| 8157 | } |
| 8158 | return new; |
| 8159 | err: |
| 8160 | Py_DECREF(new); |
| 8161 | return NULL; |
| 8162 | } |
| 8163 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8164 | PyDoc_STRVAR(translate__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8165 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8166 | \n\ |
| 8167 | Return a copy of the string S, where all characters have been mapped\n\ |
| 8168 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8169 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 8170 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 8171 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8172 | |
| 8173 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8174 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8175 | { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8176 | return PyUnicode_TranslateCharmap(self->str, self->length, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8177 | } |
| 8178 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8179 | PyDoc_STRVAR(upper__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8180 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8181 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8182 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8183 | |
| 8184 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8185 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8186 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8187 | return fixup(self, fixupper); |
| 8188 | } |
| 8189 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8190 | PyDoc_STRVAR(zfill__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8191 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8192 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 8193 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 8194 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8195 | |
| 8196 | static PyObject * |
| 8197 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 8198 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8199 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8200 | PyUnicodeObject *u; |
| 8201 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8202 | Py_ssize_t width; |
| 8203 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8204 | return NULL; |
| 8205 | |
| 8206 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 8207 | if (PyUnicode_CheckExact(self)) { |
| 8208 | Py_INCREF(self); |
| 8209 | return (PyObject*) self; |
| 8210 | } |
| 8211 | else |
| 8212 | return PyUnicode_FromUnicode( |
| 8213 | PyUnicode_AS_UNICODE(self), |
| 8214 | PyUnicode_GET_SIZE(self) |
| 8215 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8216 | } |
| 8217 | |
| 8218 | fill = width - self->length; |
| 8219 | |
| 8220 | u = pad(self, fill, 0, '0'); |
| 8221 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8222 | if (u == NULL) |
| 8223 | return NULL; |
| 8224 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8225 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 8226 | /* move sign to beginning of string */ |
| 8227 | u->str[0] = u->str[fill]; |
| 8228 | u->str[fill] = '0'; |
| 8229 | } |
| 8230 | |
| 8231 | return (PyObject*) u; |
| 8232 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8233 | |
| 8234 | #if 0 |
| 8235 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8236 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8237 | { |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 8238 | return PyLong_FromLong(numfree); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8239 | } |
| 8240 | #endif |
| 8241 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8242 | PyDoc_STRVAR(startswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 8243 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8244 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 8245 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 8246 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8247 | With optional end, stop comparing S at that position.\n\ |
| 8248 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8249 | |
| 8250 | static PyObject * |
| 8251 | unicode_startswith(PyUnicodeObject *self, |
| 8252 | PyObject *args) |
| 8253 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8254 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8255 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8256 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8257 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8258 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8259 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8260 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 8261 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8262 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8263 | if (PyTuple_Check(subobj)) { |
| 8264 | Py_ssize_t i; |
| 8265 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 8266 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 8267 | PyTuple_GET_ITEM(subobj, i)); |
| 8268 | if (substring == NULL) |
| 8269 | return NULL; |
| 8270 | result = tailmatch(self, substring, start, end, -1); |
| 8271 | Py_DECREF(substring); |
| 8272 | if (result) { |
| 8273 | Py_RETURN_TRUE; |
| 8274 | } |
| 8275 | } |
| 8276 | /* nothing matched */ |
| 8277 | Py_RETURN_FALSE; |
| 8278 | } |
| 8279 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8280 | if (substring == NULL) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8281 | return NULL; |
| 8282 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8283 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8284 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8285 | } |
| 8286 | |
| 8287 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8288 | PyDoc_STRVAR(endswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 8289 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8290 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 8291 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 8292 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8293 | With optional end, stop comparing S at that position.\n\ |
| 8294 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8295 | |
| 8296 | static PyObject * |
| 8297 | unicode_endswith(PyUnicodeObject *self, |
| 8298 | PyObject *args) |
| 8299 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8300 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8301 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8302 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8303 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8304 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8305 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8306 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
| 8307 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8308 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8309 | if (PyTuple_Check(subobj)) { |
| 8310 | Py_ssize_t i; |
| 8311 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 8312 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
| 8313 | PyTuple_GET_ITEM(subobj, i)); |
| 8314 | if (substring == NULL) |
| 8315 | return NULL; |
| 8316 | result = tailmatch(self, substring, start, end, +1); |
| 8317 | Py_DECREF(substring); |
| 8318 | if (result) { |
| 8319 | Py_RETURN_TRUE; |
| 8320 | } |
| 8321 | } |
| 8322 | Py_RETURN_FALSE; |
| 8323 | } |
| 8324 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8325 | if (substring == NULL) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8326 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8327 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8328 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8329 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8330 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8331 | } |
| 8332 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8333 | #include "stringlib/string_format.h" |
| 8334 | |
| 8335 | PyDoc_STRVAR(format__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8336 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8337 | \n\ |
| 8338 | "); |
| 8339 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 8340 | static PyObject * |
| 8341 | unicode__format__(PyObject* self, PyObject* args) |
| 8342 | { |
| 8343 | PyObject *format_spec; |
| 8344 | |
| 8345 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 8346 | return NULL; |
| 8347 | |
| 8348 | return _PyUnicode_FormatAdvanced(self, |
| 8349 | PyUnicode_AS_UNICODE(format_spec), |
| 8350 | PyUnicode_GET_SIZE(format_spec)); |
| 8351 | } |
| 8352 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8353 | PyDoc_STRVAR(p_format__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8354 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8355 | \n\ |
| 8356 | "); |
| 8357 | |
| 8358 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8359 | unicode__sizeof__(PyUnicodeObject *v) |
| 8360 | { |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 8361 | return PyLong_FromSsize_t(sizeof(PyUnicodeObject) + |
| 8362 | sizeof(Py_UNICODE) * (v->length + 1)); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8363 | } |
| 8364 | |
| 8365 | PyDoc_STRVAR(sizeof__doc__, |
| 8366 | "S.__sizeof__() -> size of S in memory, in bytes"); |
| 8367 | |
| 8368 | static PyObject * |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8369 | unicode_getnewargs(PyUnicodeObject *v) |
| 8370 | { |
| 8371 | return Py_BuildValue("(u#)", v->str, v->length); |
| 8372 | } |
| 8373 | |
| 8374 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8375 | static PyMethodDef unicode_methods[] = { |
| 8376 | |
| 8377 | /* Order is according to common usage: often used methods should |
| 8378 | appear first, since lookup is done sequentially. */ |
| 8379 | |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8380 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS, encode__doc__}, |
| 8381 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 8382 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8383 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8384 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 8385 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 8386 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 8387 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 8388 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 8389 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 8390 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8391 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8392 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 8393 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 8394 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8395 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8396 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 8397 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 8398 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8399 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8400 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8401 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8402 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8403 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 8404 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 8405 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 8406 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 8407 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 8408 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 8409 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 8410 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 8411 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 8412 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 8413 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 8414 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 8415 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 8416 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 8417 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8418 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8419 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 8420 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 8421 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Eric Smith | f6db409 | 2007-08-27 23:52:26 +0000 | [diff] [blame] | 8422 | {"_formatter_field_name_split", (PyCFunction) formatter_field_name_split, METH_NOARGS}, |
| 8423 | {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8424 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 8425 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8426 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8427 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8428 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8429 | #endif |
| 8430 | |
| 8431 | #if 0 |
| 8432 | /* This one is just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8433 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8434 | #endif |
| 8435 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8436 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8437 | {NULL, NULL} |
| 8438 | }; |
| 8439 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8440 | static PyObject * |
| 8441 | unicode_mod(PyObject *v, PyObject *w) |
| 8442 | { |
| 8443 | if (!PyUnicode_Check(v)) { |
| 8444 | Py_INCREF(Py_NotImplemented); |
| 8445 | return Py_NotImplemented; |
| 8446 | } |
| 8447 | return PyUnicode_Format(v, w); |
| 8448 | } |
| 8449 | |
| 8450 | static PyNumberMethods unicode_as_number = { |
| 8451 | 0, /*nb_add*/ |
| 8452 | 0, /*nb_subtract*/ |
| 8453 | 0, /*nb_multiply*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8454 | unicode_mod, /*nb_remainder*/ |
| 8455 | }; |
| 8456 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8457 | static PySequenceMethods unicode_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8458 | (lenfunc) unicode_length, /* sq_length */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8459 | PyUnicode_Concat, /* sq_concat */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8460 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 8461 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
Thomas Wouters | d2cf20e | 2007-08-30 22:57:53 +0000 | [diff] [blame] | 8462 | 0, /* sq_slice */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8463 | 0, /* sq_ass_item */ |
| 8464 | 0, /* sq_ass_slice */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8465 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8466 | }; |
| 8467 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8468 | static PyObject* |
| 8469 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 8470 | { |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 8471 | if (PyIndex_Check(item)) { |
| 8472 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8473 | if (i == -1 && PyErr_Occurred()) |
| 8474 | return NULL; |
| 8475 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8476 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8477 | return unicode_getitem(self, i); |
| 8478 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8479 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8480 | Py_UNICODE* source_buf; |
| 8481 | Py_UNICODE* result_buf; |
| 8482 | PyObject* result; |
| 8483 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8484 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8485 | &start, &stop, &step, &slicelength) < 0) { |
| 8486 | return NULL; |
| 8487 | } |
| 8488 | |
| 8489 | if (slicelength <= 0) { |
| 8490 | return PyUnicode_FromUnicode(NULL, 0); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 8491 | } else if (start == 0 && step == 1 && slicelength == self->length && |
| 8492 | PyUnicode_CheckExact(self)) { |
| 8493 | Py_INCREF(self); |
| 8494 | return (PyObject *)self; |
| 8495 | } else if (step == 1) { |
| 8496 | return PyUnicode_FromUnicode(self->str + start, slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8497 | } else { |
| 8498 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 8499 | result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* |
| 8500 | sizeof(Py_UNICODE)); |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8501 | |
| 8502 | if (result_buf == NULL) |
| 8503 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8504 | |
| 8505 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 8506 | result_buf[i] = source_buf[cur]; |
| 8507 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8508 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8509 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 8510 | PyObject_FREE(result_buf); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8511 | return result; |
| 8512 | } |
| 8513 | } else { |
| 8514 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 8515 | return NULL; |
| 8516 | } |
| 8517 | } |
| 8518 | |
| 8519 | static PyMappingMethods unicode_as_mapping = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8520 | (lenfunc)unicode_length, /* mp_length */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8521 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 8522 | (objobjargproc)0, /* mp_ass_subscript */ |
| 8523 | }; |
| 8524 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8525 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8526 | /* Helpers for PyUnicode_Format() */ |
| 8527 | |
| 8528 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8529 | 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] | 8530 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8531 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8532 | if (argidx < arglen) { |
| 8533 | (*p_argidx)++; |
| 8534 | if (arglen < 0) |
| 8535 | return args; |
| 8536 | else |
| 8537 | return PyTuple_GetItem(args, argidx); |
| 8538 | } |
| 8539 | PyErr_SetString(PyExc_TypeError, |
| 8540 | "not enough arguments for format string"); |
| 8541 | return NULL; |
| 8542 | } |
| 8543 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8544 | static Py_ssize_t |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8545 | strtounicode(Py_UNICODE *buffer, const char *charbuffer) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8546 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8547 | register Py_ssize_t i; |
| 8548 | Py_ssize_t len = strlen(charbuffer); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8549 | for (i = len - 1; i >= 0; i--) |
| 8550 | buffer[i] = (Py_UNICODE) charbuffer[i]; |
| 8551 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8552 | return len; |
| 8553 | } |
| 8554 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8555 | static int |
| 8556 | doubletounicode(Py_UNICODE *buffer, size_t len, const char *format, double x) |
| 8557 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8558 | Py_ssize_t result; |
| 8559 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8560 | PyOS_ascii_formatd((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8561 | result = strtounicode(buffer, (char *)buffer); |
| 8562 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8563 | } |
| 8564 | |
Christian Heimes | 3fd1399 | 2008-03-21 01:05:49 +0000 | [diff] [blame] | 8565 | #if 0 |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8566 | static int |
| 8567 | longtounicode(Py_UNICODE *buffer, size_t len, const char *format, long x) |
| 8568 | { |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8569 | Py_ssize_t result; |
| 8570 | |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8571 | PyOS_snprintf((char *)buffer, len, format, x); |
Tim Peters | 1523154 | 2006-02-16 01:08:01 +0000 | [diff] [blame] | 8572 | result = strtounicode(buffer, (char *)buffer); |
| 8573 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8574 | } |
Christian Heimes | 3fd1399 | 2008-03-21 01:05:49 +0000 | [diff] [blame] | 8575 | #endif |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8576 | |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 8577 | /* XXX To save some code duplication, formatfloat/long/int could have been |
| 8578 | shared with stringobject.c, converting from 8-bit to Unicode after the |
| 8579 | formatting is done. */ |
| 8580 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8581 | static int |
| 8582 | formatfloat(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8583 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8584 | int flags, |
| 8585 | int prec, |
| 8586 | int type, |
| 8587 | PyObject *v) |
| 8588 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8589 | /* fmt = '%#.' + `prec` + `type` |
| 8590 | 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] | 8591 | char fmt[20]; |
| 8592 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8593 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8594 | x = PyFloat_AsDouble(v); |
| 8595 | if (x == -1.0 && PyErr_Occurred()) |
| 8596 | return -1; |
| 8597 | if (prec < 0) |
| 8598 | prec = 6; |
Eric Smith | 22b85b3 | 2008-07-17 19:18:29 +0000 | [diff] [blame] | 8599 | if (type == 'f' && (fabs(x) / 1e25) >= 1e25) |
| 8600 | type = 'g'; |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8601 | /* Worst case length calc to ensure no buffer overrun: |
| 8602 | |
| 8603 | 'g' formats: |
| 8604 | fmt = %#.<prec>g |
| 8605 | buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
| 8606 | for any double rep.) |
| 8607 | len = 1 + prec + 1 + 2 + 5 = 9 + prec |
| 8608 | |
| 8609 | 'f' formats: |
| 8610 | buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
| 8611 | len = 1 + 50 + 1 + prec = 52 + prec |
| 8612 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8613 | If prec=0 the effective precision is 1 (the leading digit is |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8614 | always given), therefore increase the length by one. |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8615 | |
| 8616 | */ |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 8617 | if (((type == 'g' || type == 'G') && |
| 8618 | buflen <= (size_t)10 + (size_t)prec) || |
Eric Smith | 22b85b3 | 2008-07-17 19:18:29 +0000 | [diff] [blame] | 8619 | (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8620 | PyErr_SetString(PyExc_OverflowError, |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8621 | "formatted float is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8622 | return -1; |
| 8623 | } |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 8624 | PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
| 8625 | (flags&F_ALT) ? "#" : "", |
| 8626 | prec, type); |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8627 | return doubletounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8628 | } |
| 8629 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8630 | static PyObject* |
| 8631 | formatlong(PyObject *val, int flags, int prec, int type) |
| 8632 | { |
| 8633 | char *buf; |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8634 | int len; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8635 | PyObject *str; /* temporary string object. */ |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8636 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8637 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 8638 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8639 | if (!str) |
| 8640 | return NULL; |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8641 | result = PyUnicode_FromStringAndSize(buf, len); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8642 | Py_DECREF(str); |
Walter Dörwald | 63a28be | 2007-06-20 15:11:12 +0000 | [diff] [blame] | 8643 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8644 | } |
| 8645 | |
Christian Heimes | 3fd1399 | 2008-03-21 01:05:49 +0000 | [diff] [blame] | 8646 | #if 0 |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8647 | static int |
| 8648 | formatint(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8649 | size_t buflen, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8650 | int flags, |
| 8651 | int prec, |
| 8652 | int type, |
| 8653 | PyObject *v) |
| 8654 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8655 | /* fmt = '%#.' + `prec` + 'l' + `type` |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8656 | * worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
| 8657 | * + 1 + 1 |
| 8658 | * = 24 |
| 8659 | */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 8660 | char fmt[64]; /* plenty big enough! */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8661 | char *sign; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8662 | long x; |
| 8663 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8664 | x = PyLong_AsLong(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8665 | if (x == -1 && PyErr_Occurred()) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8666 | return -1; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8667 | if (x < 0 && type == 'u') { |
| 8668 | type = 'd'; |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 8669 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8670 | if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) |
| 8671 | sign = "-"; |
| 8672 | else |
| 8673 | sign = ""; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8674 | if (prec < 0) |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8675 | prec = 1; |
| 8676 | |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8677 | /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) |
| 8678 | * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8679 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8680 | if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8681 | PyErr_SetString(PyExc_OverflowError, |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8682 | "formatted integer is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8683 | return -1; |
| 8684 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8685 | |
| 8686 | if ((flags & F_ALT) && |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 8687 | (type == 'x' || type == 'X' || type == 'o')) { |
| 8688 | /* When converting under %#o, %#x or %#X, there are a number |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8689 | * of issues that cause pain: |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 8690 | * - for %#o, we want a different base marker than C |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8691 | * - when 0 is being converted, the C standard leaves off |
| 8692 | * the '0x' or '0X', which is inconsistent with other |
| 8693 | * %#x/%#X conversions and inconsistent with Python's |
| 8694 | * hex() function |
| 8695 | * - there are platforms that violate the standard and |
| 8696 | * convert 0 with the '0x' or '0X' |
| 8697 | * (Metrowerks, Compaq Tru64) |
| 8698 | * - there are platforms that give '0x' when converting |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8699 | * under %#X, but convert 0 in accordance with the |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8700 | * standard (OS/2 EMX) |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8701 | * |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8702 | * We can achieve the desired consistency by inserting our |
| 8703 | * own '0x' or '0X' prefix, and substituting %x/%X in place |
| 8704 | * of %#x/%#X. |
| 8705 | * |
| 8706 | * Note that this is the same approach as used in |
| 8707 | * formatint() in stringobject.c |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 8708 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8709 | PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", |
| 8710 | sign, type, prec, type); |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 8711 | } |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8712 | else { |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8713 | PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", |
| 8714 | sign, (flags&F_ALT) ? "#" : "", |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 8715 | prec, type); |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 8716 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8717 | if (sign[0]) |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8718 | return longtounicode(buf, buflen, fmt, -x); |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 8719 | else |
Neal Norwitz | fc76d63 | 2006-01-10 06:03:13 +0000 | [diff] [blame] | 8720 | return longtounicode(buf, buflen, fmt, x); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8721 | } |
Christian Heimes | 3fd1399 | 2008-03-21 01:05:49 +0000 | [diff] [blame] | 8722 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8723 | |
| 8724 | static int |
| 8725 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8726 | size_t buflen, |
| 8727 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8728 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 8729 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8730 | if (PyUnicode_Check(v)) { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 8731 | if (PyUnicode_GET_SIZE(v) == 1) { |
| 8732 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
| 8733 | buf[1] = '\0'; |
| 8734 | return 1; |
| 8735 | } |
| 8736 | #ifndef Py_UNICODE_WIDE |
| 8737 | if (PyUnicode_GET_SIZE(v) == 2) { |
| 8738 | /* Decode a valid surrogate pair */ |
| 8739 | int c0 = PyUnicode_AS_UNICODE(v)[0]; |
| 8740 | int c1 = PyUnicode_AS_UNICODE(v)[1]; |
| 8741 | if (0xD800 <= c0 && c0 <= 0xDBFF && |
| 8742 | 0xDC00 <= c1 && c1 <= 0xDFFF) { |
| 8743 | buf[0] = c0; |
| 8744 | buf[1] = c1; |
| 8745 | buf[2] = '\0'; |
| 8746 | return 2; |
| 8747 | } |
| 8748 | } |
| 8749 | #endif |
| 8750 | goto onError; |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8751 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8752 | else { |
| 8753 | /* Integer input truncated to a character */ |
| 8754 | long x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8755 | x = PyLong_AsLong(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8756 | if (x == -1 && PyErr_Occurred()) |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8757 | goto onError; |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 8758 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8759 | if (x < 0 || x > 0x10ffff) { |
Walter Dörwald | 44f527f | 2003-04-02 16:37:24 +0000 | [diff] [blame] | 8760 | PyErr_SetString(PyExc_OverflowError, |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 8761 | "%c arg not in range(0x110000)"); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8762 | return -1; |
| 8763 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 8764 | |
| 8765 | #ifndef Py_UNICODE_WIDE |
| 8766 | if (x > 0xffff) { |
| 8767 | x -= 0x10000; |
| 8768 | buf[0] = (Py_UNICODE)(0xD800 | (x >> 10)); |
| 8769 | buf[1] = (Py_UNICODE)(0xDC00 | (x & 0x3FF)); |
| 8770 | return 2; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 8771 | } |
| 8772 | #endif |
| 8773 | buf[0] = (Py_UNICODE) x; |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 8774 | buf[1] = '\0'; |
| 8775 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8776 | } |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 8777 | |
| 8778 | onError: |
| 8779 | PyErr_SetString(PyExc_TypeError, |
| 8780 | "%c requires int or char"); |
| 8781 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8782 | } |
| 8783 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8784 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 8785 | |
| 8786 | FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
| 8787 | chars are formatted. XXX This is a magic number. Each formatting |
| 8788 | routine does bounds checking to ensure no overflow, but a better |
| 8789 | solution may be to malloc a buffer of appropriate size for each |
| 8790 | format. For now, the current solution is sufficient. |
| 8791 | */ |
| 8792 | #define FORMATBUFLEN (size_t)120 |
| 8793 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8794 | PyObject *PyUnicode_Format(PyObject *format, |
| 8795 | PyObject *args) |
| 8796 | { |
| 8797 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8798 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8799 | int args_owned = 0; |
| 8800 | PyUnicodeObject *result = NULL; |
| 8801 | PyObject *dict = NULL; |
| 8802 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8803 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8804 | if (format == NULL || args == NULL) { |
| 8805 | PyErr_BadInternalCall(); |
| 8806 | return NULL; |
| 8807 | } |
| 8808 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8809 | if (uformat == NULL) |
| 8810 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8811 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 8812 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 8813 | |
| 8814 | reslen = rescnt = fmtcnt + 100; |
| 8815 | result = _PyUnicode_New(reslen); |
| 8816 | if (result == NULL) |
| 8817 | goto onError; |
| 8818 | res = PyUnicode_AS_UNICODE(result); |
| 8819 | |
| 8820 | if (PyTuple_Check(args)) { |
| 8821 | arglen = PyTuple_Size(args); |
| 8822 | argidx = 0; |
| 8823 | } |
| 8824 | else { |
| 8825 | arglen = -1; |
| 8826 | argidx = -2; |
| 8827 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 8828 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 8829 | !PyUnicode_Check(args)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8830 | dict = args; |
| 8831 | |
| 8832 | while (--fmtcnt >= 0) { |
| 8833 | if (*fmt != '%') { |
| 8834 | if (--rescnt < 0) { |
| 8835 | rescnt = fmtcnt + 100; |
| 8836 | reslen += rescnt; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 8837 | if (_PyUnicode_Resize(&result, reslen) < 0) |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 8838 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8839 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 8840 | --rescnt; |
| 8841 | } |
| 8842 | *res++ = *fmt++; |
| 8843 | } |
| 8844 | else { |
| 8845 | /* Got a format specifier */ |
| 8846 | int flags = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8847 | Py_ssize_t width = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8848 | int prec = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8849 | Py_UNICODE c = '\0'; |
| 8850 | Py_UNICODE fill; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 8851 | int isnumok; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8852 | PyObject *v = NULL; |
| 8853 | PyObject *temp = NULL; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8854 | Py_UNICODE *pbuf; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8855 | Py_UNICODE sign; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8856 | Py_ssize_t len; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 8857 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8858 | |
| 8859 | fmt++; |
| 8860 | if (*fmt == '(') { |
| 8861 | Py_UNICODE *keystart; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8862 | Py_ssize_t keylen; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8863 | PyObject *key; |
| 8864 | int pcount = 1; |
| 8865 | |
| 8866 | if (dict == NULL) { |
| 8867 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8868 | "format requires a mapping"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8869 | goto onError; |
| 8870 | } |
| 8871 | ++fmt; |
| 8872 | --fmtcnt; |
| 8873 | keystart = fmt; |
| 8874 | /* Skip over balanced parentheses */ |
| 8875 | while (pcount > 0 && --fmtcnt >= 0) { |
| 8876 | if (*fmt == ')') |
| 8877 | --pcount; |
| 8878 | else if (*fmt == '(') |
| 8879 | ++pcount; |
| 8880 | fmt++; |
| 8881 | } |
| 8882 | keylen = fmt - keystart - 1; |
| 8883 | if (fmtcnt < 0 || pcount > 0) { |
| 8884 | PyErr_SetString(PyExc_ValueError, |
| 8885 | "incomplete format key"); |
| 8886 | goto onError; |
| 8887 | } |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 8888 | #if 0 |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8889 | /* keys are converted to strings using UTF-8 and |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8890 | then looked up since Python uses strings to hold |
| 8891 | variables names etc. in its namespaces and we |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 8892 | wouldn't want to break common idioms. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8893 | key = PyUnicode_EncodeUTF8(keystart, |
| 8894 | keylen, |
| 8895 | NULL); |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 8896 | #else |
| 8897 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 8898 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8899 | if (key == NULL) |
| 8900 | goto onError; |
| 8901 | if (args_owned) { |
| 8902 | Py_DECREF(args); |
| 8903 | args_owned = 0; |
| 8904 | } |
| 8905 | args = PyObject_GetItem(dict, key); |
| 8906 | Py_DECREF(key); |
| 8907 | if (args == NULL) { |
| 8908 | goto onError; |
| 8909 | } |
| 8910 | args_owned = 1; |
| 8911 | arglen = -1; |
| 8912 | argidx = -2; |
| 8913 | } |
| 8914 | while (--fmtcnt >= 0) { |
| 8915 | switch (c = *fmt++) { |
| 8916 | case '-': flags |= F_LJUST; continue; |
| 8917 | case '+': flags |= F_SIGN; continue; |
| 8918 | case ' ': flags |= F_BLANK; continue; |
| 8919 | case '#': flags |= F_ALT; continue; |
| 8920 | case '0': flags |= F_ZERO; continue; |
| 8921 | } |
| 8922 | break; |
| 8923 | } |
| 8924 | if (c == '*') { |
| 8925 | v = getnextarg(args, arglen, &argidx); |
| 8926 | if (v == NULL) |
| 8927 | goto onError; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8928 | if (!PyLong_Check(v)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8929 | PyErr_SetString(PyExc_TypeError, |
| 8930 | "* wants int"); |
| 8931 | goto onError; |
| 8932 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8933 | width = PyLong_AsLong(v); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 8934 | if (width == -1 && PyErr_Occurred()) |
| 8935 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8936 | if (width < 0) { |
| 8937 | flags |= F_LJUST; |
| 8938 | width = -width; |
| 8939 | } |
| 8940 | if (--fmtcnt >= 0) |
| 8941 | c = *fmt++; |
| 8942 | } |
| 8943 | else if (c >= '0' && c <= '9') { |
| 8944 | width = c - '0'; |
| 8945 | while (--fmtcnt >= 0) { |
| 8946 | c = *fmt++; |
| 8947 | if (c < '0' || c > '9') |
| 8948 | break; |
| 8949 | if ((width*10) / 10 != width) { |
| 8950 | PyErr_SetString(PyExc_ValueError, |
| 8951 | "width too big"); |
| 8952 | goto onError; |
| 8953 | } |
| 8954 | width = width*10 + (c - '0'); |
| 8955 | } |
| 8956 | } |
| 8957 | if (c == '.') { |
| 8958 | prec = 0; |
| 8959 | if (--fmtcnt >= 0) |
| 8960 | c = *fmt++; |
| 8961 | if (c == '*') { |
| 8962 | v = getnextarg(args, arglen, &argidx); |
| 8963 | if (v == NULL) |
| 8964 | goto onError; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8965 | if (!PyLong_Check(v)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8966 | PyErr_SetString(PyExc_TypeError, |
| 8967 | "* wants int"); |
| 8968 | goto onError; |
| 8969 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8970 | prec = PyLong_AsLong(v); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 8971 | if (prec == -1 && PyErr_Occurred()) |
| 8972 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8973 | if (prec < 0) |
| 8974 | prec = 0; |
| 8975 | if (--fmtcnt >= 0) |
| 8976 | c = *fmt++; |
| 8977 | } |
| 8978 | else if (c >= '0' && c <= '9') { |
| 8979 | prec = c - '0'; |
| 8980 | while (--fmtcnt >= 0) { |
| 8981 | c = Py_CHARMASK(*fmt++); |
| 8982 | if (c < '0' || c > '9') |
| 8983 | break; |
| 8984 | if ((prec*10) / 10 != prec) { |
| 8985 | PyErr_SetString(PyExc_ValueError, |
| 8986 | "prec too big"); |
| 8987 | goto onError; |
| 8988 | } |
| 8989 | prec = prec*10 + (c - '0'); |
| 8990 | } |
| 8991 | } |
| 8992 | } /* prec */ |
| 8993 | if (fmtcnt >= 0) { |
| 8994 | if (c == 'h' || c == 'l' || c == 'L') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8995 | if (--fmtcnt >= 0) |
| 8996 | c = *fmt++; |
| 8997 | } |
| 8998 | } |
| 8999 | if (fmtcnt < 0) { |
| 9000 | PyErr_SetString(PyExc_ValueError, |
| 9001 | "incomplete format"); |
| 9002 | goto onError; |
| 9003 | } |
| 9004 | if (c != '%') { |
| 9005 | v = getnextarg(args, arglen, &argidx); |
| 9006 | if (v == NULL) |
| 9007 | goto onError; |
| 9008 | } |
| 9009 | sign = 0; |
| 9010 | fill = ' '; |
| 9011 | switch (c) { |
| 9012 | |
| 9013 | case '%': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9014 | pbuf = formatbuf; |
| 9015 | /* presume that buffer length is at least 1 */ |
| 9016 | pbuf[0] = '%'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9017 | len = 1; |
| 9018 | break; |
| 9019 | |
| 9020 | case 's': |
| 9021 | case 'r': |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 9022 | case 'a': |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9023 | if (PyUnicode_Check(v) && c == 's') { |
| 9024 | temp = v; |
| 9025 | Py_INCREF(temp); |
| 9026 | } |
| 9027 | else { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9028 | if (c == 's') |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 9029 | temp = PyObject_Str(v); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 9030 | else if (c == 'r') |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9031 | temp = PyObject_Repr(v); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 9032 | else |
| 9033 | temp = PyObject_ASCII(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9034 | if (temp == NULL) |
| 9035 | goto onError; |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 9036 | if (PyUnicode_Check(temp)) |
| 9037 | /* nothing to do */; |
Marc-André Lemburg | d25c650 | 2004-07-23 16:13:25 +0000 | [diff] [blame] | 9038 | else { |
| 9039 | Py_DECREF(temp); |
| 9040 | PyErr_SetString(PyExc_TypeError, |
| 9041 | "%s argument has non-string str()"); |
| 9042 | goto onError; |
| 9043 | } |
| 9044 | } |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9045 | pbuf = PyUnicode_AS_UNICODE(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9046 | len = PyUnicode_GET_SIZE(temp); |
| 9047 | if (prec >= 0 && len > prec) |
| 9048 | len = prec; |
| 9049 | break; |
| 9050 | |
| 9051 | case 'i': |
| 9052 | case 'd': |
| 9053 | case 'u': |
| 9054 | case 'o': |
| 9055 | case 'x': |
| 9056 | case 'X': |
| 9057 | if (c == 'i') |
| 9058 | c = 'd'; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 9059 | isnumok = 0; |
| 9060 | if (PyNumber_Check(v)) { |
| 9061 | PyObject *iobj=NULL; |
| 9062 | |
| 9063 | if (PyLong_Check(v)) { |
| 9064 | iobj = v; |
| 9065 | Py_INCREF(iobj); |
| 9066 | } |
| 9067 | else { |
| 9068 | iobj = PyNumber_Long(v); |
| 9069 | } |
| 9070 | if (iobj!=NULL) { |
| 9071 | if (PyLong_Check(iobj)) { |
| 9072 | isnumok = 1; |
| 9073 | temp = formatlong(iobj, flags, prec, c); |
| 9074 | Py_DECREF(iobj); |
| 9075 | if (!temp) |
| 9076 | goto onError; |
| 9077 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9078 | len = PyUnicode_GET_SIZE(temp); |
| 9079 | sign = 1; |
| 9080 | } |
| 9081 | else { |
| 9082 | Py_DECREF(iobj); |
| 9083 | } |
| 9084 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9085 | } |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 9086 | if (!isnumok) { |
| 9087 | PyErr_Format(PyExc_TypeError, |
| 9088 | "%%%c format: a number is required, " |
Martin v. Löwis | 5a6f458 | 2008-04-07 03:22:07 +0000 | [diff] [blame] | 9089 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9090 | goto onError; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9091 | } |
| 9092 | if (flags & F_ZERO) |
| 9093 | fill = '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9094 | break; |
| 9095 | |
| 9096 | case 'e': |
| 9097 | case 'E': |
| 9098 | case 'f': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 9099 | case 'F': |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9100 | case 'g': |
| 9101 | case 'G': |
Eric Smith | 22b85b3 | 2008-07-17 19:18:29 +0000 | [diff] [blame] | 9102 | if (c == 'F') |
| 9103 | c = 'f'; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9104 | pbuf = formatbuf; |
| 9105 | len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), |
| 9106 | flags, prec, c, v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9107 | if (len < 0) |
| 9108 | goto onError; |
| 9109 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9110 | if (flags & F_ZERO) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9111 | fill = '0'; |
| 9112 | break; |
| 9113 | |
| 9114 | case 'c': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9115 | pbuf = formatbuf; |
| 9116 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9117 | if (len < 0) |
| 9118 | goto onError; |
| 9119 | break; |
| 9120 | |
| 9121 | default: |
| 9122 | PyErr_Format(PyExc_ValueError, |
Andrew M. Kuchling | 6ca8917 | 2000-12-15 13:07:46 +0000 | [diff] [blame] | 9123 | "unsupported format character '%c' (0x%x) " |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 9124 | "at index %zd", |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9125 | (31<=c && c<=126) ? (char)c : '?', |
Marc-André Lemburg | 24e53b6 | 2002-09-24 09:32:14 +0000 | [diff] [blame] | 9126 | (int)c, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 9127 | (Py_ssize_t)(fmt - 1 - |
| 9128 | PyUnicode_AS_UNICODE(uformat))); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9129 | goto onError; |
| 9130 | } |
| 9131 | if (sign) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9132 | if (*pbuf == '-' || *pbuf == '+') { |
| 9133 | sign = *pbuf++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9134 | len--; |
| 9135 | } |
| 9136 | else if (flags & F_SIGN) |
| 9137 | sign = '+'; |
| 9138 | else if (flags & F_BLANK) |
| 9139 | sign = ' '; |
| 9140 | else |
| 9141 | sign = 0; |
| 9142 | } |
| 9143 | if (width < len) |
| 9144 | width = len; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 9145 | if (rescnt - (sign != 0) < width) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9146 | reslen -= rescnt; |
| 9147 | rescnt = width + fmtcnt + 100; |
| 9148 | reslen += rescnt; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 9149 | if (reslen < 0) { |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 9150 | Py_XDECREF(temp); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9151 | PyErr_NoMemory(); |
| 9152 | goto onError; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 9153 | } |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9154 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 9155 | Py_XDECREF(temp); |
| 9156 | goto onError; |
| 9157 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9158 | res = PyUnicode_AS_UNICODE(result) |
| 9159 | + reslen - rescnt; |
| 9160 | } |
| 9161 | if (sign) { |
| 9162 | if (fill != ' ') |
| 9163 | *res++ = sign; |
| 9164 | rescnt--; |
| 9165 | if (width > len) |
| 9166 | width--; |
| 9167 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 9168 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9169 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 9170 | assert(pbuf[1] == c); |
| 9171 | if (fill != ' ') { |
| 9172 | *res++ = *pbuf++; |
| 9173 | *res++ = *pbuf++; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9174 | } |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 9175 | rescnt -= 2; |
| 9176 | width -= 2; |
| 9177 | if (width < 0) |
| 9178 | width = 0; |
| 9179 | len -= 2; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9180 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9181 | if (width > len && !(flags & F_LJUST)) { |
| 9182 | do { |
| 9183 | --rescnt; |
| 9184 | *res++ = fill; |
| 9185 | } while (--width > len); |
| 9186 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9187 | if (fill == ' ') { |
| 9188 | if (sign) |
| 9189 | *res++ = sign; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 9190 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9191 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 9192 | assert(pbuf[1] == c); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9193 | *res++ = *pbuf++; |
| 9194 | *res++ = *pbuf++; |
| 9195 | } |
| 9196 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9197 | Py_UNICODE_COPY(res, pbuf, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9198 | res += len; |
| 9199 | rescnt -= len; |
| 9200 | while (--width >= len) { |
| 9201 | --rescnt; |
| 9202 | *res++ = ' '; |
| 9203 | } |
| 9204 | if (dict && (argidx < arglen) && c != '%') { |
| 9205 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 9206 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9207 | Py_XDECREF(temp); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9208 | goto onError; |
| 9209 | } |
| 9210 | Py_XDECREF(temp); |
| 9211 | } /* '%' */ |
| 9212 | } /* until end */ |
| 9213 | if (argidx < arglen && !dict) { |
| 9214 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 9215 | "not all arguments converted during string formatting"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9216 | goto onError; |
| 9217 | } |
| 9218 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9219 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
| 9220 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9221 | if (args_owned) { |
| 9222 | Py_DECREF(args); |
| 9223 | } |
| 9224 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9225 | return (PyObject *)result; |
| 9226 | |
| 9227 | onError: |
| 9228 | Py_XDECREF(result); |
| 9229 | Py_DECREF(uformat); |
| 9230 | if (args_owned) { |
| 9231 | Py_DECREF(args); |
| 9232 | } |
| 9233 | return NULL; |
| 9234 | } |
| 9235 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 9236 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9237 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 9238 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9239 | static PyObject * |
| 9240 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9241 | { |
| 9242 | PyObject *x = NULL; |
Guido van Rossum | 55b4a7b | 2007-07-11 09:28:11 +0000 | [diff] [blame] | 9243 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9244 | char *encoding = NULL; |
| 9245 | char *errors = NULL; |
| 9246 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9247 | if (type != &PyUnicode_Type) |
| 9248 | return unicode_subtype_new(type, args, kwds); |
Alexandre Vassalotti | 999679a | 2008-05-03 04:42:16 +0000 | [diff] [blame] | 9249 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9250 | kwlist, &x, &encoding, &errors)) |
| 9251 | return NULL; |
| 9252 | if (x == NULL) |
| 9253 | return (PyObject *)_PyUnicode_New(0); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 9254 | if (encoding == NULL && errors == NULL) |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 9255 | return PyObject_Str(x); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 9256 | else |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9257 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
| 9258 | } |
| 9259 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9260 | static PyObject * |
| 9261 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9262 | { |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 9263 | PyUnicodeObject *tmp, *pnew; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9264 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9265 | |
| 9266 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 9267 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 9268 | if (tmp == NULL) |
| 9269 | return NULL; |
| 9270 | assert(PyUnicode_Check(tmp)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 9271 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 9272 | if (pnew == NULL) { |
| 9273 | Py_DECREF(tmp); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9274 | return NULL; |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 9275 | } |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 9276 | pnew->str = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 9277 | if (pnew->str == NULL) { |
| 9278 | _Py_ForgetReference((PyObject *)pnew); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 9279 | PyObject_Del(pnew); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 9280 | Py_DECREF(tmp); |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 9281 | return PyErr_NoMemory(); |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9282 | } |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 9283 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 9284 | pnew->length = n; |
| 9285 | pnew->hash = tmp->hash; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9286 | Py_DECREF(tmp); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 9287 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9288 | } |
| 9289 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9290 | PyDoc_STRVAR(unicode_doc, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 9291 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9292 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 9293 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 9294 | encoding defaults to the current default string encoding.\n\ |
| 9295 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9296 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9297 | static PyObject *unicode_iter(PyObject *seq); |
| 9298 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9299 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 9300 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | 84fc66d | 2007-05-03 17:18:26 +0000 | [diff] [blame] | 9301 | "str", /* tp_name */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9302 | sizeof(PyUnicodeObject), /* tp_size */ |
| 9303 | 0, /* tp_itemsize */ |
| 9304 | /* Slots */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 9305 | (destructor)unicode_dealloc, /* tp_dealloc */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9306 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9307 | 0, /* tp_getattr */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9308 | 0, /* tp_setattr */ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9309 | 0, /* tp_compare */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9310 | unicode_repr, /* tp_repr */ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 9311 | &unicode_as_number, /* tp_as_number */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9312 | &unicode_as_sequence, /* tp_as_sequence */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 9313 | &unicode_as_mapping, /* tp_as_mapping */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9314 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 9315 | 0, /* tp_call*/ |
| 9316 | (reprfunc) unicode_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9317 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9318 | 0, /* tp_setattro */ |
Alexandre Vassalotti | 70a2371 | 2007-10-14 02:05:51 +0000 | [diff] [blame] | 9319 | 0, /* tp_as_buffer */ |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 9320 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 9321 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9322 | unicode_doc, /* tp_doc */ |
| 9323 | 0, /* tp_traverse */ |
| 9324 | 0, /* tp_clear */ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 9325 | PyUnicode_RichCompare, /* tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9326 | 0, /* tp_weaklistoffset */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9327 | unicode_iter, /* tp_iter */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9328 | 0, /* tp_iternext */ |
| 9329 | unicode_methods, /* tp_methods */ |
| 9330 | 0, /* tp_members */ |
| 9331 | 0, /* tp_getset */ |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 9332 | &PyBaseObject_Type, /* tp_base */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9333 | 0, /* tp_dict */ |
| 9334 | 0, /* tp_descr_get */ |
| 9335 | 0, /* tp_descr_set */ |
| 9336 | 0, /* tp_dictoffset */ |
| 9337 | 0, /* tp_init */ |
| 9338 | 0, /* tp_alloc */ |
| 9339 | unicode_new, /* tp_new */ |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 9340 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9341 | }; |
| 9342 | |
| 9343 | /* Initialize the Unicode implementation */ |
| 9344 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9345 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9346 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9347 | int i; |
| 9348 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9349 | /* XXX - move this array to unicodectype.c ? */ |
| 9350 | Py_UNICODE linebreak[] = { |
| 9351 | 0x000A, /* LINE FEED */ |
| 9352 | 0x000D, /* CARRIAGE RETURN */ |
| 9353 | 0x001C, /* FILE SEPARATOR */ |
| 9354 | 0x001D, /* GROUP SEPARATOR */ |
| 9355 | 0x001E, /* RECORD SEPARATOR */ |
| 9356 | 0x0085, /* NEXT LINE */ |
| 9357 | 0x2028, /* LINE SEPARATOR */ |
| 9358 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 9359 | }; |
| 9360 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 9361 | /* Init the implementation */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 9362 | free_list = NULL; |
| 9363 | numfree = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9364 | unicode_empty = _PyUnicode_New(0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9365 | if (!unicode_empty) |
| 9366 | return; |
| 9367 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9368 | for (i = 0; i < 256; i++) |
| 9369 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 9370 | if (PyType_Ready(&PyUnicode_Type) < 0) |
| 9371 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9372 | |
| 9373 | /* initialize the linebreak bloom filter */ |
| 9374 | bloom_linebreak = make_bloom_mask( |
| 9375 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 9376 | ); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9377 | |
| 9378 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9379 | } |
| 9380 | |
| 9381 | /* Finalize the Unicode implementation */ |
| 9382 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9383 | int |
| 9384 | PyUnicode_ClearFreeList(void) |
| 9385 | { |
| 9386 | int freelist_size = numfree; |
| 9387 | PyUnicodeObject *u; |
| 9388 | |
| 9389 | for (u = free_list; u != NULL;) { |
| 9390 | PyUnicodeObject *v = u; |
| 9391 | u = *(PyUnicodeObject **)u; |
| 9392 | if (v->str) |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 9393 | PyObject_DEL(v->str); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9394 | Py_XDECREF(v->defenc); |
| 9395 | PyObject_Del(v); |
| 9396 | numfree--; |
| 9397 | } |
| 9398 | free_list = NULL; |
| 9399 | assert(numfree == 0); |
| 9400 | return freelist_size; |
| 9401 | } |
| 9402 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9403 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9404 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9405 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9406 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9407 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 9408 | Py_XDECREF(unicode_empty); |
| 9409 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 9410 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9411 | for (i = 0; i < 256; i++) { |
| 9412 | if (unicode_latin1[i]) { |
| 9413 | Py_DECREF(unicode_latin1[i]); |
| 9414 | unicode_latin1[i] = NULL; |
| 9415 | } |
| 9416 | } |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9417 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9418 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9419 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9420 | void |
| 9421 | PyUnicode_InternInPlace(PyObject **p) |
| 9422 | { |
| 9423 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 9424 | PyObject *t; |
| 9425 | if (s == NULL || !PyUnicode_Check(s)) |
| 9426 | Py_FatalError( |
| 9427 | "PyUnicode_InternInPlace: unicode strings only please!"); |
| 9428 | /* If it's a subclass, we don't really know what putting |
| 9429 | it in the interned dict might do. */ |
| 9430 | if (!PyUnicode_CheckExact(s)) |
| 9431 | return; |
| 9432 | if (PyUnicode_CHECK_INTERNED(s)) |
| 9433 | return; |
| 9434 | if (interned == NULL) { |
| 9435 | interned = PyDict_New(); |
| 9436 | if (interned == NULL) { |
| 9437 | PyErr_Clear(); /* Don't leave an exception */ |
| 9438 | return; |
| 9439 | } |
| 9440 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9441 | /* It might be that the GetItem call fails even |
| 9442 | though the key is present in the dictionary, |
| 9443 | namely when this happens during a stack overflow. */ |
| 9444 | Py_ALLOW_RECURSION |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9445 | t = PyDict_GetItem(interned, (PyObject *)s); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9446 | Py_END_ALLOW_RECURSION |
| 9447 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9448 | if (t) { |
| 9449 | Py_INCREF(t); |
| 9450 | Py_DECREF(*p); |
| 9451 | *p = t; |
| 9452 | return; |
| 9453 | } |
| 9454 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9455 | PyThreadState_GET()->recursion_critical = 1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9456 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 9457 | PyErr_Clear(); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9458 | PyThreadState_GET()->recursion_critical = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9459 | return; |
| 9460 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9461 | PyThreadState_GET()->recursion_critical = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9462 | /* The two references in interned are not counted by refcnt. |
| 9463 | The deallocator will take care of this */ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 9464 | Py_REFCNT(s) -= 2; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9465 | PyUnicode_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL; |
| 9466 | } |
| 9467 | |
| 9468 | void |
| 9469 | PyUnicode_InternImmortal(PyObject **p) |
| 9470 | { |
| 9471 | PyUnicode_InternInPlace(p); |
| 9472 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
| 9473 | PyUnicode_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL; |
| 9474 | Py_INCREF(*p); |
| 9475 | } |
| 9476 | } |
| 9477 | |
| 9478 | PyObject * |
| 9479 | PyUnicode_InternFromString(const char *cp) |
| 9480 | { |
| 9481 | PyObject *s = PyUnicode_FromString(cp); |
| 9482 | if (s == NULL) |
| 9483 | return NULL; |
| 9484 | PyUnicode_InternInPlace(&s); |
| 9485 | return s; |
| 9486 | } |
| 9487 | |
| 9488 | void _Py_ReleaseInternedUnicodeStrings(void) |
| 9489 | { |
| 9490 | PyObject *keys; |
| 9491 | PyUnicodeObject *s; |
| 9492 | Py_ssize_t i, n; |
| 9493 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
| 9494 | |
| 9495 | if (interned == NULL || !PyDict_Check(interned)) |
| 9496 | return; |
| 9497 | keys = PyDict_Keys(interned); |
| 9498 | if (keys == NULL || !PyList_Check(keys)) { |
| 9499 | PyErr_Clear(); |
| 9500 | return; |
| 9501 | } |
| 9502 | |
| 9503 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 9504 | detector, interned unicode strings are not forcibly deallocated; |
| 9505 | rather, we give them their stolen references back, and then clear |
| 9506 | and DECREF the interned dict. */ |
| 9507 | |
| 9508 | n = PyList_GET_SIZE(keys); |
| 9509 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
| 9510 | n); |
| 9511 | for (i = 0; i < n; i++) { |
| 9512 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
| 9513 | switch (s->state) { |
| 9514 | case SSTATE_NOT_INTERNED: |
| 9515 | /* XXX Shouldn't happen */ |
| 9516 | break; |
| 9517 | case SSTATE_INTERNED_IMMORTAL: |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 9518 | Py_REFCNT(s) += 1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9519 | immortal_size += s->length; |
| 9520 | break; |
| 9521 | case SSTATE_INTERNED_MORTAL: |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 9522 | Py_REFCNT(s) += 2; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9523 | mortal_size += s->length; |
| 9524 | break; |
| 9525 | default: |
| 9526 | Py_FatalError("Inconsistent interned string state."); |
| 9527 | } |
| 9528 | s->state = SSTATE_NOT_INTERNED; |
| 9529 | } |
| 9530 | fprintf(stderr, "total size of all interned strings: " |
| 9531 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 9532 | "mortal/immortal\n", mortal_size, immortal_size); |
| 9533 | Py_DECREF(keys); |
| 9534 | PyDict_Clear(interned); |
| 9535 | Py_DECREF(interned); |
| 9536 | interned = NULL; |
| 9537 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9538 | |
| 9539 | |
| 9540 | /********************* Unicode Iterator **************************/ |
| 9541 | |
| 9542 | typedef struct { |
| 9543 | PyObject_HEAD |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9544 | Py_ssize_t it_index; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9545 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
| 9546 | } unicodeiterobject; |
| 9547 | |
| 9548 | static void |
| 9549 | unicodeiter_dealloc(unicodeiterobject *it) |
| 9550 | { |
| 9551 | _PyObject_GC_UNTRACK(it); |
| 9552 | Py_XDECREF(it->it_seq); |
| 9553 | PyObject_GC_Del(it); |
| 9554 | } |
| 9555 | |
| 9556 | static int |
| 9557 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 9558 | { |
| 9559 | Py_VISIT(it->it_seq); |
| 9560 | return 0; |
| 9561 | } |
| 9562 | |
| 9563 | static PyObject * |
| 9564 | unicodeiter_next(unicodeiterobject *it) |
| 9565 | { |
| 9566 | PyUnicodeObject *seq; |
| 9567 | PyObject *item; |
| 9568 | |
| 9569 | assert(it != NULL); |
| 9570 | seq = it->it_seq; |
| 9571 | if (seq == NULL) |
| 9572 | return NULL; |
| 9573 | assert(PyUnicode_Check(seq)); |
| 9574 | |
| 9575 | if (it->it_index < PyUnicode_GET_SIZE(seq)) { |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9576 | item = PyUnicode_FromUnicode( |
| 9577 | PyUnicode_AS_UNICODE(seq)+it->it_index, 1); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9578 | if (item != NULL) |
| 9579 | ++it->it_index; |
| 9580 | return item; |
| 9581 | } |
| 9582 | |
| 9583 | Py_DECREF(seq); |
| 9584 | it->it_seq = NULL; |
| 9585 | return NULL; |
| 9586 | } |
| 9587 | |
| 9588 | static PyObject * |
| 9589 | unicodeiter_len(unicodeiterobject *it) |
| 9590 | { |
| 9591 | Py_ssize_t len = 0; |
| 9592 | if (it->it_seq) |
| 9593 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 9594 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9595 | } |
| 9596 | |
| 9597 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 9598 | |
| 9599 | static PyMethodDef unicodeiter_methods[] = { |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9600 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
| 9601 | length_hint_doc}, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9602 | {NULL, NULL} /* sentinel */ |
| 9603 | }; |
| 9604 | |
| 9605 | PyTypeObject PyUnicodeIter_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 9606 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Christian Heimes | f83be4e | 2007-11-28 09:44:38 +0000 | [diff] [blame] | 9607 | "str_iterator", /* tp_name */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9608 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 9609 | 0, /* tp_itemsize */ |
| 9610 | /* methods */ |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 9611 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9612 | 0, /* tp_print */ |
| 9613 | 0, /* tp_getattr */ |
| 9614 | 0, /* tp_setattr */ |
| 9615 | 0, /* tp_compare */ |
| 9616 | 0, /* tp_repr */ |
| 9617 | 0, /* tp_as_number */ |
| 9618 | 0, /* tp_as_sequence */ |
| 9619 | 0, /* tp_as_mapping */ |
| 9620 | 0, /* tp_hash */ |
| 9621 | 0, /* tp_call */ |
| 9622 | 0, /* tp_str */ |
| 9623 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9624 | 0, /* tp_setattro */ |
| 9625 | 0, /* tp_as_buffer */ |
| 9626 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 9627 | 0, /* tp_doc */ |
| 9628 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 9629 | 0, /* tp_clear */ |
| 9630 | 0, /* tp_richcompare */ |
| 9631 | 0, /* tp_weaklistoffset */ |
| 9632 | PyObject_SelfIter, /* tp_iter */ |
| 9633 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 9634 | unicodeiter_methods, /* tp_methods */ |
| 9635 | 0, |
| 9636 | }; |
| 9637 | |
| 9638 | static PyObject * |
| 9639 | unicode_iter(PyObject *seq) |
| 9640 | { |
| 9641 | unicodeiterobject *it; |
| 9642 | |
| 9643 | if (!PyUnicode_Check(seq)) { |
| 9644 | PyErr_BadInternalCall(); |
| 9645 | return NULL; |
| 9646 | } |
| 9647 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 9648 | if (it == NULL) |
| 9649 | return NULL; |
| 9650 | it->it_index = 0; |
| 9651 | Py_INCREF(seq); |
| 9652 | it->it_seq = (PyUnicodeObject *)seq; |
| 9653 | _PyObject_GC_TRACK(it); |
| 9654 | return (PyObject *)it; |
| 9655 | } |
| 9656 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9657 | size_t |
| 9658 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 9659 | { |
| 9660 | int res = 0; |
| 9661 | while(*u++) |
| 9662 | res++; |
| 9663 | return res; |
| 9664 | } |
| 9665 | |
| 9666 | Py_UNICODE* |
| 9667 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9668 | { |
| 9669 | Py_UNICODE *u = s1; |
| 9670 | while ((*u++ = *s2++)); |
| 9671 | return s1; |
| 9672 | } |
| 9673 | |
| 9674 | Py_UNICODE* |
| 9675 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 9676 | { |
| 9677 | Py_UNICODE *u = s1; |
| 9678 | while ((*u++ = *s2++)) |
| 9679 | if (n-- == 0) |
| 9680 | break; |
| 9681 | return s1; |
| 9682 | } |
| 9683 | |
| 9684 | int |
| 9685 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9686 | { |
| 9687 | while (*s1 && *s2 && *s1 == *s2) |
| 9688 | s1++, s2++; |
| 9689 | if (*s1 && *s2) |
| 9690 | return (*s1 < *s2) ? -1 : +1; |
| 9691 | if (*s1) |
| 9692 | return 1; |
| 9693 | if (*s2) |
| 9694 | return -1; |
| 9695 | return 0; |
| 9696 | } |
| 9697 | |
| 9698 | Py_UNICODE* |
| 9699 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 9700 | { |
| 9701 | const Py_UNICODE *p; |
| 9702 | for (p = s; *p; p++) |
| 9703 | if (*p == c) |
| 9704 | return (Py_UNICODE*)p; |
| 9705 | return NULL; |
| 9706 | } |
| 9707 | |
| 9708 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9709 | #ifdef __cplusplus |
| 9710 | } |
| 9711 | #endif |
| 9712 | |
| 9713 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9714 | /* |
| 9715 | Local variables: |
| 9716 | c-basic-offset: 4 |
| 9717 | indent-tabs-mode: nil |
| 9718 | End: |
| 9719 | */ |