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 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +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[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 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 */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +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 */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +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 */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +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, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 146 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 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 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 157 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
| 158 | PyObject **errorHandler,const char *encoding, const char *reason, |
| 159 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 160 | Py_ssize_t startpos, Py_ssize_t endpos, Py_ssize_t *newpos); |
| 161 | |
Victor Stinner | 158701d | 2010-04-22 19:41:01 +0000 | [diff] [blame] | 162 | static void raise_encode_exception(PyObject **exceptionObject, |
| 163 | const char *encoding, |
| 164 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 165 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 166 | const char *reason); |
| 167 | |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 168 | /* Same for linebreaks */ |
| 169 | static unsigned char ascii_linebreak[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 170 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 171 | /* 0x000A, * LINE FEED */ |
| 172 | /* 0x000D, * CARRIAGE RETURN */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 173 | 0, 0, 1, 0, 0, 1, 0, 0, |
| 174 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 175 | /* 0x001C, * FILE SEPARATOR */ |
| 176 | /* 0x001D, * GROUP SEPARATOR */ |
| 177 | /* 0x001E, * RECORD SEPARATOR */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 178 | 0, 0, 0, 0, 1, 1, 1, 0, |
| 179 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 180 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 181 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 182 | 0, 0, 0, 0, 0, 0, 0, 0, |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 183 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 184 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 185 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 186 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 187 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 188 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 189 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 190 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 191 | 0, 0, 0, 0, 0, 0, 0, 0 |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 192 | }; |
| 193 | |
| 194 | |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 195 | Py_UNICODE |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 196 | PyUnicode_GetMax(void) |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 197 | { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 198 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 199 | return 0x10FFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 200 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 201 | /* This is actually an illegal character, so it should |
| 202 | not be passed to unichr. */ |
| 203 | return 0xFFFF; |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 204 | #endif |
| 205 | } |
| 206 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 207 | /* --- Bloom Filters ----------------------------------------------------- */ |
| 208 | |
| 209 | /* stuff to implement simple "bloom filters" for Unicode characters. |
| 210 | to keep things simple, we use a single bitmask, using the least 5 |
| 211 | bits from each unicode characters as the bit index. */ |
| 212 | |
| 213 | /* the linebreak mask is set up by Unicode_Init below */ |
| 214 | |
| 215 | #define BLOOM_MASK unsigned long |
| 216 | |
| 217 | static BLOOM_MASK bloom_linebreak; |
| 218 | |
| 219 | #define BLOOM(mask, ch) ((mask & (1 << ((ch) & 0x1F)))) |
| 220 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 221 | #define BLOOM_LINEBREAK(ch) \ |
| 222 | ((ch) < 128U ? ascii_linebreak[(ch)] : \ |
| 223 | (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 224 | |
| 225 | Py_LOCAL_INLINE(BLOOM_MASK) make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len) |
| 226 | { |
| 227 | /* calculate simple bloom-style bitmask for a given unicode string */ |
| 228 | |
| 229 | long mask; |
| 230 | Py_ssize_t i; |
| 231 | |
| 232 | mask = 0; |
| 233 | for (i = 0; i < len; i++) |
| 234 | mask |= (1 << (ptr[i] & 0x1F)); |
| 235 | |
| 236 | return mask; |
| 237 | } |
| 238 | |
| 239 | Py_LOCAL_INLINE(int) unicode_member(Py_UNICODE chr, Py_UNICODE* set, Py_ssize_t setlen) |
| 240 | { |
| 241 | Py_ssize_t i; |
| 242 | |
| 243 | for (i = 0; i < setlen; i++) |
| 244 | if (set[i] == chr) |
| 245 | return 1; |
| 246 | |
| 247 | return 0; |
| 248 | } |
| 249 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 250 | #define BLOOM_MEMBER(mask, chr, set, setlen) \ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 251 | BLOOM(mask, chr) && unicode_member(chr, set, setlen) |
| 252 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 253 | /* --- Unicode Object ----------------------------------------------------- */ |
| 254 | |
| 255 | static |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 256 | int unicode_resize(register PyUnicodeObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 257 | Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 258 | { |
| 259 | void *oldstr; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 260 | |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 261 | /* Shortcut if there's nothing much to do. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 262 | if (unicode->length == length) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 263 | goto reset; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 264 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 265 | /* Resizing shared object (unicode_empty or single character |
| 266 | objects) in-place is not allowed. Use PyUnicode_Resize() |
| 267 | instead ! */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 268 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 269 | if (unicode == unicode_empty || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 270 | (unicode->length == 1 && |
| 271 | unicode->str[0] < 256U && |
| 272 | unicode_latin1[unicode->str[0]] == unicode)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 273 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 274 | "can't resize shared str objects"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 275 | return -1; |
| 276 | } |
| 277 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 278 | /* We allocate one more byte to make sure the string is Ux0000 terminated. |
| 279 | The overallocation is also used by fastsearch, which assumes that it's |
| 280 | safe to look at str[length] (without making any assumptions about what |
| 281 | it contains). */ |
| 282 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 283 | oldstr = unicode->str; |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 284 | unicode->str = PyObject_REALLOC(unicode->str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 285 | sizeof(Py_UNICODE) * (length + 1)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 286 | if (!unicode->str) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 287 | unicode->str = (Py_UNICODE *)oldstr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 288 | PyErr_NoMemory(); |
| 289 | return -1; |
| 290 | } |
| 291 | unicode->str[length] = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 292 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 293 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 294 | reset: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 295 | /* Reset the object caches */ |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 296 | if (unicode->defenc) { |
Georg Brandl | 1fa11af | 2010-08-01 21:03:01 +0000 | [diff] [blame] | 297 | Py_CLEAR(unicode->defenc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 298 | } |
| 299 | unicode->hash = -1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 300 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | /* 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] | 305 | Ux0000 terminated; some code (e.g. new_identifier) |
| 306 | relies on that. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 307 | |
| 308 | XXX This allocator could further be enhanced by assuring that the |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 309 | free list never reduces its size below 1. |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 310 | |
| 311 | */ |
| 312 | |
| 313 | static |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 314 | PyUnicodeObject *_PyUnicode_New(Py_ssize_t length) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 315 | { |
| 316 | register PyUnicodeObject *unicode; |
| 317 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 318 | /* Optimization for empty strings */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 319 | if (length == 0 && unicode_empty != NULL) { |
| 320 | Py_INCREF(unicode_empty); |
| 321 | return unicode_empty; |
| 322 | } |
| 323 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 324 | /* Ensure we won't overflow the size. */ |
| 325 | if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { |
| 326 | return (PyUnicodeObject *)PyErr_NoMemory(); |
| 327 | } |
| 328 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 329 | /* Unicode freelist & memory allocation */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 330 | if (free_list) { |
| 331 | unicode = free_list; |
| 332 | free_list = *(PyUnicodeObject **)unicode; |
| 333 | numfree--; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 334 | if (unicode->str) { |
| 335 | /* Keep-Alive optimization: we only upsize the buffer, |
| 336 | never downsize it. */ |
| 337 | if ((unicode->length < length) && |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 338 | unicode_resize(unicode, length) < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 339 | PyObject_DEL(unicode->str); |
| 340 | unicode->str = NULL; |
| 341 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 342 | } |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 343 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 344 | size_t new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 345 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | ad98db1 | 2001-06-14 17:52:02 +0000 | [diff] [blame] | 346 | } |
| 347 | PyObject_INIT(unicode, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 348 | } |
| 349 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 350 | size_t new_size; |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 351 | unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 352 | if (unicode == NULL) |
| 353 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 354 | new_size = sizeof(Py_UNICODE) * ((size_t)length + 1); |
| 355 | unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 358 | if (!unicode->str) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 359 | PyErr_NoMemory(); |
| 360 | goto onError; |
Guido van Rossum | 3c1bb80 | 2000-04-27 20:13:50 +0000 | [diff] [blame] | 361 | } |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 362 | /* Initialize the first element to guard against cases where |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 363 | * the caller fails before initializing str -- unicode_resize() |
| 364 | * reads str[0], and the Keep-Alive optimization can keep memory |
| 365 | * allocated for str alive across a call to unicode_dealloc(unicode). |
| 366 | * We don't want unicode_resize to read uninitialized memory in |
| 367 | * that case. |
| 368 | */ |
Jeremy Hylton | d808279 | 2003-09-16 19:41:39 +0000 | [diff] [blame] | 369 | unicode->str[0] = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 370 | unicode->str[length] = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 371 | unicode->length = length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 372 | unicode->hash = -1; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 373 | unicode->state = 0; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 374 | unicode->defenc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 375 | return unicode; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 376 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 377 | onError: |
Amaury Forgeot d'Arc | 7888d08 | 2008-08-01 01:06:32 +0000 | [diff] [blame] | 378 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 379 | _Py_DEC_REFTOTAL; |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 380 | _Py_ForgetReference((PyObject *)unicode); |
Neil Schemenauer | 58aa861 | 2002-04-12 03:07:20 +0000 | [diff] [blame] | 381 | PyObject_Del(unicode); |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 382 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | static |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 386 | void unicode_dealloc(register PyUnicodeObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 387 | { |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 388 | switch (PyUnicode_CHECK_INTERNED(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 389 | case SSTATE_NOT_INTERNED: |
| 390 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 391 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 392 | case SSTATE_INTERNED_MORTAL: |
| 393 | /* revive dead object temporarily for DelItem */ |
| 394 | Py_REFCNT(unicode) = 3; |
| 395 | if (PyDict_DelItem(interned, (PyObject *)unicode) != 0) |
| 396 | Py_FatalError( |
| 397 | "deletion of interned string failed"); |
| 398 | break; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 399 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 400 | case SSTATE_INTERNED_IMMORTAL: |
| 401 | Py_FatalError("Immortal interned string died."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 402 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 403 | default: |
| 404 | Py_FatalError("Inconsistent interned string state."); |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Guido van Rossum | 604ddf8 | 2001-12-06 20:03:56 +0000 | [diff] [blame] | 407 | if (PyUnicode_CheckExact(unicode) && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 408 | numfree < PyUnicode_MAXFREELIST) { |
Guido van Rossum | fd4b957 | 2000-04-10 13:51:10 +0000 | [diff] [blame] | 409 | /* Keep-Alive optimization */ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 410 | if (unicode->length >= KEEPALIVE_SIZE_LIMIT) { |
| 411 | PyObject_DEL(unicode->str); |
| 412 | unicode->str = NULL; |
| 413 | unicode->length = 0; |
| 414 | } |
| 415 | if (unicode->defenc) { |
Georg Brandl | 1fa11af | 2010-08-01 21:03:01 +0000 | [diff] [blame] | 416 | Py_CLEAR(unicode->defenc); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 417 | } |
| 418 | /* Add to free list */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 419 | *(PyUnicodeObject **)unicode = free_list; |
| 420 | free_list = unicode; |
| 421 | numfree++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 422 | } |
| 423 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 424 | PyObject_DEL(unicode->str); |
| 425 | Py_XDECREF(unicode->defenc); |
| 426 | Py_TYPE(unicode)->tp_free((PyObject *)unicode); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 430 | static |
| 431 | int _PyUnicode_Resize(PyUnicodeObject **unicode, Py_ssize_t length) |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 432 | { |
| 433 | register PyUnicodeObject *v; |
| 434 | |
| 435 | /* Argument checks */ |
| 436 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 437 | PyErr_BadInternalCall(); |
| 438 | return -1; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 439 | } |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 440 | v = *unicode; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 441 | if (v == NULL || !PyUnicode_Check(v) || Py_REFCNT(v) != 1 || length < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 442 | PyErr_BadInternalCall(); |
| 443 | return -1; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | /* Resizing unicode_empty and single character objects is not |
| 447 | possible since these are being shared. We simply return a fresh |
| 448 | copy with the same Unicode content. */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 449 | if (v->length != length && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 450 | (v == unicode_empty || v->length == 1)) { |
| 451 | PyUnicodeObject *w = _PyUnicode_New(length); |
| 452 | if (w == NULL) |
| 453 | return -1; |
| 454 | Py_UNICODE_COPY(w->str, v->str, |
| 455 | length < v->length ? length : v->length); |
| 456 | Py_DECREF(*unicode); |
| 457 | *unicode = w; |
| 458 | return 0; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | /* Note that we don't have to modify *unicode for unshared Unicode |
| 462 | objects, since we can modify them in-place. */ |
| 463 | return unicode_resize(v, length); |
| 464 | } |
| 465 | |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 466 | int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) |
| 467 | { |
| 468 | return _PyUnicode_Resize((PyUnicodeObject **)unicode, length); |
| 469 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 470 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 471 | PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 472 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 473 | { |
| 474 | PyUnicodeObject *unicode; |
| 475 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 476 | /* If the Unicode data is known at construction time, we can apply |
| 477 | some optimizations which share commonly used objects. */ |
| 478 | if (u != NULL) { |
| 479 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 480 | /* Optimization for empty strings */ |
| 481 | if (size == 0 && unicode_empty != NULL) { |
| 482 | Py_INCREF(unicode_empty); |
| 483 | return (PyObject *)unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 484 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 485 | |
| 486 | /* Single character Unicode objects in the Latin-1 range are |
| 487 | shared when using this constructor */ |
| 488 | if (size == 1 && *u < 256) { |
| 489 | unicode = unicode_latin1[*u]; |
| 490 | if (!unicode) { |
| 491 | unicode = _PyUnicode_New(1); |
| 492 | if (!unicode) |
| 493 | return NULL; |
| 494 | unicode->str[0] = *u; |
| 495 | unicode_latin1[*u] = unicode; |
| 496 | } |
| 497 | Py_INCREF(unicode); |
| 498 | return (PyObject *)unicode; |
| 499 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 500 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 501 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 502 | unicode = _PyUnicode_New(size); |
| 503 | if (!unicode) |
| 504 | return NULL; |
| 505 | |
| 506 | /* Copy the Unicode data into the new object */ |
| 507 | if (u != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 508 | Py_UNICODE_COPY(unicode->str, u, size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 509 | |
| 510 | return (PyObject *)unicode; |
| 511 | } |
| 512 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 513 | PyObject *PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 514 | { |
| 515 | PyUnicodeObject *unicode; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 516 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 517 | if (size < 0) { |
| 518 | PyErr_SetString(PyExc_SystemError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 519 | "Negative size passed to PyUnicode_FromStringAndSize"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 520 | return NULL; |
| 521 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 522 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 523 | /* 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] | 524 | some optimizations which share commonly used objects. |
| 525 | Also, this means the input must be UTF-8, so fall back to the |
| 526 | UTF-8 decoder at the end. */ |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 527 | if (u != NULL) { |
| 528 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 529 | /* Optimization for empty strings */ |
| 530 | if (size == 0 && unicode_empty != NULL) { |
| 531 | Py_INCREF(unicode_empty); |
| 532 | return (PyObject *)unicode_empty; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 533 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 534 | |
| 535 | /* Single characters are shared when using this constructor. |
| 536 | Restrict to ASCII, since the input must be UTF-8. */ |
| 537 | if (size == 1 && Py_CHARMASK(*u) < 128) { |
| 538 | unicode = unicode_latin1[Py_CHARMASK(*u)]; |
| 539 | if (!unicode) { |
| 540 | unicode = _PyUnicode_New(1); |
| 541 | if (!unicode) |
| 542 | return NULL; |
| 543 | unicode->str[0] = Py_CHARMASK(*u); |
| 544 | unicode_latin1[Py_CHARMASK(*u)] = unicode; |
| 545 | } |
| 546 | Py_INCREF(unicode); |
| 547 | return (PyObject *)unicode; |
| 548 | } |
Martin v. Löwis | 9c12106 | 2007-08-05 20:26:11 +0000 | [diff] [blame] | 549 | |
| 550 | return PyUnicode_DecodeUTF8(u, size, NULL); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Walter Dörwald | 5550731 | 2007-05-18 13:12:10 +0000 | [diff] [blame] | 553 | unicode = _PyUnicode_New(size); |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 554 | if (!unicode) |
| 555 | return NULL; |
| 556 | |
Walter Dörwald | acaa5a1 | 2007-05-05 12:00:46 +0000 | [diff] [blame] | 557 | return (PyObject *)unicode; |
| 558 | } |
| 559 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 560 | PyObject *PyUnicode_FromString(const char *u) |
| 561 | { |
| 562 | size_t size = strlen(u); |
| 563 | if (size > PY_SSIZE_T_MAX) { |
| 564 | PyErr_SetString(PyExc_OverflowError, "input too long"); |
| 565 | return NULL; |
| 566 | } |
| 567 | |
| 568 | return PyUnicode_FromStringAndSize(u, size); |
| 569 | } |
| 570 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 571 | #ifdef HAVE_WCHAR_H |
| 572 | |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 573 | #if (Py_UNICODE_SIZE == 2) && defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
| 574 | # define CONVERT_WCHAR_TO_SURROGATES |
| 575 | #endif |
| 576 | |
| 577 | #ifdef CONVERT_WCHAR_TO_SURROGATES |
| 578 | |
| 579 | /* Here sizeof(wchar_t) is 4 but Py_UNICODE_SIZE == 2, so we need |
| 580 | to convert from UTF32 to UTF16. */ |
| 581 | |
| 582 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
| 583 | Py_ssize_t size) |
| 584 | { |
| 585 | PyUnicodeObject *unicode; |
| 586 | register Py_ssize_t i; |
| 587 | Py_ssize_t alloc; |
| 588 | const wchar_t *orig_w; |
| 589 | |
| 590 | if (w == NULL) { |
| 591 | if (size == 0) |
| 592 | return PyUnicode_FromStringAndSize(NULL, 0); |
| 593 | PyErr_BadInternalCall(); |
| 594 | return NULL; |
| 595 | } |
| 596 | |
| 597 | if (size == -1) { |
| 598 | size = wcslen(w); |
| 599 | } |
| 600 | |
| 601 | alloc = size; |
| 602 | orig_w = w; |
| 603 | for (i = size; i > 0; i--) { |
| 604 | if (*w > 0xFFFF) |
| 605 | alloc++; |
| 606 | w++; |
| 607 | } |
| 608 | w = orig_w; |
| 609 | unicode = _PyUnicode_New(alloc); |
| 610 | if (!unicode) |
| 611 | return NULL; |
| 612 | |
| 613 | /* Copy the wchar_t data into the new object */ |
| 614 | { |
| 615 | register Py_UNICODE *u; |
| 616 | u = PyUnicode_AS_UNICODE(unicode); |
| 617 | for (i = size; i > 0; i--) { |
| 618 | if (*w > 0xFFFF) { |
| 619 | wchar_t ordinal = *w++; |
| 620 | ordinal -= 0x10000; |
| 621 | *u++ = 0xD800 | (ordinal >> 10); |
| 622 | *u++ = 0xDC00 | (ordinal & 0x3FF); |
| 623 | } |
| 624 | else |
| 625 | *u++ = *w++; |
| 626 | } |
| 627 | } |
| 628 | return (PyObject *)unicode; |
| 629 | } |
| 630 | |
| 631 | #else |
| 632 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 633 | PyObject *PyUnicode_FromWideChar(register const wchar_t *w, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 634 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 635 | { |
| 636 | PyUnicodeObject *unicode; |
| 637 | |
| 638 | if (w == NULL) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 639 | if (size == 0) |
| 640 | return PyUnicode_FromStringAndSize(NULL, 0); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 641 | PyErr_BadInternalCall(); |
| 642 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 645 | if (size == -1) { |
| 646 | size = wcslen(w); |
| 647 | } |
| 648 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 649 | unicode = _PyUnicode_New(size); |
| 650 | if (!unicode) |
| 651 | return NULL; |
| 652 | |
| 653 | /* Copy the wchar_t data into the new object */ |
| 654 | #ifdef HAVE_USABLE_WCHAR_T |
| 655 | memcpy(unicode->str, w, size * sizeof(wchar_t)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 656 | #else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 657 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 658 | register Py_UNICODE *u; |
| 659 | register Py_ssize_t i; |
| 660 | u = PyUnicode_AS_UNICODE(unicode); |
| 661 | for (i = size; i > 0; i--) |
| 662 | *u++ = *w++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 663 | } |
| 664 | #endif |
| 665 | |
| 666 | return (PyObject *)unicode; |
| 667 | } |
| 668 | |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 669 | #endif /* CONVERT_WCHAR_TO_SURROGATES */ |
| 670 | |
| 671 | #undef CONVERT_WCHAR_TO_SURROGATES |
| 672 | |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 673 | static void |
| 674 | makefmt(char *fmt, int longflag, int size_tflag, int zeropad, int width, int precision, char c) |
| 675 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 676 | *fmt++ = '%'; |
| 677 | if (width) { |
| 678 | if (zeropad) |
| 679 | *fmt++ = '0'; |
| 680 | fmt += sprintf(fmt, "%d", width); |
| 681 | } |
| 682 | if (precision) |
| 683 | fmt += sprintf(fmt, ".%d", precision); |
| 684 | if (longflag) |
| 685 | *fmt++ = 'l'; |
| 686 | else if (size_tflag) { |
| 687 | char *f = PY_FORMAT_SIZE_T; |
| 688 | while (*f) |
| 689 | *fmt++ = *f++; |
| 690 | } |
| 691 | *fmt++ = c; |
| 692 | *fmt = '\0'; |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 693 | } |
| 694 | |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 695 | #define appendstring(string) {for (copy = string;*copy;) *s++ = *copy++;} |
| 696 | |
| 697 | PyObject * |
| 698 | PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 699 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 700 | va_list count; |
| 701 | Py_ssize_t callcount = 0; |
| 702 | PyObject **callresults = NULL; |
| 703 | PyObject **callresult = NULL; |
| 704 | Py_ssize_t n = 0; |
| 705 | int width = 0; |
| 706 | int precision = 0; |
| 707 | int zeropad; |
| 708 | const char* f; |
| 709 | Py_UNICODE *s; |
| 710 | PyObject *string; |
| 711 | /* used by sprintf */ |
| 712 | char buffer[21]; |
| 713 | /* use abuffer instead of buffer, if we need more space |
| 714 | * (which can happen if there's a format specifier with width). */ |
| 715 | char *abuffer = NULL; |
| 716 | char *realbuffer; |
| 717 | Py_ssize_t abuffersize = 0; |
| 718 | char fmt[60]; /* should be enough for %0width.precisionld */ |
| 719 | const char *copy; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 720 | |
| 721 | #ifdef VA_LIST_IS_ARRAY |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 722 | Py_MEMCPY(count, vargs, sizeof(va_list)); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 723 | #else |
| 724 | #ifdef __va_copy |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 725 | __va_copy(count, vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 726 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 727 | count = vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 728 | #endif |
| 729 | #endif |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 730 | /* step 1: count the number of %S/%R/%A/%s format specifications |
| 731 | * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/ |
| 732 | * PyUnicode_DecodeUTF8() for these objects once during step 3 and put the |
| 733 | * result in an array) */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 734 | for (f = format; *f; f++) { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 735 | if (*f == '%') { |
| 736 | if (*(f+1)=='%') |
| 737 | continue; |
| 738 | if (*(f+1)=='S' || *(f+1)=='R' || *(f+1)=='A') |
| 739 | ++callcount; |
| 740 | while (ISDIGIT((unsigned)*f)) |
| 741 | width = (width*10) + *f++ - '0'; |
| 742 | while (*++f && *f != '%' && !ISALPHA((unsigned)*f)) |
| 743 | ; |
| 744 | if (*f == 's') |
| 745 | ++callcount; |
| 746 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 747 | } |
| 748 | /* step 2: allocate memory for the results of |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 749 | * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 750 | if (callcount) { |
| 751 | callresults = PyObject_Malloc(sizeof(PyObject *)*callcount); |
| 752 | if (!callresults) { |
| 753 | PyErr_NoMemory(); |
| 754 | return NULL; |
| 755 | } |
| 756 | callresult = callresults; |
| 757 | } |
| 758 | /* step 3: figure out how large a buffer we need */ |
| 759 | for (f = format; *f; f++) { |
| 760 | if (*f == '%') { |
| 761 | const char* p = f; |
| 762 | width = 0; |
| 763 | while (ISDIGIT((unsigned)*f)) |
| 764 | width = (width*10) + *f++ - '0'; |
| 765 | while (*++f && *f != '%' && !ISALPHA((unsigned)*f)) |
| 766 | ; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 767 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 768 | /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
| 769 | * they don't affect the amount of space we reserve. |
| 770 | */ |
| 771 | if ((*f == 'l' || *f == 'z') && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 772 | (f[1] == 'd' || f[1] == 'u')) |
| 773 | ++f; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 774 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 775 | switch (*f) { |
| 776 | case 'c': |
| 777 | (void)va_arg(count, int); |
| 778 | /* fall through... */ |
| 779 | case '%': |
| 780 | n++; |
| 781 | break; |
| 782 | case 'd': case 'u': case 'i': case 'x': |
| 783 | (void) va_arg(count, int); |
| 784 | /* 20 bytes is enough to hold a 64-bit |
| 785 | integer. Decimal takes the most space. |
| 786 | This isn't enough for octal. |
| 787 | If a width is specified we need more |
| 788 | (which we allocate later). */ |
| 789 | if (width < 20) |
| 790 | width = 20; |
| 791 | n += width; |
| 792 | if (abuffersize < width) |
| 793 | abuffersize = width; |
| 794 | break; |
| 795 | case 's': |
| 796 | { |
| 797 | /* UTF-8 */ |
Georg Brandl | 780b2a6 | 2009-05-05 09:19:59 +0000 | [diff] [blame] | 798 | const char *s = va_arg(count, const char*); |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 799 | PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace"); |
| 800 | if (!str) |
| 801 | goto fail; |
| 802 | n += PyUnicode_GET_SIZE(str); |
| 803 | /* Remember the str and switch to the next slot */ |
| 804 | *callresult++ = str; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 805 | break; |
| 806 | } |
| 807 | case 'U': |
| 808 | { |
| 809 | PyObject *obj = va_arg(count, PyObject *); |
| 810 | assert(obj && PyUnicode_Check(obj)); |
| 811 | n += PyUnicode_GET_SIZE(obj); |
| 812 | break; |
| 813 | } |
| 814 | case 'V': |
| 815 | { |
| 816 | PyObject *obj = va_arg(count, PyObject *); |
| 817 | const char *str = va_arg(count, const char *); |
| 818 | assert(obj || str); |
| 819 | assert(!obj || PyUnicode_Check(obj)); |
| 820 | if (obj) |
| 821 | n += PyUnicode_GET_SIZE(obj); |
| 822 | else |
| 823 | n += strlen(str); |
| 824 | break; |
| 825 | } |
| 826 | case 'S': |
| 827 | { |
| 828 | PyObject *obj = va_arg(count, PyObject *); |
| 829 | PyObject *str; |
| 830 | assert(obj); |
| 831 | str = PyObject_Str(obj); |
| 832 | if (!str) |
| 833 | goto fail; |
| 834 | n += PyUnicode_GET_SIZE(str); |
| 835 | /* Remember the str and switch to the next slot */ |
| 836 | *callresult++ = str; |
| 837 | break; |
| 838 | } |
| 839 | case 'R': |
| 840 | { |
| 841 | PyObject *obj = va_arg(count, PyObject *); |
| 842 | PyObject *repr; |
| 843 | assert(obj); |
| 844 | repr = PyObject_Repr(obj); |
| 845 | if (!repr) |
| 846 | goto fail; |
| 847 | n += PyUnicode_GET_SIZE(repr); |
| 848 | /* Remember the repr and switch to the next slot */ |
| 849 | *callresult++ = repr; |
| 850 | break; |
| 851 | } |
| 852 | case 'A': |
| 853 | { |
| 854 | PyObject *obj = va_arg(count, PyObject *); |
| 855 | PyObject *ascii; |
| 856 | assert(obj); |
| 857 | ascii = PyObject_ASCII(obj); |
| 858 | if (!ascii) |
| 859 | goto fail; |
| 860 | n += PyUnicode_GET_SIZE(ascii); |
| 861 | /* Remember the repr and switch to the next slot */ |
| 862 | *callresult++ = ascii; |
| 863 | break; |
| 864 | } |
| 865 | case 'p': |
| 866 | (void) va_arg(count, int); |
| 867 | /* maximum 64-bit pointer representation: |
| 868 | * 0xffffffffffffffff |
| 869 | * so 19 characters is enough. |
| 870 | * XXX I count 18 -- what's the extra for? |
| 871 | */ |
| 872 | n += 19; |
| 873 | break; |
| 874 | default: |
| 875 | /* if we stumble upon an unknown |
| 876 | formatting code, copy the rest of |
| 877 | the format string to the output |
| 878 | string. (we cannot just skip the |
| 879 | code, since there's no way to know |
| 880 | what's in the argument list) */ |
| 881 | n += strlen(p); |
| 882 | goto expand; |
| 883 | } |
| 884 | } else |
| 885 | n++; |
| 886 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 887 | expand: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 888 | if (abuffersize > 20) { |
| 889 | abuffer = PyObject_Malloc(abuffersize); |
| 890 | if (!abuffer) { |
| 891 | PyErr_NoMemory(); |
| 892 | goto fail; |
| 893 | } |
| 894 | realbuffer = abuffer; |
| 895 | } |
| 896 | else |
| 897 | realbuffer = buffer; |
| 898 | /* step 4: fill the buffer */ |
| 899 | /* Since we've analyzed how much space we need for the worst case, |
| 900 | we don't have to resize the string. |
| 901 | There can be no errors beyond this point. */ |
| 902 | string = PyUnicode_FromUnicode(NULL, n); |
| 903 | if (!string) |
| 904 | goto fail; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 905 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 906 | s = PyUnicode_AS_UNICODE(string); |
| 907 | callresult = callresults; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 908 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 909 | for (f = format; *f; f++) { |
| 910 | if (*f == '%') { |
| 911 | const char* p = f++; |
| 912 | int longflag = 0; |
| 913 | int size_tflag = 0; |
| 914 | zeropad = (*f == '0'); |
| 915 | /* parse the width.precision part */ |
| 916 | width = 0; |
| 917 | while (ISDIGIT((unsigned)*f)) |
| 918 | width = (width*10) + *f++ - '0'; |
| 919 | precision = 0; |
| 920 | if (*f == '.') { |
| 921 | f++; |
| 922 | while (ISDIGIT((unsigned)*f)) |
| 923 | precision = (precision*10) + *f++ - '0'; |
| 924 | } |
| 925 | /* handle the long flag, but only for %ld and %lu. |
| 926 | others can be added when necessary. */ |
| 927 | if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) { |
| 928 | longflag = 1; |
| 929 | ++f; |
| 930 | } |
| 931 | /* handle the size_t flag. */ |
| 932 | if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
| 933 | size_tflag = 1; |
| 934 | ++f; |
| 935 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 936 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 937 | switch (*f) { |
| 938 | case 'c': |
| 939 | *s++ = va_arg(vargs, int); |
| 940 | break; |
| 941 | case 'd': |
| 942 | makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'd'); |
| 943 | if (longflag) |
| 944 | sprintf(realbuffer, fmt, va_arg(vargs, long)); |
| 945 | else if (size_tflag) |
| 946 | sprintf(realbuffer, fmt, va_arg(vargs, Py_ssize_t)); |
| 947 | else |
| 948 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 949 | appendstring(realbuffer); |
| 950 | break; |
| 951 | case 'u': |
| 952 | makefmt(fmt, longflag, size_tflag, zeropad, width, precision, 'u'); |
| 953 | if (longflag) |
| 954 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned long)); |
| 955 | else if (size_tflag) |
| 956 | sprintf(realbuffer, fmt, va_arg(vargs, size_t)); |
| 957 | else |
| 958 | sprintf(realbuffer, fmt, va_arg(vargs, unsigned int)); |
| 959 | appendstring(realbuffer); |
| 960 | break; |
| 961 | case 'i': |
| 962 | makefmt(fmt, 0, 0, zeropad, width, precision, 'i'); |
| 963 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 964 | appendstring(realbuffer); |
| 965 | break; |
| 966 | case 'x': |
| 967 | makefmt(fmt, 0, 0, zeropad, width, precision, 'x'); |
| 968 | sprintf(realbuffer, fmt, va_arg(vargs, int)); |
| 969 | appendstring(realbuffer); |
| 970 | break; |
| 971 | case 's': |
| 972 | { |
Walter Dörwald | c1651a0 | 2009-05-03 22:55:55 +0000 | [diff] [blame] | 973 | /* unused, since we already have the result */ |
| 974 | (void) va_arg(vargs, char *); |
| 975 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(*callresult), |
| 976 | PyUnicode_GET_SIZE(*callresult)); |
| 977 | s += PyUnicode_GET_SIZE(*callresult); |
| 978 | /* We're done with the unicode()/repr() => forget it */ |
| 979 | Py_DECREF(*callresult); |
| 980 | /* switch to next unicode()/repr() result */ |
| 981 | ++callresult; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 982 | break; |
| 983 | } |
| 984 | case 'U': |
| 985 | { |
| 986 | PyObject *obj = va_arg(vargs, PyObject *); |
| 987 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 988 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 989 | s += size; |
| 990 | break; |
| 991 | } |
| 992 | case 'V': |
| 993 | { |
| 994 | PyObject *obj = va_arg(vargs, PyObject *); |
| 995 | const char *str = va_arg(vargs, const char *); |
| 996 | if (obj) { |
| 997 | Py_ssize_t size = PyUnicode_GET_SIZE(obj); |
| 998 | Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size); |
| 999 | s += size; |
| 1000 | } else { |
| 1001 | appendstring(str); |
| 1002 | } |
| 1003 | break; |
| 1004 | } |
| 1005 | case 'S': |
| 1006 | case 'R': |
| 1007 | { |
| 1008 | Py_UNICODE *ucopy; |
| 1009 | Py_ssize_t usize; |
| 1010 | Py_ssize_t upos; |
| 1011 | /* unused, since we already have the result */ |
| 1012 | (void) va_arg(vargs, PyObject *); |
| 1013 | ucopy = PyUnicode_AS_UNICODE(*callresult); |
| 1014 | usize = PyUnicode_GET_SIZE(*callresult); |
| 1015 | for (upos = 0; upos<usize;) |
| 1016 | *s++ = ucopy[upos++]; |
| 1017 | /* We're done with the unicode()/repr() => forget it */ |
| 1018 | Py_DECREF(*callresult); |
| 1019 | /* switch to next unicode()/repr() result */ |
| 1020 | ++callresult; |
| 1021 | break; |
| 1022 | } |
| 1023 | case 'p': |
| 1024 | sprintf(buffer, "%p", va_arg(vargs, void*)); |
| 1025 | /* %p is ill-defined: ensure leading 0x. */ |
| 1026 | if (buffer[1] == 'X') |
| 1027 | buffer[1] = 'x'; |
| 1028 | else if (buffer[1] != 'x') { |
| 1029 | memmove(buffer+2, buffer, strlen(buffer)+1); |
| 1030 | buffer[0] = '0'; |
| 1031 | buffer[1] = 'x'; |
| 1032 | } |
| 1033 | appendstring(buffer); |
| 1034 | break; |
| 1035 | case '%': |
| 1036 | *s++ = '%'; |
| 1037 | break; |
| 1038 | default: |
| 1039 | appendstring(p); |
| 1040 | goto end; |
| 1041 | } |
| 1042 | } else |
| 1043 | *s++ = *f; |
| 1044 | } |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1045 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1046 | end: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1047 | if (callresults) |
| 1048 | PyObject_Free(callresults); |
| 1049 | if (abuffer) |
| 1050 | PyObject_Free(abuffer); |
| 1051 | PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string)); |
| 1052 | return string; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1053 | fail: |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1054 | if (callresults) { |
| 1055 | PyObject **callresult2 = callresults; |
| 1056 | while (callresult2 < callresult) { |
| 1057 | Py_DECREF(*callresult2); |
| 1058 | ++callresult2; |
| 1059 | } |
| 1060 | PyObject_Free(callresults); |
| 1061 | } |
| 1062 | if (abuffer) |
| 1063 | PyObject_Free(abuffer); |
| 1064 | return NULL; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | #undef appendstring |
| 1068 | |
| 1069 | PyObject * |
| 1070 | PyUnicode_FromFormat(const char *format, ...) |
| 1071 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1072 | PyObject* ret; |
| 1073 | va_list vargs; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1074 | |
| 1075 | #ifdef HAVE_STDARG_PROTOTYPES |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1076 | va_start(vargs, format); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1077 | #else |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1078 | va_start(vargs); |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1079 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1080 | ret = PyUnicode_FromFormatV(format, vargs); |
| 1081 | va_end(vargs); |
| 1082 | return ret; |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1085 | Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1086 | wchar_t *w, |
| 1087 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1088 | { |
| 1089 | if (unicode == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1090 | PyErr_BadInternalCall(); |
| 1091 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1092 | } |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1093 | |
| 1094 | /* If possible, try to copy the 0-termination as well */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1095 | if (size > PyUnicode_GET_SIZE(unicode)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1096 | size = PyUnicode_GET_SIZE(unicode) + 1; |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1097 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1098 | #ifdef HAVE_USABLE_WCHAR_T |
| 1099 | memcpy(w, unicode->str, size * sizeof(wchar_t)); |
| 1100 | #else |
| 1101 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1102 | register Py_UNICODE *u; |
| 1103 | register Py_ssize_t i; |
| 1104 | u = PyUnicode_AS_UNICODE(unicode); |
| 1105 | for (i = size; i > 0; i--) |
| 1106 | *w++ = *u++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1107 | } |
| 1108 | #endif |
| 1109 | |
Marc-André Lemburg | a9cadcd | 2004-11-22 13:02:31 +0000 | [diff] [blame] | 1110 | if (size > PyUnicode_GET_SIZE(unicode)) |
| 1111 | return PyUnicode_GET_SIZE(unicode); |
| 1112 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1113 | return size; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | #endif |
| 1117 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1118 | PyObject *PyUnicode_FromOrdinal(int ordinal) |
| 1119 | { |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1120 | Py_UNICODE s[2]; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1121 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1122 | if (ordinal < 0 || ordinal > 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1123 | PyErr_SetString(PyExc_ValueError, |
| 1124 | "chr() arg not in range(0x110000)"); |
| 1125 | return NULL; |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1126 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1127 | |
| 1128 | #ifndef Py_UNICODE_WIDE |
| 1129 | if (ordinal > 0xffff) { |
| 1130 | ordinal -= 0x10000; |
| 1131 | s[0] = 0xD800 | (ordinal >> 10); |
| 1132 | s[1] = 0xDC00 | (ordinal & 0x3FF); |
| 1133 | return PyUnicode_FromUnicode(s, 2); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1134 | } |
| 1135 | #endif |
| 1136 | |
Hye-Shik Chang | 4057483 | 2004-04-06 07:24:51 +0000 | [diff] [blame] | 1137 | s[0] = (Py_UNICODE)ordinal; |
| 1138 | return PyUnicode_FromUnicode(s, 1); |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1141 | PyObject *PyUnicode_FromObject(register PyObject *obj) |
| 1142 | { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1143 | /* XXX Perhaps we should make this API an alias of |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1144 | PyObject_Str() instead ?! */ |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1145 | if (PyUnicode_CheckExact(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1146 | Py_INCREF(obj); |
| 1147 | return obj; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1148 | } |
| 1149 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1150 | /* For a Unicode subtype that's not a Unicode object, |
| 1151 | return a true Unicode object with the same data. */ |
| 1152 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj), |
| 1153 | PyUnicode_GET_SIZE(obj)); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1154 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1155 | PyErr_Format(PyExc_TypeError, |
| 1156 | "Can't convert '%.100s' object to str implicitly", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1157 | Py_TYPE(obj)->tp_name); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1158 | return NULL; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1162 | const char *encoding, |
| 1163 | const char *errors) |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1164 | { |
Antoine Pitrou | a2983c6 | 2010-09-01 15:16:41 +0000 | [diff] [blame] | 1165 | Py_buffer buffer; |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1166 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1167 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1168 | if (obj == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1169 | PyErr_BadInternalCall(); |
| 1170 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1171 | } |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1172 | |
Antoine Pitrou | a2983c6 | 2010-09-01 15:16:41 +0000 | [diff] [blame] | 1173 | /* Decoding bytes objects is the most common case and should be fast */ |
| 1174 | if (PyBytes_Check(obj)) { |
| 1175 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 1176 | Py_INCREF(unicode_empty); |
| 1177 | v = (PyObject *) unicode_empty; |
| 1178 | } |
| 1179 | else { |
| 1180 | v = PyUnicode_Decode( |
| 1181 | PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), |
| 1182 | encoding, errors); |
| 1183 | } |
| 1184 | return v; |
| 1185 | } |
| 1186 | |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1187 | if (PyUnicode_Check(obj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1188 | PyErr_SetString(PyExc_TypeError, |
| 1189 | "decoding str is not supported"); |
| 1190 | return NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1191 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 1192 | |
Antoine Pitrou | a2983c6 | 2010-09-01 15:16:41 +0000 | [diff] [blame] | 1193 | /* Retrieve a bytes buffer view through the PEP 3118 buffer interface */ |
| 1194 | if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) { |
| 1195 | PyErr_Format(PyExc_TypeError, |
| 1196 | "coercing to str: need bytes, bytearray " |
| 1197 | "or buffer-like object, %.80s found", |
| 1198 | Py_TYPE(obj)->tp_name); |
| 1199 | return NULL; |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 1200 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1201 | |
Antoine Pitrou | a2983c6 | 2010-09-01 15:16:41 +0000 | [diff] [blame] | 1202 | if (buffer.len == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1203 | Py_INCREF(unicode_empty); |
Antoine Pitrou | a2983c6 | 2010-09-01 15:16:41 +0000 | [diff] [blame] | 1204 | v = (PyObject *) unicode_empty; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1205 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1206 | else |
Antoine Pitrou | a2983c6 | 2010-09-01 15:16:41 +0000 | [diff] [blame] | 1207 | v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors); |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 1208 | |
Antoine Pitrou | a2983c6 | 2010-09-01 15:16:41 +0000 | [diff] [blame] | 1209 | PyBuffer_Release(&buffer); |
Marc-André Lemburg | 5a5c81a | 2000-07-07 13:46:42 +0000 | [diff] [blame] | 1210 | return v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | PyObject *PyUnicode_Decode(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1214 | Py_ssize_t size, |
| 1215 | const char *encoding, |
| 1216 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1217 | { |
| 1218 | PyObject *buffer = NULL, *unicode; |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1219 | Py_buffer info; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1220 | char lower[20]; /* Enough for any encoding name we recognize */ |
| 1221 | char *l; |
| 1222 | const char *e; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1223 | |
| 1224 | if (encoding == NULL) |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1225 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1226 | |
| 1227 | /* Convert encoding to lower case and replace '_' with '-' in order to |
| 1228 | catch e.g. UTF_8 */ |
| 1229 | e = encoding; |
| 1230 | l = lower; |
| 1231 | while (*e && l < &lower[(sizeof lower) - 2]) { |
| 1232 | if (ISUPPER(*e)) { |
| 1233 | *l++ = TOLOWER(*e++); |
| 1234 | } |
| 1235 | else if (*e == '_') { |
| 1236 | *l++ = '-'; |
| 1237 | e++; |
| 1238 | } |
| 1239 | else { |
| 1240 | *l++ = *e++; |
| 1241 | } |
| 1242 | } |
| 1243 | *l = '\0'; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1244 | |
| 1245 | /* Shortcuts for common default encodings */ |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1246 | if (strcmp(lower, "utf-8") == 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1247 | return PyUnicode_DecodeUTF8(s, size, errors); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1248 | else if ((strcmp(lower, "latin-1") == 0) || |
| 1249 | (strcmp(lower, "iso-8859-1") == 0)) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1250 | return PyUnicode_DecodeLatin1(s, size, errors); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1251 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1252 | else if (strcmp(lower, "mbcs") == 0) |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1253 | return PyUnicode_DecodeMBCS(s, size, errors); |
| 1254 | #endif |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1255 | else if (strcmp(lower, "ascii") == 0) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1256 | return PyUnicode_DecodeASCII(s, size, errors); |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 1257 | else if (strcmp(lower, "utf-16") == 0) |
| 1258 | return PyUnicode_DecodeUTF16(s, size, errors, 0); |
| 1259 | else if (strcmp(lower, "utf-32") == 0) |
| 1260 | return PyUnicode_DecodeUTF32(s, size, errors, 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1261 | |
| 1262 | /* Decode via the codec registry */ |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1263 | buffer = NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 1264 | if (PyBuffer_FillInfo(&info, NULL, (void *)s, size, 1, PyBUF_FULL_RO) < 0) |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 1265 | goto onError; |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 1266 | buffer = PyMemoryView_FromBuffer(&info); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1267 | if (buffer == NULL) |
| 1268 | goto onError; |
| 1269 | unicode = PyCodec_Decode(buffer, encoding, errors); |
| 1270 | if (unicode == NULL) |
| 1271 | goto onError; |
| 1272 | if (!PyUnicode_Check(unicode)) { |
| 1273 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1274 | "decoder did not return a str object (type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1275 | Py_TYPE(unicode)->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1276 | Py_DECREF(unicode); |
| 1277 | goto onError; |
| 1278 | } |
| 1279 | Py_DECREF(buffer); |
| 1280 | return unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1281 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1282 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1283 | Py_XDECREF(buffer); |
| 1284 | return NULL; |
| 1285 | } |
| 1286 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1287 | PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, |
| 1288 | const char *encoding, |
| 1289 | const char *errors) |
| 1290 | { |
| 1291 | PyObject *v; |
| 1292 | |
| 1293 | if (!PyUnicode_Check(unicode)) { |
| 1294 | PyErr_BadArgument(); |
| 1295 | goto onError; |
| 1296 | } |
| 1297 | |
| 1298 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1299 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1300 | |
| 1301 | /* Decode via the codec registry */ |
| 1302 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1303 | if (v == NULL) |
| 1304 | goto onError; |
| 1305 | return v; |
| 1306 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1307 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1308 | return NULL; |
| 1309 | } |
| 1310 | |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1311 | PyObject *PyUnicode_AsDecodedUnicode(PyObject *unicode, |
| 1312 | const char *encoding, |
| 1313 | const char *errors) |
| 1314 | { |
| 1315 | PyObject *v; |
| 1316 | |
| 1317 | if (!PyUnicode_Check(unicode)) { |
| 1318 | PyErr_BadArgument(); |
| 1319 | goto onError; |
| 1320 | } |
| 1321 | |
| 1322 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1323 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1324 | |
| 1325 | /* Decode via the codec registry */ |
| 1326 | v = PyCodec_Decode(unicode, encoding, errors); |
| 1327 | if (v == NULL) |
| 1328 | goto onError; |
| 1329 | if (!PyUnicode_Check(v)) { |
| 1330 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1331 | "decoder did not return a str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1332 | Py_TYPE(v)->tp_name); |
| 1333 | Py_DECREF(v); |
| 1334 | goto onError; |
| 1335 | } |
| 1336 | return v; |
| 1337 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1338 | onError: |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1339 | return NULL; |
| 1340 | } |
| 1341 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1342 | PyObject *PyUnicode_Encode(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1343 | Py_ssize_t size, |
| 1344 | const char *encoding, |
| 1345 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1346 | { |
| 1347 | PyObject *v, *unicode; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1348 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1349 | unicode = PyUnicode_FromUnicode(s, size); |
| 1350 | if (unicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1351 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1352 | v = PyUnicode_AsEncodedString(unicode, encoding, errors); |
| 1353 | Py_DECREF(unicode); |
| 1354 | return v; |
| 1355 | } |
| 1356 | |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1357 | PyObject *PyUnicode_AsEncodedObject(PyObject *unicode, |
| 1358 | const char *encoding, |
| 1359 | const char *errors) |
| 1360 | { |
| 1361 | PyObject *v; |
| 1362 | |
| 1363 | if (!PyUnicode_Check(unicode)) { |
| 1364 | PyErr_BadArgument(); |
| 1365 | goto onError; |
| 1366 | } |
| 1367 | |
| 1368 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1369 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1370 | |
| 1371 | /* Encode via the codec registry */ |
| 1372 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1373 | if (v == NULL) |
| 1374 | goto onError; |
| 1375 | return v; |
| 1376 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1377 | onError: |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 1378 | return NULL; |
| 1379 | } |
| 1380 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1381 | PyObject *PyUnicode_AsEncodedString(PyObject *unicode, |
| 1382 | const char *encoding, |
| 1383 | const char *errors) |
| 1384 | { |
| 1385 | PyObject *v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1386 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1387 | if (!PyUnicode_Check(unicode)) { |
| 1388 | PyErr_BadArgument(); |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1389 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1390 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1391 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1392 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1393 | encoding = PyUnicode_GetDefaultEncoding(); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1394 | |
| 1395 | /* Shortcuts for common default encodings */ |
| 1396 | if (errors == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1397 | if (strcmp(encoding, "utf-8") == 0) |
| 1398 | return PyUnicode_AsUTF8String(unicode); |
| 1399 | else if (strcmp(encoding, "latin-1") == 0) |
| 1400 | return PyUnicode_AsLatin1String(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1401 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1402 | else if (strcmp(encoding, "mbcs") == 0) |
| 1403 | return PyUnicode_AsMBCSString(unicode); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 1404 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1405 | else if (strcmp(encoding, "ascii") == 0) |
| 1406 | return PyUnicode_AsASCIIString(unicode); |
Christian Heimes | 6a27efa | 2008-10-30 21:48:26 +0000 | [diff] [blame] | 1407 | /* During bootstrap, we may need to find the encodings |
| 1408 | package, to load the file system encoding, and require the |
| 1409 | file system encoding in order to load the encodings |
| 1410 | package. |
| 1411 | |
| 1412 | Break out of this dependency by assuming that the path to |
| 1413 | the encodings module is ASCII-only. XXX could try wcstombs |
| 1414 | instead, if the file system encoding is the locale's |
| 1415 | encoding. */ |
| 1416 | else if (Py_FileSystemDefaultEncoding && |
| 1417 | strcmp(encoding, Py_FileSystemDefaultEncoding) == 0 && |
| 1418 | !PyThreadState_GET()->interp->codecs_initialized) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1419 | return PyUnicode_AsASCIIString(unicode); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1420 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1421 | |
| 1422 | /* Encode via the codec registry */ |
| 1423 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1424 | if (v == NULL) |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1425 | return NULL; |
| 1426 | |
| 1427 | /* The normal path */ |
| 1428 | if (PyBytes_Check(v)) |
| 1429 | return v; |
| 1430 | |
| 1431 | /* 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] | 1432 | if (PyByteArray_Check(v)) { |
| 1433 | char msg[100]; |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1434 | PyObject *b; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1435 | PyOS_snprintf(msg, sizeof(msg), |
| 1436 | "encoder %s returned buffer instead of bytes", |
| 1437 | encoding); |
| 1438 | if (PyErr_WarnEx(PyExc_RuntimeWarning, msg, 1) < 0) { |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1439 | Py_DECREF(v); |
| 1440 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1441 | } |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1442 | |
Amaury Forgeot d'Arc | f048111 | 2008-09-05 20:48:47 +0000 | [diff] [blame] | 1443 | b = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(v), Py_SIZE(v)); |
| 1444 | Py_DECREF(v); |
| 1445 | return b; |
| 1446 | } |
| 1447 | |
| 1448 | PyErr_Format(PyExc_TypeError, |
| 1449 | "encoder did not return a bytes object (type=%.400s)", |
| 1450 | Py_TYPE(v)->tp_name); |
| 1451 | Py_DECREF(v); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1452 | return NULL; |
| 1453 | } |
| 1454 | |
| 1455 | PyObject *PyUnicode_AsEncodedUnicode(PyObject *unicode, |
| 1456 | const char *encoding, |
| 1457 | const char *errors) |
| 1458 | { |
| 1459 | PyObject *v; |
| 1460 | |
| 1461 | if (!PyUnicode_Check(unicode)) { |
| 1462 | PyErr_BadArgument(); |
| 1463 | goto onError; |
| 1464 | } |
| 1465 | |
| 1466 | if (encoding == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1467 | encoding = PyUnicode_GetDefaultEncoding(); |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1468 | |
| 1469 | /* Encode via the codec registry */ |
| 1470 | v = PyCodec_Encode(unicode, encoding, errors); |
| 1471 | if (v == NULL) |
| 1472 | goto onError; |
| 1473 | if (!PyUnicode_Check(v)) { |
| 1474 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1475 | "encoder did not return an str object (type=%.400s)", |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 1476 | Py_TYPE(v)->tp_name); |
| 1477 | Py_DECREF(v); |
| 1478 | goto onError; |
| 1479 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1480 | return v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1481 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1482 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1483 | return NULL; |
| 1484 | } |
| 1485 | |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1486 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1487 | const char *errors) |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1488 | { |
| 1489 | PyObject *v = ((PyUnicodeObject *)unicode)->defenc; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1490 | if (v) |
| 1491 | return v; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1492 | if (errors != NULL) |
| 1493 | Py_FatalError("non-NULL encoding in _PyUnicode_AsDefaultEncodedString"); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1494 | v = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Guido van Rossum | 0661009 | 2007-08-16 21:02:22 +0000 | [diff] [blame] | 1495 | PyUnicode_GET_SIZE(unicode), |
| 1496 | NULL); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1497 | if (!v) |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1498 | return NULL; |
Guido van Rossum | e7a0d39 | 2007-07-12 07:53:00 +0000 | [diff] [blame] | 1499 | ((PyUnicodeObject *)unicode)->defenc = v; |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 1500 | return v; |
| 1501 | } |
| 1502 | |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1503 | PyObject* |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1504 | PyUnicode_DecodeFSDefault(const char *s) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1505 | Py_ssize_t size = (Py_ssize_t)strlen(s); |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1506 | return PyUnicode_DecodeFSDefaultAndSize(s, size); |
| 1507 | } |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1508 | |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1509 | PyObject* |
| 1510 | PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
| 1511 | { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1512 | /* During the early bootstrapping process, Py_FileSystemDefaultEncoding |
| 1513 | can be undefined. If it is case, decode using UTF-8. The following assumes |
| 1514 | that Py_FileSystemDefaultEncoding is set to a built-in encoding during the |
| 1515 | bootstrapping process where the codecs aren't ready yet. |
| 1516 | */ |
| 1517 | if (Py_FileSystemDefaultEncoding) { |
| 1518 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1519 | if (strcmp(Py_FileSystemDefaultEncoding, "mbcs") == 0) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1520 | return PyUnicode_DecodeMBCS(s, size, "replace"); |
| 1521 | } |
| 1522 | #elif defined(__APPLE__) |
Christian Heimes | 5894ba7 | 2007-11-04 11:43:14 +0000 | [diff] [blame] | 1523 | if (strcmp(Py_FileSystemDefaultEncoding, "utf-8") == 0) { |
Guido van Rossum | 00bc0e0 | 2007-10-15 02:52:41 +0000 | [diff] [blame] | 1524 | return PyUnicode_DecodeUTF8(s, size, "replace"); |
| 1525 | } |
| 1526 | #endif |
| 1527 | return PyUnicode_Decode(s, size, |
| 1528 | Py_FileSystemDefaultEncoding, |
| 1529 | "replace"); |
| 1530 | } |
| 1531 | else { |
| 1532 | return PyUnicode_DecodeUTF8(s, size, "replace"); |
| 1533 | } |
| 1534 | } |
| 1535 | |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1536 | /* Convert the argument to a bytes object, according to the file |
| 1537 | system encoding */ |
| 1538 | |
| 1539 | int |
| 1540 | PyUnicode_FSConverter(PyObject* arg, void* addr) |
| 1541 | { |
| 1542 | PyObject *output = NULL; |
| 1543 | Py_ssize_t size; |
| 1544 | void *data; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 1545 | if (arg == NULL) { |
| 1546 | Py_DECREF(*(PyObject**)addr); |
| 1547 | return 1; |
| 1548 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1549 | if (PyBytes_Check(arg) || PyByteArray_Check(arg)) { |
| 1550 | output = arg; |
| 1551 | Py_INCREF(output); |
| 1552 | } |
| 1553 | else { |
| 1554 | arg = PyUnicode_FromObject(arg); |
| 1555 | if (!arg) |
| 1556 | return 0; |
| 1557 | output = PyUnicode_AsEncodedObject(arg, |
| 1558 | Py_FileSystemDefaultEncoding, |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 1559 | "surrogateescape"); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1560 | Py_DECREF(arg); |
| 1561 | if (!output) |
| 1562 | return 0; |
| 1563 | if (!PyBytes_Check(output)) { |
| 1564 | Py_DECREF(output); |
| 1565 | PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes"); |
| 1566 | return 0; |
| 1567 | } |
| 1568 | } |
| 1569 | if (PyBytes_Check(output)) { |
| 1570 | size = PyBytes_GET_SIZE(output); |
| 1571 | data = PyBytes_AS_STRING(output); |
| 1572 | } |
| 1573 | else { |
| 1574 | size = PyByteArray_GET_SIZE(output); |
| 1575 | data = PyByteArray_AS_STRING(output); |
| 1576 | } |
| 1577 | if (size != strlen(data)) { |
| 1578 | PyErr_SetString(PyExc_TypeError, "embedded NUL character"); |
| 1579 | Py_DECREF(output); |
| 1580 | return 0; |
| 1581 | } |
| 1582 | *(PyObject**)addr = output; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 1583 | return Py_CLEANUP_SUPPORTED; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1584 | } |
| 1585 | |
| 1586 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1587 | char* |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1588 | _PyUnicode_AsStringAndSize(PyObject *unicode, Py_ssize_t *psize) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1589 | { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1590 | PyObject *bytes; |
Neal Norwitz | e0a0a6e | 2007-08-25 01:04:21 +0000 | [diff] [blame] | 1591 | if (!PyUnicode_Check(unicode)) { |
| 1592 | PyErr_BadArgument(); |
| 1593 | return NULL; |
| 1594 | } |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 1595 | bytes = _PyUnicode_AsDefaultEncodedString(unicode, NULL); |
| 1596 | if (bytes == NULL) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1597 | return NULL; |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1598 | if (psize != NULL) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1599 | *psize = PyBytes_GET_SIZE(bytes); |
| 1600 | return PyBytes_AS_STRING(bytes); |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1601 | } |
| 1602 | |
| 1603 | char* |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1604 | _PyUnicode_AsString(PyObject *unicode) |
Guido van Rossum | 7d1df6c | 2007-08-29 13:53:23 +0000 | [diff] [blame] | 1605 | { |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1606 | return _PyUnicode_AsStringAndSize(unicode, NULL); |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1607 | } |
| 1608 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1609 | Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) |
| 1610 | { |
| 1611 | if (!PyUnicode_Check(unicode)) { |
| 1612 | PyErr_BadArgument(); |
| 1613 | goto onError; |
| 1614 | } |
| 1615 | return PyUnicode_AS_UNICODE(unicode); |
| 1616 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1617 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1618 | return NULL; |
| 1619 | } |
| 1620 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1621 | Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1622 | { |
| 1623 | if (!PyUnicode_Check(unicode)) { |
| 1624 | PyErr_BadArgument(); |
| 1625 | goto onError; |
| 1626 | } |
| 1627 | return PyUnicode_GET_SIZE(unicode); |
| 1628 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1629 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 1630 | return -1; |
| 1631 | } |
| 1632 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 1633 | const char *PyUnicode_GetDefaultEncoding(void) |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1634 | { |
| 1635 | return unicode_default_encoding; |
| 1636 | } |
| 1637 | |
| 1638 | int PyUnicode_SetDefaultEncoding(const char *encoding) |
| 1639 | { |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 1640 | if (strcmp(encoding, unicode_default_encoding) != 0) { |
| 1641 | PyErr_Format(PyExc_ValueError, |
| 1642 | "Can only set default encoding to %s", |
| 1643 | unicode_default_encoding); |
| 1644 | return -1; |
| 1645 | } |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1646 | return 0; |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1649 | /* error handling callback helper: |
| 1650 | build arguments, call the callback and check the arguments, |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 1651 | if no exception occurred, copy the replacement to the output |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1652 | and adjust various state variables. |
| 1653 | return 0 on success, -1 on error |
| 1654 | */ |
| 1655 | |
| 1656 | static |
| 1657 | int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1658 | const char *encoding, const char *reason, |
| 1659 | const char **input, const char **inend, Py_ssize_t *startinpos, |
| 1660 | Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, |
| 1661 | PyUnicodeObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1662 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 1663 | 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] | 1664 | |
| 1665 | PyObject *restuple = NULL; |
| 1666 | PyObject *repunicode = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1667 | Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1668 | Py_ssize_t insize; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1669 | Py_ssize_t requiredsize; |
| 1670 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1671 | Py_UNICODE *repptr; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1672 | PyObject *inputobj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1673 | Py_ssize_t repsize; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1674 | int res = -1; |
| 1675 | |
| 1676 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1677 | *errorHandler = PyCodec_LookupError(errors); |
| 1678 | if (*errorHandler == NULL) |
| 1679 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1680 | } |
| 1681 | |
| 1682 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 1683 | *exceptionObject = PyUnicodeDecodeError_Create( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1684 | encoding, *input, *inend-*input, *startinpos, *endinpos, reason); |
| 1685 | if (*exceptionObject == NULL) |
| 1686 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1687 | } |
| 1688 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1689 | if (PyUnicodeDecodeError_SetStart(*exceptionObject, *startinpos)) |
| 1690 | goto onError; |
| 1691 | if (PyUnicodeDecodeError_SetEnd(*exceptionObject, *endinpos)) |
| 1692 | goto onError; |
| 1693 | if (PyUnicodeDecodeError_SetReason(*exceptionObject, reason)) |
| 1694 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1695 | } |
| 1696 | |
| 1697 | restuple = PyObject_CallFunctionObjArgs(*errorHandler, *exceptionObject, NULL); |
| 1698 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1699 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1700 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 1701 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1702 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1703 | } |
| 1704 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1705 | goto onError; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1706 | |
| 1707 | /* Copy back the bytes variables, which might have been modified by the |
| 1708 | callback */ |
| 1709 | inputobj = PyUnicodeDecodeError_GetObject(*exceptionObject); |
| 1710 | if (!inputobj) |
| 1711 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1712 | if (!PyBytes_Check(inputobj)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1713 | PyErr_Format(PyExc_TypeError, "exception attribute object must be bytes"); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1714 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1715 | *input = PyBytes_AS_STRING(inputobj); |
| 1716 | insize = PyBytes_GET_SIZE(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1717 | *inend = *input + insize; |
Walter Dörwald | 36f938f | 2007-08-10 10:11:43 +0000 | [diff] [blame] | 1718 | /* we can DECREF safely, as the exception has another reference, |
| 1719 | so the object won't go away. */ |
| 1720 | Py_DECREF(inputobj); |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1721 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1722 | if (newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1723 | newpos = insize+newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1724 | if (newpos<0 || newpos>insize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1725 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos); |
| 1726 | goto onError; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 1727 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1728 | |
| 1729 | /* need more space? (at least enough for what we |
| 1730 | have+the replacement+the rest of the string (starting |
| 1731 | at the new input position), so we won't have to check space |
| 1732 | when there are no errors in the rest of the string) */ |
| 1733 | repptr = PyUnicode_AS_UNICODE(repunicode); |
| 1734 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 1735 | requiredsize = *outpos + repsize + insize-newpos; |
| 1736 | if (requiredsize > outsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1737 | if (requiredsize<2*outsize) |
| 1738 | requiredsize = 2*outsize; |
| 1739 | if (_PyUnicode_Resize(output, requiredsize) < 0) |
| 1740 | goto onError; |
| 1741 | *outptr = PyUnicode_AS_UNICODE(*output) + *outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1742 | } |
| 1743 | *endinpos = newpos; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1744 | *inptr = *input + newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1745 | Py_UNICODE_COPY(*outptr, repptr, repsize); |
| 1746 | *outptr += repsize; |
| 1747 | *outpos += repsize; |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 1748 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1749 | /* we made it! */ |
| 1750 | res = 0; |
| 1751 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1752 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1753 | Py_XDECREF(restuple); |
| 1754 | return res; |
| 1755 | } |
| 1756 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1757 | /* --- UTF-7 Codec -------------------------------------------------------- */ |
| 1758 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1759 | /* See RFC2152 for details. We encode conservatively and decode liberally. */ |
| 1760 | |
| 1761 | /* Three simple macros defining base-64. */ |
| 1762 | |
| 1763 | /* Is c a base-64 character? */ |
| 1764 | |
| 1765 | #define IS_BASE64(c) \ |
| 1766 | (((c) >= 'A' && (c) <= 'Z') || \ |
| 1767 | ((c) >= 'a' && (c) <= 'z') || \ |
| 1768 | ((c) >= '0' && (c) <= '9') || \ |
| 1769 | (c) == '+' || (c) == '/') |
| 1770 | |
| 1771 | /* given that c is a base-64 character, what is its base-64 value? */ |
| 1772 | |
| 1773 | #define FROM_BASE64(c) \ |
| 1774 | (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' : \ |
| 1775 | ((c) >= 'a' && (c) <= 'z') ? (c) - 'a' + 26 : \ |
| 1776 | ((c) >= '0' && (c) <= '9') ? (c) - '0' + 52 : \ |
| 1777 | (c) == '+' ? 62 : 63) |
| 1778 | |
| 1779 | /* What is the base-64 character of the bottom 6 bits of n? */ |
| 1780 | |
| 1781 | #define TO_BASE64(n) \ |
| 1782 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(n) & 0x3f]) |
| 1783 | |
| 1784 | /* DECODE_DIRECT: this byte encountered in a UTF-7 string should be |
| 1785 | * decoded as itself. We are permissive on decoding; the only ASCII |
| 1786 | * byte not decoding to itself is the + which begins a base64 |
| 1787 | * string. */ |
| 1788 | |
| 1789 | #define DECODE_DIRECT(c) \ |
| 1790 | ((c) <= 127 && (c) != '+') |
| 1791 | |
| 1792 | /* The UTF-7 encoder treats ASCII characters differently according to |
| 1793 | * whether they are Set D, Set O, Whitespace, or special (i.e. none of |
| 1794 | * the above). See RFC2152. This array identifies these different |
| 1795 | * sets: |
| 1796 | * 0 : "Set D" |
| 1797 | * alphanumeric and '(),-./:? |
| 1798 | * 1 : "Set O" |
| 1799 | * !"#$%&*;<=>@[]^_`{|} |
| 1800 | * 2 : "whitespace" |
| 1801 | * ht nl cr sp |
| 1802 | * 3 : special (must be base64 encoded) |
| 1803 | * everything else (i.e. +\~ and non-printing codes 0-8 11-12 14-31 127) |
| 1804 | */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1805 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1806 | static |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1807 | char utf7_category[128] = { |
| 1808 | /* nul soh stx etx eot enq ack bel bs ht nl vt np cr so si */ |
| 1809 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 2, 3, 3, |
| 1810 | /* dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us */ |
| 1811 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 1812 | /* sp ! " # $ % & ' ( ) * + , - . / */ |
| 1813 | 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, |
| 1814 | /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ |
| 1815 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, |
| 1816 | /* @ A B C D E F G H I J K L M N O */ |
| 1817 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1818 | /* P Q R S T U V W X Y Z [ \ ] ^ _ */ |
| 1819 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 1, 1, |
| 1820 | /* ` a b c d e f g h i j k l m n o */ |
| 1821 | 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1822 | /* p q r s t u v w x y z { | } ~ del */ |
| 1823 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 3, 3, |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1824 | }; |
| 1825 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1826 | /* ENCODE_DIRECT: this character should be encoded as itself. The |
| 1827 | * answer depends on whether we are encoding set O as itself, and also |
| 1828 | * on whether we are encoding whitespace as itself. RFC2152 makes it |
| 1829 | * clear that the answers to these questions vary between |
| 1830 | * applications, so this code needs to be flexible. */ |
Marc-André Lemburg | e115ec8 | 2005-10-19 22:33:31 +0000 | [diff] [blame] | 1831 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1832 | #define ENCODE_DIRECT(c, directO, directWS) \ |
| 1833 | ((c) < 128 && (c) > 0 && \ |
| 1834 | ((utf7_category[(c)] == 0) || \ |
| 1835 | (directWS && (utf7_category[(c)] == 2)) || \ |
| 1836 | (directO && (utf7_category[(c)] == 1)))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1837 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1838 | PyObject *PyUnicode_DecodeUTF7(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1839 | Py_ssize_t size, |
| 1840 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1841 | { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1842 | return PyUnicode_DecodeUTF7Stateful(s, size, errors, NULL); |
| 1843 | } |
| 1844 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1845 | /* The decoder. The only state we preserve is our read position, |
| 1846 | * i.e. how many characters we have consumed. So if we end in the |
| 1847 | * middle of a shift sequence we have to back off the read position |
| 1848 | * and the output to the beginning of the sequence, otherwise we lose |
| 1849 | * all the shift state (seen bits, number of bits seen, high |
| 1850 | * surrogate). */ |
| 1851 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1852 | PyObject *PyUnicode_DecodeUTF7Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1853 | Py_ssize_t size, |
| 1854 | const char *errors, |
| 1855 | Py_ssize_t *consumed) |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1856 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1857 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1858 | Py_ssize_t startinpos; |
| 1859 | Py_ssize_t endinpos; |
| 1860 | Py_ssize_t outpos; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1861 | const char *e; |
| 1862 | PyUnicodeObject *unicode; |
| 1863 | Py_UNICODE *p; |
| 1864 | const char *errmsg = ""; |
| 1865 | int inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1866 | Py_UNICODE *shiftOutStart; |
| 1867 | unsigned int base64bits = 0; |
| 1868 | unsigned long base64buffer = 0; |
| 1869 | Py_UNICODE surrogate = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1870 | PyObject *errorHandler = NULL; |
| 1871 | PyObject *exc = NULL; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1872 | |
| 1873 | unicode = _PyUnicode_New(size); |
| 1874 | if (!unicode) |
| 1875 | return NULL; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1876 | if (size == 0) { |
| 1877 | if (consumed) |
| 1878 | *consumed = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1879 | return (PyObject *)unicode; |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 1880 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1881 | |
| 1882 | p = unicode->str; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1883 | shiftOutStart = p; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1884 | e = s + size; |
| 1885 | |
| 1886 | while (s < e) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1887 | Py_UNICODE ch; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1888 | restart: |
Antoine Pitrou | 5ffd9e9 | 2008-07-25 18:05:24 +0000 | [diff] [blame] | 1889 | ch = (unsigned char) *s; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1890 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1891 | if (inShift) { /* in a base-64 section */ |
| 1892 | if (IS_BASE64(ch)) { /* consume a base-64 character */ |
| 1893 | base64buffer = (base64buffer << 6) | FROM_BASE64(ch); |
| 1894 | base64bits += 6; |
| 1895 | s++; |
| 1896 | if (base64bits >= 16) { |
| 1897 | /* we have enough bits for a UTF-16 value */ |
| 1898 | Py_UNICODE outCh = (Py_UNICODE) |
| 1899 | (base64buffer >> (base64bits-16)); |
| 1900 | base64bits -= 16; |
| 1901 | base64buffer &= (1 << base64bits) - 1; /* clear high bits */ |
| 1902 | if (surrogate) { |
| 1903 | /* expecting a second surrogate */ |
| 1904 | if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 1905 | #ifdef Py_UNICODE_WIDE |
| 1906 | *p++ = (((surrogate & 0x3FF)<<10) |
| 1907 | | (outCh & 0x3FF)) + 0x10000; |
| 1908 | #else |
| 1909 | *p++ = surrogate; |
| 1910 | *p++ = outCh; |
| 1911 | #endif |
| 1912 | surrogate = 0; |
| 1913 | } |
| 1914 | else { |
| 1915 | surrogate = 0; |
| 1916 | errmsg = "second surrogate missing"; |
| 1917 | goto utf7Error; |
| 1918 | } |
| 1919 | } |
| 1920 | else if (outCh >= 0xD800 && outCh <= 0xDBFF) { |
| 1921 | /* first surrogate */ |
| 1922 | surrogate = outCh; |
| 1923 | } |
| 1924 | else if (outCh >= 0xDC00 && outCh <= 0xDFFF) { |
| 1925 | errmsg = "unexpected second surrogate"; |
| 1926 | goto utf7Error; |
| 1927 | } |
| 1928 | else { |
| 1929 | *p++ = outCh; |
| 1930 | } |
| 1931 | } |
| 1932 | } |
| 1933 | else { /* now leaving a base-64 section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1934 | inShift = 0; |
| 1935 | s++; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1936 | if (surrogate) { |
| 1937 | errmsg = "second surrogate missing at end of shift sequence"; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 1938 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1939 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1940 | if (base64bits > 0) { /* left-over bits */ |
| 1941 | if (base64bits >= 6) { |
| 1942 | /* We've seen at least one base-64 character */ |
| 1943 | errmsg = "partial character in shift sequence"; |
| 1944 | goto utf7Error; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1945 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1946 | else { |
| 1947 | /* Some bits remain; they should be zero */ |
| 1948 | if (base64buffer != 0) { |
| 1949 | errmsg = "non-zero padding bits in shift sequence"; |
| 1950 | goto utf7Error; |
| 1951 | } |
| 1952 | } |
| 1953 | } |
| 1954 | if (ch != '-') { |
| 1955 | /* '-' is absorbed; other terminating |
| 1956 | characters are preserved */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1957 | *p++ = ch; |
| 1958 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1959 | } |
| 1960 | } |
| 1961 | else if ( ch == '+' ) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1962 | startinpos = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1963 | s++; /* consume '+' */ |
| 1964 | if (s < e && *s == '-') { /* '+-' encodes '+' */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1965 | s++; |
| 1966 | *p++ = '+'; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1967 | } |
| 1968 | else { /* begin base64-encoded section */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1969 | inShift = 1; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1970 | shiftOutStart = p; |
| 1971 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1972 | } |
| 1973 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1974 | else if (DECODE_DIRECT(ch)) { /* character decodes as itself */ |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1975 | *p++ = ch; |
| 1976 | s++; |
| 1977 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1978 | else { |
| 1979 | startinpos = s-starts; |
| 1980 | s++; |
| 1981 | errmsg = "unexpected special character"; |
| 1982 | goto utf7Error; |
| 1983 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1984 | continue; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1985 | utf7Error: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1986 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 1987 | endinpos = s-starts; |
| 1988 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 1989 | errors, &errorHandler, |
| 1990 | "utf7", errmsg, |
| 1991 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 1992 | &unicode, &outpos, &p)) |
| 1993 | goto onError; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 1994 | } |
| 1995 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 1996 | /* end of string */ |
| 1997 | |
| 1998 | if (inShift && !consumed) { /* in shift sequence, no more to follow */ |
| 1999 | /* if we're in an inconsistent state, that's an error */ |
| 2000 | if (surrogate || |
| 2001 | (base64bits >= 6) || |
| 2002 | (base64bits > 0 && base64buffer != 0)) { |
| 2003 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2004 | endinpos = size; |
| 2005 | if (unicode_decode_call_errorhandler( |
| 2006 | errors, &errorHandler, |
| 2007 | "utf7", "unterminated shift sequence", |
| 2008 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2009 | &unicode, &outpos, &p)) |
| 2010 | goto onError; |
| 2011 | if (s < e) |
| 2012 | goto restart; |
| 2013 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2014 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2015 | |
| 2016 | /* return state */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2017 | if (consumed) { |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2018 | if (inShift) { |
| 2019 | p = shiftOutStart; /* back off output */ |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2020 | *consumed = startinpos; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2021 | } |
| 2022 | else { |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2023 | *consumed = s-starts; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2024 | } |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 2025 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2026 | |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2027 | if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2028 | goto onError; |
| 2029 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2030 | Py_XDECREF(errorHandler); |
| 2031 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2032 | return (PyObject *)unicode; |
| 2033 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2034 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2035 | Py_XDECREF(errorHandler); |
| 2036 | Py_XDECREF(exc); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2037 | Py_DECREF(unicode); |
| 2038 | return NULL; |
| 2039 | } |
| 2040 | |
| 2041 | |
| 2042 | PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2043 | Py_ssize_t size, |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2044 | int base64SetO, |
| 2045 | int base64WhiteSpace, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2046 | const char *errors) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2047 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2048 | PyObject *v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2049 | /* It might be possible to tighten this worst case */ |
Georg Brandl | 194da4a | 2009-08-13 09:34:05 +0000 | [diff] [blame] | 2050 | Py_ssize_t allocated = 8 * size; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2051 | int inShift = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2052 | Py_ssize_t i = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2053 | unsigned int base64bits = 0; |
| 2054 | unsigned long base64buffer = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2055 | char * out; |
| 2056 | char * start; |
| 2057 | |
| 2058 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2059 | return PyBytes_FromStringAndSize(NULL, 0); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2060 | |
Georg Brandl | 194da4a | 2009-08-13 09:34:05 +0000 | [diff] [blame] | 2061 | if (allocated / 8 != size) |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2062 | return PyErr_NoMemory(); |
| 2063 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2064 | v = PyBytes_FromStringAndSize(NULL, allocated); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2065 | if (v == NULL) |
| 2066 | return NULL; |
| 2067 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2068 | start = out = PyBytes_AS_STRING(v); |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2069 | for (;i < size; ++i) { |
| 2070 | Py_UNICODE ch = s[i]; |
| 2071 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2072 | if (inShift) { |
| 2073 | if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 2074 | /* shifting out */ |
| 2075 | if (base64bits) { /* output remaining bits */ |
| 2076 | *out++ = TO_BASE64(base64buffer << (6-base64bits)); |
| 2077 | base64buffer = 0; |
| 2078 | base64bits = 0; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2079 | } |
| 2080 | inShift = 0; |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2081 | /* Characters not in the BASE64 set implicitly unshift the sequence |
| 2082 | so no '-' is required, except if the character is itself a '-' */ |
| 2083 | if (IS_BASE64(ch) || ch == '-') { |
| 2084 | *out++ = '-'; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2085 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2086 | *out++ = (char) ch; |
| 2087 | } |
| 2088 | else { |
| 2089 | goto encode_char; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2090 | } |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2091 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2092 | else { /* not in a shift sequence */ |
| 2093 | if (ch == '+') { |
| 2094 | *out++ = '+'; |
| 2095 | *out++ = '-'; |
| 2096 | } |
| 2097 | else if (ENCODE_DIRECT(ch, !base64SetO, !base64WhiteSpace)) { |
| 2098 | *out++ = (char) ch; |
| 2099 | } |
| 2100 | else { |
| 2101 | *out++ = '+'; |
| 2102 | inShift = 1; |
| 2103 | goto encode_char; |
| 2104 | } |
| 2105 | } |
| 2106 | continue; |
| 2107 | encode_char: |
| 2108 | #ifdef Py_UNICODE_WIDE |
| 2109 | if (ch >= 0x10000) { |
| 2110 | /* code first surrogate */ |
| 2111 | base64bits += 16; |
| 2112 | base64buffer = (base64buffer << 16) | 0xd800 | ((ch-0x10000) >> 10); |
| 2113 | while (base64bits >= 6) { |
| 2114 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 2115 | base64bits -= 6; |
| 2116 | } |
| 2117 | /* prepare second surrogate */ |
| 2118 | ch = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2119 | } |
| 2120 | #endif |
| 2121 | base64bits += 16; |
| 2122 | base64buffer = (base64buffer << 16) | ch; |
| 2123 | while (base64bits >= 6) { |
| 2124 | *out++ = TO_BASE64(base64buffer >> (base64bits-6)); |
| 2125 | base64bits -= 6; |
| 2126 | } |
Hye-Shik Chang | 1bc09b7 | 2004-01-03 19:35:43 +0000 | [diff] [blame] | 2127 | } |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2128 | if (base64bits) |
| 2129 | *out++= TO_BASE64(base64buffer << (6-base64bits) ); |
| 2130 | if (inShift) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2131 | *out++ = '-'; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2132 | if (_PyBytes_Resize(&v, out - start) < 0) |
| 2133 | return NULL; |
| 2134 | return v; |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2135 | } |
| 2136 | |
Antoine Pitrou | 244651a | 2009-05-04 18:56:13 +0000 | [diff] [blame] | 2137 | #undef IS_BASE64 |
| 2138 | #undef FROM_BASE64 |
| 2139 | #undef TO_BASE64 |
| 2140 | #undef DECODE_DIRECT |
| 2141 | #undef ENCODE_DIRECT |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 2142 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2143 | /* --- UTF-8 Codec -------------------------------------------------------- */ |
| 2144 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2145 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2146 | char utf8_code_length[256] = { |
Ezio Melotti | 25bc019 | 2010-07-03 05:18:50 +0000 | [diff] [blame] | 2147 | /* Map UTF-8 encoded prefix byte to sequence length. Zero means |
| 2148 | illegal prefix. See RFC 3629 for details */ |
| 2149 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */ |
| 2150 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2151 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2152 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2153 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2154 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2155 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
Ezio Melotti | 25bc019 | 2010-07-03 05:18:50 +0000 | [diff] [blame] | 2156 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 70-7F */ |
| 2157 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80-8F */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2158 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2159 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
Ezio Melotti | 25bc019 | 2010-07-03 05:18:50 +0000 | [diff] [blame] | 2160 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0-BF */ |
| 2161 | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* C0-C1 + C2-CF */ |
| 2162 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* D0-DF */ |
| 2163 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* E0-EF */ |
| 2164 | 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* F0-F4 + F5-FF */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2165 | }; |
| 2166 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2167 | PyObject *PyUnicode_DecodeUTF8(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2168 | Py_ssize_t size, |
| 2169 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2170 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2171 | return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); |
| 2172 | } |
| 2173 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2174 | /* Mask to check or force alignment of a pointer to C 'long' boundaries */ |
| 2175 | #define LONG_PTR_MASK (size_t) (SIZEOF_LONG - 1) |
| 2176 | |
| 2177 | /* Mask to quickly check whether a C 'long' contains a |
| 2178 | non-ASCII, UTF8-encoded char. */ |
| 2179 | #if (SIZEOF_LONG == 8) |
| 2180 | # define ASCII_CHAR_MASK 0x8080808080808080L |
| 2181 | #elif (SIZEOF_LONG == 4) |
| 2182 | # define ASCII_CHAR_MASK 0x80808080L |
| 2183 | #else |
| 2184 | # error C 'long' size should be either 4 or 8! |
| 2185 | #endif |
| 2186 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2187 | PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2188 | Py_ssize_t size, |
| 2189 | const char *errors, |
| 2190 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2191 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2192 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2193 | int n; |
Ezio Melotti | 25bc019 | 2010-07-03 05:18:50 +0000 | [diff] [blame] | 2194 | int k; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2195 | Py_ssize_t startinpos; |
| 2196 | Py_ssize_t endinpos; |
| 2197 | Py_ssize_t outpos; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2198 | const char *e, *aligned_end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2199 | PyUnicodeObject *unicode; |
| 2200 | Py_UNICODE *p; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2201 | const char *errmsg = ""; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2202 | PyObject *errorHandler = NULL; |
| 2203 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2204 | |
| 2205 | /* Note: size will always be longer than the resulting Unicode |
| 2206 | character count */ |
| 2207 | unicode = _PyUnicode_New(size); |
| 2208 | if (!unicode) |
| 2209 | return NULL; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2210 | if (size == 0) { |
| 2211 | if (consumed) |
| 2212 | *consumed = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2213 | return (PyObject *)unicode; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2214 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2215 | |
| 2216 | /* Unpack UTF-8 encoded data */ |
| 2217 | p = unicode->str; |
| 2218 | e = s + size; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2219 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2220 | |
| 2221 | while (s < e) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2222 | Py_UCS4 ch = (unsigned char)*s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2223 | |
| 2224 | if (ch < 0x80) { |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2225 | /* Fast path for runs of ASCII characters. Given that common UTF-8 |
| 2226 | input will consist of an overwhelming majority of ASCII |
| 2227 | characters, we try to optimize for this case by checking |
| 2228 | as many characters as a C 'long' can contain. |
| 2229 | First, check if we can do an aligned read, as most CPUs have |
| 2230 | a penalty for unaligned reads. |
| 2231 | */ |
| 2232 | if (!((size_t) s & LONG_PTR_MASK)) { |
| 2233 | /* Help register allocation */ |
| 2234 | register const char *_s = s; |
| 2235 | register Py_UNICODE *_p = p; |
| 2236 | while (_s < aligned_end) { |
| 2237 | /* Read a whole long at a time (either 4 or 8 bytes), |
| 2238 | and do a fast unrolled copy if it only contains ASCII |
| 2239 | characters. */ |
| 2240 | unsigned long data = *(unsigned long *) _s; |
| 2241 | if (data & ASCII_CHAR_MASK) |
| 2242 | break; |
| 2243 | _p[0] = (unsigned char) _s[0]; |
| 2244 | _p[1] = (unsigned char) _s[1]; |
| 2245 | _p[2] = (unsigned char) _s[2]; |
| 2246 | _p[3] = (unsigned char) _s[3]; |
| 2247 | #if (SIZEOF_LONG == 8) |
| 2248 | _p[4] = (unsigned char) _s[4]; |
| 2249 | _p[5] = (unsigned char) _s[5]; |
| 2250 | _p[6] = (unsigned char) _s[6]; |
| 2251 | _p[7] = (unsigned char) _s[7]; |
| 2252 | #endif |
| 2253 | _s += SIZEOF_LONG; |
| 2254 | _p += SIZEOF_LONG; |
| 2255 | } |
| 2256 | s = _s; |
| 2257 | p = _p; |
| 2258 | if (s == e) |
| 2259 | break; |
| 2260 | ch = (unsigned char)*s; |
| 2261 | } |
| 2262 | } |
| 2263 | |
| 2264 | if (ch < 0x80) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2265 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2266 | s++; |
| 2267 | continue; |
| 2268 | } |
| 2269 | |
| 2270 | n = utf8_code_length[ch]; |
| 2271 | |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2272 | if (s + n > e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2273 | if (consumed) |
| 2274 | break; |
| 2275 | else { |
| 2276 | errmsg = "unexpected end of data"; |
| 2277 | startinpos = s-starts; |
Ezio Melotti | 25bc019 | 2010-07-03 05:18:50 +0000 | [diff] [blame] | 2278 | endinpos = startinpos+1; |
| 2279 | for (k=1; (k < size-startinpos) && ((s[k]&0xC0) == 0x80); k++) |
| 2280 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2281 | goto utf8Error; |
| 2282 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2283 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2284 | |
| 2285 | switch (n) { |
| 2286 | |
| 2287 | case 0: |
Ezio Melotti | 25bc019 | 2010-07-03 05:18:50 +0000 | [diff] [blame] | 2288 | errmsg = "invalid start byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2289 | startinpos = s-starts; |
| 2290 | endinpos = startinpos+1; |
| 2291 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2292 | |
| 2293 | case 1: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2294 | errmsg = "internal error"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2295 | startinpos = s-starts; |
| 2296 | endinpos = startinpos+1; |
| 2297 | goto utf8Error; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2298 | |
| 2299 | case 2: |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2300 | if ((s[1] & 0xc0) != 0x80) { |
Ezio Melotti | 25bc019 | 2010-07-03 05:18:50 +0000 | [diff] [blame] | 2301 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2302 | startinpos = s-starts; |
Ezio Melotti | 25bc019 | 2010-07-03 05:18:50 +0000 | [diff] [blame] | 2303 | endinpos = startinpos + 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2304 | goto utf8Error; |
| 2305 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2306 | ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f); |
Ezio Melotti | 25bc019 | 2010-07-03 05:18:50 +0000 | [diff] [blame] | 2307 | assert ((ch > 0x007F) && (ch <= 0x07FF)); |
| 2308 | *p++ = (Py_UNICODE)ch; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2309 | break; |
| 2310 | |
| 2311 | case 3: |
Ezio Melotti | 25bc019 | 2010-07-03 05:18:50 +0000 | [diff] [blame] | 2312 | /* Decoding UTF-8 sequences in range \xed\xa0\x80-\xed\xbf\xbf |
| 2313 | will result in surrogates in range d800-dfff. Surrogates are |
| 2314 | not valid UTF-8 so they are rejected. |
| 2315 | See http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf |
| 2316 | (table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2317 | if ((s[1] & 0xc0) != 0x80 || |
Ezio Melotti | 25bc019 | 2010-07-03 05:18:50 +0000 | [diff] [blame] | 2318 | (s[2] & 0xc0) != 0x80 || |
| 2319 | ((unsigned char)s[0] == 0xE0 && |
| 2320 | (unsigned char)s[1] < 0xA0) || |
| 2321 | ((unsigned char)s[0] == 0xED && |
| 2322 | (unsigned char)s[1] > 0x9F)) { |
| 2323 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2324 | startinpos = s-starts; |
Ezio Melotti | 25bc019 | 2010-07-03 05:18:50 +0000 | [diff] [blame] | 2325 | endinpos = startinpos + 1; |
| 2326 | |
| 2327 | /* if s[1] first two bits are 1 and 0, then the invalid |
| 2328 | continuation byte is s[2], so increment endinpos by 1, |
| 2329 | if not, s[1] is invalid and endinpos doesn't need to |
| 2330 | be incremented. */ |
| 2331 | if ((s[1] & 0xC0) == 0x80) |
| 2332 | endinpos++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2333 | goto utf8Error; |
| 2334 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2335 | ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f); |
Ezio Melotti | 25bc019 | 2010-07-03 05:18:50 +0000 | [diff] [blame] | 2336 | assert ((ch > 0x07FF) && (ch <= 0xFFFF)); |
| 2337 | *p++ = (Py_UNICODE)ch; |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2338 | break; |
| 2339 | |
| 2340 | case 4: |
| 2341 | if ((s[1] & 0xc0) != 0x80 || |
| 2342 | (s[2] & 0xc0) != 0x80 || |
Ezio Melotti | 25bc019 | 2010-07-03 05:18:50 +0000 | [diff] [blame] | 2343 | (s[3] & 0xc0) != 0x80 || |
| 2344 | ((unsigned char)s[0] == 0xF0 && |
| 2345 | (unsigned char)s[1] < 0x90) || |
| 2346 | ((unsigned char)s[0] == 0xF4 && |
| 2347 | (unsigned char)s[1] > 0x8F)) { |
| 2348 | errmsg = "invalid continuation byte"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2349 | startinpos = s-starts; |
Ezio Melotti | 25bc019 | 2010-07-03 05:18:50 +0000 | [diff] [blame] | 2350 | endinpos = startinpos + 1; |
| 2351 | if ((s[1] & 0xC0) == 0x80) { |
| 2352 | endinpos++; |
| 2353 | if ((s[2] & 0xC0) == 0x80) |
| 2354 | endinpos++; |
| 2355 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2356 | goto utf8Error; |
| 2357 | } |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2358 | ch = ((s[0] & 0x7) << 18) + ((s[1] & 0x3f) << 12) + |
Ezio Melotti | 25bc019 | 2010-07-03 05:18:50 +0000 | [diff] [blame] | 2359 | ((s[2] & 0x3f) << 6) + (s[3] & 0x3f); |
| 2360 | assert ((ch > 0xFFFF) && (ch <= 0x10ffff)); |
| 2361 | |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 2362 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2363 | *p++ = (Py_UNICODE)ch; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2364 | #else |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2365 | /* compute and append the two surrogates: */ |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2366 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2367 | /* translate from 10000..10FFFF to 0..FFFF */ |
| 2368 | ch -= 0x10000; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2369 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2370 | /* high surrogate = top 10 bits added to D800 */ |
| 2371 | *p++ = (Py_UNICODE)(0xD800 + (ch >> 10)); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2372 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2373 | /* low surrogate = bottom 10 bits added to DC00 */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 2374 | *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF)); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 2375 | #endif |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2376 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2377 | } |
| 2378 | s += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2379 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2380 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2381 | utf8Error: |
| 2382 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2383 | if (unicode_decode_call_errorhandler( |
| 2384 | errors, &errorHandler, |
| 2385 | "utf8", errmsg, |
| 2386 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 2387 | &unicode, &outpos, &p)) |
| 2388 | goto onError; |
| 2389 | aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2390 | } |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2391 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2392 | *consumed = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2393 | |
| 2394 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 2395 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2396 | goto onError; |
| 2397 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2398 | Py_XDECREF(errorHandler); |
| 2399 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2400 | return (PyObject *)unicode; |
| 2401 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2402 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2403 | Py_XDECREF(errorHandler); |
| 2404 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2405 | Py_DECREF(unicode); |
| 2406 | return NULL; |
| 2407 | } |
| 2408 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2409 | #undef ASCII_CHAR_MASK |
| 2410 | |
| 2411 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2412 | /* Allocation strategy: if the string is short, convert into a stack buffer |
| 2413 | and allocate exactly as much space needed at the end. Else allocate the |
| 2414 | maximum possible needed (4 result bytes per Unicode character), and return |
| 2415 | the excess memory at the end. |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2416 | */ |
Tim Peters | 7e3d961 | 2002-04-21 03:26:37 +0000 | [diff] [blame] | 2417 | PyObject * |
| 2418 | PyUnicode_EncodeUTF8(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2419 | Py_ssize_t size, |
| 2420 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2421 | { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2422 | #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] | 2423 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2424 | Py_ssize_t i; /* index into s of next input byte */ |
| 2425 | PyObject *result; /* result string object */ |
| 2426 | char *p; /* next free byte in output buffer */ |
| 2427 | Py_ssize_t nallocated; /* number of result bytes allocated */ |
| 2428 | Py_ssize_t nneeded; /* number of result bytes needed */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2429 | char stackbuf[MAX_SHORT_UNICHARS * 4]; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2430 | PyObject *errorHandler = NULL; |
| 2431 | PyObject *exc = NULL; |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 2432 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2433 | assert(s != NULL); |
| 2434 | assert(size >= 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2435 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2436 | if (size <= MAX_SHORT_UNICHARS) { |
| 2437 | /* Write into the stack buffer; nallocated can't overflow. |
| 2438 | * At the end, we'll allocate exactly as much heap space as it |
| 2439 | * turns out we need. |
| 2440 | */ |
| 2441 | nallocated = Py_SAFE_DOWNCAST(sizeof(stackbuf), size_t, int); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2442 | result = NULL; /* will allocate after we're done */ |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2443 | p = stackbuf; |
| 2444 | } |
| 2445 | else { |
| 2446 | /* Overallocate on the heap, and give the excess back at the end. */ |
| 2447 | nallocated = size * 4; |
| 2448 | if (nallocated / 4 != size) /* overflow! */ |
| 2449 | return PyErr_NoMemory(); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2450 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2451 | if (result == NULL) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2452 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2453 | p = PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2454 | } |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2455 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2456 | for (i = 0; i < size;) { |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 2457 | Py_UCS4 ch = s[i++]; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2458 | |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2459 | if (ch < 0x80) |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2460 | /* Encode ASCII */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2461 | *p++ = (char) ch; |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 2462 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2463 | else if (ch < 0x0800) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2464 | /* Encode Latin-1 */ |
Marc-André Lemburg | dc724d6 | 2002-02-06 18:20:19 +0000 | [diff] [blame] | 2465 | *p++ = (char)(0xc0 | (ch >> 6)); |
| 2466 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 158701d | 2010-04-22 19:41:01 +0000 | [diff] [blame] | 2467 | } else if (0xD800 <= ch && ch <= 0xDFFF) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2468 | #ifndef Py_UNICODE_WIDE |
Victor Stinner | 158701d | 2010-04-22 19:41:01 +0000 | [diff] [blame] | 2469 | /* Special case: check for high and low surrogate */ |
| 2470 | if (ch <= 0xDBFF && i != size && 0xDC00 <= s[i] && s[i] <= 0xDFFF) { |
| 2471 | Py_UCS4 ch2 = s[i]; |
| 2472 | /* Combine the two surrogates to form a UCS4 value */ |
| 2473 | ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000; |
| 2474 | i++; |
| 2475 | |
| 2476 | /* Encode UCS4 Unicode ordinals */ |
| 2477 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2478 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2479 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2480 | *p++ = (char)(0x80 | (ch & 0x3f)); |
Victor Stinner | 158701d | 2010-04-22 19:41:01 +0000 | [diff] [blame] | 2481 | } else { |
Victor Stinner | 0b79b76 | 2010-04-22 20:07:28 +0000 | [diff] [blame] | 2482 | #endif |
Victor Stinner | 158701d | 2010-04-22 19:41:01 +0000 | [diff] [blame] | 2483 | Py_ssize_t newpos; |
| 2484 | PyObject *rep; |
| 2485 | Py_ssize_t repsize, k; |
| 2486 | rep = unicode_encode_call_errorhandler |
| 2487 | (errors, &errorHandler, "utf-8", "surrogates not allowed", |
| 2488 | s, size, &exc, i-1, i, &newpos); |
| 2489 | if (!rep) |
| 2490 | goto error; |
| 2491 | |
| 2492 | if (PyBytes_Check(rep)) |
| 2493 | repsize = PyBytes_GET_SIZE(rep); |
| 2494 | else |
| 2495 | repsize = PyUnicode_GET_SIZE(rep); |
| 2496 | |
| 2497 | if (repsize > 4) { |
| 2498 | Py_ssize_t offset; |
| 2499 | |
| 2500 | if (result == NULL) |
| 2501 | offset = p - stackbuf; |
| 2502 | else |
| 2503 | offset = p - PyBytes_AS_STRING(result); |
| 2504 | |
| 2505 | if (nallocated > PY_SSIZE_T_MAX - repsize + 4) { |
| 2506 | /* integer overflow */ |
| 2507 | PyErr_NoMemory(); |
| 2508 | goto error; |
| 2509 | } |
| 2510 | nallocated += repsize - 4; |
| 2511 | if (result != NULL) { |
| 2512 | if (_PyBytes_Resize(&result, nallocated) < 0) |
| 2513 | goto error; |
| 2514 | } else { |
| 2515 | result = PyBytes_FromStringAndSize(NULL, nallocated); |
| 2516 | if (result == NULL) |
| 2517 | goto error; |
| 2518 | Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset); |
| 2519 | } |
| 2520 | p = PyBytes_AS_STRING(result) + offset; |
| 2521 | } |
| 2522 | |
| 2523 | if (PyBytes_Check(rep)) { |
| 2524 | char *prep = PyBytes_AS_STRING(rep); |
| 2525 | for(k = repsize; k > 0; k--) |
| 2526 | *p++ = *prep++; |
| 2527 | } else /* rep is unicode */ { |
| 2528 | Py_UNICODE *prep = PyUnicode_AS_UNICODE(rep); |
| 2529 | Py_UNICODE c; |
| 2530 | |
| 2531 | for(k=0; k<repsize; k++) { |
| 2532 | c = prep[k]; |
| 2533 | if (0x80 <= c) { |
| 2534 | raise_encode_exception(&exc, "utf-8", s, size, |
| 2535 | i-1, i, "surrogates not allowed"); |
| 2536 | goto error; |
| 2537 | } |
| 2538 | *p++ = (char)prep[k]; |
| 2539 | } |
| 2540 | } |
| 2541 | Py_DECREF(rep); |
Victor Stinner | 0b79b76 | 2010-04-22 20:07:28 +0000 | [diff] [blame] | 2542 | #ifndef Py_UNICODE_WIDE |
Victor Stinner | 158701d | 2010-04-22 19:41:01 +0000 | [diff] [blame] | 2543 | } |
Victor Stinner | 0b79b76 | 2010-04-22 20:07:28 +0000 | [diff] [blame] | 2544 | #endif |
Victor Stinner | 158701d | 2010-04-22 19:41:01 +0000 | [diff] [blame] | 2545 | } else if (ch < 0x10000) { |
| 2546 | *p++ = (char)(0xe0 | (ch >> 12)); |
| 2547 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2548 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2549 | } else /* ch >= 0x10000 */ { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2550 | /* Encode UCS4 Unicode ordinals */ |
| 2551 | *p++ = (char)(0xf0 | (ch >> 18)); |
| 2552 | *p++ = (char)(0x80 | ((ch >> 12) & 0x3f)); |
| 2553 | *p++ = (char)(0x80 | ((ch >> 6) & 0x3f)); |
| 2554 | *p++ = (char)(0x80 | (ch & 0x3f)); |
| 2555 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2556 | } |
Tim Peters | 0eca65c | 2002-04-21 17:28:06 +0000 | [diff] [blame] | 2557 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2558 | if (result == NULL) { |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2559 | /* This was stack allocated. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2560 | nneeded = p - stackbuf; |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2561 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2562 | result = PyBytes_FromStringAndSize(stackbuf, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2563 | } |
| 2564 | else { |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 2565 | /* Cut back to size actually needed. */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2566 | nneeded = p - PyBytes_AS_STRING(result); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2567 | assert(nneeded <= nallocated); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2568 | _PyBytes_Resize(&result, nneeded); |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2569 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2570 | Py_XDECREF(errorHandler); |
| 2571 | Py_XDECREF(exc); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2572 | return result; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 2573 | error: |
| 2574 | Py_XDECREF(errorHandler); |
| 2575 | Py_XDECREF(exc); |
| 2576 | Py_XDECREF(result); |
| 2577 | return NULL; |
Martin v. Löwis | 2a7ff35 | 2002-04-21 09:59:45 +0000 | [diff] [blame] | 2578 | |
Tim Peters | 602f740 | 2002-04-27 18:03:26 +0000 | [diff] [blame] | 2579 | #undef MAX_SHORT_UNICHARS |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2580 | } |
| 2581 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2582 | PyObject *PyUnicode_AsUTF8String(PyObject *unicode) |
| 2583 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2584 | if (!PyUnicode_Check(unicode)) { |
| 2585 | PyErr_BadArgument(); |
| 2586 | return NULL; |
| 2587 | } |
Barry Warsaw | 2dd4abf | 2000-08-18 06:58:15 +0000 | [diff] [blame] | 2588 | return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2589 | PyUnicode_GET_SIZE(unicode), |
| 2590 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2591 | } |
| 2592 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2593 | /* --- UTF-32 Codec ------------------------------------------------------- */ |
| 2594 | |
| 2595 | PyObject * |
| 2596 | PyUnicode_DecodeUTF32(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2597 | Py_ssize_t size, |
| 2598 | const char *errors, |
| 2599 | int *byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2600 | { |
| 2601 | return PyUnicode_DecodeUTF32Stateful(s, size, errors, byteorder, NULL); |
| 2602 | } |
| 2603 | |
| 2604 | PyObject * |
| 2605 | PyUnicode_DecodeUTF32Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2606 | Py_ssize_t size, |
| 2607 | const char *errors, |
| 2608 | int *byteorder, |
| 2609 | Py_ssize_t *consumed) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2610 | { |
| 2611 | const char *starts = s; |
| 2612 | Py_ssize_t startinpos; |
| 2613 | Py_ssize_t endinpos; |
| 2614 | Py_ssize_t outpos; |
| 2615 | PyUnicodeObject *unicode; |
| 2616 | Py_UNICODE *p; |
| 2617 | #ifndef Py_UNICODE_WIDE |
Antoine Pitrou | 6107a68 | 2010-06-11 21:48:34 +0000 | [diff] [blame] | 2618 | int pairs = 0; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2619 | #else |
| 2620 | const int pairs = 0; |
| 2621 | #endif |
Antoine Pitrou | 6107a68 | 2010-06-11 21:48:34 +0000 | [diff] [blame] | 2622 | const unsigned char *q, *e, *qq; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2623 | int bo = 0; /* assume native ordering by default */ |
| 2624 | const char *errmsg = ""; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2625 | /* Offsets from q for retrieving bytes in the right order. */ |
| 2626 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2627 | int iorder[] = {0, 1, 2, 3}; |
| 2628 | #else |
| 2629 | int iorder[] = {3, 2, 1, 0}; |
| 2630 | #endif |
| 2631 | PyObject *errorHandler = NULL; |
| 2632 | PyObject *exc = NULL; |
Antoine Pitrou | 6107a68 | 2010-06-11 21:48:34 +0000 | [diff] [blame] | 2633 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2634 | q = (unsigned char *)s; |
| 2635 | e = q + size; |
| 2636 | |
| 2637 | if (byteorder) |
| 2638 | bo = *byteorder; |
| 2639 | |
| 2640 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2641 | byte order setting accordingly. In native mode, the leading BOM |
| 2642 | mark is skipped, in all other modes, it is copied to the output |
| 2643 | stream as-is (giving a ZWNBSP character). */ |
| 2644 | if (bo == 0) { |
| 2645 | if (size >= 4) { |
| 2646 | const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2647 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2648 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2649 | if (bom == 0x0000FEFF) { |
| 2650 | q += 4; |
| 2651 | bo = -1; |
| 2652 | } |
| 2653 | else if (bom == 0xFFFE0000) { |
| 2654 | q += 4; |
| 2655 | bo = 1; |
| 2656 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2657 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2658 | if (bom == 0x0000FEFF) { |
| 2659 | q += 4; |
| 2660 | bo = 1; |
| 2661 | } |
| 2662 | else if (bom == 0xFFFE0000) { |
| 2663 | q += 4; |
| 2664 | bo = -1; |
| 2665 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2666 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2667 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2668 | } |
| 2669 | |
| 2670 | if (bo == -1) { |
| 2671 | /* force LE */ |
| 2672 | iorder[0] = 0; |
| 2673 | iorder[1] = 1; |
| 2674 | iorder[2] = 2; |
| 2675 | iorder[3] = 3; |
| 2676 | } |
| 2677 | else if (bo == 1) { |
| 2678 | /* force BE */ |
| 2679 | iorder[0] = 3; |
| 2680 | iorder[1] = 2; |
| 2681 | iorder[2] = 1; |
| 2682 | iorder[3] = 0; |
| 2683 | } |
| 2684 | |
Antoine Pitrou | 6107a68 | 2010-06-11 21:48:34 +0000 | [diff] [blame] | 2685 | /* On narrow builds we split characters outside the BMP into two |
| 2686 | codepoints => count how much extra space we need. */ |
| 2687 | #ifndef Py_UNICODE_WIDE |
| 2688 | for (qq = q; qq < e; qq += 4) |
| 2689 | if (qq[iorder[2]] != 0 || qq[iorder[3]] != 0) |
| 2690 | pairs++; |
| 2691 | #endif |
| 2692 | |
| 2693 | /* This might be one to much, because of a BOM */ |
| 2694 | unicode = _PyUnicode_New((size+3)/4+pairs); |
| 2695 | if (!unicode) |
| 2696 | return NULL; |
| 2697 | if (size == 0) |
| 2698 | return (PyObject *)unicode; |
| 2699 | |
| 2700 | /* Unpack UTF-32 encoded data */ |
| 2701 | p = unicode->str; |
| 2702 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2703 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2704 | Py_UCS4 ch; |
| 2705 | /* remaining bytes at the end? (size should be divisible by 4) */ |
| 2706 | if (e-q<4) { |
| 2707 | if (consumed) |
| 2708 | break; |
| 2709 | errmsg = "truncated data"; |
| 2710 | startinpos = ((const char *)q)-starts; |
| 2711 | endinpos = ((const char *)e)-starts; |
| 2712 | goto utf32Error; |
| 2713 | /* The remaining input chars are ignored if the callback |
| 2714 | chooses to skip the input */ |
| 2715 | } |
| 2716 | ch = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) | |
| 2717 | (q[iorder[1]] << 8) | q[iorder[0]]; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2718 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2719 | if (ch >= 0x110000) |
| 2720 | { |
| 2721 | errmsg = "codepoint not in range(0x110000)"; |
| 2722 | startinpos = ((const char *)q)-starts; |
| 2723 | endinpos = startinpos+4; |
| 2724 | goto utf32Error; |
| 2725 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2726 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2727 | if (ch >= 0x10000) |
| 2728 | { |
| 2729 | *p++ = 0xD800 | ((ch-0x10000) >> 10); |
| 2730 | *p++ = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 2731 | } |
| 2732 | else |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2733 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2734 | *p++ = ch; |
| 2735 | q += 4; |
| 2736 | continue; |
| 2737 | utf32Error: |
| 2738 | outpos = p-PyUnicode_AS_UNICODE(unicode); |
| 2739 | if (unicode_decode_call_errorhandler( |
| 2740 | errors, &errorHandler, |
| 2741 | "utf32", errmsg, |
| 2742 | &starts, (const char **)&e, &startinpos, &endinpos, &exc, (const char **)&q, |
| 2743 | &unicode, &outpos, &p)) |
| 2744 | goto onError; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2745 | } |
| 2746 | |
| 2747 | if (byteorder) |
| 2748 | *byteorder = bo; |
| 2749 | |
| 2750 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2751 | *consumed = (const char *)q-starts; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2752 | |
| 2753 | /* Adjust length */ |
| 2754 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
| 2755 | goto onError; |
| 2756 | |
| 2757 | Py_XDECREF(errorHandler); |
| 2758 | Py_XDECREF(exc); |
| 2759 | return (PyObject *)unicode; |
| 2760 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2761 | onError: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2762 | Py_DECREF(unicode); |
| 2763 | Py_XDECREF(errorHandler); |
| 2764 | Py_XDECREF(exc); |
| 2765 | return NULL; |
| 2766 | } |
| 2767 | |
| 2768 | PyObject * |
| 2769 | PyUnicode_EncodeUTF32(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2770 | Py_ssize_t size, |
| 2771 | const char *errors, |
| 2772 | int byteorder) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2773 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2774 | PyObject *v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2775 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2776 | Py_ssize_t nsize, bytesize; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2777 | #ifndef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2778 | Py_ssize_t i, pairs; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2779 | #else |
| 2780 | const int pairs = 0; |
| 2781 | #endif |
| 2782 | /* Offsets from p for storing byte pairs in the right order. */ |
| 2783 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2784 | int iorder[] = {0, 1, 2, 3}; |
| 2785 | #else |
| 2786 | int iorder[] = {3, 2, 1, 0}; |
| 2787 | #endif |
| 2788 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2789 | #define STORECHAR(CH) \ |
| 2790 | do { \ |
| 2791 | p[iorder[3]] = ((CH) >> 24) & 0xff; \ |
| 2792 | p[iorder[2]] = ((CH) >> 16) & 0xff; \ |
| 2793 | p[iorder[1]] = ((CH) >> 8) & 0xff; \ |
| 2794 | p[iorder[0]] = (CH) & 0xff; \ |
| 2795 | p += 4; \ |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2796 | } while(0) |
| 2797 | |
| 2798 | /* In narrow builds we can output surrogate pairs as one codepoint, |
| 2799 | so we need less space. */ |
| 2800 | #ifndef Py_UNICODE_WIDE |
| 2801 | for (i = pairs = 0; i < size-1; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2802 | if (0xD800 <= s[i] && s[i] <= 0xDBFF && |
| 2803 | 0xDC00 <= s[i+1] && s[i+1] <= 0xDFFF) |
| 2804 | pairs++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2805 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 2806 | nsize = (size - pairs + (byteorder == 0)); |
| 2807 | bytesize = nsize * 4; |
| 2808 | if (bytesize / 4 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2809 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2810 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2811 | if (v == NULL) |
| 2812 | return NULL; |
| 2813 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2814 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2815 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2816 | STORECHAR(0xFEFF); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2817 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2818 | goto done; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2819 | |
| 2820 | if (byteorder == -1) { |
| 2821 | /* force LE */ |
| 2822 | iorder[0] = 0; |
| 2823 | iorder[1] = 1; |
| 2824 | iorder[2] = 2; |
| 2825 | iorder[3] = 3; |
| 2826 | } |
| 2827 | else if (byteorder == 1) { |
| 2828 | /* force BE */ |
| 2829 | iorder[0] = 3; |
| 2830 | iorder[1] = 2; |
| 2831 | iorder[2] = 1; |
| 2832 | iorder[3] = 0; |
| 2833 | } |
| 2834 | |
| 2835 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2836 | Py_UCS4 ch = *s++; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2837 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2838 | if (0xD800 <= ch && ch <= 0xDBFF && size > 0) { |
| 2839 | Py_UCS4 ch2 = *s; |
| 2840 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
| 2841 | ch = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
| 2842 | s++; |
| 2843 | size--; |
| 2844 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 2845 | } |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2846 | #endif |
| 2847 | STORECHAR(ch); |
| 2848 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2849 | |
| 2850 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 2851 | return v; |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2852 | #undef STORECHAR |
| 2853 | } |
| 2854 | |
| 2855 | PyObject *PyUnicode_AsUTF32String(PyObject *unicode) |
| 2856 | { |
| 2857 | if (!PyUnicode_Check(unicode)) { |
| 2858 | PyErr_BadArgument(); |
| 2859 | return NULL; |
| 2860 | } |
| 2861 | return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2862 | PyUnicode_GET_SIZE(unicode), |
| 2863 | NULL, |
| 2864 | 0); |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 2865 | } |
| 2866 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2867 | /* --- UTF-16 Codec ------------------------------------------------------- */ |
| 2868 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2869 | PyObject * |
| 2870 | PyUnicode_DecodeUTF16(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2871 | Py_ssize_t size, |
| 2872 | const char *errors, |
| 2873 | int *byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2874 | { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2875 | return PyUnicode_DecodeUTF16Stateful(s, size, errors, byteorder, NULL); |
| 2876 | } |
| 2877 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2878 | /* Two masks for fast checking of whether a C 'long' may contain |
| 2879 | UTF16-encoded surrogate characters. This is an efficient heuristic, |
| 2880 | assuming that non-surrogate characters with a code point >= 0x8000 are |
| 2881 | rare in most input. |
| 2882 | FAST_CHAR_MASK is used when the input is in native byte ordering, |
| 2883 | SWAPPED_FAST_CHAR_MASK when the input is in byteswapped ordering. |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2884 | */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2885 | #if (SIZEOF_LONG == 8) |
| 2886 | # define FAST_CHAR_MASK 0x8000800080008000L |
| 2887 | # define SWAPPED_FAST_CHAR_MASK 0x0080008000800080L |
| 2888 | #elif (SIZEOF_LONG == 4) |
| 2889 | # define FAST_CHAR_MASK 0x80008000L |
| 2890 | # define SWAPPED_FAST_CHAR_MASK 0x00800080L |
| 2891 | #else |
| 2892 | # error C 'long' size should be either 4 or 8! |
| 2893 | #endif |
| 2894 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2895 | PyObject * |
| 2896 | PyUnicode_DecodeUTF16Stateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2897 | Py_ssize_t size, |
| 2898 | const char *errors, |
| 2899 | int *byteorder, |
| 2900 | Py_ssize_t *consumed) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2901 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2902 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2903 | Py_ssize_t startinpos; |
| 2904 | Py_ssize_t endinpos; |
| 2905 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2906 | PyUnicodeObject *unicode; |
| 2907 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2908 | const unsigned char *q, *e, *aligned_end; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2909 | int bo = 0; /* assume native ordering by default */ |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2910 | int native_ordering = 0; |
Marc-André Lemburg | 9542f48 | 2000-07-17 18:23:13 +0000 | [diff] [blame] | 2911 | const char *errmsg = ""; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2912 | /* Offsets from q for retrieving byte pairs in the right order. */ |
| 2913 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2914 | int ihi = 1, ilo = 0; |
| 2915 | #else |
| 2916 | int ihi = 0, ilo = 1; |
| 2917 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2918 | PyObject *errorHandler = NULL; |
| 2919 | PyObject *exc = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2920 | |
| 2921 | /* Note: size will always be longer than the resulting Unicode |
| 2922 | character count */ |
| 2923 | unicode = _PyUnicode_New(size); |
| 2924 | if (!unicode) |
| 2925 | return NULL; |
| 2926 | if (size == 0) |
| 2927 | return (PyObject *)unicode; |
| 2928 | |
| 2929 | /* Unpack UTF-16 encoded data */ |
| 2930 | p = unicode->str; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2931 | q = (unsigned char *)s; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2932 | e = q + size - 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2933 | |
| 2934 | if (byteorder) |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2935 | bo = *byteorder; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2936 | |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2937 | /* Check for BOM marks (U+FEFF) in the input and adjust current |
| 2938 | byte order setting accordingly. In native mode, the leading BOM |
| 2939 | mark is skipped, in all other modes, it is copied to the output |
| 2940 | stream as-is (giving a ZWNBSP character). */ |
| 2941 | if (bo == 0) { |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 2942 | if (size >= 2) { |
| 2943 | const Py_UNICODE bom = (q[ihi] << 8) | q[ilo]; |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2944 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2945 | if (bom == 0xFEFF) { |
| 2946 | q += 2; |
| 2947 | bo = -1; |
| 2948 | } |
| 2949 | else if (bom == 0xFFFE) { |
| 2950 | q += 2; |
| 2951 | bo = 1; |
| 2952 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 2953 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2954 | if (bom == 0xFEFF) { |
| 2955 | q += 2; |
| 2956 | bo = 1; |
| 2957 | } |
| 2958 | else if (bom == 0xFFFE) { |
| 2959 | q += 2; |
| 2960 | bo = -1; |
| 2961 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2962 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2963 | } |
Marc-André Lemburg | 489b56e | 2001-05-21 20:30:15 +0000 | [diff] [blame] | 2964 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 2965 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2966 | if (bo == -1) { |
| 2967 | /* force LE */ |
| 2968 | ihi = 1; |
| 2969 | ilo = 0; |
| 2970 | } |
| 2971 | else if (bo == 1) { |
| 2972 | /* force BE */ |
| 2973 | ihi = 0; |
| 2974 | ilo = 1; |
| 2975 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2976 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 2977 | native_ordering = ilo < ihi; |
| 2978 | #else |
| 2979 | native_ordering = ilo > ihi; |
| 2980 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2981 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2982 | aligned_end = (const unsigned char *) ((size_t) e & ~LONG_PTR_MASK); |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 2983 | while (q < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 2984 | Py_UNICODE ch; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 2985 | /* First check for possible aligned read of a C 'long'. Unaligned |
| 2986 | reads are more expensive, better to defer to another iteration. */ |
| 2987 | if (!((size_t) q & LONG_PTR_MASK)) { |
| 2988 | /* Fast path for runs of non-surrogate chars. */ |
| 2989 | register const unsigned char *_q = q; |
| 2990 | Py_UNICODE *_p = p; |
| 2991 | if (native_ordering) { |
| 2992 | /* Native ordering is simple: as long as the input cannot |
| 2993 | possibly contain a surrogate char, do an unrolled copy |
| 2994 | of several 16-bit code points to the target object. |
| 2995 | The non-surrogate check is done on several input bytes |
| 2996 | at a time (as many as a C 'long' can contain). */ |
| 2997 | while (_q < aligned_end) { |
| 2998 | unsigned long data = * (unsigned long *) _q; |
| 2999 | if (data & FAST_CHAR_MASK) |
| 3000 | break; |
| 3001 | _p[0] = ((unsigned short *) _q)[0]; |
| 3002 | _p[1] = ((unsigned short *) _q)[1]; |
| 3003 | #if (SIZEOF_LONG == 8) |
| 3004 | _p[2] = ((unsigned short *) _q)[2]; |
| 3005 | _p[3] = ((unsigned short *) _q)[3]; |
| 3006 | #endif |
| 3007 | _q += SIZEOF_LONG; |
| 3008 | _p += SIZEOF_LONG / 2; |
| 3009 | } |
| 3010 | } |
| 3011 | else { |
| 3012 | /* Byteswapped ordering is similar, but we must decompose |
| 3013 | the copy bytewise, and take care of zero'ing out the |
| 3014 | upper bytes if the target object is in 32-bit units |
| 3015 | (that is, in UCS-4 builds). */ |
| 3016 | while (_q < aligned_end) { |
| 3017 | unsigned long data = * (unsigned long *) _q; |
| 3018 | if (data & SWAPPED_FAST_CHAR_MASK) |
| 3019 | break; |
| 3020 | /* Zero upper bytes in UCS-4 builds */ |
| 3021 | #if (Py_UNICODE_SIZE > 2) |
| 3022 | _p[0] = 0; |
| 3023 | _p[1] = 0; |
| 3024 | #if (SIZEOF_LONG == 8) |
| 3025 | _p[2] = 0; |
| 3026 | _p[3] = 0; |
| 3027 | #endif |
| 3028 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 3029 | /* Issue #4916; UCS-4 builds on big endian machines must |
| 3030 | fill the two last bytes of each 4-byte unit. */ |
| 3031 | #if (!defined(BYTEORDER_IS_LITTLE_ENDIAN) && Py_UNICODE_SIZE > 2) |
| 3032 | # define OFF 2 |
| 3033 | #else |
| 3034 | # define OFF 0 |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3035 | #endif |
Antoine Pitrou | d6e8de1 | 2009-01-11 23:56:55 +0000 | [diff] [blame] | 3036 | ((unsigned char *) _p)[OFF + 1] = _q[0]; |
| 3037 | ((unsigned char *) _p)[OFF + 0] = _q[1]; |
| 3038 | ((unsigned char *) _p)[OFF + 1 + Py_UNICODE_SIZE] = _q[2]; |
| 3039 | ((unsigned char *) _p)[OFF + 0 + Py_UNICODE_SIZE] = _q[3]; |
| 3040 | #if (SIZEOF_LONG == 8) |
| 3041 | ((unsigned char *) _p)[OFF + 1 + 2 * Py_UNICODE_SIZE] = _q[4]; |
| 3042 | ((unsigned char *) _p)[OFF + 0 + 2 * Py_UNICODE_SIZE] = _q[5]; |
| 3043 | ((unsigned char *) _p)[OFF + 1 + 3 * Py_UNICODE_SIZE] = _q[6]; |
| 3044 | ((unsigned char *) _p)[OFF + 0 + 3 * Py_UNICODE_SIZE] = _q[7]; |
| 3045 | #endif |
| 3046 | #undef OFF |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3047 | _q += SIZEOF_LONG; |
| 3048 | _p += SIZEOF_LONG / 2; |
| 3049 | } |
| 3050 | } |
| 3051 | p = _p; |
| 3052 | q = _q; |
| 3053 | if (q >= e) |
| 3054 | break; |
| 3055 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3056 | ch = (q[ihi] << 8) | q[ilo]; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3057 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3058 | q += 2; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3059 | |
| 3060 | if (ch < 0xD800 || ch > 0xDFFF) { |
| 3061 | *p++ = ch; |
| 3062 | continue; |
| 3063 | } |
| 3064 | |
| 3065 | /* UTF-16 code pair: */ |
| 3066 | if (q > e) { |
| 3067 | errmsg = "unexpected end of data"; |
| 3068 | startinpos = (((const char *)q) - 2) - starts; |
| 3069 | endinpos = ((const char *)e) + 1 - starts; |
| 3070 | goto utf16Error; |
| 3071 | } |
| 3072 | if (0xD800 <= ch && ch <= 0xDBFF) { |
| 3073 | Py_UNICODE ch2 = (q[ihi] << 8) | q[ilo]; |
| 3074 | q += 2; |
| 3075 | if (0xDC00 <= ch2 && ch2 <= 0xDFFF) { |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 3076 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3077 | *p++ = ch; |
| 3078 | *p++ = ch2; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3079 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3080 | *p++ = (((ch & 0x3FF)<<10) | (ch2 & 0x3FF)) + 0x10000; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3081 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3082 | continue; |
| 3083 | } |
| 3084 | else { |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3085 | errmsg = "illegal UTF-16 surrogate"; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3086 | startinpos = (((const char *)q)-4)-starts; |
| 3087 | endinpos = startinpos+2; |
| 3088 | goto utf16Error; |
| 3089 | } |
| 3090 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3091 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3092 | errmsg = "illegal encoding"; |
| 3093 | startinpos = (((const char *)q)-2)-starts; |
| 3094 | endinpos = startinpos+2; |
| 3095 | /* Fall through to report the error */ |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3096 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3097 | utf16Error: |
| 3098 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 3099 | if (unicode_decode_call_errorhandler( |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3100 | errors, |
| 3101 | &errorHandler, |
| 3102 | "utf16", errmsg, |
| 3103 | &starts, |
| 3104 | (const char **)&e, |
| 3105 | &startinpos, |
| 3106 | &endinpos, |
| 3107 | &exc, |
| 3108 | (const char **)&q, |
| 3109 | &unicode, |
| 3110 | &outpos, |
| 3111 | &p)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3112 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3113 | } |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3114 | /* remaining byte at the end? (size should be even) */ |
| 3115 | if (e == q) { |
| 3116 | if (!consumed) { |
| 3117 | errmsg = "truncated data"; |
| 3118 | startinpos = ((const char *)q) - starts; |
| 3119 | endinpos = ((const char *)e) + 1 - starts; |
| 3120 | outpos = p - PyUnicode_AS_UNICODE(unicode); |
| 3121 | if (unicode_decode_call_errorhandler( |
| 3122 | errors, |
| 3123 | &errorHandler, |
| 3124 | "utf16", errmsg, |
| 3125 | &starts, |
| 3126 | (const char **)&e, |
| 3127 | &startinpos, |
| 3128 | &endinpos, |
| 3129 | &exc, |
| 3130 | (const char **)&q, |
| 3131 | &unicode, |
| 3132 | &outpos, |
| 3133 | &p)) |
| 3134 | goto onError; |
| 3135 | /* The remaining input chars are ignored if the callback |
| 3136 | chooses to skip the input */ |
| 3137 | } |
| 3138 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3139 | |
| 3140 | if (byteorder) |
| 3141 | *byteorder = bo; |
| 3142 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3143 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3144 | *consumed = (const char *)q-starts; |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 3145 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3146 | /* Adjust length */ |
Jeremy Hylton | deb2dc6 | 2003-09-16 03:41:45 +0000 | [diff] [blame] | 3147 | if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3148 | goto onError; |
| 3149 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3150 | Py_XDECREF(errorHandler); |
| 3151 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3152 | return (PyObject *)unicode; |
| 3153 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3154 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3155 | Py_DECREF(unicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3156 | Py_XDECREF(errorHandler); |
| 3157 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3158 | return NULL; |
| 3159 | } |
| 3160 | |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3161 | #undef FAST_CHAR_MASK |
| 3162 | #undef SWAPPED_FAST_CHAR_MASK |
| 3163 | |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3164 | PyObject * |
| 3165 | PyUnicode_EncodeUTF16(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3166 | Py_ssize_t size, |
| 3167 | const char *errors, |
| 3168 | int byteorder) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3169 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3170 | PyObject *v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3171 | unsigned char *p; |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3172 | Py_ssize_t nsize, bytesize; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3173 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3174 | Py_ssize_t i, pairs; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3175 | #else |
| 3176 | const int pairs = 0; |
| 3177 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3178 | /* Offsets from p for storing byte pairs in the right order. */ |
| 3179 | #ifdef BYTEORDER_IS_LITTLE_ENDIAN |
| 3180 | int ihi = 1, ilo = 0; |
| 3181 | #else |
| 3182 | int ihi = 0, ilo = 1; |
| 3183 | #endif |
| 3184 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3185 | #define STORECHAR(CH) \ |
| 3186 | do { \ |
| 3187 | p[ihi] = ((CH) >> 8) & 0xff; \ |
| 3188 | p[ilo] = (CH) & 0xff; \ |
| 3189 | p += 2; \ |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3190 | } while(0) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3191 | |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3192 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3193 | for (i = pairs = 0; i < size; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3194 | if (s[i] >= 0x10000) |
| 3195 | pairs++; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3196 | #endif |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3197 | /* 2 * (size + pairs + (byteorder == 0)) */ |
| 3198 | if (size > PY_SSIZE_T_MAX || |
| 3199 | size > PY_SSIZE_T_MAX - pairs - (byteorder == 0)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3200 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3201 | nsize = size + pairs + (byteorder == 0); |
| 3202 | bytesize = nsize * 2; |
| 3203 | if (bytesize / 2 != nsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3204 | return PyErr_NoMemory(); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3205 | v = PyBytes_FromStringAndSize(NULL, bytesize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3206 | if (v == NULL) |
| 3207 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3208 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3209 | p = (unsigned char *)PyBytes_AS_STRING(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3210 | if (byteorder == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3211 | STORECHAR(0xFEFF); |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 3212 | if (size == 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3213 | goto done; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3214 | |
| 3215 | if (byteorder == -1) { |
| 3216 | /* force LE */ |
| 3217 | ihi = 1; |
| 3218 | ilo = 0; |
| 3219 | } |
| 3220 | else if (byteorder == 1) { |
| 3221 | /* force BE */ |
| 3222 | ihi = 0; |
| 3223 | ilo = 1; |
| 3224 | } |
| 3225 | |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3226 | while (size-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3227 | Py_UNICODE ch = *s++; |
| 3228 | Py_UNICODE ch2 = 0; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3229 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3230 | if (ch >= 0x10000) { |
| 3231 | ch2 = 0xDC00 | ((ch-0x10000) & 0x3FF); |
| 3232 | ch = 0xD800 | ((ch-0x10000) >> 10); |
| 3233 | } |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3234 | #endif |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3235 | STORECHAR(ch); |
| 3236 | if (ch2) |
| 3237 | STORECHAR(ch2); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3238 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3239 | |
| 3240 | done: |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3241 | return v; |
Tim Peters | 772747b | 2001-08-09 22:21:55 +0000 | [diff] [blame] | 3242 | #undef STORECHAR |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3243 | } |
| 3244 | |
| 3245 | PyObject *PyUnicode_AsUTF16String(PyObject *unicode) |
| 3246 | { |
| 3247 | if (!PyUnicode_Check(unicode)) { |
| 3248 | PyErr_BadArgument(); |
| 3249 | return NULL; |
| 3250 | } |
| 3251 | return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3252 | PyUnicode_GET_SIZE(unicode), |
| 3253 | NULL, |
| 3254 | 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3255 | } |
| 3256 | |
| 3257 | /* --- Unicode Escape Codec ----------------------------------------------- */ |
| 3258 | |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 3259 | static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; |
Marc-André Lemburg | 0f774e3 | 2000-06-28 16:43:35 +0000 | [diff] [blame] | 3260 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3261 | PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3262 | Py_ssize_t size, |
| 3263 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3264 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3265 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3266 | Py_ssize_t startinpos; |
| 3267 | Py_ssize_t endinpos; |
| 3268 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3269 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3270 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3271 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3272 | const char *end; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3273 | char* message; |
| 3274 | Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3275 | PyObject *errorHandler = NULL; |
| 3276 | PyObject *exc = NULL; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +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. |
| 3281 | (but if the error callback returns a long replacement string |
| 3282 | we'll have to allocate more space) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3283 | v = _PyUnicode_New(size); |
| 3284 | if (v == NULL) |
| 3285 | goto onError; |
| 3286 | if (size == 0) |
| 3287 | return (PyObject *)v; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3288 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3289 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3290 | end = s + size; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3291 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3292 | while (s < end) { |
| 3293 | unsigned char c; |
Marc-André Lemburg | 063e0cb | 2000-07-07 11:27:45 +0000 | [diff] [blame] | 3294 | Py_UNICODE x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3295 | int digits; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3296 | |
| 3297 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3298 | if (*s != '\\') { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3299 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3300 | continue; |
| 3301 | } |
| 3302 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3303 | startinpos = s-starts; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3304 | /* \ - Escapes */ |
| 3305 | s++; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3306 | c = *s++; |
| 3307 | if (s > end) |
| 3308 | c = '\0'; /* Invalid after \ */ |
| 3309 | switch (c) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3310 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3311 | /* \x escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3312 | case '\n': break; |
| 3313 | case '\\': *p++ = '\\'; break; |
| 3314 | case '\'': *p++ = '\''; break; |
| 3315 | case '\"': *p++ = '\"'; break; |
| 3316 | case 'b': *p++ = '\b'; break; |
| 3317 | case 'f': *p++ = '\014'; break; /* FF */ |
| 3318 | case 't': *p++ = '\t'; break; |
| 3319 | case 'n': *p++ = '\n'; break; |
| 3320 | case 'r': *p++ = '\r'; break; |
| 3321 | case 'v': *p++ = '\013'; break; /* VT */ |
| 3322 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 3323 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3324 | /* \OOO (octal) escapes */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3325 | case '0': case '1': case '2': case '3': |
| 3326 | case '4': case '5': case '6': case '7': |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3327 | x = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3328 | if (s < end && '0' <= *s && *s <= '7') { |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3329 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3330 | if (s < end && '0' <= *s && *s <= '7') |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3331 | x = (x<<3) + *s++ - '0'; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3332 | } |
Guido van Rossum | 0e4f657 | 2000-05-01 21:27:20 +0000 | [diff] [blame] | 3333 | *p++ = x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3334 | break; |
| 3335 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3336 | /* hex escapes */ |
| 3337 | /* \xXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3338 | case 'x': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3339 | digits = 2; |
| 3340 | message = "truncated \\xXX escape"; |
| 3341 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3342 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3343 | /* \uXXXX */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3344 | case 'u': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3345 | digits = 4; |
| 3346 | message = "truncated \\uXXXX escape"; |
| 3347 | goto hexescape; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3348 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3349 | /* \UXXXXXXXX */ |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3350 | case 'U': |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3351 | digits = 8; |
| 3352 | message = "truncated \\UXXXXXXXX escape"; |
| 3353 | hexescape: |
| 3354 | chr = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3355 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3356 | if (s+digits>end) { |
| 3357 | endinpos = size; |
| 3358 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3359 | errors, &errorHandler, |
| 3360 | "unicodeescape", "end of string in escape sequence", |
| 3361 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3362 | &v, &outpos, &p)) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3363 | goto onError; |
| 3364 | goto nextByte; |
| 3365 | } |
| 3366 | for (i = 0; i < digits; ++i) { |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3367 | c = (unsigned char) s[i]; |
Guido van Rossum | daa251c | 2007-10-25 23:47:33 +0000 | [diff] [blame] | 3368 | if (!ISXDIGIT(c)) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3369 | endinpos = (s+i+1)-starts; |
| 3370 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3371 | errors, &errorHandler, |
| 3372 | "unicodeescape", message, |
| 3373 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3374 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3375 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3376 | goto nextByte; |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3377 | } |
| 3378 | chr = (chr<<4) & ~0xF; |
| 3379 | if (c >= '0' && c <= '9') |
| 3380 | chr += c - '0'; |
| 3381 | else if (c >= 'a' && c <= 'f') |
| 3382 | chr += 10 + c - 'a'; |
| 3383 | else |
| 3384 | chr += 10 + c - 'A'; |
| 3385 | } |
| 3386 | s += i; |
Jeremy Hylton | 504de6b | 2003-10-06 05:08:26 +0000 | [diff] [blame] | 3387 | if (chr == 0xffffffff && PyErr_Occurred()) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3388 | /* _decoding_error will have already written into the |
| 3389 | target buffer. */ |
| 3390 | break; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3391 | store: |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3392 | /* when we get here, chr is a 32-bit unicode character */ |
| 3393 | if (chr <= 0xffff) |
| 3394 | /* UCS-2 character */ |
| 3395 | *p++ = (Py_UNICODE) chr; |
| 3396 | else if (chr <= 0x10ffff) { |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3397 | /* UCS-4 character. Either store directly, or as |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3398 | surrogate pair. */ |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 3399 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3400 | *p++ = chr; |
| 3401 | #else |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3402 | chr -= 0x10000L; |
| 3403 | *p++ = 0xD800 + (Py_UNICODE) (chr >> 10); |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 3404 | *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF); |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3405 | #endif |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3406 | } else { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3407 | endinpos = s-starts; |
| 3408 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3409 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3410 | errors, &errorHandler, |
| 3411 | "unicodeescape", "illegal Unicode character", |
| 3412 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3413 | &v, &outpos, &p)) |
Fredrik Lundh | df84675 | 2000-09-03 11:29:49 +0000 | [diff] [blame] | 3414 | goto onError; |
| 3415 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3416 | break; |
| 3417 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3418 | /* \N{name} */ |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3419 | case 'N': |
| 3420 | message = "malformed \\N character escape"; |
| 3421 | if (ucnhash_CAPI == NULL) { |
| 3422 | /* load the unicode data module */ |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3423 | ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import(PyUnicodeData_CAPSULE_NAME, 1); |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3424 | if (ucnhash_CAPI == NULL) |
| 3425 | goto ucnhashError; |
| 3426 | } |
| 3427 | if (*s == '{') { |
| 3428 | const char *start = s+1; |
| 3429 | /* look for the closing brace */ |
| 3430 | while (*s != '}' && s < end) |
| 3431 | s++; |
| 3432 | if (s > start && s < end && *s == '}') { |
| 3433 | /* found a name. look it up in the unicode database */ |
| 3434 | message = "unknown Unicode character name"; |
| 3435 | s++; |
Martin v. Löwis | 480f1bb | 2006-03-09 23:38:20 +0000 | [diff] [blame] | 3436 | if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3437 | goto store; |
| 3438 | } |
| 3439 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3440 | endinpos = s-starts; |
| 3441 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3442 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3443 | errors, &errorHandler, |
| 3444 | "unicodeescape", message, |
| 3445 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3446 | &v, &outpos, &p)) |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3447 | goto onError; |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3448 | break; |
| 3449 | |
| 3450 | default: |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3451 | if (s > end) { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3452 | message = "\\ at end of string"; |
| 3453 | s--; |
| 3454 | endinpos = s-starts; |
| 3455 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3456 | if (unicode_decode_call_errorhandler( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3457 | errors, &errorHandler, |
| 3458 | "unicodeescape", message, |
| 3459 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3460 | &v, &outpos, &p)) |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3461 | goto onError; |
| 3462 | } |
| 3463 | else { |
| 3464 | *p++ = '\\'; |
| 3465 | *p++ = (unsigned char)s[-1]; |
| 3466 | } |
Fredrik Lundh | ccc7473 | 2001-02-18 22:13:49 +0000 | [diff] [blame] | 3467 | break; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3468 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3469 | nextByte: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3470 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3471 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3472 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3473 | goto onError; |
Walter Dörwald | d4ade08 | 2003-08-15 15:00:26 +0000 | [diff] [blame] | 3474 | Py_XDECREF(errorHandler); |
| 3475 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3476 | return (PyObject *)v; |
Walter Dörwald | 8c07722 | 2002-03-25 11:16:18 +0000 | [diff] [blame] | 3477 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3478 | ucnhashError: |
Fredrik Lundh | 06d1268 | 2001-01-24 07:59:11 +0000 | [diff] [blame] | 3479 | PyErr_SetString( |
| 3480 | PyExc_UnicodeError, |
| 3481 | "\\N escapes not supported (can't load unicodedata module)" |
| 3482 | ); |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3483 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3484 | Py_XDECREF(errorHandler); |
| 3485 | Py_XDECREF(exc); |
Fredrik Lundh | f605606 | 2001-01-20 11:15:25 +0000 | [diff] [blame] | 3486 | return NULL; |
| 3487 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3488 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3489 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3490 | Py_XDECREF(errorHandler); |
| 3491 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3492 | return NULL; |
| 3493 | } |
| 3494 | |
| 3495 | /* Return a Unicode-Escape string version of the Unicode object. |
| 3496 | |
| 3497 | If quotes is true, the string is enclosed in u"" or u'' quotes as |
| 3498 | appropriate. |
| 3499 | |
| 3500 | */ |
| 3501 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3502 | Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3503 | Py_ssize_t size, |
| 3504 | Py_UNICODE ch) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3505 | { |
| 3506 | /* like wcschr, but doesn't stop at NULL characters */ |
| 3507 | |
| 3508 | while (size-- > 0) { |
| 3509 | if (*s == ch) |
| 3510 | return s; |
| 3511 | s++; |
| 3512 | } |
| 3513 | |
| 3514 | return NULL; |
| 3515 | } |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 3516 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3517 | static const char *hexdigits = "0123456789abcdef"; |
| 3518 | |
| 3519 | PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3520 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3521 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3522 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3523 | char *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3524 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3525 | #ifdef Py_UNICODE_WIDE |
| 3526 | const Py_ssize_t expandsize = 10; |
| 3527 | #else |
| 3528 | const Py_ssize_t expandsize = 6; |
| 3529 | #endif |
| 3530 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3531 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 3532 | better to choose a different scheme. Perhaps scan the |
| 3533 | first N-chars of the string and allocate based on that size. |
| 3534 | */ |
| 3535 | /* Initial allocation is based on the longest-possible unichr |
| 3536 | escape. |
| 3537 | |
| 3538 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 3539 | unichr, so in this case it's the longest unichr escape. In |
| 3540 | narrow (UTF-16) builds this is five chars per source unichr |
| 3541 | since there are two unichrs in the surrogate pair, so in narrow |
| 3542 | (UTF-16) builds it's not the longest unichr escape. |
| 3543 | |
| 3544 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 3545 | so in the narrow (UTF-16) build case it's the longest unichr |
| 3546 | escape. |
| 3547 | */ |
| 3548 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3549 | if (size == 0) |
| 3550 | return PyBytes_FromStringAndSize(NULL, 0); |
| 3551 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3552 | if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3553 | return PyErr_NoMemory(); |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3554 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3555 | repr = PyBytes_FromStringAndSize(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3556 | 2 |
| 3557 | + expandsize*size |
| 3558 | + 1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3559 | if (repr == NULL) |
| 3560 | return NULL; |
| 3561 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3562 | p = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3563 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3564 | while (size-- > 0) { |
| 3565 | Py_UNICODE ch = *s++; |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3566 | |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3567 | /* Escape backslashes */ |
| 3568 | if (ch == '\\') { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3569 | *p++ = '\\'; |
| 3570 | *p++ = (char) ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3571 | continue; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3572 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3573 | |
Guido van Rossum | 0d42e0c | 2001-07-20 16:36:21 +0000 | [diff] [blame] | 3574 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3575 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 3576 | else if (ch >= 0x10000) { |
| 3577 | *p++ = '\\'; |
| 3578 | *p++ = 'U'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3579 | *p++ = hexdigits[(ch >> 28) & 0x0000000F]; |
| 3580 | *p++ = hexdigits[(ch >> 24) & 0x0000000F]; |
| 3581 | *p++ = hexdigits[(ch >> 20) & 0x0000000F]; |
| 3582 | *p++ = hexdigits[(ch >> 16) & 0x0000000F]; |
| 3583 | *p++ = hexdigits[(ch >> 12) & 0x0000000F]; |
| 3584 | *p++ = hexdigits[(ch >> 8) & 0x0000000F]; |
| 3585 | *p++ = hexdigits[(ch >> 4) & 0x0000000F]; |
| 3586 | *p++ = hexdigits[ch & 0x0000000F]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3587 | continue; |
Martin v. Löwis | 0ba70cc | 2001-06-26 22:22:37 +0000 | [diff] [blame] | 3588 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3589 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3590 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 3591 | else if (ch >= 0xD800 && ch < 0xDC00) { |
| 3592 | Py_UNICODE ch2; |
| 3593 | Py_UCS4 ucs; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3594 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3595 | ch2 = *s++; |
| 3596 | size--; |
Georg Brandl | 0f14709 | 2010-08-01 20:54:22 +0000 | [diff] [blame] | 3597 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3598 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 3599 | *p++ = '\\'; |
| 3600 | *p++ = 'U'; |
| 3601 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 3602 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 3603 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 3604 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 3605 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 3606 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 3607 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 3608 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 3609 | continue; |
| 3610 | } |
| 3611 | /* Fall through: isolated surrogates are copied as-is */ |
| 3612 | s--; |
| 3613 | size++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3614 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3615 | #endif |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3616 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3617 | /* Map 16-bit characters to '\uxxxx' */ |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 3618 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3619 | *p++ = '\\'; |
| 3620 | *p++ = 'u'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3621 | *p++ = hexdigits[(ch >> 12) & 0x000F]; |
| 3622 | *p++ = hexdigits[(ch >> 8) & 0x000F]; |
| 3623 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 3624 | *p++ = hexdigits[ch & 0x000F]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3625 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3626 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3627 | /* Map special whitespace to '\t', \n', '\r' */ |
| 3628 | else if (ch == '\t') { |
| 3629 | *p++ = '\\'; |
| 3630 | *p++ = 't'; |
| 3631 | } |
| 3632 | else if (ch == '\n') { |
| 3633 | *p++ = '\\'; |
| 3634 | *p++ = 'n'; |
| 3635 | } |
| 3636 | else if (ch == '\r') { |
| 3637 | *p++ = '\\'; |
| 3638 | *p++ = 'r'; |
| 3639 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3640 | |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3641 | /* Map non-printable US ASCII to '\xhh' */ |
Marc-André Lemburg | 11326de | 2001-11-28 12:56:20 +0000 | [diff] [blame] | 3642 | else if (ch < ' ' || ch >= 0x7F) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3643 | *p++ = '\\'; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 3644 | *p++ = 'x'; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3645 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 3646 | *p++ = hexdigits[ch & 0x000F]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3647 | } |
Marc-André Lemburg | 80d1dd5 | 2001-07-25 16:05:59 +0000 | [diff] [blame] | 3648 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3649 | /* Copy everything else as-is */ |
| 3650 | else |
| 3651 | *p++ = (char) ch; |
| 3652 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3653 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3654 | assert(p - PyBytes_AS_STRING(repr) > 0); |
| 3655 | if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) |
| 3656 | return NULL; |
| 3657 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3658 | } |
| 3659 | |
Alexandre Vassalotti | 2056bed | 2008-12-27 19:46:35 +0000 | [diff] [blame] | 3660 | PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3661 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3662 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3663 | if (!PyUnicode_Check(unicode)) { |
| 3664 | PyErr_BadArgument(); |
| 3665 | return NULL; |
| 3666 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 3667 | s = PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3668 | PyUnicode_GET_SIZE(unicode)); |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3669 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3670 | } |
| 3671 | |
| 3672 | /* --- Raw Unicode Escape Codec ------------------------------------------- */ |
| 3673 | |
| 3674 | PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3675 | Py_ssize_t size, |
| 3676 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3677 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3678 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3679 | Py_ssize_t startinpos; |
| 3680 | Py_ssize_t endinpos; |
| 3681 | Py_ssize_t outpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3682 | PyUnicodeObject *v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3683 | Py_UNICODE *p; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3684 | const char *end; |
| 3685 | const char *bs; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3686 | PyObject *errorHandler = NULL; |
| 3687 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3688 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3689 | /* Escaped strings will always be longer than the resulting |
| 3690 | 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] | 3691 | length after conversion to the true value. (But decoding error |
| 3692 | handler might have to resize the string) */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3693 | v = _PyUnicode_New(size); |
| 3694 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3695 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3696 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3697 | return (PyObject *)v; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3698 | p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3699 | end = s + size; |
| 3700 | while (s < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3701 | unsigned char c; |
| 3702 | Py_UCS4 x; |
| 3703 | int i; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3704 | int count; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3705 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3706 | /* Non-escape characters are interpreted as Unicode ordinals */ |
| 3707 | if (*s != '\\') { |
| 3708 | *p++ = (unsigned char)*s++; |
| 3709 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3710 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3711 | startinpos = s-starts; |
| 3712 | |
| 3713 | /* \u-escapes are only interpreted iff the number of leading |
| 3714 | backslashes if odd */ |
| 3715 | bs = s; |
| 3716 | for (;s < end;) { |
| 3717 | if (*s != '\\') |
| 3718 | break; |
| 3719 | *p++ = (unsigned char)*s++; |
| 3720 | } |
| 3721 | if (((s - bs) & 1) == 0 || |
| 3722 | s >= end || |
| 3723 | (*s != 'u' && *s != 'U')) { |
| 3724 | continue; |
| 3725 | } |
| 3726 | p--; |
| 3727 | count = *s=='u' ? 4 : 8; |
| 3728 | s++; |
| 3729 | |
| 3730 | /* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */ |
| 3731 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 3732 | for (x = 0, i = 0; i < count; ++i, ++s) { |
| 3733 | c = (unsigned char)*s; |
| 3734 | if (!ISXDIGIT(c)) { |
| 3735 | endinpos = s-starts; |
| 3736 | if (unicode_decode_call_errorhandler( |
| 3737 | errors, &errorHandler, |
| 3738 | "rawunicodeescape", "truncated \\uXXXX", |
| 3739 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3740 | &v, &outpos, &p)) |
| 3741 | goto onError; |
| 3742 | goto nextByte; |
| 3743 | } |
| 3744 | x = (x<<4) & ~0xF; |
| 3745 | if (c >= '0' && c <= '9') |
| 3746 | x += c - '0'; |
| 3747 | else if (c >= 'a' && c <= 'f') |
| 3748 | x += 10 + c - 'a'; |
| 3749 | else |
| 3750 | x += 10 + c - 'A'; |
| 3751 | } |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3752 | if (x <= 0xffff) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3753 | /* UCS-2 character */ |
| 3754 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3755 | else if (x <= 0x10ffff) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3756 | /* UCS-4 character. Either store directly, or as |
| 3757 | surrogate pair. */ |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3758 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3759 | *p++ = (Py_UNICODE) x; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3760 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3761 | x -= 0x10000L; |
| 3762 | *p++ = 0xD800 + (Py_UNICODE) (x >> 10); |
| 3763 | *p++ = 0xDC00 + (Py_UNICODE) (x & 0x03FF); |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3764 | #endif |
| 3765 | } else { |
| 3766 | endinpos = s-starts; |
| 3767 | outpos = p-PyUnicode_AS_UNICODE(v); |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3768 | if (unicode_decode_call_errorhandler( |
| 3769 | errors, &errorHandler, |
| 3770 | "rawunicodeescape", "\\Uxxxxxxxx out of range", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3771 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
| 3772 | &v, &outpos, &p)) |
| 3773 | goto onError; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3774 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3775 | nextByte: |
| 3776 | ; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3777 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3778 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3779 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3780 | Py_XDECREF(errorHandler); |
| 3781 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3782 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3783 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3784 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3785 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3786 | Py_XDECREF(errorHandler); |
| 3787 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3788 | return NULL; |
| 3789 | } |
| 3790 | |
| 3791 | PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3792 | Py_ssize_t size) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3793 | { |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3794 | PyObject *repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3795 | char *p; |
| 3796 | char *q; |
| 3797 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3798 | #ifdef Py_UNICODE_WIDE |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3799 | const Py_ssize_t expandsize = 10; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3800 | #else |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3801 | const Py_ssize_t expandsize = 6; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3802 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3803 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 3804 | if (size > PY_SSIZE_T_MAX / expandsize) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3805 | return PyErr_NoMemory(); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 3806 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3807 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3808 | if (repr == NULL) |
| 3809 | return NULL; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 3810 | if (size == 0) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3811 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3812 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3813 | p = q = PyBytes_AS_STRING(repr); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3814 | while (size-- > 0) { |
| 3815 | Py_UNICODE ch = *s++; |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3816 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3817 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 3818 | if (ch >= 0x10000) { |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3819 | *p++ = '\\'; |
| 3820 | *p++ = 'U'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 3821 | *p++ = hexdigits[(ch >> 28) & 0xf]; |
| 3822 | *p++ = hexdigits[(ch >> 24) & 0xf]; |
| 3823 | *p++ = hexdigits[(ch >> 20) & 0xf]; |
| 3824 | *p++ = hexdigits[(ch >> 16) & 0xf]; |
| 3825 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 3826 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 3827 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 3828 | *p++ = hexdigits[ch & 15]; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3829 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3830 | else |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3831 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3832 | /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */ |
| 3833 | if (ch >= 0xD800 && ch < 0xDC00) { |
| 3834 | Py_UNICODE ch2; |
| 3835 | Py_UCS4 ucs; |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 3836 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3837 | ch2 = *s++; |
| 3838 | size--; |
Georg Brandl | 0f14709 | 2010-08-01 20:54:22 +0000 | [diff] [blame] | 3839 | if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3840 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000; |
| 3841 | *p++ = '\\'; |
| 3842 | *p++ = 'U'; |
| 3843 | *p++ = hexdigits[(ucs >> 28) & 0xf]; |
| 3844 | *p++ = hexdigits[(ucs >> 24) & 0xf]; |
| 3845 | *p++ = hexdigits[(ucs >> 20) & 0xf]; |
| 3846 | *p++ = hexdigits[(ucs >> 16) & 0xf]; |
| 3847 | *p++ = hexdigits[(ucs >> 12) & 0xf]; |
| 3848 | *p++ = hexdigits[(ucs >> 8) & 0xf]; |
| 3849 | *p++ = hexdigits[(ucs >> 4) & 0xf]; |
| 3850 | *p++ = hexdigits[ucs & 0xf]; |
| 3851 | continue; |
| 3852 | } |
| 3853 | /* Fall through: isolated surrogates are copied as-is */ |
| 3854 | s--; |
| 3855 | size++; |
| 3856 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 3857 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3858 | /* Map 16-bit characters to '\uxxxx' */ |
| 3859 | if (ch >= 256) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3860 | *p++ = '\\'; |
| 3861 | *p++ = 'u'; |
Walter Dörwald | db5d33e | 2007-05-12 11:13:47 +0000 | [diff] [blame] | 3862 | *p++ = hexdigits[(ch >> 12) & 0xf]; |
| 3863 | *p++ = hexdigits[(ch >> 8) & 0xf]; |
| 3864 | *p++ = hexdigits[(ch >> 4) & 0xf]; |
| 3865 | *p++ = hexdigits[ch & 15]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3866 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3867 | /* Copy everything else as-is */ |
| 3868 | else |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3869 | *p++ = (char) ch; |
| 3870 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3871 | size = p - q; |
| 3872 | |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 3873 | assert(size > 0); |
| 3874 | if (_PyBytes_Resize(&repr, size) < 0) |
| 3875 | return NULL; |
| 3876 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3877 | } |
| 3878 | |
| 3879 | PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 3880 | { |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3881 | PyObject *s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3882 | if (!PyUnicode_Check(unicode)) { |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3883 | PyErr_BadArgument(); |
| 3884 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3885 | } |
Walter Dörwald | 711005d | 2007-05-12 12:03:26 +0000 | [diff] [blame] | 3886 | s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode), |
| 3887 | PyUnicode_GET_SIZE(unicode)); |
| 3888 | |
Alexandre Vassalotti | 9cb6f7f | 2008-12-27 09:09:15 +0000 | [diff] [blame] | 3889 | return s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3890 | } |
| 3891 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3892 | /* --- Unicode Internal Codec ------------------------------------------- */ |
| 3893 | |
| 3894 | PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3895 | Py_ssize_t size, |
| 3896 | const char *errors) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3897 | { |
| 3898 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3899 | Py_ssize_t startinpos; |
| 3900 | Py_ssize_t endinpos; |
| 3901 | Py_ssize_t outpos; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3902 | PyUnicodeObject *v; |
| 3903 | Py_UNICODE *p; |
| 3904 | const char *end; |
| 3905 | const char *reason; |
| 3906 | PyObject *errorHandler = NULL; |
| 3907 | PyObject *exc = NULL; |
| 3908 | |
Neal Norwitz | d43069c | 2006-01-08 01:12:10 +0000 | [diff] [blame] | 3909 | #ifdef Py_UNICODE_WIDE |
| 3910 | Py_UNICODE unimax = PyUnicode_GetMax(); |
| 3911 | #endif |
| 3912 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3913 | /* XXX overflow detection missing */ |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3914 | v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE); |
| 3915 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3916 | goto onError; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3917 | if (PyUnicode_GetSize((PyObject *)v) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3918 | return (PyObject *)v; |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3919 | p = PyUnicode_AS_UNICODE(v); |
| 3920 | end = s + size; |
| 3921 | |
| 3922 | while (s < end) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3923 | memcpy(p, s, sizeof(Py_UNICODE)); |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3924 | /* We have to sanity check the raw data, otherwise doom looms for |
| 3925 | some malformed UCS-4 data. */ |
| 3926 | if ( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3927 | #ifdef Py_UNICODE_WIDE |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3928 | *p > unimax || *p < 0 || |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3929 | #endif |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3930 | end-s < Py_UNICODE_SIZE |
| 3931 | ) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3932 | { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3933 | startinpos = s - starts; |
| 3934 | if (end-s < Py_UNICODE_SIZE) { |
| 3935 | endinpos = end-starts; |
| 3936 | reason = "truncated input"; |
| 3937 | } |
| 3938 | else { |
| 3939 | endinpos = s - starts + Py_UNICODE_SIZE; |
| 3940 | reason = "illegal code point (> 0x10FFFF)"; |
| 3941 | } |
| 3942 | outpos = p - PyUnicode_AS_UNICODE(v); |
| 3943 | if (unicode_decode_call_errorhandler( |
| 3944 | errors, &errorHandler, |
| 3945 | "unicode_internal", reason, |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 3946 | &starts, &end, &startinpos, &endinpos, &exc, &s, |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 3947 | &v, &outpos, &p)) { |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3948 | goto onError; |
| 3949 | } |
| 3950 | } |
| 3951 | else { |
| 3952 | p++; |
| 3953 | s += Py_UNICODE_SIZE; |
| 3954 | } |
| 3955 | } |
| 3956 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3957 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3958 | goto onError; |
| 3959 | Py_XDECREF(errorHandler); |
| 3960 | Py_XDECREF(exc); |
| 3961 | return (PyObject *)v; |
| 3962 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3963 | onError: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 3964 | Py_XDECREF(v); |
| 3965 | Py_XDECREF(errorHandler); |
| 3966 | Py_XDECREF(exc); |
| 3967 | return NULL; |
| 3968 | } |
| 3969 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3970 | /* --- Latin-1 Codec ------------------------------------------------------ */ |
| 3971 | |
| 3972 | PyObject *PyUnicode_DecodeLatin1(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3973 | Py_ssize_t size, |
| 3974 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3975 | { |
| 3976 | PyUnicodeObject *v; |
| 3977 | Py_UNICODE *p; |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3978 | const char *e, *unrolled_end; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 3979 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3980 | /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */ |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 3981 | if (size == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3982 | Py_UNICODE r = *(unsigned char*)s; |
| 3983 | return PyUnicode_FromUnicode(&r, 1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 3984 | } |
| 3985 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3986 | v = _PyUnicode_New(size); |
| 3987 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3988 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3989 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 3990 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 3991 | p = PyUnicode_AS_UNICODE(v); |
Antoine Pitrou | ab86831 | 2009-01-10 15:40:25 +0000 | [diff] [blame] | 3992 | e = s + size; |
| 3993 | /* Unrolling the copy makes it much faster by reducing the looping |
| 3994 | overhead. This is similar to what many memcpy() implementations do. */ |
| 3995 | unrolled_end = e - 4; |
| 3996 | while (s < unrolled_end) { |
| 3997 | p[0] = (unsigned char) s[0]; |
| 3998 | p[1] = (unsigned char) s[1]; |
| 3999 | p[2] = (unsigned char) s[2]; |
| 4000 | p[3] = (unsigned char) s[3]; |
| 4001 | s += 4; |
| 4002 | p += 4; |
| 4003 | } |
| 4004 | while (s < e) |
| 4005 | *p++ = (unsigned char) *s++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4006 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4007 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4008 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4009 | Py_XDECREF(v); |
| 4010 | return NULL; |
| 4011 | } |
| 4012 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4013 | /* create or adjust a UnicodeEncodeError */ |
| 4014 | static void make_encode_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4015 | const char *encoding, |
| 4016 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4017 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4018 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4019 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4020 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4021 | *exceptionObject = PyUnicodeEncodeError_Create( |
| 4022 | encoding, unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4023 | } |
| 4024 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4025 | if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos)) |
| 4026 | goto onError; |
| 4027 | if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos)) |
| 4028 | goto onError; |
| 4029 | if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason)) |
| 4030 | goto onError; |
| 4031 | return; |
| 4032 | onError: |
| 4033 | Py_DECREF(*exceptionObject); |
| 4034 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4035 | } |
| 4036 | } |
| 4037 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4038 | /* raises a UnicodeEncodeError */ |
| 4039 | static void raise_encode_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4040 | const char *encoding, |
| 4041 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 4042 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4043 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4044 | { |
| 4045 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4046 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4047 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4048 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4049 | } |
| 4050 | |
| 4051 | /* error handling callback helper: |
| 4052 | build arguments, call the callback and check the arguments, |
| 4053 | put the result into newpos and return the replacement string, which |
| 4054 | has to be freed by the caller */ |
| 4055 | static PyObject *unicode_encode_call_errorhandler(const char *errors, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4056 | PyObject **errorHandler, |
| 4057 | const char *encoding, const char *reason, |
| 4058 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 4059 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 4060 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4061 | { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4062 | static char *argparse = "On;encoding error handler must return (str/bytes, int) tuple"; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4063 | |
| 4064 | PyObject *restuple; |
| 4065 | PyObject *resunicode; |
| 4066 | |
| 4067 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4068 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4069 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4070 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4071 | } |
| 4072 | |
| 4073 | make_encode_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4074 | encoding, unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4075 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4076 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4077 | |
| 4078 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4079 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4080 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4081 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4082 | if (!PyTuple_Check(restuple)) { |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4083 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4084 | Py_DECREF(restuple); |
| 4085 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4086 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4087 | if (!PyArg_ParseTuple(restuple, argparse, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4088 | &resunicode, newpos)) { |
| 4089 | Py_DECREF(restuple); |
| 4090 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4091 | } |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4092 | if (!PyUnicode_Check(resunicode) && !PyBytes_Check(resunicode)) { |
| 4093 | PyErr_SetString(PyExc_TypeError, &argparse[3]); |
| 4094 | Py_DECREF(restuple); |
| 4095 | return NULL; |
| 4096 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4097 | if (*newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4098 | *newpos = size+*newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4099 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4100 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 4101 | Py_DECREF(restuple); |
| 4102 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4103 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4104 | Py_INCREF(resunicode); |
| 4105 | Py_DECREF(restuple); |
| 4106 | return resunicode; |
| 4107 | } |
| 4108 | |
| 4109 | static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4110 | Py_ssize_t size, |
| 4111 | const char *errors, |
| 4112 | int limit) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4113 | { |
| 4114 | /* output object */ |
| 4115 | PyObject *res; |
| 4116 | /* pointers to the beginning and end+1 of input */ |
| 4117 | const Py_UNICODE *startp = p; |
| 4118 | const Py_UNICODE *endp = p + size; |
| 4119 | /* pointer to the beginning of the unencodable characters */ |
| 4120 | /* const Py_UNICODE *badp = NULL; */ |
| 4121 | /* pointer into the output */ |
| 4122 | char *str; |
| 4123 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4124 | Py_ssize_t ressize; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4125 | const char *encoding = (limit == 256) ? "latin-1" : "ascii"; |
| 4126 | 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] | 4127 | PyObject *errorHandler = NULL; |
| 4128 | PyObject *exc = NULL; |
| 4129 | /* the following variable is used for caching string comparisons |
| 4130 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 4131 | int known_errorHandler = -1; |
| 4132 | |
| 4133 | /* allocate enough for a simple encoding without |
| 4134 | replacements, if we need more, we'll resize */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4135 | if (size == 0) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 4136 | return PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4137 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4138 | if (res == NULL) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 4139 | return NULL; |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4140 | str = PyBytes_AS_STRING(res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4141 | ressize = size; |
| 4142 | |
| 4143 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4144 | Py_UNICODE c = *p; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4145 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4146 | /* can we encode this? */ |
| 4147 | if (c<limit) { |
| 4148 | /* no overflow check, because we know that the space is enough */ |
| 4149 | *str++ = (char)c; |
| 4150 | ++p; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4151 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4152 | else { |
| 4153 | Py_ssize_t unicodepos = p-startp; |
| 4154 | Py_ssize_t requiredsize; |
| 4155 | PyObject *repunicode; |
| 4156 | Py_ssize_t repsize; |
| 4157 | Py_ssize_t newpos; |
| 4158 | Py_ssize_t respos; |
| 4159 | Py_UNICODE *uni2; |
| 4160 | /* startpos for collecting unencodable chars */ |
| 4161 | const Py_UNICODE *collstart = p; |
| 4162 | const Py_UNICODE *collend = p; |
| 4163 | /* find all unecodable characters */ |
| 4164 | while ((collend < endp) && ((*collend)>=limit)) |
| 4165 | ++collend; |
| 4166 | /* cache callback name lookup (if not done yet, i.e. it's the first error) */ |
| 4167 | if (known_errorHandler==-1) { |
| 4168 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 4169 | known_errorHandler = 1; |
| 4170 | else if (!strcmp(errors, "replace")) |
| 4171 | known_errorHandler = 2; |
| 4172 | else if (!strcmp(errors, "ignore")) |
| 4173 | known_errorHandler = 3; |
| 4174 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 4175 | known_errorHandler = 4; |
| 4176 | else |
| 4177 | known_errorHandler = 0; |
| 4178 | } |
| 4179 | switch (known_errorHandler) { |
| 4180 | case 1: /* strict */ |
| 4181 | raise_encode_exception(&exc, encoding, startp, size, collstart-startp, collend-startp, reason); |
| 4182 | goto onError; |
| 4183 | case 2: /* replace */ |
| 4184 | while (collstart++<collend) |
| 4185 | *str++ = '?'; /* fall through */ |
| 4186 | case 3: /* ignore */ |
| 4187 | p = collend; |
| 4188 | break; |
| 4189 | case 4: /* xmlcharrefreplace */ |
| 4190 | respos = str - PyBytes_AS_STRING(res); |
| 4191 | /* determine replacement size (temporarily (mis)uses p) */ |
| 4192 | for (p = collstart, repsize = 0; p < collend; ++p) { |
| 4193 | if (*p<10) |
| 4194 | repsize += 2+1+1; |
| 4195 | else if (*p<100) |
| 4196 | repsize += 2+2+1; |
| 4197 | else if (*p<1000) |
| 4198 | repsize += 2+3+1; |
| 4199 | else if (*p<10000) |
| 4200 | repsize += 2+4+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 4201 | #ifndef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4202 | else |
| 4203 | repsize += 2+5+1; |
Hye-Shik Chang | 40e9509 | 2003-12-22 01:31:13 +0000 | [diff] [blame] | 4204 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4205 | else if (*p<100000) |
| 4206 | repsize += 2+5+1; |
| 4207 | else if (*p<1000000) |
| 4208 | repsize += 2+6+1; |
| 4209 | else |
| 4210 | repsize += 2+7+1; |
Hye-Shik Chang | 4a264fb | 2003-12-19 01:59:56 +0000 | [diff] [blame] | 4211 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4212 | } |
| 4213 | requiredsize = respos+repsize+(endp-collend); |
| 4214 | if (requiredsize > ressize) { |
| 4215 | if (requiredsize<2*ressize) |
| 4216 | requiredsize = 2*ressize; |
| 4217 | if (_PyBytes_Resize(&res, requiredsize)) |
| 4218 | goto onError; |
| 4219 | str = PyBytes_AS_STRING(res) + respos; |
| 4220 | ressize = requiredsize; |
| 4221 | } |
| 4222 | /* generate replacement (temporarily (mis)uses p) */ |
| 4223 | for (p = collstart; p < collend; ++p) { |
| 4224 | str += sprintf(str, "&#%d;", (int)*p); |
| 4225 | } |
| 4226 | p = collend; |
| 4227 | break; |
| 4228 | default: |
| 4229 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 4230 | encoding, reason, startp, size, &exc, |
| 4231 | collstart-startp, collend-startp, &newpos); |
| 4232 | if (repunicode == NULL) |
| 4233 | goto onError; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4234 | if (PyBytes_Check(repunicode)) { |
| 4235 | /* Directly copy bytes result to output. */ |
| 4236 | repsize = PyBytes_Size(repunicode); |
| 4237 | if (repsize > 1) { |
| 4238 | /* Make room for all additional bytes. */ |
Amaury Forgeot d'Arc | e5344d6 | 2009-06-29 22:38:54 +0000 | [diff] [blame] | 4239 | respos = str - PyBytes_AS_STRING(res); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4240 | if (_PyBytes_Resize(&res, ressize+repsize-1)) { |
| 4241 | Py_DECREF(repunicode); |
| 4242 | goto onError; |
| 4243 | } |
Amaury Forgeot d'Arc | e5344d6 | 2009-06-29 22:38:54 +0000 | [diff] [blame] | 4244 | str = PyBytes_AS_STRING(res) + respos; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4245 | ressize += repsize-1; |
| 4246 | } |
| 4247 | memcpy(str, PyBytes_AsString(repunicode), repsize); |
| 4248 | str += repsize; |
| 4249 | p = startp + newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4250 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 4251 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 4252 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4253 | /* need more space? (at least enough for what we |
| 4254 | have+the replacement+the rest of the string, so |
| 4255 | we won't have to check space for encodable characters) */ |
| 4256 | respos = str - PyBytes_AS_STRING(res); |
| 4257 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 4258 | requiredsize = respos+repsize+(endp-collend); |
| 4259 | if (requiredsize > ressize) { |
| 4260 | if (requiredsize<2*ressize) |
| 4261 | requiredsize = 2*ressize; |
| 4262 | if (_PyBytes_Resize(&res, requiredsize)) { |
| 4263 | Py_DECREF(repunicode); |
| 4264 | goto onError; |
| 4265 | } |
| 4266 | str = PyBytes_AS_STRING(res) + respos; |
| 4267 | ressize = requiredsize; |
| 4268 | } |
| 4269 | /* check if there is anything unencodable in the replacement |
| 4270 | and copy it to the output */ |
| 4271 | for (uni2 = PyUnicode_AS_UNICODE(repunicode);repsize-->0; ++uni2, ++str) { |
| 4272 | c = *uni2; |
| 4273 | if (c >= limit) { |
| 4274 | raise_encode_exception(&exc, encoding, startp, size, |
| 4275 | unicodepos, unicodepos+1, reason); |
| 4276 | Py_DECREF(repunicode); |
| 4277 | goto onError; |
| 4278 | } |
| 4279 | *str = (char)c; |
| 4280 | } |
| 4281 | p = startp + newpos; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4282 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4283 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4284 | } |
| 4285 | } |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4286 | /* Resize if we allocated to much */ |
| 4287 | size = str - PyBytes_AS_STRING(res); |
| 4288 | if (size < ressize) { /* If this falls res will be NULL */ |
Alexandre Vassalotti | bad1b92 | 2008-12-27 09:49:09 +0000 | [diff] [blame] | 4289 | assert(size >= 0); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4290 | if (_PyBytes_Resize(&res, size) < 0) |
| 4291 | goto onError; |
| 4292 | } |
| 4293 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4294 | Py_XDECREF(errorHandler); |
| 4295 | Py_XDECREF(exc); |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 4296 | return res; |
| 4297 | |
| 4298 | onError: |
| 4299 | Py_XDECREF(res); |
| 4300 | Py_XDECREF(errorHandler); |
| 4301 | Py_XDECREF(exc); |
| 4302 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4303 | } |
| 4304 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4305 | PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4306 | Py_ssize_t size, |
| 4307 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4308 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4309 | return unicode_encode_ucs1(p, size, errors, 256); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4310 | } |
| 4311 | |
| 4312 | PyObject *PyUnicode_AsLatin1String(PyObject *unicode) |
| 4313 | { |
| 4314 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4315 | PyErr_BadArgument(); |
| 4316 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4317 | } |
| 4318 | return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4319 | PyUnicode_GET_SIZE(unicode), |
| 4320 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4321 | } |
| 4322 | |
| 4323 | /* --- 7-bit ASCII Codec -------------------------------------------------- */ |
| 4324 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4325 | PyObject *PyUnicode_DecodeASCII(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4326 | Py_ssize_t size, |
| 4327 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4328 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4329 | const char *starts = s; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4330 | PyUnicodeObject *v; |
| 4331 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4332 | Py_ssize_t startinpos; |
| 4333 | Py_ssize_t endinpos; |
| 4334 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4335 | const char *e; |
| 4336 | PyObject *errorHandler = NULL; |
| 4337 | PyObject *exc = NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4338 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4339 | /* ASCII is equivalent to the first 128 ordinals in Unicode. */ |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4340 | if (size == 1 && *(unsigned char*)s < 128) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4341 | Py_UNICODE r = *(unsigned char*)s; |
| 4342 | return PyUnicode_FromUnicode(&r, 1); |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 4343 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4344 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4345 | v = _PyUnicode_New(size); |
| 4346 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4347 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4348 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4349 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4350 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4351 | e = s + size; |
| 4352 | while (s < e) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4353 | register unsigned char c = (unsigned char)*s; |
| 4354 | if (c < 128) { |
| 4355 | *p++ = c; |
| 4356 | ++s; |
| 4357 | } |
| 4358 | else { |
| 4359 | startinpos = s-starts; |
| 4360 | endinpos = startinpos + 1; |
| 4361 | outpos = p - (Py_UNICODE *)PyUnicode_AS_UNICODE(v); |
| 4362 | if (unicode_decode_call_errorhandler( |
| 4363 | errors, &errorHandler, |
| 4364 | "ascii", "ordinal not in range(128)", |
| 4365 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 4366 | &v, &outpos, &p)) |
| 4367 | goto onError; |
| 4368 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4369 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 4370 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4371 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 4372 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4373 | Py_XDECREF(errorHandler); |
| 4374 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4375 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4376 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4377 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4378 | Py_XDECREF(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4379 | Py_XDECREF(errorHandler); |
| 4380 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4381 | return NULL; |
| 4382 | } |
| 4383 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4384 | PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4385 | Py_ssize_t size, |
| 4386 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4387 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4388 | return unicode_encode_ucs1(p, size, errors, 128); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4389 | } |
| 4390 | |
| 4391 | PyObject *PyUnicode_AsASCIIString(PyObject *unicode) |
| 4392 | { |
| 4393 | if (!PyUnicode_Check(unicode)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4394 | PyErr_BadArgument(); |
| 4395 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4396 | } |
| 4397 | return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4398 | PyUnicode_GET_SIZE(unicode), |
| 4399 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4400 | } |
| 4401 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4402 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4403 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4404 | /* --- MBCS codecs for Windows -------------------------------------------- */ |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4405 | |
Hirokazu Yamamoto | 3530246 | 2009-03-21 13:23:27 +0000 | [diff] [blame] | 4406 | #if SIZEOF_INT < SIZEOF_SIZE_T |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4407 | #define NEED_RETRY |
| 4408 | #endif |
| 4409 | |
| 4410 | /* XXX This code is limited to "true" double-byte encodings, as |
| 4411 | a) it assumes an incomplete character consists of a single byte, and |
| 4412 | b) IsDBCSLeadByte (probably) does not work for non-DBCS multi-byte |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4413 | encodings, see IsDBCSLeadByteEx documentation. */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4414 | |
| 4415 | static int is_dbcs_lead_byte(const char *s, int offset) |
| 4416 | { |
| 4417 | const char *curr = s + offset; |
| 4418 | |
| 4419 | if (IsDBCSLeadByte(*curr)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4420 | const char *prev = CharPrev(s, curr); |
| 4421 | return (prev == curr) || !IsDBCSLeadByte(*prev) || (curr - prev == 2); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4422 | } |
| 4423 | return 0; |
| 4424 | } |
| 4425 | |
| 4426 | /* |
| 4427 | * Decode MBCS string into unicode object. If 'final' is set, converts |
| 4428 | * trailing lead-byte too. Returns consumed size if succeed, -1 otherwise. |
| 4429 | */ |
| 4430 | static int decode_mbcs(PyUnicodeObject **v, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4431 | const char *s, /* MBCS string */ |
| 4432 | int size, /* sizeof MBCS string */ |
| 4433 | int final) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4434 | { |
| 4435 | Py_UNICODE *p; |
| 4436 | Py_ssize_t n = 0; |
| 4437 | int usize = 0; |
| 4438 | |
| 4439 | assert(size >= 0); |
| 4440 | |
| 4441 | /* Skip trailing lead-byte unless 'final' is set */ |
| 4442 | if (!final && size >= 1 && is_dbcs_lead_byte(s, size - 1)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4443 | --size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4444 | |
| 4445 | /* First get the size of the result */ |
| 4446 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4447 | usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); |
| 4448 | if (usize == 0) { |
| 4449 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4450 | return -1; |
| 4451 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4452 | } |
| 4453 | |
| 4454 | if (*v == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4455 | /* Create unicode object */ |
| 4456 | *v = _PyUnicode_New(usize); |
| 4457 | if (*v == NULL) |
| 4458 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4459 | } |
| 4460 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4461 | /* Extend unicode object */ |
| 4462 | n = PyUnicode_GET_SIZE(*v); |
| 4463 | if (_PyUnicode_Resize(v, n + usize) < 0) |
| 4464 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4465 | } |
| 4466 | |
| 4467 | /* Do the conversion */ |
| 4468 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4469 | p = PyUnicode_AS_UNICODE(*v) + n; |
| 4470 | if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { |
| 4471 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4472 | return -1; |
| 4473 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4474 | } |
| 4475 | |
| 4476 | return size; |
| 4477 | } |
| 4478 | |
| 4479 | PyObject *PyUnicode_DecodeMBCSStateful(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4480 | Py_ssize_t size, |
| 4481 | const char *errors, |
| 4482 | Py_ssize_t *consumed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4483 | { |
| 4484 | PyUnicodeObject *v = NULL; |
| 4485 | int done; |
| 4486 | |
| 4487 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4488 | *consumed = 0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4489 | |
| 4490 | #ifdef NEED_RETRY |
| 4491 | retry: |
| 4492 | if (size > INT_MAX) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4493 | done = decode_mbcs(&v, s, INT_MAX, 0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4494 | else |
| 4495 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4496 | done = decode_mbcs(&v, s, (int)size, !consumed); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4497 | |
| 4498 | if (done < 0) { |
| 4499 | Py_XDECREF(v); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4500 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4501 | } |
| 4502 | |
| 4503 | if (consumed) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4504 | *consumed += done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4505 | |
| 4506 | #ifdef NEED_RETRY |
| 4507 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4508 | s += done; |
| 4509 | size -= done; |
| 4510 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4511 | } |
| 4512 | #endif |
| 4513 | |
| 4514 | return (PyObject *)v; |
| 4515 | } |
| 4516 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4517 | PyObject *PyUnicode_DecodeMBCS(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4518 | Py_ssize_t size, |
| 4519 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4520 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4521 | return PyUnicode_DecodeMBCSStateful(s, size, errors, NULL); |
| 4522 | } |
| 4523 | |
| 4524 | /* |
| 4525 | * Convert unicode into string object (MBCS). |
| 4526 | * Returns 0 if succeed, -1 otherwise. |
| 4527 | */ |
| 4528 | static int encode_mbcs(PyObject **repr, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4529 | const Py_UNICODE *p, /* unicode */ |
| 4530 | int size) /* size of unicode */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4531 | { |
| 4532 | int mbcssize = 0; |
| 4533 | Py_ssize_t n = 0; |
| 4534 | |
| 4535 | assert(size >= 0); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4536 | |
| 4537 | /* First get the size of the result */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4538 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4539 | mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); |
| 4540 | if (mbcssize == 0) { |
| 4541 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4542 | return -1; |
| 4543 | } |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4544 | } |
| 4545 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4546 | if (*repr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4547 | /* Create string object */ |
| 4548 | *repr = PyBytes_FromStringAndSize(NULL, mbcssize); |
| 4549 | if (*repr == NULL) |
| 4550 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4551 | } |
| 4552 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4553 | /* Extend string object */ |
| 4554 | n = PyBytes_Size(*repr); |
| 4555 | if (_PyBytes_Resize(repr, n + mbcssize) < 0) |
| 4556 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4557 | } |
| 4558 | |
| 4559 | /* Do the conversion */ |
| 4560 | if (size > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4561 | char *s = PyBytes_AS_STRING(*repr) + n; |
| 4562 | if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { |
| 4563 | PyErr_SetFromWindowsErrWithFilename(0, NULL); |
| 4564 | return -1; |
| 4565 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4566 | } |
| 4567 | |
| 4568 | return 0; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4569 | } |
| 4570 | |
| 4571 | PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4572 | Py_ssize_t size, |
| 4573 | const char *errors) |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4574 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4575 | PyObject *repr = NULL; |
| 4576 | int ret; |
Guido van Rossum | 03e29f1 | 2000-05-04 15:52:20 +0000 | [diff] [blame] | 4577 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4578 | #ifdef NEED_RETRY |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4579 | retry: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4580 | if (size > INT_MAX) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4581 | ret = encode_mbcs(&repr, p, INT_MAX); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4582 | else |
| 4583 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4584 | ret = encode_mbcs(&repr, p, (int)size); |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4585 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4586 | if (ret < 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4587 | Py_XDECREF(repr); |
| 4588 | return NULL; |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4589 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4590 | |
| 4591 | #ifdef NEED_RETRY |
| 4592 | if (size > INT_MAX) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4593 | p += INT_MAX; |
| 4594 | size -= INT_MAX; |
| 4595 | goto retry; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4596 | } |
| 4597 | #endif |
| 4598 | |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4599 | return repr; |
| 4600 | } |
Guido van Rossum | 2ea3e14 | 2000-03-31 17:24:09 +0000 | [diff] [blame] | 4601 | |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 4602 | PyObject *PyUnicode_AsMBCSString(PyObject *unicode) |
| 4603 | { |
| 4604 | if (!PyUnicode_Check(unicode)) { |
| 4605 | PyErr_BadArgument(); |
| 4606 | return NULL; |
| 4607 | } |
| 4608 | return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4609 | PyUnicode_GET_SIZE(unicode), |
| 4610 | NULL); |
Mark Hammond | 0ccda1e | 2003-07-01 00:13:27 +0000 | [diff] [blame] | 4611 | } |
| 4612 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4613 | #undef NEED_RETRY |
| 4614 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4615 | #endif /* MS_WINDOWS */ |
Guido van Rossum | b7a40ba | 2000-03-28 02:01:52 +0000 | [diff] [blame] | 4616 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4617 | /* --- Character Mapping Codec -------------------------------------------- */ |
| 4618 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4619 | PyObject *PyUnicode_DecodeCharmap(const char *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4620 | Py_ssize_t size, |
| 4621 | PyObject *mapping, |
| 4622 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4623 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4624 | const char *starts = s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4625 | Py_ssize_t startinpos; |
| 4626 | Py_ssize_t endinpos; |
| 4627 | Py_ssize_t outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4628 | const char *e; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4629 | PyUnicodeObject *v; |
| 4630 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4631 | Py_ssize_t extrachars = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4632 | PyObject *errorHandler = NULL; |
| 4633 | PyObject *exc = NULL; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4634 | Py_UNICODE *mapstring = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4635 | Py_ssize_t maplen = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4636 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4637 | /* Default to Latin-1 */ |
| 4638 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4639 | return PyUnicode_DecodeLatin1(s, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4640 | |
| 4641 | v = _PyUnicode_New(size); |
| 4642 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4643 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4644 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4645 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4646 | p = PyUnicode_AS_UNICODE(v); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4647 | e = s + size; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4648 | if (PyUnicode_CheckExact(mapping)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4649 | mapstring = PyUnicode_AS_UNICODE(mapping); |
| 4650 | maplen = PyUnicode_GET_SIZE(mapping); |
| 4651 | while (s < e) { |
| 4652 | unsigned char ch = *s; |
| 4653 | Py_UNICODE x = 0xfffe; /* illegal value */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4654 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4655 | if (ch < maplen) |
| 4656 | x = mapstring[ch]; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4657 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4658 | if (x == 0xfffe) { |
| 4659 | /* undefined mapping */ |
| 4660 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4661 | startinpos = s-starts; |
| 4662 | endinpos = startinpos+1; |
| 4663 | if (unicode_decode_call_errorhandler( |
| 4664 | errors, &errorHandler, |
| 4665 | "charmap", "character maps to <undefined>", |
| 4666 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 4667 | &v, &outpos, &p)) { |
| 4668 | goto onError; |
| 4669 | } |
| 4670 | continue; |
| 4671 | } |
| 4672 | *p++ = x; |
| 4673 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4674 | } |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4675 | } |
| 4676 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4677 | while (s < e) { |
| 4678 | unsigned char ch = *s; |
| 4679 | PyObject *w, *x; |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 4680 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4681 | /* Get mapping (char ordinal -> integer, Unicode char or None) */ |
| 4682 | w = PyLong_FromLong((long)ch); |
| 4683 | if (w == NULL) |
| 4684 | goto onError; |
| 4685 | x = PyObject_GetItem(mapping, w); |
| 4686 | Py_DECREF(w); |
| 4687 | if (x == NULL) { |
| 4688 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 4689 | /* No mapping found means: mapping is undefined. */ |
| 4690 | PyErr_Clear(); |
| 4691 | x = Py_None; |
| 4692 | Py_INCREF(x); |
| 4693 | } else |
| 4694 | goto onError; |
| 4695 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4696 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4697 | /* Apply mapping */ |
| 4698 | if (PyLong_Check(x)) { |
| 4699 | long value = PyLong_AS_LONG(x); |
| 4700 | if (value < 0 || value > 65535) { |
| 4701 | PyErr_SetString(PyExc_TypeError, |
| 4702 | "character mapping must be in range(65536)"); |
| 4703 | Py_DECREF(x); |
| 4704 | goto onError; |
| 4705 | } |
| 4706 | *p++ = (Py_UNICODE)value; |
| 4707 | } |
| 4708 | else if (x == Py_None) { |
| 4709 | /* undefined mapping */ |
| 4710 | outpos = p-PyUnicode_AS_UNICODE(v); |
| 4711 | startinpos = s-starts; |
| 4712 | endinpos = startinpos+1; |
| 4713 | if (unicode_decode_call_errorhandler( |
| 4714 | errors, &errorHandler, |
| 4715 | "charmap", "character maps to <undefined>", |
| 4716 | &starts, &e, &startinpos, &endinpos, &exc, &s, |
| 4717 | &v, &outpos, &p)) { |
| 4718 | Py_DECREF(x); |
| 4719 | goto onError; |
| 4720 | } |
| 4721 | Py_DECREF(x); |
| 4722 | continue; |
| 4723 | } |
| 4724 | else if (PyUnicode_Check(x)) { |
| 4725 | Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4726 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4727 | if (targetsize == 1) |
| 4728 | /* 1-1 mapping */ |
| 4729 | *p++ = *PyUnicode_AS_UNICODE(x); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4730 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4731 | else if (targetsize > 1) { |
| 4732 | /* 1-n mapping */ |
| 4733 | if (targetsize > extrachars) { |
| 4734 | /* resize first */ |
| 4735 | Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); |
| 4736 | Py_ssize_t needed = (targetsize - extrachars) + \ |
| 4737 | (targetsize << 2); |
| 4738 | extrachars += needed; |
| 4739 | /* XXX overflow detection missing */ |
| 4740 | if (_PyUnicode_Resize(&v, |
| 4741 | PyUnicode_GET_SIZE(v) + needed) < 0) { |
| 4742 | Py_DECREF(x); |
| 4743 | goto onError; |
| 4744 | } |
| 4745 | p = PyUnicode_AS_UNICODE(v) + oldpos; |
| 4746 | } |
| 4747 | Py_UNICODE_COPY(p, |
| 4748 | PyUnicode_AS_UNICODE(x), |
| 4749 | targetsize); |
| 4750 | p += targetsize; |
| 4751 | extrachars -= targetsize; |
| 4752 | } |
| 4753 | /* 1-0 mapping: skip the character */ |
| 4754 | } |
| 4755 | else { |
| 4756 | /* wrong return value */ |
| 4757 | PyErr_SetString(PyExc_TypeError, |
| 4758 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4759 | Py_DECREF(x); |
| 4760 | goto onError; |
| 4761 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4762 | Py_DECREF(x); |
| 4763 | ++s; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4764 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4765 | } |
| 4766 | if (p - PyUnicode_AS_UNICODE(v) < PyUnicode_GET_SIZE(v)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4767 | if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0) |
| 4768 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4769 | Py_XDECREF(errorHandler); |
| 4770 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4771 | return (PyObject *)v; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 4772 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4773 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4774 | Py_XDECREF(errorHandler); |
| 4775 | Py_XDECREF(exc); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4776 | Py_XDECREF(v); |
| 4777 | return NULL; |
| 4778 | } |
| 4779 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4780 | /* Charmap encoding: the lookup table */ |
| 4781 | |
| 4782 | struct encoding_map{ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4783 | PyObject_HEAD |
| 4784 | unsigned char level1[32]; |
| 4785 | int count2, count3; |
| 4786 | unsigned char level23[1]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4787 | }; |
| 4788 | |
| 4789 | static PyObject* |
| 4790 | encoding_map_size(PyObject *obj, PyObject* args) |
| 4791 | { |
| 4792 | struct encoding_map *map = (struct encoding_map*)obj; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4793 | return PyLong_FromLong(sizeof(*map) - 1 + 16*map->count2 + |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4794 | 128*map->count3); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4795 | } |
| 4796 | |
| 4797 | static PyMethodDef encoding_map_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4798 | {"size", encoding_map_size, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4799 | PyDoc_STR("Return the size (in bytes) of this object") }, |
| 4800 | { 0 } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4801 | }; |
| 4802 | |
| 4803 | static void |
| 4804 | encoding_map_dealloc(PyObject* o) |
| 4805 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4806 | PyObject_FREE(o); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4807 | } |
| 4808 | |
| 4809 | static PyTypeObject EncodingMapType = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4810 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4811 | "EncodingMap", /*tp_name*/ |
| 4812 | sizeof(struct encoding_map), /*tp_basicsize*/ |
| 4813 | 0, /*tp_itemsize*/ |
| 4814 | /* methods */ |
| 4815 | encoding_map_dealloc, /*tp_dealloc*/ |
| 4816 | 0, /*tp_print*/ |
| 4817 | 0, /*tp_getattr*/ |
| 4818 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 4819 | 0, /*tp_reserved*/ |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4820 | 0, /*tp_repr*/ |
| 4821 | 0, /*tp_as_number*/ |
| 4822 | 0, /*tp_as_sequence*/ |
| 4823 | 0, /*tp_as_mapping*/ |
| 4824 | 0, /*tp_hash*/ |
| 4825 | 0, /*tp_call*/ |
| 4826 | 0, /*tp_str*/ |
| 4827 | 0, /*tp_getattro*/ |
| 4828 | 0, /*tp_setattro*/ |
| 4829 | 0, /*tp_as_buffer*/ |
| 4830 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 4831 | 0, /*tp_doc*/ |
| 4832 | 0, /*tp_traverse*/ |
| 4833 | 0, /*tp_clear*/ |
| 4834 | 0, /*tp_richcompare*/ |
| 4835 | 0, /*tp_weaklistoffset*/ |
| 4836 | 0, /*tp_iter*/ |
| 4837 | 0, /*tp_iternext*/ |
| 4838 | encoding_map_methods, /*tp_methods*/ |
| 4839 | 0, /*tp_members*/ |
| 4840 | 0, /*tp_getset*/ |
| 4841 | 0, /*tp_base*/ |
| 4842 | 0, /*tp_dict*/ |
| 4843 | 0, /*tp_descr_get*/ |
| 4844 | 0, /*tp_descr_set*/ |
| 4845 | 0, /*tp_dictoffset*/ |
| 4846 | 0, /*tp_init*/ |
| 4847 | 0, /*tp_alloc*/ |
| 4848 | 0, /*tp_new*/ |
| 4849 | 0, /*tp_free*/ |
| 4850 | 0, /*tp_is_gc*/ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4851 | }; |
| 4852 | |
| 4853 | PyObject* |
| 4854 | PyUnicode_BuildEncodingMap(PyObject* string) |
| 4855 | { |
| 4856 | Py_UNICODE *decode; |
| 4857 | PyObject *result; |
| 4858 | struct encoding_map *mresult; |
| 4859 | int i; |
| 4860 | int need_dict = 0; |
| 4861 | unsigned char level1[32]; |
| 4862 | unsigned char level2[512]; |
| 4863 | unsigned char *mlevel1, *mlevel2, *mlevel3; |
| 4864 | int count2 = 0, count3 = 0; |
| 4865 | |
| 4866 | if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) { |
| 4867 | PyErr_BadArgument(); |
| 4868 | return NULL; |
| 4869 | } |
| 4870 | decode = PyUnicode_AS_UNICODE(string); |
| 4871 | memset(level1, 0xFF, sizeof level1); |
| 4872 | memset(level2, 0xFF, sizeof level2); |
| 4873 | |
| 4874 | /* If there isn't a one-to-one mapping of NULL to \0, |
| 4875 | or if there are non-BMP characters, we need to use |
| 4876 | a mapping dictionary. */ |
| 4877 | if (decode[0] != 0) |
| 4878 | need_dict = 1; |
| 4879 | for (i = 1; i < 256; i++) { |
| 4880 | int l1, l2; |
| 4881 | if (decode[i] == 0 |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4882 | #ifdef Py_UNICODE_WIDE |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4883 | || decode[i] > 0xFFFF |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4884 | #endif |
| 4885 | ) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4886 | need_dict = 1; |
| 4887 | break; |
| 4888 | } |
| 4889 | if (decode[i] == 0xFFFE) |
| 4890 | /* unmapped character */ |
| 4891 | continue; |
| 4892 | l1 = decode[i] >> 11; |
| 4893 | l2 = decode[i] >> 7; |
| 4894 | if (level1[l1] == 0xFF) |
| 4895 | level1[l1] = count2++; |
| 4896 | if (level2[l2] == 0xFF) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 4897 | level2[l2] = count3++; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4898 | } |
| 4899 | |
| 4900 | if (count2 >= 0xFF || count3 >= 0xFF) |
| 4901 | need_dict = 1; |
| 4902 | |
| 4903 | if (need_dict) { |
| 4904 | PyObject *result = PyDict_New(); |
| 4905 | PyObject *key, *value; |
| 4906 | if (!result) |
| 4907 | return NULL; |
| 4908 | for (i = 0; i < 256; i++) { |
| 4909 | key = value = NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4910 | key = PyLong_FromLong(decode[i]); |
| 4911 | value = PyLong_FromLong(i); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4912 | if (!key || !value) |
| 4913 | goto failed1; |
| 4914 | if (PyDict_SetItem(result, key, value) == -1) |
| 4915 | goto failed1; |
| 4916 | Py_DECREF(key); |
| 4917 | Py_DECREF(value); |
| 4918 | } |
| 4919 | return result; |
| 4920 | failed1: |
| 4921 | Py_XDECREF(key); |
| 4922 | Py_XDECREF(value); |
| 4923 | Py_DECREF(result); |
| 4924 | return NULL; |
| 4925 | } |
| 4926 | |
| 4927 | /* Create a three-level trie */ |
| 4928 | result = PyObject_MALLOC(sizeof(struct encoding_map) + |
| 4929 | 16*count2 + 128*count3 - 1); |
| 4930 | if (!result) |
| 4931 | return PyErr_NoMemory(); |
| 4932 | PyObject_Init(result, &EncodingMapType); |
| 4933 | mresult = (struct encoding_map*)result; |
| 4934 | mresult->count2 = count2; |
| 4935 | mresult->count3 = count3; |
| 4936 | mlevel1 = mresult->level1; |
| 4937 | mlevel2 = mresult->level23; |
| 4938 | mlevel3 = mresult->level23 + 16*count2; |
| 4939 | memcpy(mlevel1, level1, 32); |
| 4940 | memset(mlevel2, 0xFF, 16*count2); |
| 4941 | memset(mlevel3, 0, 128*count3); |
| 4942 | count3 = 0; |
| 4943 | for (i = 1; i < 256; i++) { |
| 4944 | int o1, o2, o3, i2, i3; |
| 4945 | if (decode[i] == 0xFFFE) |
| 4946 | /* unmapped character */ |
| 4947 | continue; |
| 4948 | o1 = decode[i]>>11; |
| 4949 | o2 = (decode[i]>>7) & 0xF; |
| 4950 | i2 = 16*mlevel1[o1] + o2; |
| 4951 | if (mlevel2[i2] == 0xFF) |
| 4952 | mlevel2[i2] = count3++; |
| 4953 | o3 = decode[i] & 0x7F; |
| 4954 | i3 = 128*mlevel2[i2] + o3; |
| 4955 | mlevel3[i3] = i; |
| 4956 | } |
| 4957 | return result; |
| 4958 | } |
| 4959 | |
| 4960 | static int |
| 4961 | encoding_map_lookup(Py_UNICODE c, PyObject *mapping) |
| 4962 | { |
| 4963 | struct encoding_map *map = (struct encoding_map*)mapping; |
| 4964 | int l1 = c>>11; |
| 4965 | int l2 = (c>>7) & 0xF; |
| 4966 | int l3 = c & 0x7F; |
| 4967 | int i; |
| 4968 | |
| 4969 | #ifdef Py_UNICODE_WIDE |
| 4970 | if (c > 0xFFFF) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 4971 | return -1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4972 | } |
| 4973 | #endif |
| 4974 | if (c == 0) |
| 4975 | return 0; |
| 4976 | /* level 1*/ |
| 4977 | i = map->level1[l1]; |
| 4978 | if (i == 0xFF) { |
| 4979 | return -1; |
| 4980 | } |
| 4981 | /* level 2*/ |
| 4982 | i = map->level23[16*i+l2]; |
| 4983 | if (i == 0xFF) { |
| 4984 | return -1; |
| 4985 | } |
| 4986 | /* level 3 */ |
| 4987 | i = map->level23[16*map->count2 + 128*i + l3]; |
| 4988 | if (i == 0) { |
| 4989 | return -1; |
| 4990 | } |
| 4991 | return i; |
| 4992 | } |
| 4993 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4994 | /* Lookup the character ch in the mapping. If the character |
| 4995 | can't be found, Py_None is returned (or NULL, if another |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 4996 | error occurred). */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 4997 | static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 4998 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 4999 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5000 | PyObject *x; |
| 5001 | |
| 5002 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5003 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5004 | x = PyObject_GetItem(mapping, w); |
| 5005 | Py_DECREF(w); |
| 5006 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5007 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 5008 | /* No mapping found means: mapping is undefined. */ |
| 5009 | PyErr_Clear(); |
| 5010 | x = Py_None; |
| 5011 | Py_INCREF(x); |
| 5012 | return x; |
| 5013 | } else |
| 5014 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5015 | } |
Walter Dörwald | adc7274 | 2003-01-08 22:01:33 +0000 | [diff] [blame] | 5016 | else if (x == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5017 | return x; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5018 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5019 | long value = PyLong_AS_LONG(x); |
| 5020 | if (value < 0 || value > 255) { |
| 5021 | PyErr_SetString(PyExc_TypeError, |
| 5022 | "character mapping must be in range(256)"); |
| 5023 | Py_DECREF(x); |
| 5024 | return NULL; |
| 5025 | } |
| 5026 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5027 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5028 | else if (PyBytes_Check(x)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5029 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5030 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5031 | /* wrong return value */ |
| 5032 | PyErr_Format(PyExc_TypeError, |
| 5033 | "character mapping must return integer, bytes or None, not %.400s", |
| 5034 | x->ob_type->tp_name); |
| 5035 | Py_DECREF(x); |
| 5036 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5037 | } |
| 5038 | } |
| 5039 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5040 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5041 | charmapencode_resize(PyObject **outobj, Py_ssize_t *outpos, Py_ssize_t requiredsize) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5042 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5043 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
| 5044 | /* exponentially overallocate to minimize reallocations */ |
| 5045 | if (requiredsize < 2*outsize) |
| 5046 | requiredsize = 2*outsize; |
| 5047 | if (_PyBytes_Resize(outobj, requiredsize)) |
| 5048 | return -1; |
| 5049 | return 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5050 | } |
| 5051 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5052 | typedef enum charmapencode_result { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5053 | enc_SUCCESS, enc_FAILED, enc_EXCEPTION |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5054 | }charmapencode_result; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5055 | /* 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] | 5056 | various state variables. Resize the output bytes object if not enough |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5057 | space is available. Return a new reference to the object that |
| 5058 | was put in the output buffer, or Py_None, if the mapping was undefined |
| 5059 | (in which case no character was written) or NULL, if a |
Andrew M. Kuchling | 8294de5 | 2005-11-02 16:36:12 +0000 | [diff] [blame] | 5060 | reallocation error occurred. The caller must decref the result */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5061 | static |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5062 | charmapencode_result charmapencode_output(Py_UNICODE c, PyObject *mapping, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5063 | PyObject **outobj, Py_ssize_t *outpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5064 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5065 | PyObject *rep; |
| 5066 | char *outstart; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5067 | Py_ssize_t outsize = PyBytes_GET_SIZE(*outobj); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5068 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5069 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5070 | int res = encoding_map_lookup(c, mapping); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5071 | Py_ssize_t requiredsize = *outpos+1; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5072 | if (res == -1) |
| 5073 | return enc_FAILED; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5074 | if (outsize<requiredsize) |
| 5075 | if (charmapencode_resize(outobj, outpos, requiredsize)) |
| 5076 | return enc_EXCEPTION; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5077 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5078 | outstart[(*outpos)++] = (char)res; |
| 5079 | return enc_SUCCESS; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5080 | } |
| 5081 | |
| 5082 | rep = charmapencode_lookup(c, mapping); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5083 | if (rep==NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5084 | return enc_EXCEPTION; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5085 | else if (rep==Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5086 | Py_DECREF(rep); |
| 5087 | return enc_FAILED; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5088 | } else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5089 | if (PyLong_Check(rep)) { |
| 5090 | Py_ssize_t requiredsize = *outpos+1; |
| 5091 | if (outsize<requiredsize) |
| 5092 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 5093 | Py_DECREF(rep); |
| 5094 | return enc_EXCEPTION; |
| 5095 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5096 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5097 | outstart[(*outpos)++] = (char)PyLong_AS_LONG(rep); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5098 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5099 | else { |
| 5100 | const char *repchars = PyBytes_AS_STRING(rep); |
| 5101 | Py_ssize_t repsize = PyBytes_GET_SIZE(rep); |
| 5102 | Py_ssize_t requiredsize = *outpos+repsize; |
| 5103 | if (outsize<requiredsize) |
| 5104 | if (charmapencode_resize(outobj, outpos, requiredsize)) { |
| 5105 | Py_DECREF(rep); |
| 5106 | return enc_EXCEPTION; |
| 5107 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5108 | outstart = PyBytes_AS_STRING(*outobj); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5109 | memcpy(outstart + *outpos, repchars, repsize); |
| 5110 | *outpos += repsize; |
| 5111 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5112 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5113 | Py_DECREF(rep); |
| 5114 | return enc_SUCCESS; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5115 | } |
| 5116 | |
| 5117 | /* handle an error in PyUnicode_EncodeCharmap |
| 5118 | Return 0 on success, -1 on error */ |
| 5119 | static |
| 5120 | int charmap_encoding_error( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5121 | 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] | 5122 | PyObject **exceptionObject, |
Walter Dörwald | e5402fb | 2003-08-14 20:25:29 +0000 | [diff] [blame] | 5123 | int *known_errorHandler, PyObject **errorHandler, const char *errors, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5124 | PyObject **res, Py_ssize_t *respos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5125 | { |
| 5126 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5127 | Py_ssize_t repsize; |
| 5128 | Py_ssize_t newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5129 | Py_UNICODE *uni2; |
| 5130 | /* startpos for collecting unencodable chars */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5131 | Py_ssize_t collstartpos = *inpos; |
| 5132 | Py_ssize_t collendpos = *inpos+1; |
| 5133 | Py_ssize_t collpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5134 | char *encoding = "charmap"; |
| 5135 | char *reason = "character maps to <undefined>"; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5136 | charmapencode_result x; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5137 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5138 | /* find all unencodable characters */ |
| 5139 | while (collendpos < size) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 5140 | PyObject *rep; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5141 | if (Py_TYPE(mapping) == &EncodingMapType) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5142 | int res = encoding_map_lookup(p[collendpos], mapping); |
| 5143 | if (res != -1) |
| 5144 | break; |
| 5145 | ++collendpos; |
| 5146 | continue; |
| 5147 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5148 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5149 | rep = charmapencode_lookup(p[collendpos], mapping); |
| 5150 | if (rep==NULL) |
| 5151 | return -1; |
| 5152 | else if (rep!=Py_None) { |
| 5153 | Py_DECREF(rep); |
| 5154 | break; |
| 5155 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5156 | Py_DECREF(rep); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5157 | ++collendpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5158 | } |
| 5159 | /* cache callback name lookup |
| 5160 | * (if not done yet, i.e. it's the first error) */ |
| 5161 | if (*known_errorHandler==-1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5162 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5163 | *known_errorHandler = 1; |
| 5164 | else if (!strcmp(errors, "replace")) |
| 5165 | *known_errorHandler = 2; |
| 5166 | else if (!strcmp(errors, "ignore")) |
| 5167 | *known_errorHandler = 3; |
| 5168 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5169 | *known_errorHandler = 4; |
| 5170 | else |
| 5171 | *known_errorHandler = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5172 | } |
| 5173 | switch (*known_errorHandler) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5174 | case 1: /* strict */ |
| 5175 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5176 | return -1; |
| 5177 | case 2: /* replace */ |
| 5178 | for (collpos = collstartpos; collpos<collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5179 | x = charmapencode_output('?', mapping, res, respos); |
| 5180 | if (x==enc_EXCEPTION) { |
| 5181 | return -1; |
| 5182 | } |
| 5183 | else if (x==enc_FAILED) { |
| 5184 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5185 | return -1; |
| 5186 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5187 | } |
| 5188 | /* fall through */ |
| 5189 | case 3: /* ignore */ |
| 5190 | *inpos = collendpos; |
| 5191 | break; |
| 5192 | case 4: /* xmlcharrefreplace */ |
| 5193 | /* generate replacement (temporarily (mis)uses p) */ |
| 5194 | for (collpos = collstartpos; collpos < collendpos; ++collpos) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5195 | char buffer[2+29+1+1]; |
| 5196 | char *cp; |
| 5197 | sprintf(buffer, "&#%d;", (int)p[collpos]); |
| 5198 | for (cp = buffer; *cp; ++cp) { |
| 5199 | x = charmapencode_output(*cp, mapping, res, respos); |
| 5200 | if (x==enc_EXCEPTION) |
| 5201 | return -1; |
| 5202 | else if (x==enc_FAILED) { |
| 5203 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5204 | return -1; |
| 5205 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5206 | } |
| 5207 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5208 | *inpos = collendpos; |
| 5209 | break; |
| 5210 | default: |
| 5211 | repunicode = unicode_encode_call_errorhandler(errors, errorHandler, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5212 | encoding, reason, p, size, exceptionObject, |
| 5213 | collstartpos, collendpos, &newpos); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5214 | if (repunicode == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5215 | return -1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5216 | if (PyBytes_Check(repunicode)) { |
| 5217 | /* Directly copy bytes result to output. */ |
| 5218 | Py_ssize_t outsize = PyBytes_Size(*res); |
| 5219 | Py_ssize_t requiredsize; |
| 5220 | repsize = PyBytes_Size(repunicode); |
| 5221 | requiredsize = *respos + repsize; |
| 5222 | if (requiredsize > outsize) |
| 5223 | /* Make room for all additional bytes. */ |
| 5224 | if (charmapencode_resize(res, respos, requiredsize)) { |
| 5225 | Py_DECREF(repunicode); |
| 5226 | return -1; |
| 5227 | } |
| 5228 | memcpy(PyBytes_AsString(*res) + *respos, |
| 5229 | PyBytes_AsString(repunicode), repsize); |
| 5230 | *respos += repsize; |
| 5231 | *inpos = newpos; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5232 | Py_DECREF(repunicode); |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5233 | break; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5234 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5235 | /* generate replacement */ |
| 5236 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5237 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5238 | x = charmapencode_output(*uni2, mapping, res, respos); |
| 5239 | if (x==enc_EXCEPTION) { |
| 5240 | return -1; |
| 5241 | } |
| 5242 | else if (x==enc_FAILED) { |
| 5243 | Py_DECREF(repunicode); |
| 5244 | raise_encode_exception(exceptionObject, encoding, p, size, collstartpos, collendpos, reason); |
| 5245 | return -1; |
| 5246 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5247 | } |
| 5248 | *inpos = newpos; |
| 5249 | Py_DECREF(repunicode); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5250 | } |
| 5251 | return 0; |
| 5252 | } |
| 5253 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5254 | PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5255 | Py_ssize_t size, |
| 5256 | PyObject *mapping, |
| 5257 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5258 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5259 | /* output object */ |
| 5260 | PyObject *res = NULL; |
| 5261 | /* current input position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5262 | Py_ssize_t inpos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5263 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5264 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5265 | PyObject *errorHandler = NULL; |
| 5266 | PyObject *exc = NULL; |
| 5267 | /* the following variable is used for caching string comparisons |
| 5268 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 5269 | * 3=ignore, 4=xmlcharrefreplace */ |
| 5270 | int known_errorHandler = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5271 | |
| 5272 | /* Default to Latin-1 */ |
| 5273 | if (mapping == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5274 | return PyUnicode_EncodeLatin1(p, size, errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5275 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5276 | /* allocate enough for a simple encoding without |
| 5277 | replacements, if we need more, we'll resize */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5278 | res = PyBytes_FromStringAndSize(NULL, size); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5279 | if (res == NULL) |
| 5280 | goto onError; |
Marc-André Lemburg | b752077 | 2000-08-14 11:29:19 +0000 | [diff] [blame] | 5281 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5282 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5283 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5284 | while (inpos<size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5285 | /* try to encode it */ |
| 5286 | charmapencode_result x = charmapencode_output(p[inpos], mapping, &res, &respos); |
| 5287 | if (x==enc_EXCEPTION) /* error */ |
| 5288 | goto onError; |
| 5289 | if (x==enc_FAILED) { /* unencodable character */ |
| 5290 | if (charmap_encoding_error(p, size, &inpos, mapping, |
| 5291 | &exc, |
| 5292 | &known_errorHandler, &errorHandler, errors, |
| 5293 | &res, &respos)) { |
| 5294 | goto onError; |
| 5295 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5296 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5297 | else |
| 5298 | /* done with this character => adjust input position */ |
| 5299 | ++inpos; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5300 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5301 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5302 | /* Resize if we allocated to much */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 5303 | if (respos<PyBytes_GET_SIZE(res)) |
Alexandre Vassalotti | 44531cb | 2008-12-27 09:16:49 +0000 | [diff] [blame] | 5304 | if (_PyBytes_Resize(&res, respos) < 0) |
| 5305 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 5306 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5307 | Py_XDECREF(exc); |
| 5308 | Py_XDECREF(errorHandler); |
| 5309 | return res; |
| 5310 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5311 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5312 | Py_XDECREF(res); |
| 5313 | Py_XDECREF(exc); |
| 5314 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5315 | return NULL; |
| 5316 | } |
| 5317 | |
| 5318 | PyObject *PyUnicode_AsCharmapString(PyObject *unicode, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5319 | PyObject *mapping) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5320 | { |
| 5321 | if (!PyUnicode_Check(unicode) || mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5322 | PyErr_BadArgument(); |
| 5323 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5324 | } |
| 5325 | return PyUnicode_EncodeCharmap(PyUnicode_AS_UNICODE(unicode), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5326 | PyUnicode_GET_SIZE(unicode), |
| 5327 | mapping, |
| 5328 | NULL); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5329 | } |
| 5330 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5331 | /* create or adjust a UnicodeTranslateError */ |
| 5332 | static void make_translate_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5333 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5334 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5335 | const char *reason) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5336 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5337 | if (*exceptionObject == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5338 | *exceptionObject = PyUnicodeTranslateError_Create( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5339 | unicode, size, startpos, endpos, reason); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5340 | } |
| 5341 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5342 | if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos)) |
| 5343 | goto onError; |
| 5344 | if (PyUnicodeTranslateError_SetEnd(*exceptionObject, endpos)) |
| 5345 | goto onError; |
| 5346 | if (PyUnicodeTranslateError_SetReason(*exceptionObject, reason)) |
| 5347 | goto onError; |
| 5348 | return; |
| 5349 | onError: |
| 5350 | Py_DECREF(*exceptionObject); |
| 5351 | *exceptionObject = NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5352 | } |
| 5353 | } |
| 5354 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5355 | /* raises a UnicodeTranslateError */ |
| 5356 | static void raise_translate_exception(PyObject **exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5357 | const Py_UNICODE *unicode, Py_ssize_t size, |
| 5358 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5359 | const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5360 | { |
| 5361 | make_translate_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5362 | unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5363 | if (*exceptionObject != NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5364 | PyCodec_StrictErrors(*exceptionObject); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5365 | } |
| 5366 | |
| 5367 | /* error handling callback helper: |
| 5368 | build arguments, call the callback and check the arguments, |
| 5369 | put the result into newpos and return the replacement string, which |
| 5370 | has to be freed by the caller */ |
| 5371 | static PyObject *unicode_translate_call_errorhandler(const char *errors, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5372 | PyObject **errorHandler, |
| 5373 | const char *reason, |
| 5374 | const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, |
| 5375 | Py_ssize_t startpos, Py_ssize_t endpos, |
| 5376 | Py_ssize_t *newpos) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5377 | { |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 5378 | 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] | 5379 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5380 | Py_ssize_t i_newpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5381 | PyObject *restuple; |
| 5382 | PyObject *resunicode; |
| 5383 | |
| 5384 | if (*errorHandler == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5385 | *errorHandler = PyCodec_LookupError(errors); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5386 | if (*errorHandler == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5387 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5388 | } |
| 5389 | |
| 5390 | make_translate_exception(exceptionObject, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5391 | unicode, size, startpos, endpos, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5392 | if (*exceptionObject == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5393 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5394 | |
| 5395 | restuple = PyObject_CallFunctionObjArgs( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5396 | *errorHandler, *exceptionObject, NULL); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5397 | if (restuple == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5398 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5399 | if (!PyTuple_Check(restuple)) { |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 5400 | PyErr_SetString(PyExc_TypeError, &argparse[4]); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5401 | Py_DECREF(restuple); |
| 5402 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5403 | } |
| 5404 | if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5405 | &resunicode, &i_newpos)) { |
| 5406 | Py_DECREF(restuple); |
| 5407 | return NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5408 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5409 | if (i_newpos<0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5410 | *newpos = size+i_newpos; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5411 | else |
| 5412 | *newpos = i_newpos; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5413 | if (*newpos<0 || *newpos>size) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5414 | PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos); |
| 5415 | Py_DECREF(restuple); |
| 5416 | return NULL; |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 5417 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5418 | Py_INCREF(resunicode); |
| 5419 | Py_DECREF(restuple); |
| 5420 | return resunicode; |
| 5421 | } |
| 5422 | |
| 5423 | /* Lookup the character ch in the mapping and put the result in result, |
| 5424 | which must be decrefed by the caller. |
| 5425 | Return 0 on success, -1 on error */ |
| 5426 | static |
| 5427 | int charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result) |
| 5428 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5429 | PyObject *w = PyLong_FromLong((long)c); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5430 | PyObject *x; |
| 5431 | |
| 5432 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5433 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5434 | x = PyObject_GetItem(mapping, w); |
| 5435 | Py_DECREF(w); |
| 5436 | if (x == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5437 | if (PyErr_ExceptionMatches(PyExc_LookupError)) { |
| 5438 | /* No mapping found means: use 1:1 mapping. */ |
| 5439 | PyErr_Clear(); |
| 5440 | *result = NULL; |
| 5441 | return 0; |
| 5442 | } else |
| 5443 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5444 | } |
| 5445 | else if (x == Py_None) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5446 | *result = x; |
| 5447 | return 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5448 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5449 | else if (PyLong_Check(x)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5450 | long value = PyLong_AS_LONG(x); |
| 5451 | long max = PyUnicode_GetMax(); |
| 5452 | if (value < 0 || value > max) { |
| 5453 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5a2f7e60 | 2007-10-24 21:13:09 +0000 | [diff] [blame] | 5454 | "character mapping must be in range(0x%x)", max+1); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5455 | Py_DECREF(x); |
| 5456 | return -1; |
| 5457 | } |
| 5458 | *result = x; |
| 5459 | return 0; |
| 5460 | } |
| 5461 | else if (PyUnicode_Check(x)) { |
| 5462 | *result = x; |
| 5463 | return 0; |
| 5464 | } |
| 5465 | else { |
| 5466 | /* wrong return value */ |
| 5467 | PyErr_SetString(PyExc_TypeError, |
| 5468 | "character mapping must return integer, None or str"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5469 | Py_DECREF(x); |
| 5470 | return -1; |
| 5471 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5472 | } |
| 5473 | /* ensure that *outobj is at least requiredsize characters long, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5474 | if not reallocate and adjust various state variables. |
| 5475 | Return 0 on success, -1 on error */ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5476 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5477 | int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5478 | Py_ssize_t requiredsize) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5479 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5480 | Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5481 | if (requiredsize > oldsize) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5482 | /* remember old output position */ |
| 5483 | Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); |
| 5484 | /* exponentially overallocate to minimize reallocations */ |
| 5485 | if (requiredsize < 2 * oldsize) |
| 5486 | requiredsize = 2 * oldsize; |
| 5487 | if (PyUnicode_Resize(outobj, requiredsize) < 0) |
| 5488 | return -1; |
| 5489 | *outp = PyUnicode_AS_UNICODE(*outobj) + outpos; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5490 | } |
| 5491 | return 0; |
| 5492 | } |
| 5493 | /* lookup the character, put the result in the output string and adjust |
| 5494 | various state variables. Return a new reference to the object that |
| 5495 | was put in the output buffer in *result, or Py_None, if the mapping was |
| 5496 | undefined (in which case no character was written). |
| 5497 | The called must decref result. |
| 5498 | Return 0 on success, -1 on error. */ |
| 5499 | static |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5500 | int charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5501 | Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, |
| 5502 | PyObject **res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5503 | { |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5504 | if (charmaptranslate_lookup(*curinp, mapping, res)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5505 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5506 | if (*res==NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5507 | /* not found => default to 1:1 mapping */ |
| 5508 | *(*outp)++ = *curinp; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5509 | } |
| 5510 | else if (*res==Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5511 | ; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 5512 | else if (PyLong_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5513 | /* no overflow check, because we know that the space is enough */ |
| 5514 | *(*outp)++ = (Py_UNICODE)PyLong_AS_LONG(*res); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5515 | } |
| 5516 | else if (PyUnicode_Check(*res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5517 | Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); |
| 5518 | if (repsize==1) { |
| 5519 | /* no overflow check, because we know that the space is enough */ |
| 5520 | *(*outp)++ = *PyUnicode_AS_UNICODE(*res); |
| 5521 | } |
| 5522 | else if (repsize!=0) { |
| 5523 | /* more than one character */ |
| 5524 | Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + |
| 5525 | (insize - (curinp-startinp)) + |
| 5526 | repsize - 1; |
| 5527 | if (charmaptranslate_makespace(outobj, outp, requiredsize)) |
| 5528 | return -1; |
| 5529 | memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize); |
| 5530 | *outp += repsize; |
| 5531 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5532 | } |
| 5533 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5534 | return -1; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5535 | return 0; |
| 5536 | } |
| 5537 | |
| 5538 | PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5539 | Py_ssize_t size, |
| 5540 | PyObject *mapping, |
| 5541 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5542 | { |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5543 | /* output object */ |
| 5544 | PyObject *res = NULL; |
| 5545 | /* pointers to the beginning and end+1 of input */ |
| 5546 | const Py_UNICODE *startp = p; |
| 5547 | const Py_UNICODE *endp = p + size; |
| 5548 | /* pointer into the output */ |
| 5549 | Py_UNICODE *str; |
| 5550 | /* current output position */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5551 | Py_ssize_t respos = 0; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5552 | char *reason = "character maps to <undefined>"; |
| 5553 | PyObject *errorHandler = NULL; |
| 5554 | PyObject *exc = NULL; |
| 5555 | /* the following variable is used for caching string comparisons |
| 5556 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, |
| 5557 | * 3=ignore, 4=xmlcharrefreplace */ |
| 5558 | int known_errorHandler = -1; |
| 5559 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5560 | if (mapping == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5561 | PyErr_BadArgument(); |
| 5562 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5563 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5564 | |
| 5565 | /* allocate enough for a simple 1:1 translation without |
| 5566 | replacements, if we need more, we'll resize */ |
| 5567 | res = PyUnicode_FromUnicode(NULL, size); |
| 5568 | if (res == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5569 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5570 | if (size == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5571 | return res; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5572 | str = PyUnicode_AS_UNICODE(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5573 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5574 | while (p<endp) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5575 | /* try to encode it */ |
| 5576 | PyObject *x = NULL; |
| 5577 | if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) { |
| 5578 | Py_XDECREF(x); |
| 5579 | goto onError; |
| 5580 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5581 | Py_XDECREF(x); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5582 | if (x!=Py_None) /* it worked => adjust input pointer */ |
| 5583 | ++p; |
| 5584 | else { /* untranslatable character */ |
| 5585 | PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ |
| 5586 | Py_ssize_t repsize; |
| 5587 | Py_ssize_t newpos; |
| 5588 | Py_UNICODE *uni2; |
| 5589 | /* startpos for collecting untranslatable chars */ |
| 5590 | const Py_UNICODE *collstart = p; |
| 5591 | const Py_UNICODE *collend = p+1; |
| 5592 | const Py_UNICODE *coll; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5593 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5594 | /* find all untranslatable characters */ |
| 5595 | while (collend < endp) { |
| 5596 | if (charmaptranslate_lookup(*collend, mapping, &x)) |
| 5597 | goto onError; |
| 5598 | Py_XDECREF(x); |
| 5599 | if (x!=Py_None) |
| 5600 | break; |
| 5601 | ++collend; |
| 5602 | } |
| 5603 | /* cache callback name lookup |
| 5604 | * (if not done yet, i.e. it's the first error) */ |
| 5605 | if (known_errorHandler==-1) { |
| 5606 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5607 | known_errorHandler = 1; |
| 5608 | else if (!strcmp(errors, "replace")) |
| 5609 | known_errorHandler = 2; |
| 5610 | else if (!strcmp(errors, "ignore")) |
| 5611 | known_errorHandler = 3; |
| 5612 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5613 | known_errorHandler = 4; |
| 5614 | else |
| 5615 | known_errorHandler = 0; |
| 5616 | } |
| 5617 | switch (known_errorHandler) { |
| 5618 | case 1: /* strict */ |
| 5619 | raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5620 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5621 | case 2: /* replace */ |
| 5622 | /* No need to check for space, this is a 1:1 replacement */ |
| 5623 | for (coll = collstart; coll<collend; ++coll) |
| 5624 | *str++ = '?'; |
| 5625 | /* fall through */ |
| 5626 | case 3: /* ignore */ |
| 5627 | p = collend; |
| 5628 | break; |
| 5629 | case 4: /* xmlcharrefreplace */ |
| 5630 | /* generate replacement (temporarily (mis)uses p) */ |
| 5631 | for (p = collstart; p < collend; ++p) { |
| 5632 | char buffer[2+29+1+1]; |
| 5633 | char *cp; |
| 5634 | sprintf(buffer, "&#%d;", (int)*p); |
| 5635 | if (charmaptranslate_makespace(&res, &str, |
| 5636 | (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend))) |
| 5637 | goto onError; |
| 5638 | for (cp = buffer; *cp; ++cp) |
| 5639 | *str++ = *cp; |
| 5640 | } |
| 5641 | p = collend; |
| 5642 | break; |
| 5643 | default: |
| 5644 | repunicode = unicode_translate_call_errorhandler(errors, &errorHandler, |
| 5645 | reason, startp, size, &exc, |
| 5646 | collstart-startp, collend-startp, &newpos); |
| 5647 | if (repunicode == NULL) |
| 5648 | goto onError; |
| 5649 | /* generate replacement */ |
| 5650 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5651 | if (charmaptranslate_makespace(&res, &str, |
| 5652 | (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) { |
| 5653 | Py_DECREF(repunicode); |
| 5654 | goto onError; |
| 5655 | } |
| 5656 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) |
| 5657 | *str++ = *uni2; |
| 5658 | p = startp + newpos; |
| 5659 | Py_DECREF(repunicode); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5660 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5661 | } |
| 5662 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5663 | /* Resize if we allocated to much */ |
| 5664 | respos = str-PyUnicode_AS_UNICODE(res); |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 5665 | if (respos<PyUnicode_GET_SIZE(res)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5666 | if (PyUnicode_Resize(&res, respos) < 0) |
| 5667 | goto onError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5668 | } |
| 5669 | Py_XDECREF(exc); |
| 5670 | Py_XDECREF(errorHandler); |
| 5671 | return res; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5672 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5673 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5674 | Py_XDECREF(res); |
| 5675 | Py_XDECREF(exc); |
| 5676 | Py_XDECREF(errorHandler); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5677 | return NULL; |
| 5678 | } |
| 5679 | |
| 5680 | PyObject *PyUnicode_Translate(PyObject *str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5681 | PyObject *mapping, |
| 5682 | const char *errors) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5683 | { |
| 5684 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5685 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5686 | str = PyUnicode_FromObject(str); |
| 5687 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5688 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5689 | result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5690 | PyUnicode_GET_SIZE(str), |
| 5691 | mapping, |
| 5692 | errors); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5693 | Py_DECREF(str); |
| 5694 | return result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5695 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5696 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5697 | Py_XDECREF(str); |
| 5698 | return NULL; |
| 5699 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5700 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5701 | /* --- Decimal Encoder ---------------------------------------------------- */ |
| 5702 | |
| 5703 | int PyUnicode_EncodeDecimal(Py_UNICODE *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5704 | Py_ssize_t length, |
| 5705 | char *output, |
| 5706 | const char *errors) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5707 | { |
| 5708 | Py_UNICODE *p, *end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5709 | PyObject *errorHandler = NULL; |
| 5710 | PyObject *exc = NULL; |
| 5711 | const char *encoding = "decimal"; |
| 5712 | const char *reason = "invalid decimal Unicode string"; |
| 5713 | /* the following variable is used for caching string comparisons |
| 5714 | * -1=not initialized, 0=unknown, 1=strict, 2=replace, 3=ignore, 4=xmlcharrefreplace */ |
| 5715 | int known_errorHandler = -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5716 | |
| 5717 | if (output == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5718 | PyErr_BadArgument(); |
| 5719 | return -1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5720 | } |
| 5721 | |
| 5722 | p = s; |
| 5723 | end = s + length; |
| 5724 | while (p < end) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5725 | register Py_UNICODE ch = *p; |
| 5726 | int decimal; |
| 5727 | PyObject *repunicode; |
| 5728 | Py_ssize_t repsize; |
| 5729 | Py_ssize_t newpos; |
| 5730 | Py_UNICODE *uni2; |
| 5731 | Py_UNICODE *collstart; |
| 5732 | Py_UNICODE *collend; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5733 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5734 | if (Py_UNICODE_ISSPACE(ch)) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5735 | *output++ = ' '; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5736 | ++p; |
| 5737 | continue; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 5738 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5739 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5740 | if (decimal >= 0) { |
| 5741 | *output++ = '0' + decimal; |
| 5742 | ++p; |
| 5743 | continue; |
| 5744 | } |
| 5745 | if (0 < ch && ch < 256) { |
| 5746 | *output++ = (char)ch; |
| 5747 | ++p; |
| 5748 | continue; |
| 5749 | } |
| 5750 | /* All other characters are considered unencodable */ |
| 5751 | collstart = p; |
| 5752 | collend = p+1; |
| 5753 | while (collend < end) { |
| 5754 | if ((0 < *collend && *collend < 256) || |
| 5755 | !Py_UNICODE_ISSPACE(*collend) || |
| 5756 | Py_UNICODE_TODECIMAL(*collend)) |
| 5757 | break; |
| 5758 | } |
| 5759 | /* cache callback name lookup |
| 5760 | * (if not done yet, i.e. it's the first error) */ |
| 5761 | if (known_errorHandler==-1) { |
| 5762 | if ((errors==NULL) || (!strcmp(errors, "strict"))) |
| 5763 | known_errorHandler = 1; |
| 5764 | else if (!strcmp(errors, "replace")) |
| 5765 | known_errorHandler = 2; |
| 5766 | else if (!strcmp(errors, "ignore")) |
| 5767 | known_errorHandler = 3; |
| 5768 | else if (!strcmp(errors, "xmlcharrefreplace")) |
| 5769 | known_errorHandler = 4; |
| 5770 | else |
| 5771 | known_errorHandler = 0; |
| 5772 | } |
| 5773 | switch (known_errorHandler) { |
| 5774 | case 1: /* strict */ |
| 5775 | raise_encode_exception(&exc, encoding, s, length, collstart-s, collend-s, reason); |
| 5776 | goto onError; |
| 5777 | case 2: /* replace */ |
| 5778 | for (p = collstart; p < collend; ++p) |
| 5779 | *output++ = '?'; |
| 5780 | /* fall through */ |
| 5781 | case 3: /* ignore */ |
| 5782 | p = collend; |
| 5783 | break; |
| 5784 | case 4: /* xmlcharrefreplace */ |
| 5785 | /* generate replacement (temporarily (mis)uses p) */ |
| 5786 | for (p = collstart; p < collend; ++p) |
| 5787 | output += sprintf(output, "&#%d;", (int)*p); |
| 5788 | p = collend; |
| 5789 | break; |
| 5790 | default: |
| 5791 | repunicode = unicode_encode_call_errorhandler(errors, &errorHandler, |
| 5792 | encoding, reason, s, length, &exc, |
| 5793 | collstart-s, collend-s, &newpos); |
| 5794 | if (repunicode == NULL) |
| 5795 | goto onError; |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5796 | if (!PyUnicode_Check(repunicode)) { |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5797 | /* Byte results not supported, since they have no decimal property. */ |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 5798 | PyErr_SetString(PyExc_TypeError, "error handler should return unicode"); |
| 5799 | Py_DECREF(repunicode); |
| 5800 | goto onError; |
| 5801 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5802 | /* generate replacement */ |
| 5803 | repsize = PyUnicode_GET_SIZE(repunicode); |
| 5804 | for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2) { |
| 5805 | Py_UNICODE ch = *uni2; |
| 5806 | if (Py_UNICODE_ISSPACE(ch)) |
| 5807 | *output++ = ' '; |
| 5808 | else { |
| 5809 | decimal = Py_UNICODE_TODECIMAL(ch); |
| 5810 | if (decimal >= 0) |
| 5811 | *output++ = '0' + decimal; |
| 5812 | else if (0 < ch && ch < 256) |
| 5813 | *output++ = (char)ch; |
| 5814 | else { |
| 5815 | Py_DECREF(repunicode); |
| 5816 | raise_encode_exception(&exc, encoding, |
| 5817 | s, length, collstart-s, collend-s, reason); |
| 5818 | goto onError; |
| 5819 | } |
| 5820 | } |
| 5821 | } |
| 5822 | p = s + newpos; |
| 5823 | Py_DECREF(repunicode); |
| 5824 | } |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5825 | } |
| 5826 | /* 0-terminate the output string */ |
| 5827 | *output++ = '\0'; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5828 | Py_XDECREF(exc); |
| 5829 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5830 | return 0; |
| 5831 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5832 | onError: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 5833 | Py_XDECREF(exc); |
| 5834 | Py_XDECREF(errorHandler); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 5835 | return -1; |
| 5836 | } |
| 5837 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5838 | /* --- Helpers ------------------------------------------------------------ */ |
| 5839 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 5840 | #include "stringlib/unicodedefs.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5841 | #include "stringlib/fastsearch.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5842 | #include "stringlib/count.h" |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 5843 | /* Include _ParseTupleFinds from find.h */ |
| 5844 | #define FROM_UNICODE |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5845 | #include "stringlib/find.h" |
| 5846 | #include "stringlib/partition.h" |
| 5847 | |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 5848 | #define _Py_InsertThousandsGrouping _PyUnicode_InsertThousandsGrouping |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame] | 5849 | #define _Py_InsertThousandsGroupingLocale _PyUnicode_InsertThousandsGroupingLocale |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 5850 | #include "stringlib/localeutil.h" |
| 5851 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5852 | /* helper macro to fixup start/end slice values */ |
| 5853 | #define FIX_START_END(obj) \ |
| 5854 | if (start < 0) \ |
| 5855 | start += (obj)->length; \ |
| 5856 | if (start < 0) \ |
| 5857 | start = 0; \ |
| 5858 | if (end > (obj)->length) \ |
| 5859 | end = (obj)->length; \ |
| 5860 | if (end < 0) \ |
| 5861 | end += (obj)->length; \ |
| 5862 | if (end < 0) \ |
| 5863 | end = 0; |
| 5864 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5865 | Py_ssize_t PyUnicode_Count(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5866 | PyObject *substr, |
| 5867 | Py_ssize_t start, |
| 5868 | Py_ssize_t end) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5869 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5870 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5871 | PyUnicodeObject* str_obj; |
| 5872 | PyUnicodeObject* sub_obj; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5873 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5874 | str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str); |
| 5875 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5876 | return -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5877 | sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr); |
| 5878 | if (!sub_obj) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5879 | Py_DECREF(str_obj); |
| 5880 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5881 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5882 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5883 | FIX_START_END(str_obj); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5884 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5885 | result = stringlib_count( |
| 5886 | str_obj->str + start, end - start, sub_obj->str, sub_obj->length |
| 5887 | ); |
| 5888 | |
| 5889 | Py_DECREF(sub_obj); |
| 5890 | Py_DECREF(str_obj); |
| 5891 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5892 | return result; |
| 5893 | } |
| 5894 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5895 | Py_ssize_t PyUnicode_Find(PyObject *str, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5896 | PyObject *sub, |
| 5897 | Py_ssize_t start, |
| 5898 | Py_ssize_t end, |
| 5899 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5900 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5901 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5902 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5903 | str = PyUnicode_FromObject(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5904 | if (!str) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5905 | return -2; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5906 | sub = PyUnicode_FromObject(sub); |
| 5907 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5908 | Py_DECREF(str); |
| 5909 | return -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5910 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5911 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5912 | if (direction > 0) |
| 5913 | result = stringlib_find_slice( |
| 5914 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5915 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5916 | start, end |
| 5917 | ); |
| 5918 | else |
| 5919 | result = stringlib_rfind_slice( |
| 5920 | PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str), |
| 5921 | PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub), |
| 5922 | start, end |
| 5923 | ); |
| 5924 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5925 | Py_DECREF(str); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5926 | Py_DECREF(sub); |
| 5927 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5928 | return result; |
| 5929 | } |
| 5930 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5931 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5932 | int tailmatch(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5933 | PyUnicodeObject *substring, |
| 5934 | Py_ssize_t start, |
| 5935 | Py_ssize_t end, |
| 5936 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5937 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5938 | if (substring->length == 0) |
| 5939 | return 1; |
| 5940 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 5941 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5942 | |
| 5943 | end -= substring->length; |
| 5944 | if (end < start) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5945 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5946 | |
| 5947 | if (direction > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5948 | if (Py_UNICODE_MATCH(self, end, substring)) |
| 5949 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5950 | } else { |
| 5951 | if (Py_UNICODE_MATCH(self, start, substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5952 | return 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5953 | } |
| 5954 | |
| 5955 | return 0; |
| 5956 | } |
| 5957 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5958 | Py_ssize_t PyUnicode_Tailmatch(PyObject *str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5959 | PyObject *substr, |
| 5960 | Py_ssize_t start, |
| 5961 | Py_ssize_t end, |
| 5962 | int direction) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5963 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5964 | Py_ssize_t result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5965 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5966 | str = PyUnicode_FromObject(str); |
| 5967 | if (str == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5968 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5969 | substr = PyUnicode_FromObject(substr); |
| 5970 | if (substr == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5971 | Py_DECREF(str); |
| 5972 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5973 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5974 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5975 | result = tailmatch((PyUnicodeObject *)str, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5976 | (PyUnicodeObject *)substr, |
| 5977 | start, end, direction); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5978 | Py_DECREF(str); |
| 5979 | Py_DECREF(substr); |
| 5980 | return result; |
| 5981 | } |
| 5982 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5983 | /* Apply fixfct filter to the Unicode object self and return a |
| 5984 | reference to the modified object */ |
| 5985 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 5986 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5987 | PyObject *fixup(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5988 | int (*fixfct)(PyUnicodeObject *s)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5989 | { |
| 5990 | |
| 5991 | PyUnicodeObject *u; |
| 5992 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5993 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 5994 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 5995 | return NULL; |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 5996 | |
| 5997 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 5998 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 5999 | if (!fixfct(u) && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6000 | /* fixfct should return TRUE if it modified the buffer. If |
| 6001 | FALSE, return a reference to the original buffer instead |
| 6002 | (to save space, not time) */ |
| 6003 | Py_INCREF(self); |
| 6004 | Py_DECREF(u); |
| 6005 | return (PyObject*) self; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6006 | } |
| 6007 | return (PyObject*) u; |
| 6008 | } |
| 6009 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6010 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6011 | int fixupper(PyUnicodeObject *self) |
| 6012 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6013 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6014 | Py_UNICODE *s = self->str; |
| 6015 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6016 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6017 | while (len-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6018 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6019 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6020 | ch = Py_UNICODE_TOUPPER(*s); |
| 6021 | if (ch != *s) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6022 | status = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6023 | *s = ch; |
| 6024 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6025 | s++; |
| 6026 | } |
| 6027 | |
| 6028 | return status; |
| 6029 | } |
| 6030 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6031 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6032 | int fixlower(PyUnicodeObject *self) |
| 6033 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6034 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6035 | Py_UNICODE *s = self->str; |
| 6036 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6037 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6038 | while (len-- > 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6039 | register Py_UNICODE ch; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6040 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6041 | ch = Py_UNICODE_TOLOWER(*s); |
| 6042 | if (ch != *s) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6043 | status = 1; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6044 | *s = ch; |
| 6045 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6046 | s++; |
| 6047 | } |
| 6048 | |
| 6049 | return status; |
| 6050 | } |
| 6051 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6052 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6053 | int fixswapcase(PyUnicodeObject *self) |
| 6054 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6055 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6056 | Py_UNICODE *s = self->str; |
| 6057 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6058 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6059 | while (len-- > 0) { |
| 6060 | if (Py_UNICODE_ISUPPER(*s)) { |
| 6061 | *s = Py_UNICODE_TOLOWER(*s); |
| 6062 | status = 1; |
| 6063 | } else if (Py_UNICODE_ISLOWER(*s)) { |
| 6064 | *s = Py_UNICODE_TOUPPER(*s); |
| 6065 | status = 1; |
| 6066 | } |
| 6067 | s++; |
| 6068 | } |
| 6069 | |
| 6070 | return status; |
| 6071 | } |
| 6072 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6073 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6074 | int fixcapitalize(PyUnicodeObject *self) |
| 6075 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6076 | Py_ssize_t len = self->length; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6077 | Py_UNICODE *s = self->str; |
| 6078 | int status = 0; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6079 | |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6080 | if (len == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6081 | return 0; |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6082 | if (Py_UNICODE_ISLOWER(*s)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6083 | *s = Py_UNICODE_TOUPPER(*s); |
| 6084 | status = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6085 | } |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 6086 | s++; |
| 6087 | while (--len > 0) { |
| 6088 | if (Py_UNICODE_ISUPPER(*s)) { |
| 6089 | *s = Py_UNICODE_TOLOWER(*s); |
| 6090 | status = 1; |
| 6091 | } |
| 6092 | s++; |
| 6093 | } |
| 6094 | return status; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6095 | } |
| 6096 | |
| 6097 | static |
| 6098 | int fixtitle(PyUnicodeObject *self) |
| 6099 | { |
| 6100 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 6101 | register Py_UNICODE *e; |
| 6102 | int previous_is_cased; |
| 6103 | |
| 6104 | /* Shortcut for single character strings */ |
| 6105 | if (PyUnicode_GET_SIZE(self) == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6106 | Py_UNICODE ch = Py_UNICODE_TOTITLE(*p); |
| 6107 | if (*p != ch) { |
| 6108 | *p = ch; |
| 6109 | return 1; |
| 6110 | } |
| 6111 | else |
| 6112 | return 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6113 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6114 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6115 | e = p + PyUnicode_GET_SIZE(self); |
| 6116 | previous_is_cased = 0; |
| 6117 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6118 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6119 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6120 | if (previous_is_cased) |
| 6121 | *p = Py_UNICODE_TOLOWER(ch); |
| 6122 | else |
| 6123 | *p = Py_UNICODE_TOTITLE(ch); |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6124 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6125 | if (Py_UNICODE_ISLOWER(ch) || |
| 6126 | Py_UNICODE_ISUPPER(ch) || |
| 6127 | Py_UNICODE_ISTITLE(ch)) |
| 6128 | previous_is_cased = 1; |
| 6129 | else |
| 6130 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6131 | } |
| 6132 | return 1; |
| 6133 | } |
| 6134 | |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6135 | PyObject * |
| 6136 | PyUnicode_Join(PyObject *separator, PyObject *seq) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6137 | { |
Skip Montanaro | 6543b45 | 2004-09-16 03:28:13 +0000 | [diff] [blame] | 6138 | const Py_UNICODE blank = ' '; |
| 6139 | const Py_UNICODE *sep = ␣ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6140 | Py_ssize_t seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6141 | PyUnicodeObject *res = NULL; /* the result */ |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6142 | Py_UNICODE *res_p; /* pointer to free byte in res's string area */ |
| 6143 | PyObject *fseq; /* PySequence_Fast(seq) */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6144 | Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ |
| 6145 | PyObject **items; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6146 | PyObject *item; |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6147 | Py_ssize_t sz, i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6148 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6149 | fseq = PySequence_Fast(seq, ""); |
| 6150 | if (fseq == NULL) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6151 | return NULL; |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6152 | } |
| 6153 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6154 | /* NOTE: the following code can't call back into Python code, |
| 6155 | * so we are sure that fseq won't be mutated. |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 6156 | */ |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6157 | |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6158 | seqlen = PySequence_Fast_GET_SIZE(fseq); |
| 6159 | /* If empty sequence, return u"". */ |
| 6160 | if (seqlen == 0) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6161 | res = _PyUnicode_New(0); /* empty sequence; return u"" */ |
| 6162 | goto Done; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6163 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6164 | items = PySequence_Fast_ITEMS(fseq); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6165 | /* If singleton sequence with an exact Unicode, return that. */ |
| 6166 | if (seqlen == 1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6167 | item = items[0]; |
| 6168 | if (PyUnicode_CheckExact(item)) { |
| 6169 | Py_INCREF(item); |
| 6170 | res = (PyUnicodeObject *)item; |
| 6171 | goto Done; |
| 6172 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6173 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6174 | else { |
| 6175 | /* Set up sep and seplen */ |
| 6176 | if (separator == NULL) { |
| 6177 | sep = ␣ |
| 6178 | seplen = 1; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6179 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6180 | else { |
| 6181 | if (!PyUnicode_Check(separator)) { |
| 6182 | PyErr_Format(PyExc_TypeError, |
| 6183 | "separator: expected str instance," |
| 6184 | " %.80s found", |
| 6185 | Py_TYPE(separator)->tp_name); |
| 6186 | goto onError; |
| 6187 | } |
| 6188 | sep = PyUnicode_AS_UNICODE(separator); |
| 6189 | seplen = PyUnicode_GET_SIZE(separator); |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6190 | } |
| 6191 | } |
| 6192 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6193 | /* There are at least two things to join, or else we have a subclass |
| 6194 | * of str in the sequence. |
| 6195 | * Do a pre-pass to figure out the total amount of space we'll |
| 6196 | * need (sz), and see whether all argument are strings. |
| 6197 | */ |
| 6198 | sz = 0; |
| 6199 | for (i = 0; i < seqlen; i++) { |
| 6200 | const Py_ssize_t old_sz = sz; |
| 6201 | item = items[i]; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6202 | if (!PyUnicode_Check(item)) { |
| 6203 | PyErr_Format(PyExc_TypeError, |
| 6204 | "sequence item %zd: expected str instance," |
| 6205 | " %.80s found", |
| 6206 | i, Py_TYPE(item)->tp_name); |
| 6207 | goto onError; |
| 6208 | } |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6209 | sz += PyUnicode_GET_SIZE(item); |
| 6210 | if (i != 0) |
| 6211 | sz += seplen; |
| 6212 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
| 6213 | PyErr_SetString(PyExc_OverflowError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6214 | "join() result is too long for a Python string"); |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6215 | goto onError; |
| 6216 | } |
| 6217 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6218 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6219 | res = _PyUnicode_New(sz); |
| 6220 | if (res == NULL) |
| 6221 | goto onError; |
Tim Peters | 91879ab | 2004-08-27 22:35:44 +0000 | [diff] [blame] | 6222 | |
Antoine Pitrou | af14b79 | 2008-08-07 21:50:41 +0000 | [diff] [blame] | 6223 | /* Catenate everything. */ |
| 6224 | res_p = PyUnicode_AS_UNICODE(res); |
| 6225 | for (i = 0; i < seqlen; ++i) { |
| 6226 | Py_ssize_t itemlen; |
| 6227 | item = items[i]; |
| 6228 | itemlen = PyUnicode_GET_SIZE(item); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6229 | /* Copy item, and maybe the separator. */ |
| 6230 | if (i) { |
| 6231 | Py_UNICODE_COPY(res_p, sep, seplen); |
| 6232 | res_p += seplen; |
| 6233 | } |
| 6234 | Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen); |
| 6235 | res_p += itemlen; |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6236 | } |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6237 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6238 | Done: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6239 | Py_DECREF(fseq); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6240 | return (PyObject *)res; |
| 6241 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6242 | onError: |
Tim Peters | 05eba1f | 2004-08-27 21:32:02 +0000 | [diff] [blame] | 6243 | Py_DECREF(fseq); |
Tim Peters | 8ce9f16 | 2004-08-27 01:49:32 +0000 | [diff] [blame] | 6244 | Py_XDECREF(res); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6245 | return NULL; |
| 6246 | } |
| 6247 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6248 | static |
| 6249 | PyUnicodeObject *pad(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6250 | Py_ssize_t left, |
| 6251 | Py_ssize_t right, |
| 6252 | Py_UNICODE fill) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6253 | { |
| 6254 | PyUnicodeObject *u; |
| 6255 | |
| 6256 | if (left < 0) |
| 6257 | left = 0; |
| 6258 | if (right < 0) |
| 6259 | right = 0; |
| 6260 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6261 | if (left == 0 && right == 0 && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6262 | Py_INCREF(self); |
| 6263 | return self; |
| 6264 | } |
| 6265 | |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 6266 | if (left > PY_SSIZE_T_MAX - self->length || |
| 6267 | right > PY_SSIZE_T_MAX - (left + self->length)) { |
| 6268 | PyErr_SetString(PyExc_OverflowError, "padded string is too long"); |
| 6269 | return NULL; |
| 6270 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6271 | u = _PyUnicode_New(left + self->length + right); |
| 6272 | if (u) { |
| 6273 | if (left) |
| 6274 | Py_UNICODE_FILL(u->str, fill, left); |
| 6275 | Py_UNICODE_COPY(u->str + left, self->str, self->length); |
| 6276 | if (right) |
| 6277 | Py_UNICODE_FILL(u->str + left + self->length, fill, right); |
| 6278 | } |
| 6279 | |
| 6280 | return u; |
| 6281 | } |
| 6282 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6283 | #define SPLIT_APPEND(data, left, right) \ |
| 6284 | str = PyUnicode_FromUnicode((data) + (left), (right) - (left)); \ |
| 6285 | if (!str) \ |
| 6286 | goto onError; \ |
| 6287 | if (PyList_Append(list, str)) { \ |
| 6288 | Py_DECREF(str); \ |
| 6289 | goto onError; \ |
| 6290 | } \ |
| 6291 | else \ |
| 6292 | Py_DECREF(str); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6293 | |
| 6294 | static |
| 6295 | PyObject *split_whitespace(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6296 | PyObject *list, |
| 6297 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6298 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6299 | register Py_ssize_t i; |
| 6300 | register Py_ssize_t j; |
| 6301 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6302 | PyObject *str; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6303 | register const Py_UNICODE *buf = self->str; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6304 | |
| 6305 | for (i = j = 0; i < len; ) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6306 | /* find a token */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6307 | while (i < len && Py_UNICODE_ISSPACE(buf[i])) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6308 | i++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6309 | j = i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6310 | while (i < len && !Py_UNICODE_ISSPACE(buf[i])) |
| 6311 | i++; |
| 6312 | if (j < i) { |
| 6313 | if (maxcount-- <= 0) |
| 6314 | break; |
| 6315 | SPLIT_APPEND(buf, j, i); |
| 6316 | while (i < len && Py_UNICODE_ISSPACE(buf[i])) |
| 6317 | i++; |
| 6318 | j = i; |
| 6319 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6320 | } |
| 6321 | if (j < len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6322 | SPLIT_APPEND(buf, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6323 | } |
| 6324 | return list; |
| 6325 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6326 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6327 | Py_DECREF(list); |
| 6328 | return NULL; |
| 6329 | } |
| 6330 | |
| 6331 | PyObject *PyUnicode_Splitlines(PyObject *string, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6332 | int keepends) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6333 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6334 | register Py_ssize_t i; |
| 6335 | register Py_ssize_t j; |
| 6336 | Py_ssize_t len; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6337 | PyObject *list; |
| 6338 | PyObject *str; |
| 6339 | Py_UNICODE *data; |
| 6340 | |
| 6341 | string = PyUnicode_FromObject(string); |
| 6342 | if (string == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6343 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6344 | data = PyUnicode_AS_UNICODE(string); |
| 6345 | len = PyUnicode_GET_SIZE(string); |
| 6346 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6347 | list = PyList_New(0); |
| 6348 | if (!list) |
| 6349 | goto onError; |
| 6350 | |
| 6351 | for (i = j = 0; i < len; ) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6352 | Py_ssize_t eol; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6353 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6354 | /* Find a line and append it */ |
| 6355 | while (i < len && !BLOOM_LINEBREAK(data[i])) |
| 6356 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6357 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6358 | /* Skip the line break reading CRLF as one line break */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6359 | eol = i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6360 | if (i < len) { |
| 6361 | if (data[i] == '\r' && i + 1 < len && |
| 6362 | data[i+1] == '\n') |
| 6363 | i += 2; |
| 6364 | else |
| 6365 | i++; |
| 6366 | if (keepends) |
| 6367 | eol = i; |
| 6368 | } |
| 6369 | SPLIT_APPEND(data, j, eol); |
| 6370 | j = i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6371 | } |
| 6372 | if (j < len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6373 | SPLIT_APPEND(data, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6374 | } |
| 6375 | |
| 6376 | Py_DECREF(string); |
| 6377 | return list; |
| 6378 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6379 | onError: |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 6380 | Py_XDECREF(list); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6381 | Py_DECREF(string); |
| 6382 | return NULL; |
| 6383 | } |
| 6384 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6385 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6386 | PyObject *split_char(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6387 | PyObject *list, |
| 6388 | Py_UNICODE ch, |
| 6389 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6390 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6391 | register Py_ssize_t i; |
| 6392 | register Py_ssize_t j; |
| 6393 | Py_ssize_t len = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6394 | PyObject *str; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6395 | register const Py_UNICODE *buf = self->str; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6396 | |
| 6397 | for (i = j = 0; i < len; ) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6398 | if (buf[i] == ch) { |
| 6399 | if (maxcount-- <= 0) |
| 6400 | break; |
| 6401 | SPLIT_APPEND(buf, j, i); |
| 6402 | i = j = i + 1; |
| 6403 | } else |
| 6404 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6405 | } |
| 6406 | if (j <= len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6407 | SPLIT_APPEND(buf, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6408 | } |
| 6409 | return list; |
| 6410 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6411 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6412 | Py_DECREF(list); |
| 6413 | return NULL; |
| 6414 | } |
| 6415 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6416 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6417 | PyObject *split_substring(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6418 | PyObject *list, |
| 6419 | PyUnicodeObject *substring, |
| 6420 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6421 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6422 | register Py_ssize_t i; |
| 6423 | register Py_ssize_t j; |
| 6424 | Py_ssize_t len = self->length; |
| 6425 | Py_ssize_t sublen = substring->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6426 | PyObject *str; |
| 6427 | |
Guido van Rossum | cda4f9a | 2000-12-19 02:23:19 +0000 | [diff] [blame] | 6428 | for (i = j = 0; i <= len - sublen; ) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6429 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 6430 | if (maxcount-- <= 0) |
| 6431 | break; |
| 6432 | SPLIT_APPEND(self->str, j, i); |
| 6433 | i = j = i + sublen; |
| 6434 | } else |
| 6435 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6436 | } |
| 6437 | if (j <= len) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6438 | SPLIT_APPEND(self->str, j, len); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6439 | } |
| 6440 | return list; |
| 6441 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6442 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6443 | Py_DECREF(list); |
| 6444 | return NULL; |
| 6445 | } |
| 6446 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6447 | static |
| 6448 | PyObject *rsplit_whitespace(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6449 | PyObject *list, |
| 6450 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6451 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6452 | register Py_ssize_t i; |
| 6453 | register Py_ssize_t j; |
| 6454 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6455 | PyObject *str; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6456 | register const Py_UNICODE *buf = self->str; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6457 | |
| 6458 | for (i = j = len - 1; i >= 0; ) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6459 | /* find a token */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6460 | while (i >= 0 && Py_UNICODE_ISSPACE(buf[i])) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6461 | i--; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6462 | j = i; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6463 | while (i >= 0 && !Py_UNICODE_ISSPACE(buf[i])) |
| 6464 | i--; |
| 6465 | if (j > i) { |
| 6466 | if (maxcount-- <= 0) |
| 6467 | break; |
| 6468 | SPLIT_APPEND(buf, i + 1, j + 1); |
| 6469 | while (i >= 0 && Py_UNICODE_ISSPACE(buf[i])) |
| 6470 | i--; |
| 6471 | j = i; |
| 6472 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6473 | } |
| 6474 | if (j >= 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6475 | SPLIT_APPEND(buf, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6476 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6477 | if (PyList_Reverse(list) < 0) |
| 6478 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6479 | return list; |
| 6480 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6481 | onError: |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6482 | Py_DECREF(list); |
| 6483 | return NULL; |
| 6484 | } |
| 6485 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6486 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6487 | PyObject *rsplit_char(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6488 | PyObject *list, |
| 6489 | Py_UNICODE ch, |
| 6490 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6491 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6492 | register Py_ssize_t i; |
| 6493 | register Py_ssize_t j; |
| 6494 | Py_ssize_t len = self->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6495 | PyObject *str; |
Christian Heimes | 190d79e | 2008-01-30 11:58:22 +0000 | [diff] [blame] | 6496 | register const Py_UNICODE *buf = self->str; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6497 | |
| 6498 | for (i = j = len - 1; i >= 0; ) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6499 | if (buf[i] == ch) { |
| 6500 | if (maxcount-- <= 0) |
| 6501 | break; |
| 6502 | SPLIT_APPEND(buf, i + 1, j + 1); |
| 6503 | j = i = i - 1; |
| 6504 | } else |
| 6505 | i--; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6506 | } |
Hye-Shik Chang | 7fc4cf5 | 2003-12-23 09:10:16 +0000 | [diff] [blame] | 6507 | if (j >= -1) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6508 | SPLIT_APPEND(buf, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6509 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6510 | if (PyList_Reverse(list) < 0) |
| 6511 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6512 | return list; |
| 6513 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6514 | onError: |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6515 | Py_DECREF(list); |
| 6516 | return NULL; |
| 6517 | } |
| 6518 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6519 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6520 | PyObject *rsplit_substring(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6521 | PyObject *list, |
| 6522 | PyUnicodeObject *substring, |
| 6523 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6524 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6525 | register Py_ssize_t i; |
| 6526 | register Py_ssize_t j; |
| 6527 | Py_ssize_t len = self->length; |
| 6528 | Py_ssize_t sublen = substring->length; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6529 | PyObject *str; |
| 6530 | |
| 6531 | for (i = len - sublen, j = len; i >= 0; ) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6532 | if (Py_UNICODE_MATCH(self, i, substring)) { |
| 6533 | if (maxcount-- <= 0) |
| 6534 | break; |
| 6535 | SPLIT_APPEND(self->str, i + sublen, j); |
| 6536 | j = i; |
| 6537 | i -= sublen; |
| 6538 | } else |
| 6539 | i--; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6540 | } |
| 6541 | if (j >= 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6542 | SPLIT_APPEND(self->str, 0, j); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6543 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6544 | if (PyList_Reverse(list) < 0) |
| 6545 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6546 | return list; |
| 6547 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6548 | onError: |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6549 | Py_DECREF(list); |
| 6550 | return NULL; |
| 6551 | } |
| 6552 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6553 | #undef SPLIT_APPEND |
| 6554 | |
| 6555 | static |
| 6556 | PyObject *split(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6557 | PyUnicodeObject *substring, |
| 6558 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6559 | { |
| 6560 | PyObject *list; |
| 6561 | |
| 6562 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6563 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6564 | |
| 6565 | list = PyList_New(0); |
| 6566 | if (!list) |
| 6567 | return NULL; |
| 6568 | |
| 6569 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6570 | return split_whitespace(self,list,maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6571 | |
| 6572 | else if (substring->length == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6573 | return split_char(self,list,substring->str[0],maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6574 | |
| 6575 | else if (substring->length == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6576 | Py_DECREF(list); |
| 6577 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 6578 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6579 | } |
| 6580 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6581 | return split_substring(self,list,substring,maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6582 | } |
| 6583 | |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6584 | static |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6585 | PyObject *rsplit(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6586 | PyUnicodeObject *substring, |
| 6587 | Py_ssize_t maxcount) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6588 | { |
| 6589 | PyObject *list; |
| 6590 | |
| 6591 | if (maxcount < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6592 | maxcount = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6593 | |
| 6594 | list = PyList_New(0); |
| 6595 | if (!list) |
| 6596 | return NULL; |
| 6597 | |
| 6598 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6599 | return rsplit_whitespace(self,list,maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6600 | |
| 6601 | else if (substring->length == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6602 | return rsplit_char(self,list,substring->str[0],maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6603 | |
| 6604 | else if (substring->length == 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6605 | Py_DECREF(list); |
| 6606 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 6607 | return NULL; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6608 | } |
| 6609 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6610 | return rsplit_substring(self,list,substring,maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 6611 | } |
| 6612 | |
| 6613 | static |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6614 | PyObject *replace(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6615 | PyUnicodeObject *str1, |
| 6616 | PyUnicodeObject *str2, |
| 6617 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6618 | { |
| 6619 | PyUnicodeObject *u; |
| 6620 | |
| 6621 | if (maxcount < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6622 | maxcount = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6623 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6624 | if (str1->length == str2->length) { |
| 6625 | /* same length */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6626 | Py_ssize_t i; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6627 | if (str1->length == 1) { |
| 6628 | /* replace characters */ |
| 6629 | Py_UNICODE u1, u2; |
| 6630 | if (!findchar(self->str, self->length, str1->str[0])) |
| 6631 | goto nothing; |
| 6632 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6633 | if (!u) |
| 6634 | return NULL; |
| 6635 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6636 | u1 = str1->str[0]; |
| 6637 | u2 = str2->str[0]; |
| 6638 | for (i = 0; i < u->length; i++) |
| 6639 | if (u->str[i] == u1) { |
| 6640 | if (--maxcount < 0) |
| 6641 | break; |
| 6642 | u->str[i] = u2; |
| 6643 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6644 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6645 | i = fastsearch( |
| 6646 | self->str, self->length, str1->str, str1->length, FAST_SEARCH |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6647 | ); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6648 | if (i < 0) |
| 6649 | goto nothing; |
| 6650 | u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length); |
| 6651 | if (!u) |
| 6652 | return NULL; |
| 6653 | Py_UNICODE_COPY(u->str, self->str, self->length); |
| 6654 | while (i <= self->length - str1->length) |
| 6655 | if (Py_UNICODE_MATCH(self, i, str1)) { |
| 6656 | if (--maxcount < 0) |
| 6657 | break; |
| 6658 | Py_UNICODE_COPY(u->str+i, str2->str, str2->length); |
| 6659 | i += str1->length; |
| 6660 | } else |
| 6661 | i++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6662 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6663 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6664 | |
| 6665 | Py_ssize_t n, i, j, e; |
| 6666 | Py_ssize_t product, new_size, delta; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6667 | Py_UNICODE *p; |
| 6668 | |
| 6669 | /* replace strings */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6670 | n = stringlib_count(self->str, self->length, str1->str, str1->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6671 | if (n > maxcount) |
| 6672 | n = maxcount; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6673 | if (n == 0) |
| 6674 | goto nothing; |
| 6675 | /* new_size = self->length + n * (str2->length - str1->length)); */ |
| 6676 | delta = (str2->length - str1->length); |
| 6677 | if (delta == 0) { |
| 6678 | new_size = self->length; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6679 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6680 | product = n * (str2->length - str1->length); |
| 6681 | if ((product / (str2->length - str1->length)) != n) { |
| 6682 | PyErr_SetString(PyExc_OverflowError, |
| 6683 | "replace string is too long"); |
| 6684 | return NULL; |
| 6685 | } |
| 6686 | new_size = self->length + product; |
| 6687 | if (new_size < 0) { |
| 6688 | PyErr_SetString(PyExc_OverflowError, |
| 6689 | "replace string is too long"); |
| 6690 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6691 | } |
| 6692 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6693 | u = _PyUnicode_New(new_size); |
| 6694 | if (!u) |
| 6695 | return NULL; |
| 6696 | i = 0; |
| 6697 | p = u->str; |
| 6698 | e = self->length - str1->length; |
| 6699 | if (str1->length > 0) { |
| 6700 | while (n-- > 0) { |
| 6701 | /* look for next match */ |
| 6702 | j = i; |
| 6703 | while (j <= e) { |
| 6704 | if (Py_UNICODE_MATCH(self, j, str1)) |
| 6705 | break; |
| 6706 | j++; |
| 6707 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6708 | if (j > i) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6709 | if (j > e) |
| 6710 | break; |
| 6711 | /* copy unchanged part [i:j] */ |
| 6712 | Py_UNICODE_COPY(p, self->str+i, j-i); |
| 6713 | p += j - i; |
| 6714 | } |
| 6715 | /* copy substitution string */ |
| 6716 | if (str2->length > 0) { |
| 6717 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6718 | p += str2->length; |
| 6719 | } |
| 6720 | i = j + str1->length; |
| 6721 | } |
| 6722 | if (i < self->length) |
| 6723 | /* copy tail [i:] */ |
| 6724 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6725 | } else { |
| 6726 | /* interleave */ |
| 6727 | while (n > 0) { |
| 6728 | Py_UNICODE_COPY(p, str2->str, str2->length); |
| 6729 | p += str2->length; |
| 6730 | if (--n <= 0) |
| 6731 | break; |
| 6732 | *p++ = self->str[i++]; |
| 6733 | } |
| 6734 | Py_UNICODE_COPY(p, self->str+i, self->length-i); |
| 6735 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6736 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6737 | return (PyObject *) u; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6738 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6739 | nothing: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 6740 | /* nothing to replace; return original string (when possible) */ |
| 6741 | if (PyUnicode_CheckExact(self)) { |
| 6742 | Py_INCREF(self); |
| 6743 | return (PyObject *) self; |
| 6744 | } |
| 6745 | return PyUnicode_FromUnicode(self->str, self->length); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6746 | } |
| 6747 | |
| 6748 | /* --- Unicode Object Methods --------------------------------------------- */ |
| 6749 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6750 | PyDoc_STRVAR(title__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6751 | "S.title() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6752 | \n\ |
| 6753 | 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] | 6754 | characters, all remaining cased characters have lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6755 | |
| 6756 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6757 | unicode_title(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6758 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6759 | return fixup(self, fixtitle); |
| 6760 | } |
| 6761 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6762 | PyDoc_STRVAR(capitalize__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6763 | "S.capitalize() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6764 | \n\ |
| 6765 | Return a capitalized version of S, i.e. make the first character\n\ |
Senthil Kumaran | 74ceac2 | 2010-07-05 12:04:23 +0000 | [diff] [blame] | 6766 | have upper case and the rest lower case."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6767 | |
| 6768 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6769 | unicode_capitalize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6770 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6771 | return fixup(self, fixcapitalize); |
| 6772 | } |
| 6773 | |
| 6774 | #if 0 |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6775 | PyDoc_STRVAR(capwords__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6776 | "S.capwords() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6777 | \n\ |
| 6778 | 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] | 6779 | normalized whitespace (all whitespace strings are replaced by ' ')."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6780 | |
| 6781 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 6782 | unicode_capwords(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6783 | { |
| 6784 | PyObject *list; |
| 6785 | PyObject *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6786 | Py_ssize_t i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6787 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6788 | /* Split into words */ |
| 6789 | list = split(self, NULL, -1); |
| 6790 | if (!list) |
| 6791 | return NULL; |
| 6792 | |
| 6793 | /* Capitalize each word */ |
| 6794 | for (i = 0; i < PyList_GET_SIZE(list); i++) { |
| 6795 | item = fixup((PyUnicodeObject *)PyList_GET_ITEM(list, i), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6796 | fixcapitalize); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6797 | if (item == NULL) |
| 6798 | goto onError; |
| 6799 | Py_DECREF(PyList_GET_ITEM(list, i)); |
| 6800 | PyList_SET_ITEM(list, i, item); |
| 6801 | } |
| 6802 | |
| 6803 | /* Join the words to form a new string */ |
| 6804 | item = PyUnicode_Join(NULL, list); |
| 6805 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6806 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6807 | Py_DECREF(list); |
| 6808 | return (PyObject *)item; |
| 6809 | } |
| 6810 | #endif |
| 6811 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6812 | /* Argument converter. Coerces to a single unicode character */ |
| 6813 | |
| 6814 | static int |
| 6815 | convert_uc(PyObject *obj, void *addr) |
| 6816 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6817 | Py_UNICODE *fillcharloc = (Py_UNICODE *)addr; |
| 6818 | PyObject *uniobj; |
| 6819 | Py_UNICODE *unistr; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6820 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6821 | uniobj = PyUnicode_FromObject(obj); |
| 6822 | if (uniobj == NULL) { |
| 6823 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6824 | "The fill character cannot be converted to Unicode"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6825 | return 0; |
| 6826 | } |
| 6827 | if (PyUnicode_GET_SIZE(uniobj) != 1) { |
| 6828 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6829 | "The fill character must be exactly one character long"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6830 | Py_DECREF(uniobj); |
| 6831 | return 0; |
| 6832 | } |
| 6833 | unistr = PyUnicode_AS_UNICODE(uniobj); |
| 6834 | *fillcharloc = unistr[0]; |
| 6835 | Py_DECREF(uniobj); |
| 6836 | return 1; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6837 | } |
| 6838 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6839 | PyDoc_STRVAR(center__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6840 | "S.center(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6841 | \n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 6842 | Return S centered in a string of length width. Padding is\n\ |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6843 | done using the specified fill character (default is a space)"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6844 | |
| 6845 | static PyObject * |
| 6846 | unicode_center(PyUnicodeObject *self, PyObject *args) |
| 6847 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6848 | Py_ssize_t marg, left; |
| 6849 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6850 | Py_UNICODE fillchar = ' '; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6851 | |
Thomas Wouters | de01774 | 2006-02-16 19:34:37 +0000 | [diff] [blame] | 6852 | if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6853 | return NULL; |
| 6854 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 6855 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6856 | Py_INCREF(self); |
| 6857 | return (PyObject*) self; |
| 6858 | } |
| 6859 | |
| 6860 | marg = width - self->length; |
| 6861 | left = marg / 2 + (marg & width & 1); |
| 6862 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 6863 | return (PyObject*) pad(self, left, marg - left, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6864 | } |
| 6865 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6866 | #if 0 |
| 6867 | |
| 6868 | /* This code should go into some future Unicode collation support |
| 6869 | module. The basic comparison should compare ordinals on a naive |
Georg Brandl | c6c3178 | 2009-06-08 13:41:29 +0000 | [diff] [blame] | 6870 | basis (this is what Java does and thus Jython too). */ |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6871 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6872 | /* speedy UTF-16 code point order comparison */ |
| 6873 | /* gleaned from: */ |
| 6874 | /* http://www-4.ibm.com/software/developer/library/utf16.html?dwzone=unicode */ |
| 6875 | |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6876 | static short utf16Fixup[32] = |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6877 | { |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6878 | 0, 0, 0, 0, 0, 0, 0, 0, |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6879 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 6880 | 0, 0, 0, 0, 0, 0, 0, 0, |
Marc-André Lemburg | e12896e | 2000-07-07 17:51:08 +0000 | [diff] [blame] | 6881 | 0, 0, 0, 0x2000, -0x800, -0x800, -0x800, -0x800 |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6882 | }; |
| 6883 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6884 | static int |
| 6885 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6886 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6887 | Py_ssize_t len1, len2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6888 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6889 | Py_UNICODE *s1 = str1->str; |
| 6890 | Py_UNICODE *s2 = str2->str; |
| 6891 | |
| 6892 | len1 = str1->length; |
| 6893 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6894 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6895 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6896 | Py_UNICODE c1, c2; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6897 | |
| 6898 | c1 = *s1++; |
| 6899 | c2 = *s2++; |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6900 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6901 | if (c1 > (1<<11) * 26) |
| 6902 | c1 += utf16Fixup[c1>>11]; |
| 6903 | if (c2 > (1<<11) * 26) |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6904 | c2 += utf16Fixup[c2>>11]; |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6905 | /* now c1 and c2 are in UTF-32-compatible order */ |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6906 | |
| 6907 | if (c1 != c2) |
| 6908 | return (c1 < c2) ? -1 : 1; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6909 | |
Marc-André Lemburg | 1e7205a | 2000-07-04 09:51:07 +0000 | [diff] [blame] | 6910 | len1--; len2--; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6911 | } |
| 6912 | |
| 6913 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6914 | } |
| 6915 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6916 | #else |
| 6917 | |
| 6918 | static int |
| 6919 | unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) |
| 6920 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 6921 | register Py_ssize_t len1, len2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6922 | |
| 6923 | Py_UNICODE *s1 = str1->str; |
| 6924 | Py_UNICODE *s2 = str2->str; |
| 6925 | |
| 6926 | len1 = str1->length; |
| 6927 | len2 = str2->length; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6928 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6929 | while (len1 > 0 && len2 > 0) { |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 6930 | Py_UNICODE c1, c2; |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6931 | |
Fredrik Lundh | 45714e9 | 2001-06-26 16:39:36 +0000 | [diff] [blame] | 6932 | c1 = *s1++; |
| 6933 | c2 = *s2++; |
| 6934 | |
| 6935 | if (c1 != c2) |
| 6936 | return (c1 < c2) ? -1 : 1; |
| 6937 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 6938 | len1--; len2--; |
| 6939 | } |
| 6940 | |
| 6941 | return (len1 < len2) ? -1 : (len1 != len2); |
| 6942 | } |
| 6943 | |
| 6944 | #endif |
| 6945 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6946 | int PyUnicode_Compare(PyObject *left, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6947 | PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6948 | { |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6949 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) |
| 6950 | return unicode_compare((PyUnicodeObject *)left, |
| 6951 | (PyUnicodeObject *)right); |
Guido van Rossum | 09dc34f | 2007-05-04 04:17:33 +0000 | [diff] [blame] | 6952 | PyErr_Format(PyExc_TypeError, |
| 6953 | "Can't compare %.100s and %.100s", |
| 6954 | left->ob_type->tp_name, |
| 6955 | right->ob_type->tp_name); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 6956 | return -1; |
| 6957 | } |
| 6958 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6959 | int |
| 6960 | PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) |
| 6961 | { |
| 6962 | int i; |
| 6963 | Py_UNICODE *id; |
| 6964 | assert(PyUnicode_Check(uni)); |
| 6965 | id = PyUnicode_AS_UNICODE(uni); |
| 6966 | /* Compare Unicode string and source character set string */ |
| 6967 | for (i = 0; id[i] && str[i]; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6968 | if (id[i] != str[i]) |
| 6969 | return ((int)id[i] < (int)str[i]) ? -1 : 1; |
Benjamin Peterson | bb81c8c | 2010-01-09 21:54:39 +0000 | [diff] [blame] | 6970 | /* This check keeps Python strings that end in '\0' from comparing equal |
| 6971 | to C strings identical up to that point. */ |
| 6972 | if (PyUnicode_GET_SIZE(uni) != i) |
| 6973 | /* We'll say the Python string is longer. */ |
| 6974 | return 1; |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6975 | if (id[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6976 | return 1; /* uni is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6977 | if (str[i]) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6978 | return -1; /* str is longer */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 6979 | return 0; |
| 6980 | } |
| 6981 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6982 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 6983 | #define TEST_COND(cond) \ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6984 | ((cond) ? Py_True : Py_False) |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6985 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 6986 | PyObject *PyUnicode_RichCompare(PyObject *left, |
| 6987 | PyObject *right, |
| 6988 | int op) |
| 6989 | { |
| 6990 | int result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 6991 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 6992 | if (PyUnicode_Check(left) && PyUnicode_Check(right)) { |
| 6993 | PyObject *v; |
| 6994 | if (((PyUnicodeObject *) left)->length != |
| 6995 | ((PyUnicodeObject *) right)->length) { |
| 6996 | if (op == Py_EQ) { |
| 6997 | Py_INCREF(Py_False); |
| 6998 | return Py_False; |
| 6999 | } |
| 7000 | if (op == Py_NE) { |
| 7001 | Py_INCREF(Py_True); |
| 7002 | return Py_True; |
| 7003 | } |
| 7004 | } |
| 7005 | if (left == right) |
| 7006 | result = 0; |
| 7007 | else |
| 7008 | result = unicode_compare((PyUnicodeObject *)left, |
| 7009 | (PyUnicodeObject *)right); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7010 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 7011 | /* Convert the return value to a Boolean */ |
| 7012 | switch (op) { |
| 7013 | case Py_EQ: |
| 7014 | v = TEST_COND(result == 0); |
| 7015 | break; |
| 7016 | case Py_NE: |
| 7017 | v = TEST_COND(result != 0); |
| 7018 | break; |
| 7019 | case Py_LE: |
| 7020 | v = TEST_COND(result <= 0); |
| 7021 | break; |
| 7022 | case Py_GE: |
| 7023 | v = TEST_COND(result >= 0); |
| 7024 | break; |
| 7025 | case Py_LT: |
| 7026 | v = TEST_COND(result == -1); |
| 7027 | break; |
| 7028 | case Py_GT: |
| 7029 | v = TEST_COND(result == 1); |
| 7030 | break; |
| 7031 | default: |
| 7032 | PyErr_BadArgument(); |
| 7033 | return NULL; |
| 7034 | } |
| 7035 | Py_INCREF(v); |
| 7036 | return v; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 7037 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7038 | |
Antoine Pitrou | 51f3ef9 | 2008-12-20 13:14:23 +0000 | [diff] [blame] | 7039 | Py_INCREF(Py_NotImplemented); |
| 7040 | return Py_NotImplemented; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 7041 | } |
| 7042 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7043 | int PyUnicode_Contains(PyObject *container, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7044 | PyObject *element) |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7045 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7046 | PyObject *str, *sub; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7047 | int result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7048 | |
| 7049 | /* Coerce the two arguments */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7050 | sub = PyUnicode_FromObject(element); |
| 7051 | if (!sub) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7052 | PyErr_Format(PyExc_TypeError, |
| 7053 | "'in <string>' requires string as left operand, not %s", |
| 7054 | element->ob_type->tp_name); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7055 | return -1; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7056 | } |
| 7057 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7058 | str = PyUnicode_FromObject(container); |
| 7059 | if (!str) { |
| 7060 | Py_DECREF(sub); |
| 7061 | return -1; |
| 7062 | } |
| 7063 | |
| 7064 | result = stringlib_contains_obj(str, sub); |
| 7065 | |
| 7066 | Py_DECREF(str); |
| 7067 | Py_DECREF(sub); |
| 7068 | |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7069 | return result; |
Guido van Rossum | 403d68b | 2000-03-13 15:55:09 +0000 | [diff] [blame] | 7070 | } |
| 7071 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7072 | /* Concat to string or Unicode object giving a new Unicode object. */ |
| 7073 | |
| 7074 | PyObject *PyUnicode_Concat(PyObject *left, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7075 | PyObject *right) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7076 | { |
| 7077 | PyUnicodeObject *u = NULL, *v = NULL, *w; |
| 7078 | |
| 7079 | /* Coerce the two arguments */ |
| 7080 | u = (PyUnicodeObject *)PyUnicode_FromObject(left); |
| 7081 | if (u == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7082 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7083 | v = (PyUnicodeObject *)PyUnicode_FromObject(right); |
| 7084 | if (v == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7085 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7086 | |
| 7087 | /* Shortcuts */ |
| 7088 | if (v == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7089 | Py_DECREF(v); |
| 7090 | return (PyObject *)u; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7091 | } |
| 7092 | if (u == unicode_empty) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7093 | Py_DECREF(u); |
| 7094 | return (PyObject *)v; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7095 | } |
| 7096 | |
| 7097 | /* Concat the two Unicode strings */ |
| 7098 | w = _PyUnicode_New(u->length + v->length); |
| 7099 | if (w == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7100 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7101 | Py_UNICODE_COPY(w->str, u->str, u->length); |
| 7102 | Py_UNICODE_COPY(w->str + u->length, v->str, v->length); |
| 7103 | |
| 7104 | Py_DECREF(u); |
| 7105 | Py_DECREF(v); |
| 7106 | return (PyObject *)w; |
| 7107 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7108 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7109 | Py_XDECREF(u); |
| 7110 | Py_XDECREF(v); |
| 7111 | return NULL; |
| 7112 | } |
| 7113 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7114 | void |
| 7115 | PyUnicode_Append(PyObject **pleft, PyObject *right) |
| 7116 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7117 | PyObject *new; |
| 7118 | if (*pleft == NULL) |
| 7119 | return; |
| 7120 | if (right == NULL || !PyUnicode_Check(*pleft)) { |
| 7121 | Py_DECREF(*pleft); |
| 7122 | *pleft = NULL; |
| 7123 | return; |
| 7124 | } |
| 7125 | new = PyUnicode_Concat(*pleft, right); |
| 7126 | Py_DECREF(*pleft); |
| 7127 | *pleft = new; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7128 | } |
| 7129 | |
| 7130 | void |
| 7131 | PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right) |
| 7132 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7133 | PyUnicode_Append(pleft, right); |
| 7134 | Py_XDECREF(right); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 7135 | } |
| 7136 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7137 | PyDoc_STRVAR(count__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7138 | "S.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7139 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7140 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7141 | 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] | 7142 | interpreted as in slice notation."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7143 | |
| 7144 | static PyObject * |
| 7145 | unicode_count(PyUnicodeObject *self, PyObject *args) |
| 7146 | { |
| 7147 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7148 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7149 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7150 | PyObject *result; |
| 7151 | |
Guido van Rossum | b8872e6 | 2000-05-09 14:14:27 +0000 | [diff] [blame] | 7152 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7153 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7154 | return NULL; |
| 7155 | |
| 7156 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7157 | (PyObject *)substring); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7158 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7159 | return NULL; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7160 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7161 | FIX_START_END(self); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7162 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7163 | result = PyLong_FromSsize_t( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7164 | stringlib_count(self->str + start, end - start, |
| 7165 | substring->str, substring->length) |
| 7166 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7167 | |
| 7168 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7169 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7170 | return result; |
| 7171 | } |
| 7172 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7173 | PyDoc_STRVAR(encode__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7174 | "S.encode([encoding[, errors]]) -> bytes\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7175 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 7176 | Encode S using the codec registered for encoding. encoding defaults\n\ |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7177 | 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] | 7178 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 7179 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 7180 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 7181 | codecs.register_error that can handle UnicodeEncodeErrors."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7182 | |
| 7183 | static PyObject * |
| 7184 | unicode_encode(PyUnicodeObject *self, PyObject *args) |
| 7185 | { |
| 7186 | char *encoding = NULL; |
| 7187 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7188 | PyObject *v; |
Guido van Rossum | 35d9428 | 2007-08-27 18:20:11 +0000 | [diff] [blame] | 7189 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7190 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 7191 | return NULL; |
Marc-André Lemburg | b2750b5 | 2008-06-06 12:18:17 +0000 | [diff] [blame] | 7192 | v = PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 7193 | if (v == NULL) |
| 7194 | goto onError; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 7195 | if (!PyBytes_Check(v)) { |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7196 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 7197 | "encoder did not return a bytes object " |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7198 | "(type=%.400s)", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7199 | Py_TYPE(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7200 | Py_DECREF(v); |
| 7201 | return NULL; |
| 7202 | } |
| 7203 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 7204 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7205 | onError: |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 7206 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 7207 | } |
| 7208 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7209 | PyDoc_STRVAR(expandtabs__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7210 | "S.expandtabs([tabsize]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7211 | \n\ |
| 7212 | 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] | 7213 | 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] | 7214 | |
| 7215 | static PyObject* |
| 7216 | unicode_expandtabs(PyUnicodeObject *self, PyObject *args) |
| 7217 | { |
| 7218 | Py_UNICODE *e; |
| 7219 | Py_UNICODE *p; |
| 7220 | Py_UNICODE *q; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7221 | Py_UNICODE *qe; |
| 7222 | Py_ssize_t i, j, incr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7223 | PyUnicodeObject *u; |
| 7224 | int tabsize = 8; |
| 7225 | |
| 7226 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7227 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7228 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 7229 | /* First pass: determine size of output string */ |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7230 | i = 0; /* chars up to and including most recent \n or \r */ |
| 7231 | j = 0; /* chars since most recent \n or \r (use in tab calculations) */ |
| 7232 | e = self->str + self->length; /* end of input */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7233 | for (p = self->str; p < e; p++) |
| 7234 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7235 | if (tabsize > 0) { |
| 7236 | incr = tabsize - (j % tabsize); /* cannot overflow */ |
| 7237 | if (j > PY_SSIZE_T_MAX - incr) |
| 7238 | goto overflow1; |
| 7239 | j += incr; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7240 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7241 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7242 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7243 | if (j > PY_SSIZE_T_MAX - 1) |
| 7244 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7245 | j++; |
| 7246 | if (*p == '\n' || *p == '\r') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7247 | if (i > PY_SSIZE_T_MAX - j) |
| 7248 | goto overflow1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7249 | i += j; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7250 | j = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7251 | } |
| 7252 | } |
| 7253 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7254 | if (i > PY_SSIZE_T_MAX - j) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7255 | goto overflow1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 7256 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7257 | /* Second pass: create output string and fill it */ |
| 7258 | u = _PyUnicode_New(i + j); |
| 7259 | if (!u) |
| 7260 | return NULL; |
| 7261 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7262 | j = 0; /* same as in first pass */ |
| 7263 | q = u->str; /* next output char */ |
| 7264 | qe = u->str + u->length; /* end of output */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7265 | |
| 7266 | for (p = self->str; p < e; p++) |
| 7267 | if (*p == '\t') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7268 | if (tabsize > 0) { |
| 7269 | i = tabsize - (j % tabsize); |
| 7270 | j += i; |
| 7271 | while (i--) { |
| 7272 | if (q >= qe) |
| 7273 | goto overflow2; |
| 7274 | *q++ = ' '; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7275 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7276 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7277 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7278 | else { |
| 7279 | if (q >= qe) |
| 7280 | goto overflow2; |
| 7281 | *q++ = *p; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7282 | j++; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7283 | if (*p == '\n' || *p == '\r') |
| 7284 | j = 0; |
| 7285 | } |
| 7286 | |
| 7287 | return (PyObject*) u; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7288 | |
| 7289 | overflow2: |
| 7290 | Py_DECREF(u); |
| 7291 | overflow1: |
| 7292 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 7293 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7294 | } |
| 7295 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7296 | PyDoc_STRVAR(find__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7297 | "S.find(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7298 | \n\ |
| 7299 | 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] | 7300 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7301 | arguments start and end are interpreted as in slice notation.\n\ |
| 7302 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7303 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7304 | |
| 7305 | static PyObject * |
| 7306 | unicode_find(PyUnicodeObject *self, PyObject *args) |
| 7307 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7308 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7309 | Py_ssize_t start; |
| 7310 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7311 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7312 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7313 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7314 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7315 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7316 | result = stringlib_find_slice( |
| 7317 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7318 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7319 | start, end |
| 7320 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7321 | |
| 7322 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7323 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7324 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7325 | } |
| 7326 | |
| 7327 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7328 | unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7329 | { |
| 7330 | if (index < 0 || index >= self->length) { |
| 7331 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 7332 | return NULL; |
| 7333 | } |
| 7334 | |
| 7335 | return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1); |
| 7336 | } |
| 7337 | |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7338 | /* Believe it or not, this produces the same value for ASCII strings |
| 7339 | as string_hash(). */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7340 | static long |
Neil Schemenauer | f8c37d1 | 2007-09-07 20:49:04 +0000 | [diff] [blame] | 7341 | unicode_hash(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7342 | { |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7343 | Py_ssize_t len; |
| 7344 | Py_UNICODE *p; |
| 7345 | long x; |
| 7346 | |
| 7347 | if (self->hash != -1) |
| 7348 | return self->hash; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7349 | len = Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7350 | p = self->str; |
| 7351 | x = *p << 7; |
| 7352 | while (--len >= 0) |
| 7353 | x = (1000003*x) ^ *p++; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 7354 | x ^= Py_SIZE(self); |
Guido van Rossum | c250493 | 2007-09-18 19:42:40 +0000 | [diff] [blame] | 7355 | if (x == -1) |
| 7356 | x = -2; |
| 7357 | self->hash = x; |
| 7358 | return x; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7359 | } |
| 7360 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7361 | PyDoc_STRVAR(index__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7362 | "S.index(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7363 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7364 | 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] | 7365 | |
| 7366 | static PyObject * |
| 7367 | unicode_index(PyUnicodeObject *self, PyObject *args) |
| 7368 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7369 | Py_ssize_t result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7370 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7371 | Py_ssize_t start; |
| 7372 | Py_ssize_t end; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7373 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7374 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7375 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7376 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7377 | result = stringlib_find_slice( |
| 7378 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 7379 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 7380 | start, end |
| 7381 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7382 | |
| 7383 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7384 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7385 | if (result < 0) { |
| 7386 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 7387 | return NULL; |
| 7388 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7389 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 7390 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7391 | } |
| 7392 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7393 | PyDoc_STRVAR(islower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7394 | "S.islower() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7395 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7396 | 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] | 7397 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7398 | |
| 7399 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7400 | unicode_islower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7401 | { |
| 7402 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7403 | register const Py_UNICODE *e; |
| 7404 | int cased; |
| 7405 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7406 | /* Shortcut for single character strings */ |
| 7407 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7408 | return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7409 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7410 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7411 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7412 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7413 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7414 | e = p + PyUnicode_GET_SIZE(self); |
| 7415 | cased = 0; |
| 7416 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7417 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7418 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7419 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 7420 | return PyBool_FromLong(0); |
| 7421 | else if (!cased && Py_UNICODE_ISLOWER(ch)) |
| 7422 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7423 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7424 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7425 | } |
| 7426 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7427 | PyDoc_STRVAR(isupper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7428 | "S.isupper() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7429 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7430 | 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] | 7431 | at least one cased character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7432 | |
| 7433 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7434 | unicode_isupper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7435 | { |
| 7436 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7437 | register const Py_UNICODE *e; |
| 7438 | int cased; |
| 7439 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7440 | /* Shortcut for single character strings */ |
| 7441 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7442 | return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7443 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7444 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7445 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7446 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7447 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7448 | e = p + PyUnicode_GET_SIZE(self); |
| 7449 | cased = 0; |
| 7450 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7451 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7452 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7453 | if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) |
| 7454 | return PyBool_FromLong(0); |
| 7455 | else if (!cased && Py_UNICODE_ISUPPER(ch)) |
| 7456 | cased = 1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7457 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7458 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7459 | } |
| 7460 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7461 | PyDoc_STRVAR(istitle__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7462 | "S.istitle() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7463 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7464 | Return True if S is a titlecased string and there is at least one\n\ |
| 7465 | character in S, i.e. upper- and titlecase characters may only\n\ |
| 7466 | follow uncased characters and lowercase characters only cased ones.\n\ |
| 7467 | Return False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7468 | |
| 7469 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7470 | unicode_istitle(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7471 | { |
| 7472 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7473 | register const Py_UNICODE *e; |
| 7474 | int cased, previous_is_cased; |
| 7475 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7476 | /* Shortcut for single character strings */ |
| 7477 | if (PyUnicode_GET_SIZE(self) == 1) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7478 | return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || |
| 7479 | (Py_UNICODE_ISUPPER(*p) != 0)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7480 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7481 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7482 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7483 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7484 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7485 | e = p + PyUnicode_GET_SIZE(self); |
| 7486 | cased = 0; |
| 7487 | previous_is_cased = 0; |
| 7488 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7489 | register const Py_UNICODE ch = *p; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 7490 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7491 | if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { |
| 7492 | if (previous_is_cased) |
| 7493 | return PyBool_FromLong(0); |
| 7494 | previous_is_cased = 1; |
| 7495 | cased = 1; |
| 7496 | } |
| 7497 | else if (Py_UNICODE_ISLOWER(ch)) { |
| 7498 | if (!previous_is_cased) |
| 7499 | return PyBool_FromLong(0); |
| 7500 | previous_is_cased = 1; |
| 7501 | cased = 1; |
| 7502 | } |
| 7503 | else |
| 7504 | previous_is_cased = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7505 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7506 | return PyBool_FromLong(cased); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7507 | } |
| 7508 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7509 | PyDoc_STRVAR(isspace__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7510 | "S.isspace() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7511 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7512 | Return True if all characters in S are whitespace\n\ |
| 7513 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7514 | |
| 7515 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7516 | unicode_isspace(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7517 | { |
| 7518 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7519 | register const Py_UNICODE *e; |
| 7520 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7521 | /* Shortcut for single character strings */ |
| 7522 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7523 | Py_UNICODE_ISSPACE(*p)) |
| 7524 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7525 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7526 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7527 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7528 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7529 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7530 | e = p + PyUnicode_GET_SIZE(self); |
| 7531 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7532 | if (!Py_UNICODE_ISSPACE(*p)) |
| 7533 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7534 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7535 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7536 | } |
| 7537 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7538 | PyDoc_STRVAR(isalpha__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7539 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7540 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7541 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7542 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7543 | |
| 7544 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7545 | unicode_isalpha(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7546 | { |
| 7547 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7548 | register const Py_UNICODE *e; |
| 7549 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7550 | /* Shortcut for single character strings */ |
| 7551 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7552 | Py_UNICODE_ISALPHA(*p)) |
| 7553 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7554 | |
| 7555 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7556 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7557 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7558 | |
| 7559 | e = p + PyUnicode_GET_SIZE(self); |
| 7560 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7561 | if (!Py_UNICODE_ISALPHA(*p)) |
| 7562 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7563 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7564 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7565 | } |
| 7566 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7567 | PyDoc_STRVAR(isalnum__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7568 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7569 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7570 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7571 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7572 | |
| 7573 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7574 | unicode_isalnum(PyUnicodeObject *self) |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7575 | { |
| 7576 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7577 | register const Py_UNICODE *e; |
| 7578 | |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7579 | /* Shortcut for single character strings */ |
| 7580 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7581 | Py_UNICODE_ISALNUM(*p)) |
| 7582 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7583 | |
| 7584 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7585 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7586 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7587 | |
| 7588 | e = p + PyUnicode_GET_SIZE(self); |
| 7589 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7590 | if (!Py_UNICODE_ISALNUM(*p)) |
| 7591 | return PyBool_FromLong(0); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7592 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7593 | return PyBool_FromLong(1); |
Marc-André Lemburg | a7acf42 | 2000-07-05 09:49:44 +0000 | [diff] [blame] | 7594 | } |
| 7595 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7596 | PyDoc_STRVAR(isdecimal__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7597 | "S.isdecimal() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7598 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7599 | 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] | 7600 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7601 | |
| 7602 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7603 | unicode_isdecimal(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7604 | { |
| 7605 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7606 | register const Py_UNICODE *e; |
| 7607 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7608 | /* Shortcut for single character strings */ |
| 7609 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7610 | Py_UNICODE_ISDECIMAL(*p)) |
| 7611 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7612 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7613 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7614 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7615 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7616 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7617 | e = p + PyUnicode_GET_SIZE(self); |
| 7618 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7619 | if (!Py_UNICODE_ISDECIMAL(*p)) |
| 7620 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7621 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7622 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7623 | } |
| 7624 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7625 | PyDoc_STRVAR(isdigit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7626 | "S.isdigit() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7627 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 7628 | Return True if all characters in S are digits\n\ |
| 7629 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7630 | |
| 7631 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7632 | unicode_isdigit(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7633 | { |
| 7634 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7635 | register const Py_UNICODE *e; |
| 7636 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7637 | /* Shortcut for single character strings */ |
| 7638 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7639 | Py_UNICODE_ISDIGIT(*p)) |
| 7640 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7641 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7642 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7643 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7644 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7645 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7646 | e = p + PyUnicode_GET_SIZE(self); |
| 7647 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7648 | if (!Py_UNICODE_ISDIGIT(*p)) |
| 7649 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7650 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7651 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7652 | } |
| 7653 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7654 | PyDoc_STRVAR(isnumeric__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7655 | "S.isnumeric() -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7656 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7657 | 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] | 7658 | False otherwise."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7659 | |
| 7660 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7661 | unicode_isnumeric(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7662 | { |
| 7663 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7664 | register const Py_UNICODE *e; |
| 7665 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7666 | /* Shortcut for single character strings */ |
| 7667 | if (PyUnicode_GET_SIZE(self) == 1 && |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7668 | Py_UNICODE_ISNUMERIC(*p)) |
| 7669 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7670 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7671 | /* Special case for empty strings */ |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 7672 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7673 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 7674 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7675 | e = p + PyUnicode_GET_SIZE(self); |
| 7676 | for (; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7677 | if (!Py_UNICODE_ISNUMERIC(*p)) |
| 7678 | return PyBool_FromLong(0); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7679 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 7680 | return PyBool_FromLong(1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7681 | } |
| 7682 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7683 | int |
| 7684 | PyUnicode_IsIdentifier(PyObject *self) |
| 7685 | { |
| 7686 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE((PyUnicodeObject*)self); |
| 7687 | register const Py_UNICODE *e; |
| 7688 | |
| 7689 | /* Special case for empty strings */ |
| 7690 | if (PyUnicode_GET_SIZE(self) == 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7691 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7692 | |
| 7693 | /* PEP 3131 says that the first character must be in |
| 7694 | XID_Start and subsequent characters in XID_Continue, |
| 7695 | and for the ASCII range, the 2.x rules apply (i.e |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7696 | start with letters and underscore, continue with |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7697 | letters, digits, underscore). However, given the current |
| 7698 | definition of XID_Start and XID_Continue, it is sufficient |
| 7699 | to check just for these, except that _ must be allowed |
| 7700 | as starting an identifier. */ |
| 7701 | if (!_PyUnicode_IsXidStart(*p) && *p != 0x5F /* LOW LINE */) |
| 7702 | return 0; |
| 7703 | |
| 7704 | e = p + PyUnicode_GET_SIZE(self); |
| 7705 | for (p++; p < e; p++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7706 | if (!_PyUnicode_IsXidContinue(*p)) |
| 7707 | return 0; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7708 | } |
| 7709 | return 1; |
| 7710 | } |
| 7711 | |
| 7712 | PyDoc_STRVAR(isidentifier__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7713 | "S.isidentifier() -> bool\n\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 7714 | \n\ |
| 7715 | Return True if S is a valid identifier according\n\ |
| 7716 | to the language definition."); |
| 7717 | |
| 7718 | static PyObject* |
| 7719 | unicode_isidentifier(PyObject *self) |
| 7720 | { |
| 7721 | return PyBool_FromLong(PyUnicode_IsIdentifier(self)); |
| 7722 | } |
| 7723 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7724 | PyDoc_STRVAR(isprintable__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7725 | "S.isprintable() -> bool\n\ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 7726 | \n\ |
| 7727 | Return True if all characters in S are considered\n\ |
| 7728 | printable in repr() or S is empty, False otherwise."); |
| 7729 | |
| 7730 | static PyObject* |
| 7731 | unicode_isprintable(PyObject *self) |
| 7732 | { |
| 7733 | register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self); |
| 7734 | register const Py_UNICODE *e; |
| 7735 | |
| 7736 | /* Shortcut for single character strings */ |
| 7737 | if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISPRINTABLE(*p)) { |
| 7738 | Py_RETURN_TRUE; |
| 7739 | } |
| 7740 | |
| 7741 | e = p + PyUnicode_GET_SIZE(self); |
| 7742 | for (; p < e; p++) { |
| 7743 | if (!Py_UNICODE_ISPRINTABLE(*p)) { |
| 7744 | Py_RETURN_FALSE; |
| 7745 | } |
| 7746 | } |
| 7747 | Py_RETURN_TRUE; |
| 7748 | } |
| 7749 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7750 | PyDoc_STRVAR(join__doc__, |
Georg Brandl | 628e6f9 | 2009-10-27 20:24:45 +0000 | [diff] [blame] | 7751 | "S.join(iterable) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7752 | \n\ |
| 7753 | Return a string which is the concatenation of the strings in the\n\ |
Georg Brandl | 628e6f9 | 2009-10-27 20:24:45 +0000 | [diff] [blame] | 7754 | iterable. The separator between elements is S."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7755 | |
| 7756 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7757 | unicode_join(PyObject *self, PyObject *data) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7758 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7759 | return PyUnicode_Join(self, data); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7760 | } |
| 7761 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7762 | static Py_ssize_t |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7763 | unicode_length(PyUnicodeObject *self) |
| 7764 | { |
| 7765 | return self->length; |
| 7766 | } |
| 7767 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7768 | PyDoc_STRVAR(ljust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7769 | "S.ljust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7770 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 7771 | 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] | 7772 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7773 | |
| 7774 | static PyObject * |
| 7775 | unicode_ljust(PyUnicodeObject *self, PyObject *args) |
| 7776 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7777 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7778 | Py_UNICODE fillchar = ' '; |
| 7779 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7780 | if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7781 | return NULL; |
| 7782 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7783 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7784 | Py_INCREF(self); |
| 7785 | return (PyObject*) self; |
| 7786 | } |
| 7787 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 7788 | return (PyObject*) pad(self, 0, width - self->length, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7789 | } |
| 7790 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7791 | PyDoc_STRVAR(lower__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7792 | "S.lower() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7793 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7794 | Return a copy of the string S converted to lowercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7795 | |
| 7796 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 7797 | unicode_lower(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7798 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7799 | return fixup(self, fixlower); |
| 7800 | } |
| 7801 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7802 | #define LEFTSTRIP 0 |
| 7803 | #define RIGHTSTRIP 1 |
| 7804 | #define BOTHSTRIP 2 |
| 7805 | |
| 7806 | /* Arrays indexed by above */ |
| 7807 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 7808 | |
| 7809 | #define STRIPNAME(i) (stripformat[i]+3) |
| 7810 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7811 | /* externally visible for str.strip(unicode) */ |
| 7812 | PyObject * |
| 7813 | _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) |
| 7814 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7815 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 7816 | Py_ssize_t len = PyUnicode_GET_SIZE(self); |
| 7817 | Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); |
| 7818 | Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); |
| 7819 | Py_ssize_t i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7820 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7821 | BLOOM_MASK sepmask = make_bloom_mask(sep, seplen); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7822 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7823 | i = 0; |
| 7824 | if (striptype != RIGHTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7825 | while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) { |
| 7826 | i++; |
| 7827 | } |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7828 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7829 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7830 | j = len; |
| 7831 | if (striptype != LEFTSTRIP) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7832 | do { |
| 7833 | j--; |
| 7834 | } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen)); |
| 7835 | j++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7836 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7837 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7838 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7839 | Py_INCREF(self); |
| 7840 | return (PyObject*)self; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7841 | } |
| 7842 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7843 | return PyUnicode_FromUnicode(s+i, j-i); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7844 | } |
| 7845 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7846 | |
| 7847 | static PyObject * |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7848 | do_strip(PyUnicodeObject *self, int striptype) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7849 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7850 | Py_UNICODE *s = PyUnicode_AS_UNICODE(self); |
| 7851 | Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7852 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7853 | i = 0; |
| 7854 | if (striptype != RIGHTSTRIP) { |
| 7855 | while (i < len && Py_UNICODE_ISSPACE(s[i])) { |
| 7856 | i++; |
| 7857 | } |
| 7858 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7859 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7860 | j = len; |
| 7861 | if (striptype != LEFTSTRIP) { |
| 7862 | do { |
| 7863 | j--; |
| 7864 | } while (j >= i && Py_UNICODE_ISSPACE(s[j])); |
| 7865 | j++; |
| 7866 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7867 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7868 | if (i == 0 && j == len && PyUnicode_CheckExact(self)) { |
| 7869 | Py_INCREF(self); |
| 7870 | return (PyObject*)self; |
| 7871 | } |
| 7872 | else |
| 7873 | return PyUnicode_FromUnicode(s+i, j-i); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7874 | } |
| 7875 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7876 | |
| 7877 | static PyObject * |
| 7878 | do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args) |
| 7879 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7880 | PyObject *sep = NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7881 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7882 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
| 7883 | return NULL; |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7884 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7885 | if (sep != NULL && sep != Py_None) { |
| 7886 | if (PyUnicode_Check(sep)) |
| 7887 | return _PyUnicode_XStrip(self, striptype, sep); |
| 7888 | else { |
| 7889 | PyErr_Format(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7890 | "%s arg must be None or str", |
| 7891 | STRIPNAME(striptype)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7892 | return NULL; |
| 7893 | } |
| 7894 | } |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7895 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7896 | return do_strip(self, striptype); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7897 | } |
| 7898 | |
| 7899 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7900 | PyDoc_STRVAR(strip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7901 | "S.strip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7902 | \n\ |
| 7903 | Return a copy of the string S with leading and trailing\n\ |
| 7904 | whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7905 | 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] | 7906 | |
| 7907 | static PyObject * |
| 7908 | unicode_strip(PyUnicodeObject *self, PyObject *args) |
| 7909 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7910 | if (PyTuple_GET_SIZE(args) == 0) |
| 7911 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 7912 | else |
| 7913 | return do_argstrip(self, BOTHSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7914 | } |
| 7915 | |
| 7916 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7917 | PyDoc_STRVAR(lstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7918 | "S.lstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7919 | \n\ |
| 7920 | Return a copy of the string S with leading whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7921 | 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] | 7922 | |
| 7923 | static PyObject * |
| 7924 | unicode_lstrip(PyUnicodeObject *self, PyObject *args) |
| 7925 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7926 | if (PyTuple_GET_SIZE(args) == 0) |
| 7927 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 7928 | else |
| 7929 | return do_argstrip(self, LEFTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7930 | } |
| 7931 | |
| 7932 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7933 | PyDoc_STRVAR(rstrip__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7934 | "S.rstrip([chars]) -> str\n\ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7935 | \n\ |
| 7936 | Return a copy of the string S with trailing whitespace removed.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 7937 | 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] | 7938 | |
| 7939 | static PyObject * |
| 7940 | unicode_rstrip(PyUnicodeObject *self, PyObject *args) |
| 7941 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 7942 | if (PyTuple_GET_SIZE(args) == 0) |
| 7943 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 7944 | else |
| 7945 | return do_argstrip(self, RIGHTSTRIP, args); |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 7946 | } |
| 7947 | |
| 7948 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7949 | static PyObject* |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7950 | unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7951 | { |
| 7952 | PyUnicodeObject *u; |
| 7953 | Py_UNICODE *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 7954 | Py_ssize_t nchars; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7955 | size_t nbytes; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7956 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7957 | if (len < 1) { |
| 7958 | Py_INCREF(unicode_empty); |
| 7959 | return (PyObject *)unicode_empty; |
| 7960 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7961 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 7962 | if (len == 1 && PyUnicode_CheckExact(str)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7963 | /* no repeat, return original string */ |
| 7964 | Py_INCREF(str); |
| 7965 | return (PyObject*) str; |
| 7966 | } |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7967 | |
| 7968 | /* ensure # of chars needed doesn't overflow int and # of bytes |
| 7969 | * needed doesn't overflow size_t |
| 7970 | */ |
| 7971 | nchars = len * str->length; |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7972 | if (nchars / len != str->length) { |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 7973 | PyErr_SetString(PyExc_OverflowError, |
| 7974 | "repeated string is too long"); |
| 7975 | return NULL; |
| 7976 | } |
| 7977 | nbytes = (nchars + 1) * sizeof(Py_UNICODE); |
| 7978 | if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) { |
| 7979 | PyErr_SetString(PyExc_OverflowError, |
| 7980 | "repeated string is too long"); |
| 7981 | return NULL; |
| 7982 | } |
| 7983 | u = _PyUnicode_New(nchars); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7984 | if (!u) |
| 7985 | return NULL; |
| 7986 | |
| 7987 | p = u->str; |
| 7988 | |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7989 | if (str->length == 1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7990 | Py_UNICODE_FILL(p, str->str[0], len); |
| 7991 | } else { |
Georg Brandl | 222de0f | 2009-04-12 12:01:50 +0000 | [diff] [blame] | 7992 | Py_ssize_t done = str->length; /* number of characters copied this far */ |
| 7993 | Py_UNICODE_COPY(p, str->str, str->length); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7994 | while (done < nchars) { |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 7995 | Py_ssize_t n = (done <= nchars-done) ? done : nchars-done; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7996 | Py_UNICODE_COPY(p+done, p, n); |
| 7997 | done += n; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 7998 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 7999 | } |
| 8000 | |
| 8001 | return (PyObject*) u; |
| 8002 | } |
| 8003 | |
| 8004 | PyObject *PyUnicode_Replace(PyObject *obj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8005 | PyObject *subobj, |
| 8006 | PyObject *replobj, |
| 8007 | Py_ssize_t maxcount) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8008 | { |
| 8009 | PyObject *self; |
| 8010 | PyObject *str1; |
| 8011 | PyObject *str2; |
| 8012 | PyObject *result; |
| 8013 | |
| 8014 | self = PyUnicode_FromObject(obj); |
| 8015 | if (self == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8016 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8017 | str1 = PyUnicode_FromObject(subobj); |
| 8018 | if (str1 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8019 | Py_DECREF(self); |
| 8020 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8021 | } |
| 8022 | str2 = PyUnicode_FromObject(replobj); |
| 8023 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8024 | Py_DECREF(self); |
| 8025 | Py_DECREF(str1); |
| 8026 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8027 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8028 | result = replace((PyUnicodeObject *)self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8029 | (PyUnicodeObject *)str1, |
| 8030 | (PyUnicodeObject *)str2, |
| 8031 | maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8032 | Py_DECREF(self); |
| 8033 | Py_DECREF(str1); |
| 8034 | Py_DECREF(str2); |
| 8035 | return result; |
| 8036 | } |
| 8037 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8038 | PyDoc_STRVAR(replace__doc__, |
Ezio Melotti | 415f340 | 2010-06-26 18:52:26 +0000 | [diff] [blame] | 8039 | "S.replace(old, new[, count]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8040 | \n\ |
| 8041 | Return a copy of S with all occurrences of substring\n\ |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 8042 | old replaced by new. If the optional argument count is\n\ |
| 8043 | given, only the first count occurrences are replaced."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8044 | |
| 8045 | static PyObject* |
| 8046 | unicode_replace(PyUnicodeObject *self, PyObject *args) |
| 8047 | { |
| 8048 | PyUnicodeObject *str1; |
| 8049 | PyUnicodeObject *str2; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8050 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8051 | PyObject *result; |
| 8052 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8053 | if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8054 | return NULL; |
| 8055 | str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); |
| 8056 | if (str1 == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8057 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8058 | str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2); |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 8059 | if (str2 == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8060 | Py_DECREF(str1); |
| 8061 | return NULL; |
Walter Dörwald | f6b56ae | 2003-02-09 23:42:56 +0000 | [diff] [blame] | 8062 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8063 | |
| 8064 | result = replace(self, str1, str2, maxcount); |
| 8065 | |
| 8066 | Py_DECREF(str1); |
| 8067 | Py_DECREF(str2); |
| 8068 | return result; |
| 8069 | } |
| 8070 | |
| 8071 | static |
| 8072 | PyObject *unicode_repr(PyObject *unicode) |
| 8073 | { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8074 | PyObject *repr; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8075 | Py_UNICODE *p; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8076 | Py_UNICODE *s = PyUnicode_AS_UNICODE(unicode); |
| 8077 | Py_ssize_t size = PyUnicode_GET_SIZE(unicode); |
| 8078 | |
| 8079 | /* XXX(nnorwitz): rather than over-allocating, it would be |
| 8080 | better to choose a different scheme. Perhaps scan the |
| 8081 | first N-chars of the string and allocate based on that size. |
| 8082 | */ |
| 8083 | /* Initial allocation is based on the longest-possible unichr |
| 8084 | escape. |
| 8085 | |
| 8086 | In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source |
| 8087 | unichr, so in this case it's the longest unichr escape. In |
| 8088 | narrow (UTF-16) builds this is five chars per source unichr |
| 8089 | since there are two unichrs in the surrogate pair, so in narrow |
| 8090 | (UTF-16) builds it's not the longest unichr escape. |
| 8091 | |
| 8092 | In wide or narrow builds '\uxxxx' is 6 chars per source unichr, |
| 8093 | so in the narrow (UTF-16) build case it's the longest unichr |
| 8094 | escape. |
| 8095 | */ |
| 8096 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8097 | repr = PyUnicode_FromUnicode(NULL, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8098 | 2 /* quotes */ |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8099 | #ifdef Py_UNICODE_WIDE |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8100 | + 10*size |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8101 | #else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8102 | + 6*size |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8103 | #endif |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8104 | + 1); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8105 | if (repr == NULL) |
| 8106 | return NULL; |
| 8107 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8108 | p = PyUnicode_AS_UNICODE(repr); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8109 | |
| 8110 | /* Add quote */ |
| 8111 | *p++ = (findchar(s, size, '\'') && |
| 8112 | !findchar(s, size, '"')) ? '"' : '\''; |
| 8113 | while (size-- > 0) { |
| 8114 | Py_UNICODE ch = *s++; |
| 8115 | |
| 8116 | /* Escape quotes and backslashes */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8117 | if ((ch == PyUnicode_AS_UNICODE(repr)[0]) || (ch == '\\')) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8118 | *p++ = '\\'; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8119 | *p++ = ch; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8120 | continue; |
| 8121 | } |
| 8122 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8123 | /* Map special whitespace to '\t', \n', '\r' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8124 | if (ch == '\t') { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8125 | *p++ = '\\'; |
| 8126 | *p++ = 't'; |
| 8127 | } |
| 8128 | else if (ch == '\n') { |
| 8129 | *p++ = '\\'; |
| 8130 | *p++ = 'n'; |
| 8131 | } |
| 8132 | else if (ch == '\r') { |
| 8133 | *p++ = '\\'; |
| 8134 | *p++ = 'r'; |
| 8135 | } |
| 8136 | |
| 8137 | /* Map non-printable US ASCII to '\xhh' */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8138 | else if (ch < ' ' || ch == 0x7F) { |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8139 | *p++ = '\\'; |
| 8140 | *p++ = 'x'; |
| 8141 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 8142 | *p++ = hexdigits[ch & 0x000F]; |
| 8143 | } |
| 8144 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8145 | /* Copy ASCII characters as-is */ |
| 8146 | else if (ch < 0x7F) { |
| 8147 | *p++ = ch; |
| 8148 | } |
| 8149 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8150 | /* Non-ASCII characters */ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8151 | else { |
| 8152 | Py_UCS4 ucs = ch; |
| 8153 | |
| 8154 | #ifndef Py_UNICODE_WIDE |
| 8155 | Py_UNICODE ch2 = 0; |
| 8156 | /* Get code point from surrogate pair */ |
| 8157 | if (size > 0) { |
| 8158 | ch2 = *s; |
| 8159 | if (ch >= 0xD800 && ch < 0xDC00 && ch2 >= 0xDC00 |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8160 | && ch2 <= 0xDFFF) { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8161 | ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8162 | + 0x00010000; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8163 | s++; |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8164 | size--; |
| 8165 | } |
| 8166 | } |
| 8167 | #endif |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8168 | /* Map Unicode whitespace and control characters |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8169 | (categories Z* and C* except ASCII space) |
| 8170 | */ |
| 8171 | if (!Py_UNICODE_ISPRINTABLE(ucs)) { |
| 8172 | /* Map 8-bit characters to '\xhh' */ |
| 8173 | if (ucs <= 0xff) { |
| 8174 | *p++ = '\\'; |
| 8175 | *p++ = 'x'; |
| 8176 | *p++ = hexdigits[(ch >> 4) & 0x000F]; |
| 8177 | *p++ = hexdigits[ch & 0x000F]; |
| 8178 | } |
| 8179 | /* Map 21-bit characters to '\U00xxxxxx' */ |
| 8180 | else if (ucs >= 0x10000) { |
| 8181 | *p++ = '\\'; |
| 8182 | *p++ = 'U'; |
| 8183 | *p++ = hexdigits[(ucs >> 28) & 0x0000000F]; |
| 8184 | *p++ = hexdigits[(ucs >> 24) & 0x0000000F]; |
| 8185 | *p++ = hexdigits[(ucs >> 20) & 0x0000000F]; |
| 8186 | *p++ = hexdigits[(ucs >> 16) & 0x0000000F]; |
| 8187 | *p++ = hexdigits[(ucs >> 12) & 0x0000000F]; |
| 8188 | *p++ = hexdigits[(ucs >> 8) & 0x0000000F]; |
| 8189 | *p++ = hexdigits[(ucs >> 4) & 0x0000000F]; |
| 8190 | *p++ = hexdigits[ucs & 0x0000000F]; |
| 8191 | } |
| 8192 | /* Map 16-bit characters to '\uxxxx' */ |
| 8193 | else { |
| 8194 | *p++ = '\\'; |
| 8195 | *p++ = 'u'; |
| 8196 | *p++ = hexdigits[(ucs >> 12) & 0x000F]; |
| 8197 | *p++ = hexdigits[(ucs >> 8) & 0x000F]; |
| 8198 | *p++ = hexdigits[(ucs >> 4) & 0x000F]; |
| 8199 | *p++ = hexdigits[ucs & 0x000F]; |
| 8200 | } |
| 8201 | } |
| 8202 | /* Copy characters as-is */ |
| 8203 | else { |
| 8204 | *p++ = ch; |
| 8205 | #ifndef Py_UNICODE_WIDE |
| 8206 | if (ucs >= 0x10000) |
| 8207 | *p++ = ch2; |
| 8208 | #endif |
| 8209 | } |
| 8210 | } |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8211 | } |
| 8212 | /* Add quote */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 8213 | *p++ = PyUnicode_AS_UNICODE(repr)[0]; |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8214 | |
| 8215 | *p = '\0'; |
Alexandre Vassalotti | aa0e531 | 2008-12-27 06:43:58 +0000 | [diff] [blame] | 8216 | PyUnicode_Resize(&repr, p - PyUnicode_AS_UNICODE(repr)); |
Walter Dörwald | 79e913e | 2007-05-12 11:08:06 +0000 | [diff] [blame] | 8217 | return repr; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8218 | } |
| 8219 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8220 | PyDoc_STRVAR(rfind__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8221 | "S.rfind(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8222 | \n\ |
| 8223 | 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] | 8224 | such that sub is contained within s[start:end]. Optional\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8225 | arguments start and end are interpreted as in slice notation.\n\ |
| 8226 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8227 | Return -1 on failure."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8228 | |
| 8229 | static PyObject * |
| 8230 | unicode_rfind(PyUnicodeObject *self, PyObject *args) |
| 8231 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8232 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8233 | Py_ssize_t start; |
| 8234 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8235 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8236 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8237 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8238 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8239 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8240 | result = stringlib_rfind_slice( |
| 8241 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 8242 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 8243 | start, end |
| 8244 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8245 | |
| 8246 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8247 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8248 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8249 | } |
| 8250 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8251 | PyDoc_STRVAR(rindex__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8252 | "S.rindex(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8253 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8254 | 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] | 8255 | |
| 8256 | static PyObject * |
| 8257 | unicode_rindex(PyUnicodeObject *self, PyObject *args) |
| 8258 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8259 | PyObject *substring; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8260 | Py_ssize_t start; |
| 8261 | Py_ssize_t end; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8262 | Py_ssize_t result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8263 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8264 | if (!_ParseTupleFinds(args, &substring, &start, &end)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8265 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8266 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8267 | result = stringlib_rfind_slice( |
| 8268 | PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self), |
| 8269 | PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring), |
| 8270 | start, end |
| 8271 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8272 | |
| 8273 | Py_DECREF(substring); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8274 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8275 | if (result < 0) { |
| 8276 | PyErr_SetString(PyExc_ValueError, "substring not found"); |
| 8277 | return NULL; |
| 8278 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8279 | return PyLong_FromSsize_t(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8280 | } |
| 8281 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8282 | PyDoc_STRVAR(rjust__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8283 | "S.rjust(width[, fillchar]) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8284 | \n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 8285 | 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] | 8286 | done using the specified fill character (default is a space)."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8287 | |
| 8288 | static PyObject * |
| 8289 | unicode_rjust(PyUnicodeObject *self, PyObject *args) |
| 8290 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8291 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8292 | Py_UNICODE fillchar = ' '; |
| 8293 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8294 | if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8295 | return NULL; |
| 8296 | |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 8297 | if (self->length >= width && PyUnicode_CheckExact(self)) { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8298 | Py_INCREF(self); |
| 8299 | return (PyObject*) self; |
| 8300 | } |
| 8301 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 8302 | return (PyObject*) pad(self, width - self->length, 0, fillchar); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8303 | } |
| 8304 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8305 | PyObject *PyUnicode_Split(PyObject *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8306 | PyObject *sep, |
| 8307 | Py_ssize_t maxsplit) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8308 | { |
| 8309 | PyObject *result; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8310 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8311 | s = PyUnicode_FromObject(s); |
| 8312 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8313 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8314 | if (sep != NULL) { |
| 8315 | sep = PyUnicode_FromObject(sep); |
| 8316 | if (sep == NULL) { |
| 8317 | Py_DECREF(s); |
| 8318 | return NULL; |
| 8319 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8320 | } |
| 8321 | |
| 8322 | result = split((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 8323 | |
| 8324 | Py_DECREF(s); |
| 8325 | Py_XDECREF(sep); |
| 8326 | return result; |
| 8327 | } |
| 8328 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8329 | PyDoc_STRVAR(split__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8330 | "S.split([sep[, maxsplit]]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8331 | \n\ |
| 8332 | Return a list of the words in S, using sep as the\n\ |
| 8333 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 8334 | 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] | 8335 | whitespace string is a separator and empty strings are\n\ |
| 8336 | removed from the result."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8337 | |
| 8338 | static PyObject* |
| 8339 | unicode_split(PyUnicodeObject *self, PyObject *args) |
| 8340 | { |
| 8341 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8342 | Py_ssize_t maxcount = -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8343 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8344 | if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8345 | return NULL; |
| 8346 | |
| 8347 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8348 | return split(self, NULL, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8349 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8350 | return split(self, (PyUnicodeObject *)substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8351 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8352 | return PyUnicode_Split((PyObject *)self, substring, maxcount); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8353 | } |
| 8354 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8355 | PyObject * |
| 8356 | PyUnicode_Partition(PyObject *str_in, PyObject *sep_in) |
| 8357 | { |
| 8358 | PyObject* str_obj; |
| 8359 | PyObject* sep_obj; |
| 8360 | PyObject* out; |
| 8361 | |
| 8362 | str_obj = PyUnicode_FromObject(str_in); |
| 8363 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8364 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8365 | sep_obj = PyUnicode_FromObject(sep_in); |
| 8366 | if (!sep_obj) { |
| 8367 | Py_DECREF(str_obj); |
| 8368 | return NULL; |
| 8369 | } |
| 8370 | |
| 8371 | out = stringlib_partition( |
| 8372 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 8373 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 8374 | ); |
| 8375 | |
| 8376 | Py_DECREF(sep_obj); |
| 8377 | Py_DECREF(str_obj); |
| 8378 | |
| 8379 | return out; |
| 8380 | } |
| 8381 | |
| 8382 | |
| 8383 | PyObject * |
| 8384 | PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in) |
| 8385 | { |
| 8386 | PyObject* str_obj; |
| 8387 | PyObject* sep_obj; |
| 8388 | PyObject* out; |
| 8389 | |
| 8390 | str_obj = PyUnicode_FromObject(str_in); |
| 8391 | if (!str_obj) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8392 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8393 | sep_obj = PyUnicode_FromObject(sep_in); |
| 8394 | if (!sep_obj) { |
| 8395 | Py_DECREF(str_obj); |
| 8396 | return NULL; |
| 8397 | } |
| 8398 | |
| 8399 | out = stringlib_rpartition( |
| 8400 | str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj), |
| 8401 | sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj) |
| 8402 | ); |
| 8403 | |
| 8404 | Py_DECREF(sep_obj); |
| 8405 | Py_DECREF(str_obj); |
| 8406 | |
| 8407 | return out; |
| 8408 | } |
| 8409 | |
| 8410 | PyDoc_STRVAR(partition__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8411 | "S.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8412 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8413 | 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] | 8414 | 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] | 8415 | found, return S and two empty strings."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8416 | |
| 8417 | static PyObject* |
| 8418 | unicode_partition(PyUnicodeObject *self, PyObject *separator) |
| 8419 | { |
| 8420 | return PyUnicode_Partition((PyObject *)self, separator); |
| 8421 | } |
| 8422 | |
| 8423 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 4c81fbb | 2010-01-25 12:02:24 +0000 | [diff] [blame] | 8424 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8425 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 8426 | 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] | 8427 | 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] | 8428 | separator is not found, return two empty strings and S."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8429 | |
| 8430 | static PyObject* |
| 8431 | unicode_rpartition(PyUnicodeObject *self, PyObject *separator) |
| 8432 | { |
| 8433 | return PyUnicode_RPartition((PyObject *)self, separator); |
| 8434 | } |
| 8435 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8436 | PyObject *PyUnicode_RSplit(PyObject *s, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8437 | PyObject *sep, |
| 8438 | Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8439 | { |
| 8440 | PyObject *result; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8441 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8442 | s = PyUnicode_FromObject(s); |
| 8443 | if (s == NULL) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8444 | return NULL; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8445 | if (sep != NULL) { |
| 8446 | sep = PyUnicode_FromObject(sep); |
| 8447 | if (sep == NULL) { |
| 8448 | Py_DECREF(s); |
| 8449 | return NULL; |
| 8450 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8451 | } |
| 8452 | |
| 8453 | result = rsplit((PyUnicodeObject *)s, (PyUnicodeObject *)sep, maxsplit); |
| 8454 | |
| 8455 | Py_DECREF(s); |
| 8456 | Py_XDECREF(sep); |
| 8457 | return result; |
| 8458 | } |
| 8459 | |
| 8460 | PyDoc_STRVAR(rsplit__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8461 | "S.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8462 | \n\ |
| 8463 | Return a list of the words in S, using sep as the\n\ |
| 8464 | delimiter string, starting at the end of the string and\n\ |
| 8465 | working to the front. If maxsplit is given, at most maxsplit\n\ |
| 8466 | splits are done. If sep is not specified, any whitespace string\n\ |
| 8467 | is a separator."); |
| 8468 | |
| 8469 | static PyObject* |
| 8470 | unicode_rsplit(PyUnicodeObject *self, PyObject *args) |
| 8471 | { |
| 8472 | PyObject *substring = Py_None; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8473 | Py_ssize_t maxcount = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8474 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8475 | if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8476 | return NULL; |
| 8477 | |
| 8478 | if (substring == Py_None) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8479 | return rsplit(self, NULL, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8480 | else if (PyUnicode_Check(substring)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8481 | return rsplit(self, (PyUnicodeObject *)substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8482 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8483 | return PyUnicode_RSplit((PyObject *)self, substring, maxcount); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8484 | } |
| 8485 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8486 | PyDoc_STRVAR(splitlines__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8487 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8488 | \n\ |
| 8489 | 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] | 8490 | 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] | 8491 | is given and true."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8492 | |
| 8493 | static PyObject* |
| 8494 | unicode_splitlines(PyUnicodeObject *self, PyObject *args) |
| 8495 | { |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8496 | int keepends = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8497 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8498 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8499 | return NULL; |
| 8500 | |
Guido van Rossum | 8666291 | 2000-04-11 15:38:46 +0000 | [diff] [blame] | 8501 | return PyUnicode_Splitlines((PyObject *)self, keepends); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8502 | } |
| 8503 | |
| 8504 | static |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 8505 | PyObject *unicode_str(PyObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8506 | { |
Walter Dörwald | 346737f | 2007-05-31 10:44:43 +0000 | [diff] [blame] | 8507 | if (PyUnicode_CheckExact(self)) { |
| 8508 | Py_INCREF(self); |
| 8509 | return self; |
| 8510 | } else |
| 8511 | /* Subtype -- return genuine unicode string with the same value. */ |
| 8512 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(self), |
| 8513 | PyUnicode_GET_SIZE(self)); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8514 | } |
| 8515 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8516 | PyDoc_STRVAR(swapcase__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8517 | "S.swapcase() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8518 | \n\ |
| 8519 | 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] | 8520 | and vice versa."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8521 | |
| 8522 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8523 | unicode_swapcase(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8524 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8525 | return fixup(self, fixswapcase); |
| 8526 | } |
| 8527 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8528 | PyDoc_STRVAR(maketrans__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8529 | "str.maketrans(x[, y[, z]]) -> dict (static method)\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8530 | \n\ |
| 8531 | Return a translation table usable for str.translate().\n\ |
| 8532 | If there is only one argument, it must be a dictionary mapping Unicode\n\ |
| 8533 | ordinals (integers) or characters to Unicode ordinals, strings or None.\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8534 | Character keys will be then converted to ordinals.\n\ |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8535 | If there are two arguments, they must be strings of equal length, and\n\ |
| 8536 | in the resulting dictionary, each character in x will be mapped to the\n\ |
| 8537 | character at the same position in y. If there is a third argument, it\n\ |
| 8538 | must be a string, whose characters will be mapped to None in the result."); |
| 8539 | |
| 8540 | static PyObject* |
| 8541 | unicode_maketrans(PyUnicodeObject *null, PyObject *args) |
| 8542 | { |
| 8543 | PyObject *x, *y = NULL, *z = NULL; |
| 8544 | PyObject *new = NULL, *key, *value; |
| 8545 | Py_ssize_t i = 0; |
| 8546 | int res; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8547 | |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8548 | if (!PyArg_ParseTuple(args, "O|UU:maketrans", &x, &y, &z)) |
| 8549 | return NULL; |
| 8550 | new = PyDict_New(); |
| 8551 | if (!new) |
| 8552 | return NULL; |
| 8553 | if (y != NULL) { |
| 8554 | /* x must be a string too, of equal length */ |
| 8555 | Py_ssize_t ylen = PyUnicode_GET_SIZE(y); |
| 8556 | if (!PyUnicode_Check(x)) { |
| 8557 | PyErr_SetString(PyExc_TypeError, "first maketrans argument must " |
| 8558 | "be a string if there is a second argument"); |
| 8559 | goto err; |
| 8560 | } |
| 8561 | if (PyUnicode_GET_SIZE(x) != ylen) { |
| 8562 | PyErr_SetString(PyExc_ValueError, "the first two maketrans " |
| 8563 | "arguments must have equal length"); |
| 8564 | goto err; |
| 8565 | } |
| 8566 | /* create entries for translating chars in x to those in y */ |
| 8567 | for (i = 0; i < PyUnicode_GET_SIZE(x); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8568 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(x)[i]); |
| 8569 | value = PyLong_FromLong(PyUnicode_AS_UNICODE(y)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8570 | if (!key || !value) |
| 8571 | goto err; |
| 8572 | res = PyDict_SetItem(new, key, value); |
| 8573 | Py_DECREF(key); |
| 8574 | Py_DECREF(value); |
| 8575 | if (res < 0) |
| 8576 | goto err; |
| 8577 | } |
| 8578 | /* create entries for deleting chars in z */ |
| 8579 | if (z != NULL) { |
| 8580 | for (i = 0; i < PyUnicode_GET_SIZE(z); i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8581 | key = PyLong_FromLong(PyUnicode_AS_UNICODE(z)[i]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8582 | if (!key) |
| 8583 | goto err; |
| 8584 | res = PyDict_SetItem(new, key, Py_None); |
| 8585 | Py_DECREF(key); |
| 8586 | if (res < 0) |
| 8587 | goto err; |
| 8588 | } |
| 8589 | } |
| 8590 | } else { |
| 8591 | /* x must be a dict */ |
Raymond Hettinger | 3ad0576 | 2009-05-29 22:11:22 +0000 | [diff] [blame] | 8592 | if (!PyDict_CheckExact(x)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8593 | PyErr_SetString(PyExc_TypeError, "if you give only one argument " |
| 8594 | "to maketrans it must be a dict"); |
| 8595 | goto err; |
| 8596 | } |
| 8597 | /* copy entries into the new dict, converting string keys to int keys */ |
| 8598 | while (PyDict_Next(x, &i, &key, &value)) { |
| 8599 | if (PyUnicode_Check(key)) { |
| 8600 | /* convert string keys to integer keys */ |
| 8601 | PyObject *newkey; |
| 8602 | if (PyUnicode_GET_SIZE(key) != 1) { |
| 8603 | PyErr_SetString(PyExc_ValueError, "string keys in translate " |
| 8604 | "table must be of length 1"); |
| 8605 | goto err; |
| 8606 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8607 | newkey = PyLong_FromLong(PyUnicode_AS_UNICODE(key)[0]); |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8608 | if (!newkey) |
| 8609 | goto err; |
| 8610 | res = PyDict_SetItem(new, newkey, value); |
| 8611 | Py_DECREF(newkey); |
| 8612 | if (res < 0) |
| 8613 | goto err; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 8614 | } else if (PyLong_Check(key)) { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8615 | /* just keep integer keys */ |
| 8616 | if (PyDict_SetItem(new, key, value) < 0) |
| 8617 | goto err; |
| 8618 | } else { |
| 8619 | PyErr_SetString(PyExc_TypeError, "keys in translate table must " |
| 8620 | "be strings or integers"); |
| 8621 | goto err; |
| 8622 | } |
| 8623 | } |
| 8624 | } |
| 8625 | return new; |
| 8626 | err: |
| 8627 | Py_DECREF(new); |
| 8628 | return NULL; |
| 8629 | } |
| 8630 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8631 | PyDoc_STRVAR(translate__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8632 | "S.translate(table) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8633 | \n\ |
| 8634 | Return a copy of the string S, where all characters have been mapped\n\ |
| 8635 | through the given translation table, which must be a mapping of\n\ |
Benjamin Peterson | 142957c | 2008-07-04 19:55:29 +0000 | [diff] [blame] | 8636 | Unicode ordinals to Unicode ordinals, strings, or None.\n\ |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 8637 | Unmapped characters are left untouched. Characters mapped to None\n\ |
| 8638 | are deleted."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8639 | |
| 8640 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8641 | unicode_translate(PyUnicodeObject *self, PyObject *table) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8642 | { |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8643 | return PyUnicode_TranslateCharmap(self->str, self->length, table, "ignore"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8644 | } |
| 8645 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8646 | PyDoc_STRVAR(upper__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8647 | "S.upper() -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8648 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8649 | Return a copy of S converted to uppercase."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8650 | |
| 8651 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8652 | unicode_upper(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8653 | { |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8654 | return fixup(self, fixupper); |
| 8655 | } |
| 8656 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8657 | PyDoc_STRVAR(zfill__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8658 | "S.zfill(width) -> str\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8659 | \n\ |
Benjamin Peterson | 9aa4299 | 2008-09-10 21:57:34 +0000 | [diff] [blame] | 8660 | Pad a numeric string S with zeros on the left, to fill a field\n\ |
| 8661 | of the specified width. The string S is never truncated."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8662 | |
| 8663 | static PyObject * |
| 8664 | unicode_zfill(PyUnicodeObject *self, PyObject *args) |
| 8665 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8666 | Py_ssize_t fill; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8667 | PyUnicodeObject *u; |
| 8668 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8669 | Py_ssize_t width; |
| 8670 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8671 | return NULL; |
| 8672 | |
| 8673 | if (self->length >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 8674 | if (PyUnicode_CheckExact(self)) { |
| 8675 | Py_INCREF(self); |
| 8676 | return (PyObject*) self; |
| 8677 | } |
| 8678 | else |
| 8679 | return PyUnicode_FromUnicode( |
| 8680 | PyUnicode_AS_UNICODE(self), |
| 8681 | PyUnicode_GET_SIZE(self) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8682 | ); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8683 | } |
| 8684 | |
| 8685 | fill = width - self->length; |
| 8686 | |
| 8687 | u = pad(self, fill, 0, '0'); |
| 8688 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8689 | if (u == NULL) |
| 8690 | return NULL; |
| 8691 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8692 | if (u->str[fill] == '+' || u->str[fill] == '-') { |
| 8693 | /* move sign to beginning of string */ |
| 8694 | u->str[0] = u->str[fill]; |
| 8695 | u->str[fill] = '0'; |
| 8696 | } |
| 8697 | |
| 8698 | return (PyObject*) u; |
| 8699 | } |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8700 | |
| 8701 | #if 0 |
| 8702 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8703 | unicode_freelistsize(PyUnicodeObject *self) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8704 | { |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 8705 | return PyLong_FromLong(numfree); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8706 | } |
| 8707 | #endif |
| 8708 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8709 | PyDoc_STRVAR(startswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8710 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8711 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 8712 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 8713 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8714 | With optional end, stop comparing S at that position.\n\ |
| 8715 | prefix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8716 | |
| 8717 | static PyObject * |
| 8718 | unicode_startswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8719 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8720 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8721 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8722 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8723 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8724 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8725 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8726 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8727 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8728 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 8729 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8730 | if (PyTuple_Check(subobj)) { |
| 8731 | Py_ssize_t i; |
| 8732 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 8733 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8734 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8735 | if (substring == NULL) |
| 8736 | return NULL; |
| 8737 | result = tailmatch(self, substring, start, end, -1); |
| 8738 | Py_DECREF(substring); |
| 8739 | if (result) { |
| 8740 | Py_RETURN_TRUE; |
| 8741 | } |
| 8742 | } |
| 8743 | /* nothing matched */ |
| 8744 | Py_RETURN_FALSE; |
| 8745 | } |
| 8746 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8747 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8748 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8749 | result = tailmatch(self, substring, start, end, -1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8750 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8751 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8752 | } |
| 8753 | |
| 8754 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8755 | PyDoc_STRVAR(endswith__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8756 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8757 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 8758 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 8759 | With optional start, test S beginning at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8760 | With optional end, stop comparing S at that position.\n\ |
| 8761 | suffix can also be a tuple of strings to try."); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8762 | |
| 8763 | static PyObject * |
| 8764 | unicode_endswith(PyUnicodeObject *self, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8765 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8766 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8767 | PyObject *subobj; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8768 | PyUnicodeObject *substring; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8769 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8770 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8771 | int result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8772 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8773 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8774 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
| 8775 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8776 | if (PyTuple_Check(subobj)) { |
| 8777 | Py_ssize_t i; |
| 8778 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 8779 | substring = (PyUnicodeObject *)PyUnicode_FromObject( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8780 | PyTuple_GET_ITEM(subobj, i)); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8781 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8782 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8783 | result = tailmatch(self, substring, start, end, +1); |
| 8784 | Py_DECREF(substring); |
| 8785 | if (result) { |
| 8786 | Py_RETURN_TRUE; |
| 8787 | } |
| 8788 | } |
| 8789 | Py_RETURN_FALSE; |
| 8790 | } |
| 8791 | substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8792 | if (substring == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8793 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8794 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8795 | result = tailmatch(self, substring, start, end, +1); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8796 | Py_DECREF(substring); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8797 | return PyBool_FromLong(result); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8798 | } |
| 8799 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8800 | #include "stringlib/string_format.h" |
| 8801 | |
| 8802 | PyDoc_STRVAR(format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8803 | "S.format(*args, **kwargs) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8804 | \n\ |
| 8805 | "); |
| 8806 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 8807 | static PyObject * |
| 8808 | unicode__format__(PyObject* self, PyObject* args) |
| 8809 | { |
| 8810 | PyObject *format_spec; |
| 8811 | |
| 8812 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
| 8813 | return NULL; |
| 8814 | |
| 8815 | return _PyUnicode_FormatAdvanced(self, |
| 8816 | PyUnicode_AS_UNICODE(format_spec), |
| 8817 | PyUnicode_GET_SIZE(format_spec)); |
| 8818 | } |
| 8819 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8820 | PyDoc_STRVAR(p_format__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8821 | "S.__format__(format_spec) -> str\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8822 | \n\ |
| 8823 | "); |
| 8824 | |
| 8825 | static PyObject * |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8826 | unicode__sizeof__(PyUnicodeObject *v) |
| 8827 | { |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 8828 | return PyLong_FromSsize_t(sizeof(PyUnicodeObject) + |
| 8829 | sizeof(Py_UNICODE) * (v->length + 1)); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8830 | } |
| 8831 | |
| 8832 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8833 | "S.__sizeof__() -> size of S in memory, in bytes"); |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8834 | |
| 8835 | static PyObject * |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8836 | unicode_getnewargs(PyUnicodeObject *v) |
| 8837 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8838 | return Py_BuildValue("(u#)", v->str, v->length); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 8839 | } |
| 8840 | |
| 8841 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8842 | static PyMethodDef unicode_methods[] = { |
| 8843 | |
| 8844 | /* Order is according to common usage: often used methods should |
| 8845 | appear first, since lookup is done sequentially. */ |
| 8846 | |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8847 | {"encode", (PyCFunction) unicode_encode, METH_VARARGS, encode__doc__}, |
| 8848 | {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, |
| 8849 | {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 8850 | {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8851 | {"join", (PyCFunction) unicode_join, METH_O, join__doc__}, |
| 8852 | {"capitalize", (PyCFunction) unicode_capitalize, METH_NOARGS, capitalize__doc__}, |
| 8853 | {"title", (PyCFunction) unicode_title, METH_NOARGS, title__doc__}, |
| 8854 | {"center", (PyCFunction) unicode_center, METH_VARARGS, center__doc__}, |
| 8855 | {"count", (PyCFunction) unicode_count, METH_VARARGS, count__doc__}, |
| 8856 | {"expandtabs", (PyCFunction) unicode_expandtabs, METH_VARARGS, expandtabs__doc__}, |
| 8857 | {"find", (PyCFunction) unicode_find, METH_VARARGS, find__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8858 | {"partition", (PyCFunction) unicode_partition, METH_O, partition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8859 | {"index", (PyCFunction) unicode_index, METH_VARARGS, index__doc__}, |
| 8860 | {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, |
| 8861 | {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8862 | {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8863 | {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, |
| 8864 | {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, |
| 8865 | {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8866 | {"rstrip", (PyCFunction) unicode_rstrip, METH_VARARGS, rstrip__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8867 | {"rpartition", (PyCFunction) unicode_rpartition, METH_O, rpartition__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8868 | {"splitlines", (PyCFunction) unicode_splitlines, METH_VARARGS, splitlines__doc__}, |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 8869 | {"strip", (PyCFunction) unicode_strip, METH_VARARGS, strip__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8870 | {"swapcase", (PyCFunction) unicode_swapcase, METH_NOARGS, swapcase__doc__}, |
| 8871 | {"translate", (PyCFunction) unicode_translate, METH_O, translate__doc__}, |
| 8872 | {"upper", (PyCFunction) unicode_upper, METH_NOARGS, upper__doc__}, |
| 8873 | {"startswith", (PyCFunction) unicode_startswith, METH_VARARGS, startswith__doc__}, |
| 8874 | {"endswith", (PyCFunction) unicode_endswith, METH_VARARGS, endswith__doc__}, |
| 8875 | {"islower", (PyCFunction) unicode_islower, METH_NOARGS, islower__doc__}, |
| 8876 | {"isupper", (PyCFunction) unicode_isupper, METH_NOARGS, isupper__doc__}, |
| 8877 | {"istitle", (PyCFunction) unicode_istitle, METH_NOARGS, istitle__doc__}, |
| 8878 | {"isspace", (PyCFunction) unicode_isspace, METH_NOARGS, isspace__doc__}, |
| 8879 | {"isdecimal", (PyCFunction) unicode_isdecimal, METH_NOARGS, isdecimal__doc__}, |
| 8880 | {"isdigit", (PyCFunction) unicode_isdigit, METH_NOARGS, isdigit__doc__}, |
| 8881 | {"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__}, |
| 8882 | {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__}, |
| 8883 | {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__}, |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 8884 | {"isidentifier", (PyCFunction) unicode_isidentifier, METH_NOARGS, isidentifier__doc__}, |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 8885 | {"isprintable", (PyCFunction) unicode_isprintable, METH_NOARGS, isprintable__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8886 | {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__}, |
Eric Smith | 9cd1e09 | 2007-08-31 18:39:38 +0000 | [diff] [blame] | 8887 | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 8888 | {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, |
Eric Smith | f6db409 | 2007-08-27 23:52:26 +0000 | [diff] [blame] | 8889 | {"_formatter_field_name_split", (PyCFunction) formatter_field_name_split, METH_NOARGS}, |
| 8890 | {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS}, |
Georg Brandl | ceee077 | 2007-11-27 23:48:05 +0000 | [diff] [blame] | 8891 | {"maketrans", (PyCFunction) unicode_maketrans, |
| 8892 | METH_VARARGS | METH_STATIC, maketrans__doc__}, |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8893 | {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 8894 | #if 0 |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8895 | {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8896 | #endif |
| 8897 | |
| 8898 | #if 0 |
| 8899 | /* This one is just used for debugging the implementation. */ |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 8900 | {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8901 | #endif |
| 8902 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8903 | {"__getnewargs__", (PyCFunction)unicode_getnewargs, METH_NOARGS}, |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8904 | {NULL, NULL} |
| 8905 | }; |
| 8906 | |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8907 | static PyObject * |
| 8908 | unicode_mod(PyObject *v, PyObject *w) |
| 8909 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8910 | if (!PyUnicode_Check(v)) { |
| 8911 | Py_INCREF(Py_NotImplemented); |
| 8912 | return Py_NotImplemented; |
| 8913 | } |
| 8914 | return PyUnicode_Format(v, w); |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8915 | } |
| 8916 | |
| 8917 | static PyNumberMethods unicode_as_number = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8918 | 0, /*nb_add*/ |
| 8919 | 0, /*nb_subtract*/ |
| 8920 | 0, /*nb_multiply*/ |
| 8921 | unicode_mod, /*nb_remainder*/ |
Neil Schemenauer | ce30bc9 | 2002-11-18 16:10:18 +0000 | [diff] [blame] | 8922 | }; |
| 8923 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8924 | static PySequenceMethods unicode_as_sequence = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8925 | (lenfunc) unicode_length, /* sq_length */ |
| 8926 | PyUnicode_Concat, /* sq_concat */ |
| 8927 | (ssizeargfunc) unicode_repeat, /* sq_repeat */ |
| 8928 | (ssizeargfunc) unicode_getitem, /* sq_item */ |
| 8929 | 0, /* sq_slice */ |
| 8930 | 0, /* sq_ass_item */ |
| 8931 | 0, /* sq_ass_slice */ |
| 8932 | PyUnicode_Contains, /* sq_contains */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8933 | }; |
| 8934 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8935 | static PyObject* |
| 8936 | unicode_subscript(PyUnicodeObject* self, PyObject* item) |
| 8937 | { |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 8938 | if (PyIndex_Check(item)) { |
| 8939 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8940 | if (i == -1 && PyErr_Occurred()) |
| 8941 | return NULL; |
| 8942 | if (i < 0) |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8943 | i += PyUnicode_GET_SIZE(self); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8944 | return unicode_getitem(self, i); |
| 8945 | } else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8946 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8947 | Py_UNICODE* source_buf; |
| 8948 | Py_UNICODE* result_buf; |
| 8949 | PyObject* result; |
| 8950 | |
Martin v. Löwis | dea59e5 | 2006-01-05 10:00:36 +0000 | [diff] [blame] | 8951 | if (PySlice_GetIndicesEx((PySliceObject*)item, PyUnicode_GET_SIZE(self), |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8952 | &start, &stop, &step, &slicelength) < 0) { |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8953 | return NULL; |
| 8954 | } |
| 8955 | |
| 8956 | if (slicelength <= 0) { |
| 8957 | return PyUnicode_FromUnicode(NULL, 0); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 8958 | } else if (start == 0 && step == 1 && slicelength == self->length && |
| 8959 | PyUnicode_CheckExact(self)) { |
| 8960 | Py_INCREF(self); |
| 8961 | return (PyObject *)self; |
| 8962 | } else if (step == 1) { |
| 8963 | return PyUnicode_FromUnicode(self->str + start, slicelength); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8964 | } else { |
| 8965 | source_buf = PyUnicode_AS_UNICODE((PyObject*)self); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 8966 | result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength* |
| 8967 | sizeof(Py_UNICODE)); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8968 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 8969 | if (result_buf == NULL) |
| 8970 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8971 | |
| 8972 | for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 8973 | result_buf[i] = source_buf[cur]; |
| 8974 | } |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 8975 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8976 | result = PyUnicode_FromUnicode(result_buf, slicelength); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 8977 | PyObject_FREE(result_buf); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8978 | return result; |
| 8979 | } |
| 8980 | } else { |
| 8981 | PyErr_SetString(PyExc_TypeError, "string indices must be integers"); |
| 8982 | return NULL; |
| 8983 | } |
| 8984 | } |
| 8985 | |
| 8986 | static PyMappingMethods unicode_as_mapping = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 8987 | (lenfunc)unicode_length, /* mp_length */ |
| 8988 | (binaryfunc)unicode_subscript, /* mp_subscript */ |
| 8989 | (objobjargproc)0, /* mp_ass_subscript */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 8990 | }; |
| 8991 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8992 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8993 | /* Helpers for PyUnicode_Format() */ |
| 8994 | |
| 8995 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8996 | 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] | 8997 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 8998 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 8999 | if (argidx < arglen) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9000 | (*p_argidx)++; |
| 9001 | if (arglen < 0) |
| 9002 | return args; |
| 9003 | else |
| 9004 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9005 | } |
| 9006 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9007 | "not enough arguments for format string"); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9008 | return NULL; |
| 9009 | } |
| 9010 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9011 | /* Returns a new reference to a PyUnicode object, or NULL on failure. */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9012 | |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9013 | static PyObject * |
| 9014 | formatfloat(PyObject *v, int flags, int prec, int type) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9015 | { |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9016 | char *p; |
| 9017 | PyObject *result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9018 | double x; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9019 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9020 | x = PyFloat_AsDouble(v); |
| 9021 | if (x == -1.0 && PyErr_Occurred()) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9022 | return NULL; |
| 9023 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9024 | if (prec < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9025 | prec = 6; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 9026 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 9027 | p = PyOS_double_to_string(x, type, prec, |
| 9028 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9029 | if (p == NULL) |
| 9030 | return NULL; |
| 9031 | result = PyUnicode_FromStringAndSize(p, strlen(p)); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 9032 | PyMem_Free(p); |
| 9033 | return result; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9034 | } |
| 9035 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9036 | static PyObject* |
| 9037 | formatlong(PyObject *val, int flags, int prec, int type) |
| 9038 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9039 | char *buf; |
| 9040 | int len; |
| 9041 | PyObject *str; /* temporary string object. */ |
| 9042 | PyObject *result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9043 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9044 | str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len); |
| 9045 | if (!str) |
| 9046 | return NULL; |
| 9047 | result = PyUnicode_FromStringAndSize(buf, len); |
| 9048 | Py_DECREF(str); |
| 9049 | return result; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9050 | } |
| 9051 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9052 | static int |
| 9053 | formatchar(Py_UNICODE *buf, |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9054 | size_t buflen, |
| 9055 | PyObject *v) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9056 | { |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 9057 | /* presume that the buffer is at least 3 characters long */ |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 9058 | if (PyUnicode_Check(v)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9059 | if (PyUnicode_GET_SIZE(v) == 1) { |
| 9060 | buf[0] = PyUnicode_AS_UNICODE(v)[0]; |
| 9061 | buf[1] = '\0'; |
| 9062 | return 1; |
| 9063 | } |
| 9064 | #ifndef Py_UNICODE_WIDE |
| 9065 | if (PyUnicode_GET_SIZE(v) == 2) { |
| 9066 | /* Decode a valid surrogate pair */ |
| 9067 | int c0 = PyUnicode_AS_UNICODE(v)[0]; |
| 9068 | int c1 = PyUnicode_AS_UNICODE(v)[1]; |
| 9069 | if (0xD800 <= c0 && c0 <= 0xDBFF && |
| 9070 | 0xDC00 <= c1 && c1 <= 0xDFFF) { |
| 9071 | buf[0] = c0; |
| 9072 | buf[1] = c1; |
| 9073 | buf[2] = '\0'; |
| 9074 | return 2; |
| 9075 | } |
| 9076 | } |
| 9077 | #endif |
| 9078 | goto onError; |
| 9079 | } |
| 9080 | else { |
| 9081 | /* Integer input truncated to a character */ |
| 9082 | long x; |
| 9083 | x = PyLong_AsLong(v); |
| 9084 | if (x == -1 && PyErr_Occurred()) |
| 9085 | goto onError; |
| 9086 | |
| 9087 | if (x < 0 || x > 0x10ffff) { |
| 9088 | PyErr_SetString(PyExc_OverflowError, |
| 9089 | "%c arg not in range(0x110000)"); |
| 9090 | return -1; |
| 9091 | } |
| 9092 | |
| 9093 | #ifndef Py_UNICODE_WIDE |
| 9094 | if (x > 0xffff) { |
| 9095 | x -= 0x10000; |
| 9096 | buf[0] = (Py_UNICODE)(0xD800 | (x >> 10)); |
| 9097 | buf[1] = (Py_UNICODE)(0xDC00 | (x & 0x3FF)); |
| 9098 | return 2; |
| 9099 | } |
| 9100 | #endif |
| 9101 | buf[0] = (Py_UNICODE) x; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9102 | buf[1] = '\0'; |
| 9103 | return 1; |
| 9104 | } |
Amaury Forgeot d'Arc | a4db686 | 2008-07-04 21:26:43 +0000 | [diff] [blame] | 9105 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9106 | onError: |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 9107 | PyErr_SetString(PyExc_TypeError, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9108 | "%c requires int or char"); |
Marc-André Lemburg | d4ab4a5 | 2000-06-08 17:54:00 +0000 | [diff] [blame] | 9109 | return -1; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9110 | } |
| 9111 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9112 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9113 | FORMATBUFLEN is the length of the buffer in which chars are formatted. |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9114 | */ |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9115 | #define FORMATBUFLEN (size_t)10 |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 9116 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9117 | PyObject *PyUnicode_Format(PyObject *format, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9118 | PyObject *args) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9119 | { |
| 9120 | Py_UNICODE *fmt, *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 9121 | Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9122 | int args_owned = 0; |
| 9123 | PyUnicodeObject *result = NULL; |
| 9124 | PyObject *dict = NULL; |
| 9125 | PyObject *uformat; |
Tim Peters | ced69f8 | 2003-09-16 20:30:58 +0000 | [diff] [blame] | 9126 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9127 | if (format == NULL || args == NULL) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9128 | PyErr_BadInternalCall(); |
| 9129 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9130 | } |
| 9131 | uformat = PyUnicode_FromObject(format); |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 9132 | if (uformat == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9133 | return NULL; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9134 | fmt = PyUnicode_AS_UNICODE(uformat); |
| 9135 | fmtcnt = PyUnicode_GET_SIZE(uformat); |
| 9136 | |
| 9137 | reslen = rescnt = fmtcnt + 100; |
| 9138 | result = _PyUnicode_New(reslen); |
| 9139 | if (result == NULL) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9140 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9141 | res = PyUnicode_AS_UNICODE(result); |
| 9142 | |
| 9143 | if (PyTuple_Check(args)) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9144 | arglen = PyTuple_Size(args); |
| 9145 | argidx = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9146 | } |
| 9147 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9148 | arglen = -1; |
| 9149 | argidx = -2; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9150 | } |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 9151 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Christian Heimes | f386311 | 2007-11-22 07:46:41 +0000 | [diff] [blame] | 9152 | !PyUnicode_Check(args)) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9153 | dict = args; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9154 | |
| 9155 | while (--fmtcnt >= 0) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9156 | if (*fmt != '%') { |
| 9157 | if (--rescnt < 0) { |
| 9158 | rescnt = fmtcnt + 100; |
| 9159 | reslen += rescnt; |
| 9160 | if (_PyUnicode_Resize(&result, reslen) < 0) |
| 9161 | goto onError; |
| 9162 | res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; |
| 9163 | --rescnt; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9164 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9165 | *res++ = *fmt++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9166 | } |
| 9167 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9168 | /* Got a format specifier */ |
| 9169 | int flags = 0; |
| 9170 | Py_ssize_t width = -1; |
| 9171 | int prec = -1; |
| 9172 | Py_UNICODE c = '\0'; |
| 9173 | Py_UNICODE fill; |
| 9174 | int isnumok; |
| 9175 | PyObject *v = NULL; |
| 9176 | PyObject *temp = NULL; |
| 9177 | Py_UNICODE *pbuf; |
| 9178 | Py_UNICODE sign; |
| 9179 | Py_ssize_t len; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9180 | Py_UNICODE formatbuf[FORMATBUFLEN]; /* For formatchar() */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9181 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9182 | fmt++; |
| 9183 | if (*fmt == '(') { |
| 9184 | Py_UNICODE *keystart; |
| 9185 | Py_ssize_t keylen; |
| 9186 | PyObject *key; |
| 9187 | int pcount = 1; |
Christian Heimes | a612dc0 | 2008-02-24 13:08:18 +0000 | [diff] [blame] | 9188 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9189 | if (dict == NULL) { |
| 9190 | PyErr_SetString(PyExc_TypeError, |
| 9191 | "format requires a mapping"); |
| 9192 | goto onError; |
| 9193 | } |
| 9194 | ++fmt; |
| 9195 | --fmtcnt; |
| 9196 | keystart = fmt; |
| 9197 | /* Skip over balanced parentheses */ |
| 9198 | while (pcount > 0 && --fmtcnt >= 0) { |
| 9199 | if (*fmt == ')') |
| 9200 | --pcount; |
| 9201 | else if (*fmt == '(') |
| 9202 | ++pcount; |
| 9203 | fmt++; |
| 9204 | } |
| 9205 | keylen = fmt - keystart - 1; |
| 9206 | if (fmtcnt < 0 || pcount > 0) { |
| 9207 | PyErr_SetString(PyExc_ValueError, |
| 9208 | "incomplete format key"); |
| 9209 | goto onError; |
| 9210 | } |
| 9211 | #if 0 |
| 9212 | /* keys are converted to strings using UTF-8 and |
| 9213 | then looked up since Python uses strings to hold |
| 9214 | variables names etc. in its namespaces and we |
| 9215 | wouldn't want to break common idioms. */ |
| 9216 | key = PyUnicode_EncodeUTF8(keystart, |
| 9217 | keylen, |
| 9218 | NULL); |
| 9219 | #else |
| 9220 | key = PyUnicode_FromUnicode(keystart, keylen); |
| 9221 | #endif |
| 9222 | if (key == NULL) |
| 9223 | goto onError; |
| 9224 | if (args_owned) { |
| 9225 | Py_DECREF(args); |
| 9226 | args_owned = 0; |
| 9227 | } |
| 9228 | args = PyObject_GetItem(dict, key); |
| 9229 | Py_DECREF(key); |
| 9230 | if (args == NULL) { |
| 9231 | goto onError; |
| 9232 | } |
| 9233 | args_owned = 1; |
| 9234 | arglen = -1; |
| 9235 | argidx = -2; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9236 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9237 | while (--fmtcnt >= 0) { |
| 9238 | switch (c = *fmt++) { |
| 9239 | case '-': flags |= F_LJUST; continue; |
| 9240 | case '+': flags |= F_SIGN; continue; |
| 9241 | case ' ': flags |= F_BLANK; continue; |
| 9242 | case '#': flags |= F_ALT; continue; |
| 9243 | case '0': flags |= F_ZERO; continue; |
| 9244 | } |
| 9245 | break; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9246 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9247 | if (c == '*') { |
| 9248 | v = getnextarg(args, arglen, &argidx); |
| 9249 | if (v == NULL) |
| 9250 | goto onError; |
| 9251 | if (!PyLong_Check(v)) { |
| 9252 | PyErr_SetString(PyExc_TypeError, |
| 9253 | "* wants int"); |
| 9254 | goto onError; |
| 9255 | } |
| 9256 | width = PyLong_AsLong(v); |
| 9257 | if (width == -1 && PyErr_Occurred()) |
| 9258 | goto onError; |
| 9259 | if (width < 0) { |
| 9260 | flags |= F_LJUST; |
| 9261 | width = -width; |
| 9262 | } |
| 9263 | if (--fmtcnt >= 0) |
| 9264 | c = *fmt++; |
| 9265 | } |
| 9266 | else if (c >= '0' && c <= '9') { |
| 9267 | width = c - '0'; |
| 9268 | while (--fmtcnt >= 0) { |
| 9269 | c = *fmt++; |
| 9270 | if (c < '0' || c > '9') |
| 9271 | break; |
| 9272 | if ((width*10) / 10 != width) { |
| 9273 | PyErr_SetString(PyExc_ValueError, |
| 9274 | "width too big"); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9275 | goto onError; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9276 | } |
| 9277 | width = width*10 + (c - '0'); |
| 9278 | } |
| 9279 | } |
| 9280 | if (c == '.') { |
| 9281 | prec = 0; |
| 9282 | if (--fmtcnt >= 0) |
| 9283 | c = *fmt++; |
| 9284 | if (c == '*') { |
| 9285 | v = getnextarg(args, arglen, &argidx); |
| 9286 | if (v == NULL) |
| 9287 | goto onError; |
| 9288 | if (!PyLong_Check(v)) { |
| 9289 | PyErr_SetString(PyExc_TypeError, |
| 9290 | "* wants int"); |
| 9291 | goto onError; |
| 9292 | } |
| 9293 | prec = PyLong_AsLong(v); |
| 9294 | if (prec == -1 && PyErr_Occurred()) |
| 9295 | goto onError; |
| 9296 | if (prec < 0) |
| 9297 | prec = 0; |
| 9298 | if (--fmtcnt >= 0) |
| 9299 | c = *fmt++; |
| 9300 | } |
| 9301 | else if (c >= '0' && c <= '9') { |
| 9302 | prec = c - '0'; |
| 9303 | while (--fmtcnt >= 0) { |
Stefan Krah | aebd6f4 | 2010-07-19 18:01:13 +0000 | [diff] [blame] | 9304 | c = *fmt++; |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9305 | if (c < '0' || c > '9') |
| 9306 | break; |
| 9307 | if ((prec*10) / 10 != prec) { |
| 9308 | PyErr_SetString(PyExc_ValueError, |
| 9309 | "prec too big"); |
| 9310 | goto onError; |
| 9311 | } |
| 9312 | prec = prec*10 + (c - '0'); |
| 9313 | } |
| 9314 | } |
| 9315 | } /* prec */ |
| 9316 | if (fmtcnt >= 0) { |
| 9317 | if (c == 'h' || c == 'l' || c == 'L') { |
| 9318 | if (--fmtcnt >= 0) |
| 9319 | c = *fmt++; |
| 9320 | } |
| 9321 | } |
| 9322 | if (fmtcnt < 0) { |
| 9323 | PyErr_SetString(PyExc_ValueError, |
| 9324 | "incomplete format"); |
| 9325 | goto onError; |
| 9326 | } |
| 9327 | if (c != '%') { |
| 9328 | v = getnextarg(args, arglen, &argidx); |
| 9329 | if (v == NULL) |
| 9330 | goto onError; |
| 9331 | } |
| 9332 | sign = 0; |
| 9333 | fill = ' '; |
| 9334 | switch (c) { |
| 9335 | |
| 9336 | case '%': |
| 9337 | pbuf = formatbuf; |
| 9338 | /* presume that buffer length is at least 1 */ |
| 9339 | pbuf[0] = '%'; |
| 9340 | len = 1; |
| 9341 | break; |
| 9342 | |
| 9343 | case 's': |
| 9344 | case 'r': |
| 9345 | case 'a': |
Victor Stinner | abdb21a | 2010-03-22 12:53:14 +0000 | [diff] [blame] | 9346 | if (PyUnicode_CheckExact(v) && c == 's') { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9347 | temp = v; |
| 9348 | Py_INCREF(temp); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9349 | } |
| 9350 | else { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9351 | if (c == 's') |
| 9352 | temp = PyObject_Str(v); |
| 9353 | else if (c == 'r') |
| 9354 | temp = PyObject_Repr(v); |
| 9355 | else |
| 9356 | temp = PyObject_ASCII(v); |
| 9357 | if (temp == NULL) |
| 9358 | goto onError; |
| 9359 | if (PyUnicode_Check(temp)) |
| 9360 | /* nothing to do */; |
| 9361 | else { |
| 9362 | Py_DECREF(temp); |
| 9363 | PyErr_SetString(PyExc_TypeError, |
| 9364 | "%s argument has non-string str()"); |
| 9365 | goto onError; |
| 9366 | } |
| 9367 | } |
| 9368 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9369 | len = PyUnicode_GET_SIZE(temp); |
| 9370 | if (prec >= 0 && len > prec) |
| 9371 | len = prec; |
| 9372 | break; |
| 9373 | |
| 9374 | case 'i': |
| 9375 | case 'd': |
| 9376 | case 'u': |
| 9377 | case 'o': |
| 9378 | case 'x': |
| 9379 | case 'X': |
| 9380 | if (c == 'i') |
| 9381 | c = 'd'; |
| 9382 | isnumok = 0; |
| 9383 | if (PyNumber_Check(v)) { |
| 9384 | PyObject *iobj=NULL; |
| 9385 | |
| 9386 | if (PyLong_Check(v)) { |
| 9387 | iobj = v; |
| 9388 | Py_INCREF(iobj); |
| 9389 | } |
| 9390 | else { |
| 9391 | iobj = PyNumber_Long(v); |
| 9392 | } |
| 9393 | if (iobj!=NULL) { |
| 9394 | if (PyLong_Check(iobj)) { |
| 9395 | isnumok = 1; |
| 9396 | temp = formatlong(iobj, flags, prec, c); |
| 9397 | Py_DECREF(iobj); |
| 9398 | if (!temp) |
| 9399 | goto onError; |
| 9400 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9401 | len = PyUnicode_GET_SIZE(temp); |
| 9402 | sign = 1; |
| 9403 | } |
| 9404 | else { |
| 9405 | Py_DECREF(iobj); |
| 9406 | } |
| 9407 | } |
| 9408 | } |
| 9409 | if (!isnumok) { |
| 9410 | PyErr_Format(PyExc_TypeError, |
| 9411 | "%%%c format: a number is required, " |
| 9412 | "not %.200s", (char)c, Py_TYPE(v)->tp_name); |
| 9413 | goto onError; |
| 9414 | } |
| 9415 | if (flags & F_ZERO) |
| 9416 | fill = '0'; |
| 9417 | break; |
| 9418 | |
| 9419 | case 'e': |
| 9420 | case 'E': |
| 9421 | case 'f': |
| 9422 | case 'F': |
| 9423 | case 'g': |
| 9424 | case 'G': |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9425 | temp = formatfloat(v, flags, prec, c); |
| 9426 | if (!temp) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9427 | goto onError; |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 9428 | pbuf = PyUnicode_AS_UNICODE(temp); |
| 9429 | len = PyUnicode_GET_SIZE(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9430 | sign = 1; |
| 9431 | if (flags & F_ZERO) |
| 9432 | fill = '0'; |
| 9433 | break; |
| 9434 | |
| 9435 | case 'c': |
| 9436 | pbuf = formatbuf; |
| 9437 | len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v); |
| 9438 | if (len < 0) |
| 9439 | goto onError; |
| 9440 | break; |
| 9441 | |
| 9442 | default: |
| 9443 | PyErr_Format(PyExc_ValueError, |
| 9444 | "unsupported format character '%c' (0x%x) " |
| 9445 | "at index %zd", |
| 9446 | (31<=c && c<=126) ? (char)c : '?', |
| 9447 | (int)c, |
| 9448 | (Py_ssize_t)(fmt - 1 - |
| 9449 | PyUnicode_AS_UNICODE(uformat))); |
| 9450 | goto onError; |
| 9451 | } |
| 9452 | if (sign) { |
| 9453 | if (*pbuf == '-' || *pbuf == '+') { |
| 9454 | sign = *pbuf++; |
| 9455 | len--; |
| 9456 | } |
| 9457 | else if (flags & F_SIGN) |
| 9458 | sign = '+'; |
| 9459 | else if (flags & F_BLANK) |
| 9460 | sign = ' '; |
| 9461 | else |
| 9462 | sign = 0; |
| 9463 | } |
| 9464 | if (width < len) |
| 9465 | width = len; |
| 9466 | if (rescnt - (sign != 0) < width) { |
| 9467 | reslen -= rescnt; |
| 9468 | rescnt = width + fmtcnt + 100; |
| 9469 | reslen += rescnt; |
| 9470 | if (reslen < 0) { |
| 9471 | Py_XDECREF(temp); |
| 9472 | PyErr_NoMemory(); |
| 9473 | goto onError; |
| 9474 | } |
| 9475 | if (_PyUnicode_Resize(&result, reslen) < 0) { |
| 9476 | Py_XDECREF(temp); |
| 9477 | goto onError; |
| 9478 | } |
| 9479 | res = PyUnicode_AS_UNICODE(result) |
| 9480 | + reslen - rescnt; |
| 9481 | } |
| 9482 | if (sign) { |
| 9483 | if (fill != ' ') |
| 9484 | *res++ = sign; |
| 9485 | rescnt--; |
| 9486 | if (width > len) |
| 9487 | width--; |
| 9488 | } |
| 9489 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
| 9490 | assert(pbuf[0] == '0'); |
| 9491 | assert(pbuf[1] == c); |
| 9492 | if (fill != ' ') { |
| 9493 | *res++ = *pbuf++; |
| 9494 | *res++ = *pbuf++; |
| 9495 | } |
| 9496 | rescnt -= 2; |
| 9497 | width -= 2; |
| 9498 | if (width < 0) |
| 9499 | width = 0; |
| 9500 | len -= 2; |
| 9501 | } |
| 9502 | if (width > len && !(flags & F_LJUST)) { |
| 9503 | do { |
| 9504 | --rescnt; |
| 9505 | *res++ = fill; |
| 9506 | } while (--width > len); |
| 9507 | } |
| 9508 | if (fill == ' ') { |
| 9509 | if (sign) |
| 9510 | *res++ = sign; |
| 9511 | if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) { |
| 9512 | assert(pbuf[0] == '0'); |
| 9513 | assert(pbuf[1] == c); |
| 9514 | *res++ = *pbuf++; |
| 9515 | *res++ = *pbuf++; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9516 | } |
| 9517 | } |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9518 | Py_UNICODE_COPY(res, pbuf, len); |
| 9519 | res += len; |
| 9520 | rescnt -= len; |
| 9521 | while (--width >= len) { |
| 9522 | --rescnt; |
| 9523 | *res++ = ' '; |
| 9524 | } |
| 9525 | if (dict && (argidx < arglen) && c != '%') { |
| 9526 | PyErr_SetString(PyExc_TypeError, |
| 9527 | "not all arguments converted during string formatting"); |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9528 | Py_XDECREF(temp); |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9529 | goto onError; |
| 9530 | } |
| 9531 | Py_XDECREF(temp); |
| 9532 | } /* '%' */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9533 | } /* until end */ |
| 9534 | if (argidx < arglen && !dict) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9535 | PyErr_SetString(PyExc_TypeError, |
| 9536 | "not all arguments converted during string formatting"); |
| 9537 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9538 | } |
| 9539 | |
Thomas Wouters | a96affe | 2006-03-12 00:29:36 +0000 | [diff] [blame] | 9540 | if (_PyUnicode_Resize(&result, reslen - rescnt) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9541 | goto onError; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9542 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9543 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9544 | } |
| 9545 | Py_DECREF(uformat); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9546 | return (PyObject *)result; |
| 9547 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9548 | onError: |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9549 | Py_XDECREF(result); |
| 9550 | Py_DECREF(uformat); |
| 9551 | if (args_owned) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9552 | Py_DECREF(args); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9553 | } |
| 9554 | return NULL; |
| 9555 | } |
| 9556 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 9557 | static PyObject * |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9558 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 9559 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9560 | static PyObject * |
| 9561 | unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9562 | { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9563 | PyObject *x = NULL; |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9564 | static char *kwlist[] = {"object", "encoding", "errors", 0}; |
| 9565 | char *encoding = NULL; |
| 9566 | char *errors = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9567 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9568 | if (type != &PyUnicode_Type) |
| 9569 | return unicode_subtype_new(type, args, kwds); |
| 9570 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9571 | kwlist, &x, &encoding, &errors)) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9572 | return NULL; |
| 9573 | if (x == NULL) |
| 9574 | return (PyObject *)_PyUnicode_New(0); |
| 9575 | if (encoding == NULL && errors == NULL) |
| 9576 | return PyObject_Str(x); |
| 9577 | else |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9578 | return PyUnicode_FromEncodedObject(x, encoding, errors); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9579 | } |
| 9580 | |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9581 | static PyObject * |
| 9582 | unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 9583 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9584 | PyUnicodeObject *tmp, *pnew; |
| 9585 | Py_ssize_t n; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9586 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9587 | assert(PyType_IsSubtype(type, &PyUnicode_Type)); |
| 9588 | tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); |
| 9589 | if (tmp == NULL) |
| 9590 | return NULL; |
| 9591 | assert(PyUnicode_Check(tmp)); |
| 9592 | pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length); |
| 9593 | if (pnew == NULL) { |
| 9594 | Py_DECREF(tmp); |
| 9595 | return NULL; |
| 9596 | } |
| 9597 | pnew->str = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1)); |
| 9598 | if (pnew->str == NULL) { |
| 9599 | _Py_ForgetReference((PyObject *)pnew); |
| 9600 | PyObject_Del(pnew); |
| 9601 | Py_DECREF(tmp); |
| 9602 | return PyErr_NoMemory(); |
| 9603 | } |
| 9604 | Py_UNICODE_COPY(pnew->str, tmp->str, n+1); |
| 9605 | pnew->length = n; |
| 9606 | pnew->hash = tmp->hash; |
| 9607 | Py_DECREF(tmp); |
| 9608 | return (PyObject *)pnew; |
Guido van Rossum | e023fe0 | 2001-08-30 03:12:59 +0000 | [diff] [blame] | 9609 | } |
| 9610 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9611 | PyDoc_STRVAR(unicode_doc, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9612 | "str(string[, encoding[, errors]]) -> str\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9613 | \n\ |
Collin Winter | d474ce8 | 2007-08-07 19:42:11 +0000 | [diff] [blame] | 9614 | Create a new string object from the given encoded string.\n\ |
Skip Montanaro | 35b37a5 | 2002-07-26 16:22:46 +0000 | [diff] [blame] | 9615 | encoding defaults to the current default string encoding.\n\ |
| 9616 | errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9617 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9618 | static PyObject *unicode_iter(PyObject *seq); |
| 9619 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9620 | PyTypeObject PyUnicode_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 9621 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9622 | "str", /* tp_name */ |
| 9623 | sizeof(PyUnicodeObject), /* tp_size */ |
| 9624 | 0, /* tp_itemsize */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9625 | /* Slots */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9626 | (destructor)unicode_dealloc, /* tp_dealloc */ |
| 9627 | 0, /* tp_print */ |
| 9628 | 0, /* tp_getattr */ |
| 9629 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 9630 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9631 | unicode_repr, /* tp_repr */ |
| 9632 | &unicode_as_number, /* tp_as_number */ |
| 9633 | &unicode_as_sequence, /* tp_as_sequence */ |
| 9634 | &unicode_as_mapping, /* tp_as_mapping */ |
| 9635 | (hashfunc) unicode_hash, /* tp_hash*/ |
| 9636 | 0, /* tp_call*/ |
| 9637 | (reprfunc) unicode_str, /* tp_str */ |
| 9638 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9639 | 0, /* tp_setattro */ |
| 9640 | 0, /* tp_as_buffer */ |
| 9641 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9642 | Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9643 | unicode_doc, /* tp_doc */ |
| 9644 | 0, /* tp_traverse */ |
| 9645 | 0, /* tp_clear */ |
| 9646 | PyUnicode_RichCompare, /* tp_richcompare */ |
| 9647 | 0, /* tp_weaklistoffset */ |
| 9648 | unicode_iter, /* tp_iter */ |
| 9649 | 0, /* tp_iternext */ |
| 9650 | unicode_methods, /* tp_methods */ |
| 9651 | 0, /* tp_members */ |
| 9652 | 0, /* tp_getset */ |
| 9653 | &PyBaseObject_Type, /* tp_base */ |
| 9654 | 0, /* tp_dict */ |
| 9655 | 0, /* tp_descr_get */ |
| 9656 | 0, /* tp_descr_set */ |
| 9657 | 0, /* tp_dictoffset */ |
| 9658 | 0, /* tp_init */ |
| 9659 | 0, /* tp_alloc */ |
| 9660 | unicode_new, /* tp_new */ |
| 9661 | PyObject_Del, /* tp_free */ |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9662 | }; |
| 9663 | |
| 9664 | /* Initialize the Unicode implementation */ |
| 9665 | |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9666 | void _PyUnicode_Init(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9667 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9668 | int i; |
| 9669 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9670 | /* XXX - move this array to unicodectype.c ? */ |
| 9671 | Py_UNICODE linebreak[] = { |
| 9672 | 0x000A, /* LINE FEED */ |
| 9673 | 0x000D, /* CARRIAGE RETURN */ |
| 9674 | 0x001C, /* FILE SEPARATOR */ |
| 9675 | 0x001D, /* GROUP SEPARATOR */ |
| 9676 | 0x001E, /* RECORD SEPARATOR */ |
| 9677 | 0x0085, /* NEXT LINE */ |
| 9678 | 0x2028, /* LINE SEPARATOR */ |
| 9679 | 0x2029, /* PARAGRAPH SEPARATOR */ |
| 9680 | }; |
| 9681 | |
Fred Drake | e4315f5 | 2000-05-09 19:53:39 +0000 | [diff] [blame] | 9682 | /* Init the implementation */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 9683 | free_list = NULL; |
| 9684 | numfree = 0; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9685 | unicode_empty = _PyUnicode_New(0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9686 | if (!unicode_empty) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9687 | return; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9688 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9689 | for (i = 0; i < 256; i++) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9690 | unicode_latin1[i] = NULL; |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 9691 | if (PyType_Ready(&PyUnicode_Type) < 0) |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9692 | Py_FatalError("Can't initialize 'unicode'"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 9693 | |
| 9694 | /* initialize the linebreak bloom filter */ |
| 9695 | bloom_linebreak = make_bloom_mask( |
| 9696 | linebreak, sizeof(linebreak) / sizeof(linebreak[0]) |
| 9697 | ); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9698 | |
| 9699 | PyType_Ready(&EncodingMapType); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9700 | } |
| 9701 | |
| 9702 | /* Finalize the Unicode implementation */ |
| 9703 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9704 | int |
| 9705 | PyUnicode_ClearFreeList(void) |
| 9706 | { |
| 9707 | int freelist_size = numfree; |
| 9708 | PyUnicodeObject *u; |
| 9709 | |
| 9710 | for (u = free_list; u != NULL;) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9711 | PyUnicodeObject *v = u; |
| 9712 | u = *(PyUnicodeObject **)u; |
| 9713 | if (v->str) |
| 9714 | PyObject_DEL(v->str); |
| 9715 | Py_XDECREF(v->defenc); |
| 9716 | PyObject_Del(v); |
| 9717 | numfree--; |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9718 | } |
| 9719 | free_list = NULL; |
| 9720 | assert(numfree == 0); |
| 9721 | return freelist_size; |
| 9722 | } |
| 9723 | |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9724 | void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 9725 | _PyUnicode_Fini(void) |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9726 | { |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9727 | int i; |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9728 | |
Guido van Rossum | 4ae8ef8 | 2000-10-03 18:09:04 +0000 | [diff] [blame] | 9729 | Py_XDECREF(unicode_empty); |
| 9730 | unicode_empty = NULL; |
Barry Warsaw | 5b4c228 | 2000-10-03 20:45:26 +0000 | [diff] [blame] | 9731 | |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9732 | for (i = 0; i < 256; i++) { |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9733 | if (unicode_latin1[i]) { |
| 9734 | Py_DECREF(unicode_latin1[i]); |
| 9735 | unicode_latin1[i] = NULL; |
| 9736 | } |
Marc-André Lemburg | 8155e0e | 2001-04-23 14:44:21 +0000 | [diff] [blame] | 9737 | } |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 9738 | (void)PyUnicode_ClearFreeList(); |
Guido van Rossum | d57fd91 | 2000-03-10 22:53:23 +0000 | [diff] [blame] | 9739 | } |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 9740 | |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9741 | void |
| 9742 | PyUnicode_InternInPlace(PyObject **p) |
| 9743 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9744 | register PyUnicodeObject *s = (PyUnicodeObject *)(*p); |
| 9745 | PyObject *t; |
| 9746 | if (s == NULL || !PyUnicode_Check(s)) |
| 9747 | Py_FatalError( |
| 9748 | "PyUnicode_InternInPlace: unicode strings only please!"); |
| 9749 | /* If it's a subclass, we don't really know what putting |
| 9750 | it in the interned dict might do. */ |
| 9751 | if (!PyUnicode_CheckExact(s)) |
| 9752 | return; |
| 9753 | if (PyUnicode_CHECK_INTERNED(s)) |
| 9754 | return; |
| 9755 | if (interned == NULL) { |
| 9756 | interned = PyDict_New(); |
| 9757 | if (interned == NULL) { |
| 9758 | PyErr_Clear(); /* Don't leave an exception */ |
| 9759 | return; |
| 9760 | } |
| 9761 | } |
| 9762 | /* It might be that the GetItem call fails even |
| 9763 | though the key is present in the dictionary, |
| 9764 | namely when this happens during a stack overflow. */ |
| 9765 | Py_ALLOW_RECURSION |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9766 | t = PyDict_GetItem(interned, (PyObject *)s); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9767 | Py_END_ALLOW_RECURSION |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9768 | |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9769 | if (t) { |
| 9770 | Py_INCREF(t); |
| 9771 | Py_DECREF(*p); |
| 9772 | *p = t; |
| 9773 | return; |
| 9774 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9775 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9776 | PyThreadState_GET()->recursion_critical = 1; |
| 9777 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
| 9778 | PyErr_Clear(); |
| 9779 | PyThreadState_GET()->recursion_critical = 0; |
| 9780 | return; |
| 9781 | } |
| 9782 | PyThreadState_GET()->recursion_critical = 0; |
| 9783 | /* The two references in interned are not counted by refcnt. |
| 9784 | The deallocator will take care of this */ |
| 9785 | Py_REFCNT(s) -= 2; |
| 9786 | PyUnicode_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9787 | } |
| 9788 | |
| 9789 | void |
| 9790 | PyUnicode_InternImmortal(PyObject **p) |
| 9791 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9792 | PyUnicode_InternInPlace(p); |
| 9793 | if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
| 9794 | PyUnicode_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL; |
| 9795 | Py_INCREF(*p); |
| 9796 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9797 | } |
| 9798 | |
| 9799 | PyObject * |
| 9800 | PyUnicode_InternFromString(const char *cp) |
| 9801 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9802 | PyObject *s = PyUnicode_FromString(cp); |
| 9803 | if (s == NULL) |
| 9804 | return NULL; |
| 9805 | PyUnicode_InternInPlace(&s); |
| 9806 | return s; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9807 | } |
| 9808 | |
| 9809 | void _Py_ReleaseInternedUnicodeStrings(void) |
| 9810 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9811 | PyObject *keys; |
| 9812 | PyUnicodeObject *s; |
| 9813 | Py_ssize_t i, n; |
| 9814 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9815 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9816 | if (interned == NULL || !PyDict_Check(interned)) |
| 9817 | return; |
| 9818 | keys = PyDict_Keys(interned); |
| 9819 | if (keys == NULL || !PyList_Check(keys)) { |
| 9820 | PyErr_Clear(); |
| 9821 | return; |
| 9822 | } |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9823 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9824 | /* Since _Py_ReleaseInternedUnicodeStrings() is intended to help a leak |
| 9825 | detector, interned unicode strings are not forcibly deallocated; |
| 9826 | rather, we give them their stolen references back, and then clear |
| 9827 | and DECREF the interned dict. */ |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9828 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9829 | n = PyList_GET_SIZE(keys); |
| 9830 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9831 | n); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9832 | for (i = 0; i < n; i++) { |
| 9833 | s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i); |
| 9834 | switch (s->state) { |
| 9835 | case SSTATE_NOT_INTERNED: |
| 9836 | /* XXX Shouldn't happen */ |
| 9837 | break; |
| 9838 | case SSTATE_INTERNED_IMMORTAL: |
| 9839 | Py_REFCNT(s) += 1; |
| 9840 | immortal_size += s->length; |
| 9841 | break; |
| 9842 | case SSTATE_INTERNED_MORTAL: |
| 9843 | Py_REFCNT(s) += 2; |
| 9844 | mortal_size += s->length; |
| 9845 | break; |
| 9846 | default: |
| 9847 | Py_FatalError("Inconsistent interned string state."); |
| 9848 | } |
| 9849 | s->state = SSTATE_NOT_INTERNED; |
| 9850 | } |
| 9851 | fprintf(stderr, "total size of all interned strings: " |
| 9852 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 9853 | "mortal/immortal\n", mortal_size, immortal_size); |
| 9854 | Py_DECREF(keys); |
| 9855 | PyDict_Clear(interned); |
| 9856 | Py_DECREF(interned); |
| 9857 | interned = NULL; |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 9858 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9859 | |
| 9860 | |
| 9861 | /********************* Unicode Iterator **************************/ |
| 9862 | |
| 9863 | typedef struct { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9864 | PyObject_HEAD |
| 9865 | Py_ssize_t it_index; |
| 9866 | PyUnicodeObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9867 | } unicodeiterobject; |
| 9868 | |
| 9869 | static void |
| 9870 | unicodeiter_dealloc(unicodeiterobject *it) |
| 9871 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9872 | _PyObject_GC_UNTRACK(it); |
| 9873 | Py_XDECREF(it->it_seq); |
| 9874 | PyObject_GC_Del(it); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9875 | } |
| 9876 | |
| 9877 | static int |
| 9878 | unicodeiter_traverse(unicodeiterobject *it, visitproc visit, void *arg) |
| 9879 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9880 | Py_VISIT(it->it_seq); |
| 9881 | return 0; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9882 | } |
| 9883 | |
| 9884 | static PyObject * |
| 9885 | unicodeiter_next(unicodeiterobject *it) |
| 9886 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9887 | PyUnicodeObject *seq; |
| 9888 | PyObject *item; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9889 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9890 | assert(it != NULL); |
| 9891 | seq = it->it_seq; |
| 9892 | if (seq == NULL) |
| 9893 | return NULL; |
| 9894 | assert(PyUnicode_Check(seq)); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9895 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9896 | if (it->it_index < PyUnicode_GET_SIZE(seq)) { |
| 9897 | item = PyUnicode_FromUnicode( |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9898 | PyUnicode_AS_UNICODE(seq)+it->it_index, 1); |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9899 | if (item != NULL) |
| 9900 | ++it->it_index; |
| 9901 | return item; |
| 9902 | } |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9903 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9904 | Py_DECREF(seq); |
| 9905 | it->it_seq = NULL; |
| 9906 | return NULL; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9907 | } |
| 9908 | |
| 9909 | static PyObject * |
| 9910 | unicodeiter_len(unicodeiterobject *it) |
| 9911 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9912 | Py_ssize_t len = 0; |
| 9913 | if (it->it_seq) |
| 9914 | len = PyUnicode_GET_SIZE(it->it_seq) - it->it_index; |
| 9915 | return PyLong_FromSsize_t(len); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9916 | } |
| 9917 | |
| 9918 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
| 9919 | |
| 9920 | static PyMethodDef unicodeiter_methods[] = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9921 | {"__length_hint__", (PyCFunction)unicodeiter_len, METH_NOARGS, |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 9922 | length_hint_doc}, |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9923 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9924 | }; |
| 9925 | |
| 9926 | PyTypeObject PyUnicodeIter_Type = { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9927 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 9928 | "str_iterator", /* tp_name */ |
| 9929 | sizeof(unicodeiterobject), /* tp_basicsize */ |
| 9930 | 0, /* tp_itemsize */ |
| 9931 | /* methods */ |
| 9932 | (destructor)unicodeiter_dealloc, /* tp_dealloc */ |
| 9933 | 0, /* tp_print */ |
| 9934 | 0, /* tp_getattr */ |
| 9935 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 9936 | 0, /* tp_reserved */ |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9937 | 0, /* tp_repr */ |
| 9938 | 0, /* tp_as_number */ |
| 9939 | 0, /* tp_as_sequence */ |
| 9940 | 0, /* tp_as_mapping */ |
| 9941 | 0, /* tp_hash */ |
| 9942 | 0, /* tp_call */ |
| 9943 | 0, /* tp_str */ |
| 9944 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 9945 | 0, /* tp_setattro */ |
| 9946 | 0, /* tp_as_buffer */ |
| 9947 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 9948 | 0, /* tp_doc */ |
| 9949 | (traverseproc)unicodeiter_traverse, /* tp_traverse */ |
| 9950 | 0, /* tp_clear */ |
| 9951 | 0, /* tp_richcompare */ |
| 9952 | 0, /* tp_weaklistoffset */ |
| 9953 | PyObject_SelfIter, /* tp_iter */ |
| 9954 | (iternextfunc)unicodeiter_next, /* tp_iternext */ |
| 9955 | unicodeiter_methods, /* tp_methods */ |
| 9956 | 0, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9957 | }; |
| 9958 | |
| 9959 | static PyObject * |
| 9960 | unicode_iter(PyObject *seq) |
| 9961 | { |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9962 | unicodeiterobject *it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9963 | |
Benjamin Peterson | 14339b6 | 2009-01-31 16:36:08 +0000 | [diff] [blame] | 9964 | if (!PyUnicode_Check(seq)) { |
| 9965 | PyErr_BadInternalCall(); |
| 9966 | return NULL; |
| 9967 | } |
| 9968 | it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type); |
| 9969 | if (it == NULL) |
| 9970 | return NULL; |
| 9971 | it->it_index = 0; |
| 9972 | Py_INCREF(seq); |
| 9973 | it->it_seq = (PyUnicodeObject *)seq; |
| 9974 | _PyObject_GC_TRACK(it); |
| 9975 | return (PyObject *)it; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 9976 | } |
| 9977 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 9978 | size_t |
| 9979 | Py_UNICODE_strlen(const Py_UNICODE *u) |
| 9980 | { |
| 9981 | int res = 0; |
| 9982 | while(*u++) |
| 9983 | res++; |
| 9984 | return res; |
| 9985 | } |
| 9986 | |
| 9987 | Py_UNICODE* |
| 9988 | Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2) |
| 9989 | { |
| 9990 | Py_UNICODE *u = s1; |
| 9991 | while ((*u++ = *s2++)); |
| 9992 | return s1; |
| 9993 | } |
| 9994 | |
| 9995 | Py_UNICODE* |
| 9996 | Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n) |
| 9997 | { |
| 9998 | Py_UNICODE *u = s1; |
| 9999 | while ((*u++ = *s2++)) |
| 10000 | if (n-- == 0) |
| 10001 | break; |
| 10002 | return s1; |
| 10003 | } |
| 10004 | |
| 10005 | int |
| 10006 | Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2) |
| 10007 | { |
| 10008 | while (*s1 && *s2 && *s1 == *s2) |
| 10009 | s1++, s2++; |
| 10010 | if (*s1 && *s2) |
| 10011 | return (*s1 < *s2) ? -1 : +1; |
| 10012 | if (*s1) |
| 10013 | return 1; |
| 10014 | if (*s2) |
| 10015 | return -1; |
| 10016 | return 0; |
| 10017 | } |
| 10018 | |
| 10019 | Py_UNICODE* |
| 10020 | Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c) |
| 10021 | { |
| 10022 | const Py_UNICODE *p; |
| 10023 | for (p = s; *p; p++) |
| 10024 | if (*p == c) |
| 10025 | return (Py_UNICODE*)p; |
| 10026 | return NULL; |
| 10027 | } |
| 10028 | |
| 10029 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10030 | #ifdef __cplusplus |
| 10031 | } |
| 10032 | #endif |
| 10033 | |
| 10034 | |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 10035 | /* |
Benjamin Peterson | 2906064 | 2009-01-31 22:14:21 +0000 | [diff] [blame] | 10036 | Local variables: |
| 10037 | c-basic-offset: 4 |
| 10038 | indent-tabs-mode: nil |
| 10039 | End: |
Martin v. Löwis | 9a3a9f7 | 2003-05-18 12:31:09 +0000 | [diff] [blame] | 10040 | */ |